Wednesday, March 27, 2013

Closet Door Lightswitch

     There is a closet in the hallway of my house that is affectionately known as "The Only Hallway Closet". It stores my family's jackets, shoes, blankets, and batteries. The problem with this closet is that it doesn't have a light. Sure, we've tried those stick-on lights that they sell in infomercials, but they don't work that well and it's very easy to forget to turn them off. My dad bought a ceiling light for the closet, but it was never installed. I decided it was about time there was a permanent light source in that closet and I wanted to do it in style. I designed some sensors that detect when the door is opened and closed and will accordingly turn the closet light on and off. This blog post details everything about creating this project, including the electronics and installation.

No lightswitch required


Video overview

     The first thing I did was to create a power supply for this project. It will run off of 120V line voltage and have the ability to power the 120V relay for the light and a 3.3V MSP430 microcontroller and its sensors. On this power supply board, I took the 120V AC line voltage and ran it through a step-down transformer and full bridge rectifier to begin changing the voltage into DC. With only a few capacitors and a LD1117V33 voltage regulator, I had a clean 3.3V DC source for my microcontroller. Also on this board, I took the line voltage through the relay (which will act as a normally open switch), a 3 amp fuse (the relay has a 3.5 amp rating), and some screw-on connectors to attach the light to. This board also has the pins that go to the microcontroller to allow it to switch the relay on and off.


Schematic for the power board

Top view of the power board

Bottom side of the power board

     The next step was to create the board for the MSP430G2001 microcontroller. The microcontroller takes the 3.3V DC voltage from the power board, after it is decoupled near the chip. (You may also notice that this board has a programming header along the side of the chip so I can program it without removing it from the board. Unfortunately, I never got this to work correctly so it's just hanging out there.) This board connects the relay line to the microcontroller so it can turn the relay on and off. The board also has two RJ14 jacks (otherwise known as a 4-conductor phone line jack) to connect to the sensors on the doors. Each jack has power and ground along with a pin for detecting when the door is open and a pin that lights an LED when the door is opened. I chose to use these jacks since I designed the boards away from home and wanted a way to easily adjust the cable length (since I couldn't measure the distance from school), or have the ability to move the project to another door if I wanted.

Schematic for the microcontroller board

Top view of the microcontroller board

Bottom side of the microcontroller board

     The final pieces that needed to be built were the two sensors to detect when the door is opened and closed. Each one has a reed switch (which is a switch that is closed when it is in a magnetic field) that is activated when a magnet, that is attached to the door, is near. The reed switch is attached to 10kΩ pull down resistor and a 100Ω resistor so the microcontroller can sense when the reed switch is opened or closed. There is also an LED and 220Ω current-limiting resistor that lights when the door is opened. This light is helpful so you know if the door was properly shut. This board also has a RJ14 jack so regular telephone line can connect this sensor to the microcontroller. This sensor is partially visible when the closet doors are open so it has been painted the same color as the frame of the doors it is getting mounted to.

Schematic for the sensor

Sensors

Magnets to be attached to doors

     The code for the MSP430 microcontroller is located on my github. It's not the most efficient code, but it does exactly what's needed so there's no use fixing what isn't broken. Essentially, the code checks each of the switches on the doors to see if the doors are open and then turns on the light if they are. Once one of the doors is open and the light is on, it counts up until five minutes has passed and then shuts the light off, in case someone forgot to shut the door. The LEDs on the door switch will still stay on as long as the door is open so there is a visual indicator of which door was left open. Once both doors are closed again, the code resets and the microcontroller waits for a door to open.

     To put everything together, I built a case for the main electronics. It consists of two Carlon 20 cubic inch 2-Gang Square Plastic Electrical Boxes. I could not find a suitable box of the right depth at my local Lowes so I cut the bottom off of one with a Dremel and used screws to mount the two boxes together. I also cut out two holes -- one for the 120V line voltage in connector and ceiling light out connector and the other for the RJ14 jacks to attach the telephone line to. Lastly, I mounted a cover plate on top to keep the box mostly sealed. The box will be mounted with the built-in mounting plate.

Power and microcontroller board stacked together

Electronics mounted inside of the gang box

Protects everything nicely

     The next step was to mount the light into the ceiling of the closet. After measuring where the light should go and removing some insulation from the attic, I used a saw to cut the opening into the ceiling. I installed the metal mount into the hole for the light. The light mounts into this bracket with some screws and a spring holds a faceplate from the ceiling to cover up the opening. There weren't any convenient places nearby to supply constant power to this circuit so I ran a power cord down through the house into the laundry room in the basement. This cord is terminated with a 3 prong plug that plugs into an outlet in the laundry room. I chose to do this so it could be easily moved or unplugged if there is an issue. I mounted the gang box with the electronics nearby and connected the light, power, and switches. I had to mount the sensors in the closet door opening so that the magnets attached to the top of the door would engage the reed switch, yet didn't come close enough to break the glass capsule of the switch. The RJ14 cables are attached to the wall with some plastic cable staples.

Mount and hole for light

Light mounted from inside closet

Snake the power cable from the attic to the basement

All electronics mounted in attic

RJ14 cables coming both both sensors into the attic
attached to the wall with cable staples

Sensor mounted on door frame

Video of operation

     Although it took nearly a year for me to get this project from idea to installation, I think it was worth every minute I worked on it for. The closet that this project is installed in is used everyday by my parents so I know the light and ease of use will be appreciated. There's something magical about only needing to open a door to turn on a light. Some cheap electronics, a little bit of know-how, and a lot of time can really create a useful device.

Here's some extra photos of the project if you're interested: Closet Door Lightswitch on Picasa Web Albums

3 comments:

  1. Isn't the bridge rectifier wrong on the schematic?

    ReplyDelete
    Replies
    1. Yes, the orientation of Diodes D2 and D4, in the 'Schematic for the power board' drawing, are incorrect. Once reversed, the Circuit will work.

      Delete
    2. Thanks for the heads up! The schematic was wrong. It is now corrected.

      Delete