Introduction: Room Visitor Counter With Relay Control
Home automation is a really neat use of technology. Automating the process of turning off the lights when you leave the room is nice if you're a tad lazy like me, and also is a way of being kind to our Earth by saving energy!
I started this project mainly as a segway into teaching myself more about the MSP430 family after I was inspired by a college course in embedded systems last year. We learned how to program the family in C++ and Assembly, but really only scratched the surface. It has been a challenging yet fun project and has helped me veer away from the Arduino.
So without further ado, here's an explanation of what this Instructable aims to show you how to build:
The room visitor counter (as you might have expected...) counts how many people are currently in the room, given this room only has one means of entry. We're not talking firepoles here - I mean a doorway. When there is at least one person in the room, the relay is switched on. And vice versa, if the room is vacant, the relay is switched off.
Using 3 blue LEDs, there is a binary count (I didn't have a 7-segment LED display/display driver at the time I built this) of how many people are in the room, with a maximum displayable count of 7 (when the count is incremented past 7, the LEDs simply remain on).
In this instructable, I assume a level of ability in soldering and prototyping skills. The detail in this instructable is on a higher level of abstraction. In other words, if you can solder, read a schematic and are comfortable downloading some software, you can do it!
Step 1: Making the Backplate
First off, we need to build something to hold it all together. I could have opted for an enclosed case, but I started off simple. If you are feeling adventurous, it wouldn't be a bad idea to enclose this in a box for better protection and visual appeal.
Here's what we'll need for my bare-bones hardware build:
* 3/16" thick plexiglass
-->This is cheap yet study enough to keep this all together (plus, I use plexi for everything)
* 2 threadedstandoffs, metal or plastic
-->These are for securing the circuit board (PCB) to the backplate
-->At least 1/4" long; I used 3/4" metal standoffs because that's what I had. 4 would certainly work better at securing the PCB
* 8 4-40 screws, sized to fit your plexiglass and standoffs
-->Make sure they are long enough to clear the plexiglass, PCB board, and then thread into the standoffs at least 1/8"
-->We'll use 4 of these to mount the PCB
* 4 nuts, sized and threaded to fit the machine screws
-->These are for mounting the IR sensors
I cut the plexiglass from a larger piece using a tablesaw. Since this is just plastic, you should be able to get by using any type of blade.
The dimensional drawing can be printed in 1:1 scale
Step 2: Build the Circuit (and a Little Technical Info)
This project involves working with 120V AC power. That is ''High Voltage''. Do not attempt this unless you are comfortable working with high voltage and are alert and aware of your surroundings, protected from possible electric shock, and able to concentrate on the work at hand. Do NOT leave the relay circuit exposed when using this circuit, and do NOT service the circuit when 120VAC is applied. If you cannot follow the above guidelines, this instructable is not for you.
Things we'll need:
* A piece of PCB/'perfboard', about 10x20cm ~$6Digikey, Radioshack, Maplin if you're over there in the UK
* MSP430G2231, DIP package $2 Digikey
* TI Launchpad~$5 Digikey ***SEE NOTES AT BOTTOM***
* 2 Sharp GP2D12 IR distance sensors$28Sparkfun
* Female headers, 0.1" pin spacing $3 Sparkfun
* 'Wall wart' DC wall adapter, 8-25V DC FREE-$20 Garbage dump, Goodwill, Radioshack, Walmart...
-->These are used for the voltage input (VIN) and the relay
The following can all be found in through-hole variants from large components distributors like Mouser/Digikey (est. $16):
* LM7805 voltage regulator
* LD1086 voltage regulator
* 1N4001 diode
* 4 2N3904 transistors
* Relay, 5V minimum coil driving voltage, 120VAC @ 5A or greater current rating (see notes in step 3)
* 14-pin DIP socket
Capacitors (**IMPORTANT:** All are rated > 25V!):
* 2 0.1uF (ceramic film/tantalum)
* 2 220nF (ceramic film/tantalum)
* 2 10uF (electrolytic)
Resistors (all are 1/4 Wattt):
* 4 2.2Kohm
* 3 820ohm
* 1 330ohm
* 3 blue (or whatever color you'd like for the binary counter)
* 1 red (POWER LED)
I used a 5x3.5cm piece of PCB/perfboard, but if you're just starting out soldering, I would quadruple the size of this to make things easier.
'''Wait until the next step to assemble the relay circuit (everything connected to PIN8 of the MSP430G2231)'''
This really comes down to soldering and circuit layout skill. But if you're not comfortable putting it right on a PCB, you can always breadboard it first (have a look below). Since you've already got a solid circuit, it should work on the perfboard (PCB) board as-is if you've routed all the connections properly!
* '''Microcontroller:''' MSP430G2231 w/ TI Launchpad for prototyping
* '''Power/Clocking:''' 8-25V DC input, regulated 3.3 & 5V rails, 1MHz internal oscillator
* '''Current Draw:''' < 100mA without relay active; ~150mA with relay coil driven
Why did I pick the MSP430 and TI Launchpad?
* No external components required, sans the pull-up resistor for /RST
* Low power
* Great hardware debugger interface
* Low cost (about $2 for a DIP package)
By the way, I'm not affiliated with TI, and I'm not promoting products. I just chose this microcontroller because I liked it.
* The female headers are not completely necessary; I used them because I like modularity. They serve to connect the wall adapter and relay circuit.
* The Launchpad used to include the G2231, but you now must order it separately since they've upgraded the dev kit with a better MSP430 variant. The code in this instructable will only work with the MSP430G2231 unless adapted properly.
* If you have trouble reading the schematic, you can open it with Fritzing. Take note that the Fritzing file does not have the completed relay section due to the way I made the schematic
* Female headers are not needed for the LM7805 (shown protruding from the board)
* The 8V in the Fritzing file is VIN
Step 3: Relay Circuit Assembly
It is important to isolate the relay circuit from the low-voltage microcontroller side. Even though there is no opto-isolation here, and although not completely necessary, it is a good idea to put it on another board in a different enclosure.
I am using the assembly I made in this Instructable.
We will be using the current schematic in this instructable, but the principles of hardware assembly should be a good guideline in that instructable. Note that because of the electrical design of the circuit in the linked instructable, I have to use 12V VIN for this project (and to drive the relay).
Guidelines for picking a relay (if you didn't use the Sparkfun link):
* Should be SPST since we're only driving a single load
* Should be rated for at least 5A if driving a standard 60W bulb just to be safe
* The coil driving voltage should be less than or equal to the unregulated voltage input to the microcontroller circuit
Step 4: Mount the Sensors and Circuitry
This is probably the easiest part! The PCB does not have to be mounted in the same manner in which I have it attached, but the sensors must be oriented in the same way, and mounted the distance apart from each other that is outlined in the dimensional drawing. If they aren't, the device simply won't work properly as it has been calibrated for this distance.
Connect the IR sensors to the male headers on the PCB. The order does matter; we will find out when we test it whether or not we need to switch the sensor cables.
Step 5: Wall Mounting
What we'll need:
* 3M 'Command' wall hooks
* Zip ties or wire ties
Both of these can be found at most hardware stores such as Lowes or Home Depot
This can be done a number of ways. I live in an apartment that is stringent on property damage, so I've used these cheap and easy plastic wall hooks. They have an adhesive backing that does not peel paint when removed from the wall.
Space them evenly such that they hook onto the ends of the backplate, and then plug in the wall adapter and relay circuit.
You may have a lot of wires at this point, so it's probably a good idea to clean them up with some wire ties or plastic zip ties.
Step 6: Programming
Luckily I've done it so you don't have to! I used IAR embedded workbench to compile my code, so if you use Code Composer, you may need to rewrite some code to qualm the compiler. This was my first time with the MSP430, so if you use something other than the Launchpad for programming, you're on your own.
If you have any questions about the code, direct them at the comments, or send me a message; I'm glad to help.
Step 7: Testing & More Side Notes
Plug in the wall transformer and attach it to VIN in your circuit. Connect the relay to the other female header pins. Make sure the red LED turns on; if not, immediately disconnect the power and check all connections.
I have tested this code enough to make my eyeballs melt, and it works flawlessly 100% of the time, as long as:
* Nobody is immediately behind you when walking in/out of the room
* You don't use the door. Unfortunately, this is not a computer vision-based project and therefore is unable to distinguish between a person and a door
Results with more, um, larger-sized individuals may vary...
A better video will be coming soon!