Showing posts with label sensors. Show all posts
Showing posts with label sensors. Show all posts

Wednesday, August 12, 2015

Concept: off-the-grid sensor module Part 2

Well over a year ago I wrote a bit about off-the-grid sensor modules. TL;DR: combining an ATmega328 with an nRF24L01+ and using low power mode makes battery powered sensor modules very feasible. Adding a small solar panel and a LiPo battery with charger circuit makes it awesome.

At that time it was just an idea that wasn't that high in my things-to-do-list, but in the past few weeks that idea has become more relevant. In our office I've got a bunch of plants. For those interested, we're trying to grow pepper and paprika plants here, which is a big step up from the indestructible spider plants that are infecting the office building :) I've got over a dozen plants, some of which are not in my room, but I'd still like to keep an eye on them. Now I could just walk into the other room, check on the plants daily and say hi to my colleagues while I'm at it, but where's the fun in that?

Instead of some physical and social labor, I'd rather just have a dashboard to monitor the soil humidity of my plants, because that's what any other sane person would want, right? Besides, I have a Raspberry Pi sitting on my desk that's just begging to be used for something useful.

I initially thought about having a single Arduino Uno connected to a bunch of sensors and to the RPi using SPI, but that's going to get messy with all the wires so I got rid of that idea pretty quickly. Instead I'd rather have tiny sensor modules. There's no need to integrate the nRF24L01+ and the sensor in the design; those can be bought separately for way less money than it would cost to integrate them. Instead the module should just have a socket to plug one of those cheap nRF boards in and a connector for a sensor. And we're going to need power of course. Using an ATmega would be overkill for this kind of project. An ATtiny would be a better choice since it's smaller, uses less current and is cheaper, plus I've got a few in my parts bin.

So what do we need?
  • connector for a nRF24L01+ module board (2x4 pins)
  • 3.3V power supply for the nRF
  • connector for an analog sensor (1x3 pins)
  • programming interface connector (ICSP, 2x3 pins)
Since the module needs to be as small as possible I would like to use an ATtiny in a soic-8 package, those are about 5x5mm in size and have eight pins. Two of those pins are for VCC and GND, and a third one is used for RESET (required if you ever want to (re)program it. So that leaves 5 pins for other uses. Here's a pin layout of the ATtiny I have lying around:
And here's the pin layout for an nRF module:
That's a lot of pins:
  1. GND
  2. VCC (3.3V)
  3. CE (Chip Enable), needs to be controlled
  4. CSN (Chip Select, SPI), needs to be controlled
  5. SCK (SPI Clock), needs to be controlled
  6. MOSI (SPI Master Out, Slave In), needs to be controlled
  7. MISO (SPI Master In, Slave Out), needs to be controlled
  8. IRQ, we're not going to use this.
So we're going to need 5 pins just to control the nRF, occupying all the available pins on the mcu. Fortunately, Nerd Ralph has a brilliant solution to this problem, using just a few off-the-shelf components to multiplex the CSN and SCK pins. This frees up one pin on the mcu which we can now use for other purposes. Ralph also uses an LED to drop the voltage from 5V to roughly 3.3V. That's not something I'm going to use since I also want to be able to power the module from a 6V solar panel or a 3.7V LiPo. Instead I'm going to use a LP2985-N voltage regulator. It has a very low dropout voltage of max 300 mV @ 150 mA, meaning it can deliver 3.3Vout with just 3.6Vin. At 50 mA the dropout voltage is even less than half of that, making it suitable for running off a LiPo battery. Other advantages are that it comes in a very small 5 pin SOT-23 package and costs about $0.25. The ATtiny will also be powered by this regulator since it can't handle the 6V a solar panel can deliver.

Another thing Ralph did was tie CE to VCC. However, this means the nRF can no longer transition between RX and TX mode without powering down.

So, fire up KiCad, throw in all components, wire them together and start laying out the board:

Neat eh? That's just 32 by 11 millimeters, or 1.25 by 0.45 inches. So it's even smaller than the nRF board to be plugged in. The IC1 pads and silk screen look a bit weird, that's because it actually are two footprints on top of each other. The ATtiny13A comes in two SOIC packages: a small one and an even smaller one. This board accommodates for both packages.

The ADC pin can be used as a digital or analog input, or you could use it as a digital output (for controlling a relay for example). The ICSP connector allows for changing the firmware (remember to unplug the sensor and nRF in case your programmer uses 5V).
The bare material cost per board is about $2.70, an nRF module costs around $0.80 and a three pin soil hygrometer costs $1.35. So for less than $5 you've got a wireless module to monitor your plant!

It still needs power though, but I'll leave that topic for a future post.

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

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.