Wednesday, April 16, 2014

LED dimmer part 2: motion activated

My PIR motion sensors finally arrived today. I added one to the LED dimmer from the previous post. The motion sensor acts like an on/off switch that you can override using the touch sensor.

The motion sensor detects movement using infrared and it outputs a high signal when it does. This triggers the dimmer into its on state. A couple of seconds after the signal goes low (no motion) the dimmer goes into off state.


While the dimmer is on (caused by the motion sensor), you can force it off using the touch sensor. There are two potentiometers on the sensor; one for sensitivity (I left it at center position) and one for time. The time pot controls for how long the sensor will output a high signal after detecting motion. I use the lowest setting and handle the rest of the timing in the micro controller since tuning the pot is really inaccurate and annoying. There appears to be a forced 4 second delay between two high signals. No idea if that can be trimmed or not.
There's also some space allocated on both sides of the sensor board for a light sensor, so it will only detect motion when it's dark.

The Arduino program can control up to 6 output channels (could be more, but it doesn't look like I need more) and has 8 inputs, any of which can be motion or touch sensors. There are 256 levels of brightness, but the distribution of actual brightness does not seem to be very linear, it looks more logarithmic to me; it ramps up to full brightness really fast and doesn't do much after that. I'd like to have better control in the low brightness region...

Sunday, April 13, 2014

Adjustable single button LED dimmer

I'm planning on adding some LED strips to my bed. I currently have one small light next to it, and it's always annoying to find the switch, it often ends up with the light on the ground. And when it's on, it's usually too bright. So I thought, what if I add a LED strip to the back of my bed? And what if I could switch it on and off by just touching some part of the frame?

I have one of these IKEA bedframes. Putting a LED strip on the back of the headboard should provide a decent amount of light. Adding a capacitive touch sensor at a sensible, easy to reach part of the frame would make a nice on/off switch, possibly concealed by a piece of matching veneer for good looks.

But what about the brightness? I'd like to be able to control the brightness without having to deal with potentiometers or reprogramming. Well, my sister in law has one of those IKEA lamps that you can turn on and off by just touching it. What's even cooler is that you can adjust its brightness by just holding it.

The logic behind it is pretty simple. Touch (a button) shortly and it will switch the light on/off. Hold it longer and the brightness increases until it reaches full brightness, after that it will decrease. If you let go and hold it again the direction of brightness change also changes. Like this:


I'm using a touch resistor here since my capacitive touch sensors haven't arrived yet, but the effect is almost the same. Touching that strip at a given place will make it have a resistance between 0 and 10KOhm. It's like a potentiometer and the strip is the wiper.

I'm using a small BC337 transistor here to drive the LED, but I can use it for LED strips as well (up to about 1.5 meter) which will be perfect.

There's also a bit of fading code in there to smoothly turn the light on and off. The PWM signal is generated in software using a timer. The code is not really worth sharing at the moment and shouldn't be hard to figure out.

Sunday, April 6, 2014

Buzzer game

You've probably all seen this type of game before, where you have to guide a metal ring over a metal wire course without the ring touching the wire. It's nothing more than a buzzer, some batteries and perhaps a light/LED, but it's a great way to teach kids some basics about electricity and electronics.

A few weeks ago I had the pleasure of building 35 of these buzzer games. I assembled the battery holder, buzzer, an LED and resistor and some wires.
Each "kit" had a piece of steel wire about 4 feet / 120cm in length with some small loops on both ends for easy mounting. A second straight piece of about 10 inch / 25 cm in length soldered to the negative wire of the battery holder (and some extra wire in between for extra length). And the buzzer/led/resistor wired to the battery holder, with a bit of bare copper wire to connect it to the steel wire.

Needless to say, I spent roughly 8 hours preparing everything. All the kids needed to do was bend the long steel wire into whatever shape they would think would work, and mount it on a piece of wood using two screws. Then they'd have to mount the battery holder with another screw, and connect loose piece of copper wire to their now very oddly shaped steel wire. Lastly they had to bend the small piece of steel wire. Doesn't sound like much work, but don't underestimate how much time kids aged 8 to 10 need for even the most trivial tasks...

Unfortunately I forgot to take pictures of their creations, but here's the example I prepared myself:


In hindsight, I wouldn't solder everything. Instead you're better off just using a bunch of plastic terminal connectors. Don't connect everything with really thing copper wire, it breaks easily. And of course, don't use solid core copper wire but stranded.

I made another one, which is slightly larger:
It has a 12V LED strip on the base, is made of much thicker steel wire and is powered using a 3 cell lithium polymer battery from my RC parts box. There's a piezo buzzer soldered onto the power rails of the LED strip. I'd like to expand on this a bit further, add a bit of logic. The LED strip and buzzer should be latched, meaning they don't turn off as soon as you break contact. They also shouldn't remain on continuously when there's contact; that's just too annoying. I'd also like to add a few red and green strips and add "lives" to the game: you'd start with three lives, and lose one each time you make contact. After the last life is lost the red strip lights up indicating game over. An ATtiny, a few transistors, some resistors and a voltage regulator are probably all that's needed to build this.




Thursday, April 3, 2014

Concept: off-the-grid sensor module

I love the idea of home automation. I'd like to keep an eye on temperatures inside and around my house, monitor power, gas and water consumption, have smart lighting, etc. The possibilities are endless, and they are in fact feasible and affordable nowadays. If I were to get a new house, I'd make sure I'd have multiple power outlets in every wall, with LAN and 12 or 5 volt as well.

Unfortunately the house I bought was built in 1968, has very few outlets (2 per bedroom at most) and walls you really don't want to put a drill in, unless you like plastering and painting. So having temperature sensors in every room would involve lots of extension cords, wall warts and possibly LAN cables.

Going wireless

The first thing to get rid of is LAN cables. Let's go wireless instead. Since my house has only wooden floors and no thick reinforced concrete walls, there should be no problem getting coverage on all three floors. WiFi and ZigBee/XBee is a bit too expensive, so I'd rather settle with the Nordic nRF24L01+, you can get those 2.4 GHz boards with PCB antenna for as little as $1,50 a piece from eBay, whereas an XBee would cost about $20 to $30 a piece. Even though the range on the nRFs is limited, it should be enough. I don't need much bandwidth either; the lowest setting (256KBit/sec) should be plenty.
There should be a master unit somewhere in the house, the meter cupboard would be a good candidate; it already contains my switch, NAS, gas meter and power meter and fuse-box. This master unit should collect all the data from the sensor units and have an Ethernet interface to present the collected data to a web server that can host a front-end (my NAS for example).

The sensor units need power to operate obviously, but I don't like the idea of having to use a wall wart for every single sensor unit, nor do I want to have put low voltage power lines throughout my house. Having wall warts would not only occupy most of my power outlets, but also adds unnecessary power consumption. One way to get rid of that is by using batteries, but how long will those last? Well, bring out the datasheets and calculator!

Calculating power consumption

The ATmega328 used in  the Arduino Uno and Nano has a couple of low power options. First of all you can run it at a lower clock speed using the 128KHz internal oscillator, this reduces active consumption from around 10 mA to just 0.12 mA; quite a difference, and it frees up 2 IO pins :) If we reduce Vcc to 3.3V then we go down even further to about 0.06 mA. If we make it enter idle mode then power consumption drops to a measly 0.015 mA. Enter power save mode and we're down to 0.001 mA, sweet.

