Room Visitor Counter With Relay Control

53K13410

Intro: 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)

Achtung! Warning!
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

     LEDs:
* 3 blue (or whatever color you'd like for the binary counter)
* 1 red (POWER LED)

Assembly: 

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! 

Technical stuff:

* '''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.

NOTES:
* 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

Once you've programmed the microcontroller, pop it in the DIP socket.

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! 

8 Comments

awesome! I like how you used two sensors to determine the direction of movement. looks like it works great!

this looks great, but how long is this project being tested by anyone continuously at their home or other place and found error-free or if they experienced any issues like EMI interference or other installation issues, Please share

"...given this room only has one means of entry...."

Not necessarily.

Some time ago I devised (but never got around to build, that's my curse) a system with a pair of IR detectors several cm apart, an IR LED between them and a mirror across every doorway leading to / from a room, all of them feeding their signals to a single controller.
Thus, Someone can enter the room, the counter increments to 1 and the lights turns on inmediately. Someone else enters, it increments to 2. One of them leaves through another doorway and it decrements to 1. After a while the other one leaves, and only then the lights go off (after a brief delay in case someone returns).

From there, I realized that a natural extension was to have a pair of sensors in every doorway, enabling the system to "know" how many peope are in every room (people who increment some room head count always decrement some other room's head count, if one define "outside" as another room).

Catch #1: How do one turns off the light whil remaining in the room? Maybe waving a hand in front of the sensor pair in the "out" direction. Of course, the system should not allow for negative counts (i.e. leaving the lights off when leaving a room whith zero count)
Catch #2: Too many wires everywhere. Maybe only wire power to the sensors and have them transmit a sequence of pulses encoding sensor and direction? 
Fantastic! Keep going! Is there a way to hide the whole system inside the drywall, leaving only two peepholes for the sensors? Put 'em in all the rooms, and take out all the manual light switches!

This could also be helpful in case of burglars or other unwanted visitors. Someone trying to hiding in a room wouldn't be able to turn off the lights, so if you come home and find the lights on...
Actually, I think that leaving it at pet height (or mounting it on the ceiling so that height is not an issue could be a good thing.

Seeing lights going on and off at random (whenever the pets moves about) is an excellent way to deter would- be-burglars.
That would definitely be cool, yes; I'm sure you could cut a small slit in the drywall, fit in the sensors an then full in the gaps with plaster of Paris or something of the like!
it would be cool to see the satistical data on which room gets the most use. with this set up it would be easy to save a file that is appended to every day with how min and max number of people to go in and out.

could also control how much lighting is actually used by the number of people in the room.

also last thing i would do is increase the height so that a house pet isnt turning on the lights since they really dont need them.
Ooh that's actually a really good idea! This would open up some really cool projects if made en masse and put into a sensor-node network in a household.