This was a small weekend experiment which I wanted to try for a long time. I have tried DHT11 sensor with Arduino and it worked great but with Raspberry Pi I don’t know how well it would work.
Much of what I learnt and did in this tutorial is taken from the below link.
How to Set Up the DHT11 Humidity Sensor on the Raspberry Pi
The DHT11 temperature and humidity sensor is a nice little module that provides digital temperature and humidity…
Another link which is a good read can be found here.
DHT11 Humidity & Temperature Sensor Module
This module integrates DHT11 sensor and other required components on a small PCB. The DHT11 sensor includes a resistive…
Now coming back to the sensors:
The diagram is given below, and I used the 4 pin version. The good thing about DHT11 is that it gives you both humidity and temperature both.
Factsheet for DHT11
- Ultra low cost
- 3 to 5V power and I/O
- 2.5mA max current use during conversion (while requesting data)
- Good for 20–80% humidity readings with 5% accuracy
- Good for 0–50°C temperature readings ±2°C accuracy
- No more than 1 Hz sampling rate (once every second)
- Body size 15.5mm x 12mm x 5.5mm
- 4 pins with 0.1" spacing
The circuit that I setup is based the below diagram
The final setup looked like below:
Basically if you see the circuit diagram you would find following connection.
VCC of DHT11 -> 5v Pin of Raspberry Pi 3
GND of DHT11 -> GND Pin of Raspberry Pi 3
Signal pin of DHT11 -> GPIO 4 Pin of Raspberry Pi3
Thing to remeber is to use a 10K ohm pullup resistor between VCC and signal pins of DHT11 4 pin version or the sensor data will not be fetched. I faced the same issue and even using 1 Ohm did not work.
For the code I used the below Adafruit library for DHT11 sensor which
Adafruit_Python_DHT - Python library to read the DHT series of humidity and temperature sensors on a Raspberry Pi or…
The code is given below:
In this code we are reading the humidity and temperature information and uploading it to a HTTP endpoint http://192.168.0.8:1880 as json.
To make it more like an IOT experiment I also created a Node-red flow. Now Node red is a great IOT workflow manager where you design the flow by dragging and dropping connectors and it starts working. I used a very simple flow where I read data from an HTTP connection running at http://192.168.0.8:1880 and wrote to a file. The actual sensor data was fetched from a python program and fed to the HTTP connector of Node-red.
The screenshots of the Node-red flow is given below:
Finally when running the example it captured the humidity and temperature data.
Given that this was my one of the first experiments with GPIO pins in Raspberry Pi, I was very happy and plan to do many more such experiments.