I've been meaning to make something cool for my dorm room this coming semester and decided that some custom closet lights would look great. In this Instructable, I'll show you how to make some nice-looking LED lights that will turn on automatically using a hall effect sensor and a magnet.
Edit: I've noticed a lot of people are hating on the excessive control used in this project so I just wanted to clarify a few things:
1. This instructable was also meant to be a lite introduction to actual AVR programming for those people who are used to only Arduino programming. I had a bit of trouble finding useful information when I was learning so I figured it would be nice to help out some others. That is why I posted the basic tutorials along with my AVR code.
2. Yes I'm aware I could have simply used a reed switch to switch the LEDs when the door opened and closed. I wanted to leave room open for myself to add different light modes, maybe using more wires and pins to create nice fading effects, possibly a remote control sensor, and maybe even an auto-shutoff routine.
Step 1: Gather Materials
The usual tools:
1. Soldering Iron/Solder
2. Electrical tape
1. Hall Effect Sensor-(one used can be found here: http://www.sparkfun.com/commerce/product_info.php?products_id=9312
2. Attiny85-(any micro can be used, mainly just for quick and easy digital input readings)
3. Led's of any color
4. Cases for the lights,
5. Wire and Wall wart
6. Resistors (1k and 3x33Ohm used in mine)
Note: A microcontroller isn't necessary for this to work. If you can get the sensor to successfully switch a transistor to saturation, you should be able to skip the whole controller and coding part.
Step 2: Prepare the LED Housings
The cases I used for this project are Icebreakers mint containers because they're very easy to Dremel through and can fit small circuit boards pretty well. All you need to do here is pick a pattern you like and drill the holes, but be sure to make the holes just big enough for the LEDs to fit through.
Note that the pattern seen below is just an example, I used a simple triangle pattern for the lights in this project.
Step 3: Solder in the LEDs
The next step is to place the LEDs in their holes and solder them together. I found that bending the positive leads in until they touch makes soldering alot easier. I attached a wire to the positive leads, taped over them with electrical tape, and did the same for the negative leads. It also saves space if you solder one leg of a resistor to either the positive or negative leads so it doesn't have to be done on your protoboard. To connect the LED units, i drilled two holes into the side of each case, ran the casing down the wire, stripped the wire and tied together the main lines to the LED wires.
The only reason I used three separate 33 ohm resistors in each case, rather than one 10 ohm resistor is because I soldered together the first light with a 33 ohm resistor and didn't feel like desoldering anything :) .
Step 4: Arduino Test
I usually test out my AVR projects on an Arduino simply because of the ease of debugging. Below is the code I used to test the output of the Hall Effect sensor. I'm not going to go into the deep workings of the Hall Effect here but if you're interested, wikipedia has a bunch of good info.
What's cool about this sensor is that the output is latched. For those of you who don't know what a latch is, it's one of the most basic memory elements in all computing. This particular latch will remember a digital High reading at the output of the sensor until an opposing magnetic field is sensed, at which point it will drop to digital Low and stay there until it is triggered again.
What this means for our code is that we only need to scan the inputs and turn the lights on or off based on what the sensor's memory holds. If you wanted to make this project really energy efficient, you could set up an interrupt to wait for a sensor trigger but since I'll be running this from a wall wart, it doesn't really matter that much to me weather the microcontroller uses 10milliamps or 50.
Step 5: Moving From Arduino to AVR
The next step is to take our circuit and code from an arduino and transfer them to a standalone AVR chip. The reason I'm using an Attiny85 is because its the smallest AVR I have and I only needed 2 pins.
The fuses are set to an 8Mhz internal RC timer since timing isn't critical. Once done with the code, go right ahead and solder the circuit together.
Below is the code I used for the AVR. If you aren't familiar with AVR programming, most of what you need can be found here:http://iamsuhasm.wordpress.com/tutsproj/avr-gcc-tutorial/ and the tutorials here: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewforum&f=11&sid=f899212b86e2e0de2b660c0999f95fd7
#define lights PB0
#define magSense PB1
volatile uint16_t val; // variable for reading the pin status
//volatile uint16_t lightMode = 0; // variable to keep the light's state
val = bit_is_set(PINB, magSense); // read input value and store it in val
if (val) // make sure we got a 1
PORTB |= (1 << lights);
PORTB &= ~(1 << lights);
PORTB = 0b000010; // enable pull up on sensor pin
DDRB = 0b000001; // set PB0 as output and rest as input
Step 6: Put Everything Togtether
Here's a video of the top unit working with the sensor:
And here is the completed unit: