Code Monkey home page Code Monkey logo

dht22-raspberry-pi's Introduction

DHT22 sensor on Raspberry Pi

output image

The DHT22 sensor software for a Raspberry Pi with WiringPi

License

The DHT22 is a temperature and relative humidity sensor. It has a digital converter on board, which communicates with the Raspberry Pi via a 1-wire interface.


Hardware connections.

The sensor requires a 3.3V power supply, available at pin 1 of the RPi. Do not connect the sensor to the 5V power supply. It will not break, but the 'high' signals from the Raspberry are 3.3V and, in this case, are not detected. In the diagram, the output is connected to pin 3, but can be connected to most GPIO pins as the pin is defined in the software.

output image


1-Wire communication.

The DHT22 has its own rather obscure 1-wire interface protocol. It is very different from the standard Dallas 1-wire protocol supported by the Raspberry Pi GPIOs.

output image
The transfer is initiated by a low of 18 mS, followed by a high of 40 µS to give the sensor some time to set up the communication flow. At the same time, the GPIO pin must be configured as an input to receive the pulse train from the sensors. As can be seen, the length of a high determines a 0 or a 1. So the software comes down to measuring pulse lengths. It can be done through interrupt or polling techniques. Since interrupts are cumbersome and error-prone, it is better to use polling. It means continuously fetching the level of the input pin. When a change occurs, the elapsed time is measured.

Accurately measuring microseconds is also another concern. We modified Daniel's software. He uses the WiringPi delayMicroseconds() function. As shown below, not the most accurate solution. We use the clock_gettime() function, which turns out to be much better. Because the Raspberry Pi operating system runs many tasks and threads simultaneously, it happens regularly that a transition is missed and subsequently the outcome errors. It is marked by the boolean Valid.

Below are a few histograms showing the difference between the WiringPi and the clock_gettime() function. First, only the application runs without other demanding processes.
output image


Next, the same situation but now with different apps.
output image


Finally, when the Raspberry Pi underclocked to 200MHz, something WiringPi's delayMicroseconds() brings into serious problems.
output image


Pin numbers.

Please find the correct WiringPi pin numbers in the diagram below.
output image


paypal

dht22-raspberry-pi's People

Contributors

qengineering avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dht22-raspberry-pi's Issues

Loop interval timing

In the documentation, it says that the loop timing must be >2 seconds while the code only waits 1 seconds. I set my pause to 2.5 seconds and it helped somewhat. Still throws quite a few errors (I am testing 7 of them simultaneously).

Thanks a million for this work! It really helped me.

prog delivers confused/zero values inbetween

When compiling and running your program with g++

$ g++ ./src/main.cpp ./src/DHT22.cpp -I./include -lwiringPi -o dht22

$ ./dht22

the output looks like

Temp : 12.00 °C,  Humidity : 99.90 %                                                            
Temp :  0.00 °C,  Humidity :  0.00 %                                                            
Temp : 12.00 °C,  Humidity : 99.90 %                                                            
Temp : 24.00 °C,  Humidity : 199.80 %                                                           
Temp :  0.00 °C,  Humidity :  0.00 %                                                            
Temp :  0.00 °C,  Humidity :  0.00 %                                                            
Temp :  0.00 °C,  Humidity :  0.00 %                                                            
Temp : 12.00 °C,  Humidity : 99.90 %                                                            
Temp : 12.00 °C,  Humidity : 99.90 %                                                            
Temp : 12.10 °C,  Humidity : 99.90 %                                                            
Temp : 12.10 °C,  Humidity : 99.90 %                                                            
Temp : 24.20 °C,  Humidity : 199.80 %

Note
I've been reformatting the output a bit and changing the loop delay to 5000ms inmain.cpp.

We can see zero values and sporadic doubled temperature values and +100 humidity values.

Why is that?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.