Building an Arduino based temperature, humidity and light sensors (Update)
17 June 2015
Recently I came across the MySensors project. This open source project for the Arduino aims to make building sensors and actuators for the internet-of-things easier. It integrates with a variety of home automation systems of which Domoticz is one of them. This project caught my interest as with this I could add sensors and actuators to my domotica system at a fraction of the cost of commercially available sensors. The sensors communicate wirelessly to a gateway and form a mesh network much like z-wave operates. This saves me from having to put wires everywhere and the mesh network will increase range and reliability.
I ordered some parts on Ebay and AliExpress and downloaded the MySensors software and got started. The first sensor I build is a temperature, humidity and light sensor. It's for outdoor use and battery powered. The light sensor I want to use to switch on some outdoor lights I have when it gets dark.
First the parts that will form this sensor. The heart of the sensor will be a Arduino Pro Mini. These come in 4 different versions and I'm using the 3.3V 8Mhz version. The sensors and radio all work on 3.3 volts and 8Mhz is more than enough for what it has to do. If you have the 5V version of the Arduino you will need a stepdown converter to convert the 5V to 3.3V.
The complete list of parts:
- Arduino Pro Mini 3.3V 8Mhz
- DHT22 temperature + humidity sensor
- BH1750 light sensor
- NRF24L01+ wireless radio
- 2x 18650 Lithium batteries
- Battery holder
- 1x 10 uF capacitor
- 1x 1 MΩ resistor & 1x 470 KΩ resistor
- 1 FTDI usb to serial convertor
Besides these parts you will need some wires, solder and a soldering iron to connect everything together. I haven't included a case as I'm planning on designing and 3d printing my own. A breadbord and dupont cables are needed for prototyping.
A note on ordering parts via Ebay or AliExpress. It's possible to get these parts at incredibly low prices and including free shipping. However as these suppliers are located in Asia and for shipping the cheapest option is used it will take several weeks before the parts are delivered. Count on 2 - 4 weeks. Some of the parts arrived within a week but most to between two to three weeks to arrive. I always order multiple quantities of the same part. Parts like the Arduino and the batteries I will use regularly for different projects I'm building. As the price is low it's very affordable to keep a few in stock. For these prices the quality control isn't always what it should be and is when you order from your local supplier. So with ordering multiple quantities I'm sure I don't get stuck waiting for a replacement part because of a faulthy item.
Prototyping the hardware
First I've wired all the parts together on a breadboard to test if it would work and work on the software part. Correcting any errors is much easier that way then having to resolder everything.
For prototyping I have one Arduino that I have soldered the header pins to. I keep this aside for prototyping and get a new Arduino to solder all the components to when the prototype is finished and everything works. This way I don't need to (de-)solder the header pins all the time.
First I wire the radio to the Arduino. The radio will be used to send the sensor values to the gateway. The gateway will pass the values on to the controller (Domoticz). The radio used by MySensors is the NRF24L01+. It's small, has a theoratical range of 60 meters and will form a mesh network with other sensors nodes that are setup as repeaters. The radio works on 3.3 volts and as my Arduino outputs 3.3 volts I can connect the radio directly to the Arduino. The cables for the VCC and the GND are connected via the breadbord as those will be shared with the other sensors. The rest of the cables connect directly between the Arduino and the radio.
|Arduino pin||Radio pin|
Now that the radio is connected the DHT22 and BH1750 can be connected. GND and VCC are wired to the Arduino via the breadbord. The DHT22 has the pins placed in such a way that it can be stuck on the breadbord directly. For the BH1750 I need wires.
Wiring diagram for the BH1750:
|BH1750 pin||Arduino pin|
Wiring diagram for the DHT22:
|DHT22 pin||Arduino pin|
The pins are numbered 1 to 4 from left to right when you look at the sensor from the front. That's the side with the openings in it.
There's also a DHT11 version of this sensor. It wil also work but there are some important differences depending on how you want to use it. The DHT11 is a bit cheaper than the DHT22 and the DHT22 is more accurate and has a greater measuring range. For indoor use I use the DHT11.
The differences are:
|Temperature range||0 to 50°C||-40 to 125°C|
|Sampling rate||once every 1 second||once every 2 seconds|
Now that all the sensor parts are connected it's time to write the code and test if everything works and data is reported to Domoticz.
Writing the code
As said I'm using MySensors as a base for developing the sensors with. I downloaded the source code from their website and put it in the libraries folder of the Arduino IDE. The source code file for this sensor is available on my Github account. It has comments in it to describe all the elements of the source code.
As the sensor will be battery powered and I don't need a sensor ready every couple of seconds I set the sleep time to 90 seconds. Every 90 seconds the Arduino will take a sensor reading. It will then compare it to the last known value and if it has changed the new value will be send to Domoticz and stored in memory as the last known value. This way the radio doesn't need to power up and transmit when the values haven't changed saving valuable power.
To upload the software to the Arduino you will need to connect the FTDI connector. The Arduino Pro Mini doesn't have a USB connection to connect it to your computer. That's what the FTDI connector is for. You will only need one of those as you disconnect it from the Arduino after the software is uploaded.
Connecting the battery
Till now I have tested the sensor using USB power coming from the FTDI connector. As I don't have a power outlet where I want to place the sensor it will need to run on batteries. For this I use two 18650 litium batteries. These batteries are specified to 3.7 volts but I measured just over 4 volts when fully charged. As it's more than 3.3 volts and the voltage will fluctuate these need to be connected to the raw pin of the Arduino. The power converter in the Arduino will convert it to 3.3V. As each battery is 5000mAh the sensor should be able to work for a long time on a single charge.
I use a dual battery holder to connect the batteries. The batteries are connected in parallel so the voltage stays at 3.7V and the capacity doubles to 10.000 mAh. We don't need the higher voltage but we do need the higher capacity.
To keep an eye on the battery charge level I added to resistors and wired that to one of the analog inputs of the Arduino. The Arduino can then report the battery level to Domoticz. When the level get's low I can see it and charge the batteries.
Adding to Domoticz
Adding the sensors to Domoticz is very simple. Just power on the sensor Arduino and you're almost done. If you changed the default setting of Domoticz to not detect new hardware you will need to click the "allow for 5 minutes" button in the settings menu. Domoticz will detect the sensor and list it under "Devices".
Enable the sensors listed in the Device screen and give them a name and done. Now that the devices are enabled I can start using them for scripts and other things. The light sensor will be listed under the "Utility" tab and the temperature + humidity will be under "Temperature". The temperature and humidity are combined into one device.
The sensors and Arduino need some protection from the elements so I designed a case and had it 3D printed through 3D Hubs. It was my first 3d printing project. With some tips from the guy printing the case I managed to get a good case for the sensor.
The first design had walls of 1 mm thick. This was too thin to print well and make a sturdy case so I updated the design and made the walls 3 mm thick. The top and bottom are screwed together with M3 screws. The round "pilars" where the srew goes proved to be too thin. I've broken three of them while adding the threads and had to glue them back. Next time I'm going to do this differently. Also the battery holder is taller then I thought or maybe it's because I increased the wall size. The case doesn't completely close in one corner. For a first project I'm happy with the results.
To give you an idea what the total costs are of this sensor here's a list of the parts with the price I paid for them.
|Arduino Pro Mini 3.3V 8Mhz||$2.49|
|DHT22 temperature + humidity sensor||$3.34|
|BH1750 light sensor||$1.99|
|NRF24L01+ wireless radio||$0.86|
|2x 18650 Lithium batteries||$3.02|
|1x 10 uF capacitor||$0.04|
|1x 1 MΩ resistor & 1x 470 KΩ resistor||$0.01|
Total costs: $12.52