Winter Break Projects 3: Portable Infrared Switch (Arduino powered)

Ready for Deployment

I have a set of under cabinet lights procured from IKEA many years ago. I have them installed under a shelf above my bed, where they are perfect for reading. They are great in most respects, but the switch was one of those cheap plastic rotary lamp switches that's integral to the lamp cord. After several months of daily use, the switch began to crumble. Every day a new piece would fall off until there were no more pieces.

At first, I alligator clipped a switch onto the cord and figured I'd devise some clever switch for it someday. That day has come.

-----------------------------------------------------------

First off, I'm not an infrared switch fan. Anybody whose ever used a public restroom has a certain dislike of them. There are countless comedy sketches about the infrared faucet that only turns on when you're walking away. But, I was tinkering with some infrared parts I had on hand and thinking, "What can I use these for right now?" I figured a light switch was worth a shot at least.

This is an Arduino project. I hesitated to use a whole ATMEGA chip for just a light switch, but I was just having fun and figured the circuit was temporary anyway. Plus it was an older 168 chip I had laying around.

 

My first hurdle was the IR emitter/detector pair itself. It was something I had picked up a year prior at Radio Shack because I figured I'd mess around with it someday. The packaging was very uninformative, and my early results weren't very hopeful. I searched for a data sheet. Many would laugh at the thought of finding a data sheet for something purchased at Radio Shack. Every component they sell says "Product may vary from depiction." So basically, they sell a range of 'similar' components under the same part number. Unless the component has clear markings, you never know exactly what you've got. My search result yielded only a plethora of posts claiming that the specs on the plastic bag were all wrong. I decided to proceed with caution and experiment until I figured them out. I only had the one pair, so if I fried them, the project was over. Eventually I started to get results.

Next I needed a plan to switch the light via the atmega chip. I assumed that the light would draw more current than the chip could source, so I decided a transistor could switch the light while the atmega switched the transistor. The lights were 12V, so I used a 12V relay as a dummy load for bench testing.

The detector changes it's resistive properties based on either it's distance from the emitter (when they are pointed at each other), or the amount of IR reflected back into the detector from the emitter (when they are side by side). Almost all simple sensors I have encountered operate by changing their resistance based on what they are sensing. The atmega chip doesn't measure resistance, so like most Arduino based sensor/actuator projects, a voltage divider is necessary. With two resistors in series, the voltage gets divided between them. The voltage reading at the junction between the resistors will change as the resistances change. The chip can read these analog voltage values and convert them into a 10 bit binary number, giving us a range 0 -1023 (representing 0V - 5V).

To control the relay, i used a 9306 NPN transistor. Any basic NPN should do the trick. I connected 12V to one pole of the relay coil, and the other pole to the collector of the transistor. The emitter goes to ground, and the base connects to any digital pin on the atmega chip/arduino. When that pin goes high, the transistor conducts, and completes the relay coil circuit, energizing it.

With the hardware setup, I turned to the software. I started very simply, just reading the 10 bit values coming in from the voltage divider and sending them to the serial monitor. This allowed me to see what range of values I would get when an object was present in front of the sensors. With that data, I used a conditional if statement to check for those values. When those values were read, it meant my hand was there, and the light should turn on. This would mean that the light would turn off when I removed my hand, so more work needed to be done.

The other problem is that this switch has only one active condition, which is the presence of my hand. That condition has to both turn the light on AND turn the light off. You can't give a computer opposing instructions for the same condition. Basically, rather than simply checking the condition of the sensor, I needed to write something that would turn the light on when my hand was present, but also latch it in that state. Then, when my hand was present again, it needed to latch in the off state. I was reminded of an older arduino book I had called Getting Started with Arduino, by Massimo Banzi. In it, he has a simple and elegant means of toggling states like this.

The basic idea is to use a variable equal to 1 minus said variable (state = 1 - state).
So if state =  0, then 1 - 0  equals 1. If state =  1, then 1 - 1 equals 0. If state = 0, it means there has been no change in the condition, so do nothing (if the light was on, keep it on - if it was off, keep it off). If state = 1, it means the condition has changed (my hand has moved in front of the sensor). In that case, it toggles the the status of the light (if it was on, it turns it off - if it was off, it turns it on). It took a lot of tinkering to get it all just right.

Before I finalized the circuit and code, I needed to test it with the actual light in question (remember the relay was just a 12V stand in). While checking the polarity of the lights 12V power supply, I discovered that it was AC, not DC. Of course, that makes total sense (they are AC halogen bulbs) but I get so used to making lower voltages synonymous with DC. I was hoping to have that 12V supply be the one and only supply for the whole circuit. Now I would need to build a bridge rectifier to pull that off. I had a piece of perfboard and an Altoids tin slated for the final circuit, and there just wasn't room.

My epiphany was to keep the relay in the circuit. That turned a simple light switch into a "whatever I want to switch" switch. Then I didn't feel like I was wasting an atmega chip. In addition, the relay was a DPDT, so I could switch more than one thing (two devices on/off simultaneously or opposite each other). I could have up to four devices connected (one on each throw of each pole) and switch between them in pairs (A+B on OR C+D on). My voltage is limited only by the relay, which is rated for 250V, and I can use a different voltage on each pole if needed. The relay was a little tall for the case, so it lays on it's side instead of being soldered through hole style.

I do have 3 changes I plan to make:
Add a power jack so the power supply can be removed (any supply from 7V to 15V should work) making it even more portable.

Swap in a trim pot for the fixed resistor paired with the detector so I can adjust it for different light level environments.

Swap the 12V relay for a 5 or 6 volt so I can use a smaller supply. It will also cut down on the heat, though the regulator is attached to the tin which makes an ample heatsink.

Below are links to the Eagle schematic and the Arduino code.

Thanks for reading. Keep making stuff.

AttachmentSize
irSwitch.sch320.36 KB
irLightSwitch.ino1.55 KB

Copyright 2012, Clockbroke. Contact Me