The nRF24L01+ doesn't exactly consume loads of power either. Full power transmission requires about 11.3 mA and receive mode at 250 Kbit/s needs 12.6 mA. Since I'm only planning on sampling data once every five minutes or so, we can just turn the nRF off for the rest of the time. Power down supply current is 0.0009 mA.

A simple temperature and humidity sensor like the DHT11 draws about 0.5mA when sampling and 0.2mA on average. We can probably power this straight from one of the ATmega's IO pins, so we can turn it off completely when we don't need it. A light sensor also draws current in the order or milliAmps, so we can use the same trick there to reduce consumption in low power mode.

I have a bunch of 120mAh Lithium Polymer batteries from a micro helicopter. LiPo's provide 4.2V when fully charged but shouldn't be drained below 3.5V. ATmega's and a lot of sensors will have no problems with this kind of supply voltage range, but the nRF requires 3.3V so we'll need a low dropout regulator (LDO) with a very low dropout voltage. A normal 7803 won't do, it needs in input voltage that is at least 1V above the desired output voltage. An MCP1700 will do the trick, it has a dropout voltage of 200 mV at full load (250 mA), perfect. The current drawn by it is extremely low as well; less than 2 micro amps at room temperature. We can use this to power everything, or just the nRF, I'm not quite sure yet.

Running the numbers

At full load the sensor setup will draw 0.06 (ATmega) + 12.6 (nRF) + 0.5 to 5 (sensor) mA of current. Add a bit for supporting hardware and our ballpark figure is 20 mA. In idle mode we end up in the micro amps range. 1 (ATmega) + 0.9 (nRF) + 2 (MCP)  = 4 uA or 0.004 mA. In order to calculate battery life we have to know the ratio between idle and active time, or the duty cycle. Say we sample and transmit data once every 5 minutes (300 seconds) and we can do this within 3 seconds, then we end up with a duty cycle of 1%. So 1% active vs 99% idle. Or average power consumption ends up being 1% x 20mA + 99% x 0.004 mA = 0.20mA (plus a few micro amps). Since my batteries have a capacity of 120mAh (meaning a 120mA device will drain it in 1 hour, or a 1 mA device can run on it for 120 hours), we end up with a battery life of 120 / 0.2 = 600 hours.

Now 600 hours sounds like a lot, but it's just 25 days. That means replacing and recharging batteries 15 times a year, probably more since you don't want to drain them completely. Having a bigger battery helps a bit, 1800mAh batteries are widely available for as little as $5, that would reduce recharging/replacement to once a year. Battery voltage can be monitored using the ADC and a 3.3V reference voltage and we can just add the voltage information to the sensor data.

Why stop there?

Why not create something that is truly off-the-grid and recharges itself? Small 6V solar panels are dirt cheap and so are small LiPo recharge controllers. A small 85 x 85 mm panel may deliver up to 150 mA at 6V and costs less that $5. A complete TP4056 charger controller board costs less than $3 and it'll accept anything between 5 and 8 volts on the input. So for less than $10 we can make the sensor unit self powered! Even if the panel gets only one hour of sunlight, that should be enough to recharge the battery with 100mAh, or 20 days of juice, that's plenty! Even something like a 50 mA panel will do the trick, as long as you can get at least 5V from it. Heck, even a couple of solar cells from those old calculators might do the trick, although you probably need a certain minimum current for the charger to work.

Efficient remote controlled switches

One last idea I'd like to pitch is a remote controlled AC switch. This kind of device would be perfect if you need to control a much larger current and already need a wall wart. The idea is to add a solid state relay between mains and a wall wart. The wall wart powers the charger and additional current hungry equipment like led strips. The ATmega can simply switch the relay on whenever current is required, or when the battery runs low. In case of automatic lights you no longer have the loss of the wall wart when the lights are off (which they are most of the time). The additional current required to recharge the battery is completely insignificant compared to a decent LED strip. The same trick can be used to create remote controlled AC switches for turning other devices on and off.