[Blog] DHT sensor library and example programs for the GR-Sakura board

Hi Everybody!

I wanted to publish this in a blog but unfortunately it is not available in the GR-Sakura group. I ask for the help of the Admin to transfer it into the Blog area (and make it available), if possible.

DHT sensor library and example programs for the GR-Sakura board

The Adafruit's  DHT library for the DHT11, DHT21 and DHT22 temperature and relative humidity sensors has been adopted for the GR-Sakura board.  These sensors use a non-standard 1-wire communication protocol, which is handled by bit-banging and sofware timing. The '1'-s and the '0' can be distinguised by measuring the data pulse lenght. The biggest difference between the Sakura and Arduino cards is the speed factor: the overhead time spent in the main cyle (when 40 data bits from the sensor are read) is much bigger in the case of the Arduino, which should be taken into account. This means, that the default parameter value used as "discrimination level" is too low for the Sakura board.In order to determine the optimal value of the "discrimination level" the debug features of the original test program were extended. 

The gr_DHT_sensor library consists of two files: DHT.h and DHT.cpp

DHTtester example sketch

This program can be used to test one of the above mentioned sensors. I used an Aosong Am2302 (aka DHT22) sensor. The schematic is shown below:

The DHTtest sketch has several debug features, but to activate all of these you have to define the DEBUG symbol. 

The DEBUG symbol can be defined in two ways:

a. You can define it by adding a -DDEBUG compiler option in the makefile (this method is preferred in the case of command line compiling)

b. You can define it in the DHT.h header file by adding the following line:

   #define DEBUG

The debug features are:

1. LED1 is ON when the board is waiting for some input from the serial (or virtual serial) port. (note the usage of the SCI_AUTO option in the sketch)

2. LED2 is ON while the relative humidity is read 

3. LED3 is ON when the temperature value is read

4. LED4 is ON if there was a communication error with the sensor 

5. Current time and Last time are printed out through the serial port (this kind of sensors are rather slow, so you should wait 2 seconds between readouts)

6. The cycle counts for each of the 40 data bits are outputted to see their statistical sacattering. This helps to determine the optimal "discriminator level", i. e. to safely distinguish the '1' bits form the '0' data bits.

7. Number of detected data bits and the read data byte values are outputted. The fifth byte (the checksum) is shown against the value calculated as the seum of the first four bytes.


 

A typical output is shown in the above figure. As one can see, the count calues can be divided into two groups:

a. lower values are between 10 and 12. These values correspond to '0' bits.

b. larger values are between 31 and 32. These values correspond to '1' bits.

This means, that an intermediate value, say 20 will be a good candidate for the discriminator level value.

"Normal output" - i. e. no DEBUG mode

When you don't define the DEBUG symbol, the only the relative humidity and temperature values are outputted. See the figure below.

DHT22_datalogger example application

This example application is the adoptation of the DHT11 + ChipKit UNO32 project from Embedded Lab. It consists of two parts: 

a. Firmware running in the Sakura board, which in each 2 seconds measures the temperature and the relative humidity, and sends data to the PC (through the USB, as virtual serial port).

b. PC application witten in Processing/Java which automatically connects to the first serial port and displays the received data in a graphic window. 

The hardware schematic is the same as in the firts figure above, i.e. the data wire is connencted to pin 2.

Changes in the software:

- Due to the higher resolution of the DHT22 sensors, we modified both the firmware and the PC application (in our version floating values are sent...).

- Due to the extra decimals the graphical display of the relative humidity was slightly reorganized.

You can see a typical output in the next figure. 

As you see, there is also a "Start Log" button. When you click on it, the measured values will be logged, together with a date/time stamp. When you click again on that button, the data logging will stop. (The sudden increase in the humidity happened due to my breathe...)

MM  DD  YYYY  HH  MM  SS    C     F   RH(%)
04  21  2013  20  43  35  26,2  79,2  32,5
04  21  2013  20  43  37  26,2  79,2  32,5
04  21  2013  20  43  40  26,2  79,2  32,5
04  21  2013  20  43  42  26,2  79,2  32,5
04  21  2013  20  43  45  26,2  79,2  32,5
04  21  2013  20  43  47  26,2  79,2  32,5
04  21  2013  20  43  50  26,2  79,2  32,5
04  21  2013  20  43  52  26,2  79,2  32,5
04  21  2013  20  43  55  26,1  79,0  32,5
04  21  2013  20  43  57  26,2  79,2  32,5
04  21  2013  20  44  00  26,2  79,2  32,5
04  21  2013  20  44  02  26,2  79,2  32,5
04  21  2013  20  44  05  26,2  79,2  32,6
04  21  2013  20  44  07  26,2  79,2  32,6
04  21  2013  20  44  10  26,2  79,2  32,6
04  21  2013  20  44  12  26,1  79,0  40            <-------
04  21  2013  20  44  15  26,3  79,3  44,8
04  21  2013  20  44  18  26,3  79,3  42,6
04  21  2013  20  44  20  26,2  79,2  40,1
04  21  2013  20  44  23  26,2  79,2  38,3
04  21  2013  20  44  25  26,2  79,2  38,3
04  21  2013  20  44  25  26,2  79,2  36,9
04  21  2013  20  44  28  26,2  79,2  36
04  21  2013  20  44  30  26,2  79,2  36
04  21  2013  20  44  30  26,2  79,2  35,2

References

DHTxx Sensors (tutorial): http://learn.adafruit.com/dht

DHT22 (AM2302) datasheet: http://www.adafruit.com/datasheets/DHT22.pdf

Adafruit DHT-sensor-library at GitHub: https://github.com/adafruit/DHT-sensor-library

ChipKIT Project, Temperature and relative humidity logger: http://embedded-lab.com/blog/?p=5453

Processing homepage: http://processing.org

Fritzing 用のパーツ(GR-SAKURA): http://japan.renesasrulz.com/gr_user_forum_japanese/m/mediagallery/19.aspx

Fritzing homepage: http://fritzing.org

Downloads

Source code and example PC application: http://renesasrulz.com/app_kits_and_demo_boards/gadget_renesas_user_forum/m/mediagallery/511.aspx

http://renesasrulz.com/cfs-filesystemfile.ashx/__key/telligent-evolution-components-attachments/13-143-00-00-00-00-05-11/DHT_5F00_datalogger.zip