Tags:
create new tag
view all tags

Measuring temperature and humidity

Goal:

The DHT11 is a low lost humidity and temperature sensor using its own serial protocol for data transfer. While for 1-wire devices like the ds18B20 temperature sensor a device driver for Linux is available as a kernel module and all the intricacies of the protocol are handled within this module, such a device driver is not available for the DHT11.

The goal of this exercise is therefore to read and understand the DHT11 data sheet and to interpret the DHT-11 protocol in order to extract the measured data.

This is a photo of the DHT11 I have got in my sensor kit. Be careful: the PCB onto which the DHT11 is mounted in the sensor kit we have here at UCC is different!

dht11.png

Be careful: The PCB on which the DHT11 is mounted in our sensor kit is cabled differently:

Looking onto the device like in the photo, the signal pin is left, labeled "S" (where the GND pin is on my DHT11), the Vcc pin is in the middle and the GND pin is right!

Exercise 1: Using a shared library

In order to simplify matters for you I wrote a few functions collected in a shared library.

These are the calls available to you:

  • int dht11Init(void);

  • void dht11SetDebug(bool);

  • int dht11Measurement(void);

  • int dth11GetValidTemperature(void);

  • int dth11GetValidHumidity(void);

  • int dht11GetValidRawData(int *);

  • int dht11GetChecksum(void);

  • int dht11GetDeviceChecksum();

  • time_t dht11GetValidMeasTimeStamp(void);

The sequence will be to first call dht11Init(), then make a measurement with dht11Measurement() and then read out whatever you are interested in.

You will find the include file dht11.h in /opt/ucc/include and the library libdht11.so in /opt/ucc/lib

Documentation has been included in doxygen format in the source files. Using the doxygen tool (installed on your Raspberry PI) you can generate documentation for this library automatically. You will find it as html file, which you can look at with your browser.

The URL is: https://dcsit.twiki.ucc.edu.gh/html/libDoc/dht11

The URL only works when looking at the TWiki site from the Raspberry Pi. Otherwise you will have to look up the index.html file in the html sub directory created by doxygen.

Exercise 1: Data taking

Carefully read the DHT-11 data sheet and fully understand the way the DHT-11 communicates with its host micro-controller. Connect the DHT-11 to Vcc and ground and connect the data pin to gpio pin 0 (see warning above!)

Write a C program to drive the data pin in such a way that the data transfer is initiated. Then program the gpio pin as input and read the data repetitively every 5 micro seconds. Save the data into an integer array (even though the data will only vary between 0 and 1). Use a pointer to take as little time as possible to transfer the data into the array. Finally print the data read onto stdout and redirect the output onto a file. How big must the data array be to hold all data in the worst case where only 1s are transmitted?

Exercise 2: Manual Data Analysis

Use gnuplot to plot the acquired data. Analyze the data and extract the values for temperature, humidity and checksum by hand. Calculate the checksum over the temperature and humidity data and compare the result to the expected value sent by the DHT11. Use hexadecimal calculus to do this.

A typical bit stream transmitted by the DHT11.

dht11Data.png

Exercise 3: Analysis done by program

Read in the acquired data through stdin and print them (I/O redirection from the file). Then analyze the pulse train and extract the temperature and humidity data. Use shift and or instructions to build up the 8 bit values for temperature and humidity. Calculate and compare the checksums. Print the results.

Finally connect the data acquisition and the data treatment programs through a pipe.

Comments:

Of course it is also possible to treat the data on the fly during data taking (you may want to give it a try!) However in this case debugging is more difficult due to real time constraints. You cannot put print statements into the code for debugging because the DHT-11 will not wait until your debugging print has finished. If you separate data taking and data analysis, the debugging becomes much easier because you have plenty of time to look at the data.

Python

For those of you who prefer Python, I have written a Python class to access the dht11 and a small example program creating an instance of this class and using it to take measurements and printing the result.

Since my programming experience in Python is very restricted (just 4 days!) please don't be too critical with my code. It seems to work though.

https://afnog.iotworkshop.africa/pub/Embedded_Systems/TemperatureHumidity/dht11Class.py.txt

https://afnog.iotworkshop.africa/pub/Embedded_Systems/TemperatureHumidity/dht11Example.py.txt

-- Uli Raich - 2017-08-13

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatodt dht11.odt r1 manage 58.9 K 2017-10-25 - 18:54 UnknownUser  
PDFpdf dht11.pdf r1 manage 93.6 K 2017-11-27 - 10:35 UnknownUser  
PNGpng dht11.png r1 manage 597.9 K 2017-10-25 - 16:12 UnknownUser  
Texttxt dht11Class.py.txt r1 manage 7.5 K 2017-11-29 - 11:07 UnknownUser  
PNGpng dht11Data.png r1 manage 38.7 K 2017-10-25 - 16:12 UnknownUser  
Texttxt dht11Example.py.txt r1 manage 0.7 K 2017-11-29 - 11:07 UnknownUser  
Edit | Attach | Watch | Print version | History: r6 < r5 < r4 < r3 < r2 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r6 - 2017-11-29 - uli
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback