Welcome to the next tutorial of our Raspberry Pi programming course. In our previous tutorial, we looked at how to interface an ultrasonic sensor with a Raspberry Pi and used Python to perform its calculations. However, in this tutorial, we'll learn how to connect a DHT11 temperature and humidity sensor to a Raspberry Pi and how it works, so we can better understand how it works.
- LCD display
- DHT11 sensor
- Raspberry pi
- Male to female jumper wires
What is DHT11 sensor?
Low-cost digital sensors are used in the sensor to measure temperature and humidity. For real-time humidity and temperature measurements, this sensor and a number of microcontrollers are available, including Arduino, Raspberry Pi, and many more.
Both the sensor and the module of the DHT11 humidity and temperature sensors are available. What sets this sensor apart from a module is the use of a pull-up resistor and an on-board LED.
What are the different types of temperature and humidity sensors?
There are many types of temperature sensors available. What should we look for in a temperature sensor for our Raspberry Pi project when there are so many options? It all depends on what we're measuring, how precise we need it to be, and where we're taking the readings. The Negative Temperature Coefficient (NTC) thermistor, thermocouple, semiconductor sensors, and the Resistance Temperature Detector are the most commonly used temperature sensors (RTD). As a result, we'll take a look at eight different kinds of sensors that have these features.
Grove – AHT20
With its dual-row flat, no-lead SMD design, Grove's new AHT20 temperature and humidity sensor is ideal for use in reflow soldering applications. In addition to the standard temperature sensor, the AHT20 has a capacitive humidity sensor made by MEMS semiconductors that is better than the standard sensor.
Digital sensors can monitor temperature as well as relative humidity. Two measurements are converted into a digital signal via an analog-to-digital converter chip. Temperature sensors with long-term stability and great performance are among the most popular on the market. This is why we'll be focusing on it in this session.
Both temperature and humidity are monitored by the DHT22, which is similar to the DHT11. The DHT22 costs a little more, but it is well worth the extra money. Furthermore, it has a wider temperature and humidity range than the DHT11, making it more precise. In terms of how it's handled and coded, the DHT22 is nearly identical to the DHT11. A temperature sensor that performs better and is more accurate should definitely be considered.
When compared to the DHT series, the BMP280 has the capability to measure both temperature and barometric pressure. You can use this in both SPI and I2C modes, making it an upgrade from the BMP180. Because the air pressure changes with elevation, it can estimate a location's estimated altitude.
While the BMP280 only monitors temperature and air pressure, the BME280 has a humidity measurement in addition to both. You get more for your money if you choose the more expensive option, because this one gives you a better picture of what's around you.
The DS18B20 chip is embedded in a waterproof probe that extends 2 meters in length. By adding the pre-assembled resistance, we have transformed it into a simple Grove sensor. Of course, the Rpi and its platforms are fully compatible with this. The wire, however, should not be exposed for long periods of time to temperatures above 70°C.
The AF5485 may be small and light, but it packs a powerful punch. In spite of its enormous size, it has an impressively complex internal system that allows it to perform at the highest level. There are several uses for this highly accurate and stable gadget, which has a short response time and excellent long-term stability. Building automation, weather stations, and temperature monitoring are just a few possible applications.
Compact and lightweight, the AM2311A is an auto-calibration module that consumes minimal power. Furthermore, it is capable of transmitting data at distances greater than 20 meters. It goes without saying that this sensor is extremely dependable and stable over the long run. As a result, it can be employed in a variety of settings. One-of-a-kind sensors that respond in milliseconds are a popular choice for many!
Pinouts of DHT11 Sensor
- Measurements of relative humidity and temperature are backed up by a digital signal that is calibrated and has good long-term stability.
- Additional components are not required.
- There are 4 pins on the sensor that are packaged and can be changed. This means that long transmission distances can be reached with low power consumption thanks to this.
Architecture of DHT11 Sensor
Using this sensor is a cinch because it is pre-calibrated and sends serial data. The following diagram depicts the sensor's wiring.
To guarantee that the dht11 is properly grounded, a 5K pull-up resistor connects the MCU's input/output pin to the data input. The temps and humidity readings are transmitted as serial data through this pin.
Using the datasheet provided below, you'll be able to connect it to another microcontroller. Humidity decimal and decimal data, temperature integer and decimal data, and the parity bit will all be sent out through the pin's data outputs. In order for the sensor to transfer this data, the Input/Output pin must be temporarily set to 0 and later changed to 1, as seen in the following diagram.
The DHT11 datasheet has detailed timing diagrams that show how long each host signal lasts.
Working Principle of DHT11 Sensor
This device uses a thermistor to monitor temperature and a capacitive humidity sensor to measure relative humidity. A humidity-detecting capacitor's electrodes are separated by a dielectric substrate that retains moisture. When the humidity level fluctuates, the capacitance value changes as well. Analog resistance values are measured, processed, and stored by the IC, which then translates them into digital values.
The resistance value of this sensor is monitored as it warms up using a negative temperature coefficient thermometer. This sensor is commonly made of semiconductor ceramics in order to achieve a higher resistance value even at the smallest temperature change.
With a 2 ° c precision, the DHT11's temperature range is zero to fifty degrees Fahrenheit. This sensor can accurately monitor humidity levels from 20% to 80% with a 5% degree of precision. One reading per second, or 1Hz, is the sampling rate of this sensor. The microcontroller DHT11 has a power consumption of 3 to 5 volts. The maximum amount of current that can be drawn during a measurement is 2.5 mA.
Four pins make up the DHT11 sensor: VDD, Ground, a data pin, and the not-connected. The sensor and the microcontroller are connected via a 5K to 10K ohm pull-up resistor.
How do we interface with a raspberry pi?
In order to use the DHT11 sensor, first connect it to the Raspberry Pi. A 16x2 LCD monitor is connected in order to see the findings.
What goes where?
- The DHT11 sensor signal pin is connected to GPIO 4 of the Raspberry Pi 3 board via the yellow wire.
- The sensor voltage pin (power supply) connects to the 5V pin on the Rpi board using the red wire.
- The sensor's GND pin is connected to the Raspberry Pi's GND pin with a black wire.
To connect the sensor to the Raspberry Pi, the following steps must be followed. This circuit now has to be connected to the 16x2 LCD screen.
- GND is connected to the Raspberry Pi's GND pin through a black wire on the LCD display.
- Connecting the LCD display's voltage pin to the Raspberry Pi board's 5V pin is done using the red wire.
- The LCD display's SDA pin is connected to the Raspberry Pi's SDA pin by a blue cable.
- The SCL pin of the LCD display is connected to the Raspberry Pi board using an orange wire.
To begin receiving power, the above connections must be made: The Raspberry Pi computer is now ready to use.
Before obtaining the temperature and humidity readings, there are a few more actions that need to be taken. To use the one-wire interface on newer Raspberry Pi models, a specific piece of code must be run. The Adafruit library must be installed before we can attach the sensor to our RPi.
Step 1: To download the Adafruit module for DHT11, use the following command in the RPi console.
Step 2: To install the library on your RPi, follow the instructions below one by one.
Step 3: This step may be required for newer versions of the Raspberry Pi. You can use the commands below if your device returns "None" values.
- In the terminal, type "sudo raspi-config"
- Click on "Interfacing Options" in the drop-down menu that opens.
- Click "One-Wire Interface" from the list, activate it, and restart.
- Check the results of the program by running it.
The device must be rebooted when the procedure has been completed. It is recommended that the main code be executed once the other steps have been completed.
In order to use system-specific parameters in our program, we first need to import the sys module. There is also a support file for the Raspberry Pi board called the I2C LCD Driver. When connecting a DHT11 sensor to a Raspberry Pi, the Adafruit DHT module is used. First, we must add the time module to our program. Then we can add delay to our program
A variable called mylcd is established to call all of the I2C LCD driver functions after the relevant libraries have been imported. Mylcd.lcd clear is the first command that does this. Then the LearnElectronics DHT11 with the RPi string will be displayed in the assigned row and column.
The infinite while loop follows the try block. For 15 iterations, the sensor's temperature and humidity are read 15 times using the read function inside the while loop. The pins of the Raspberry Pi may not be able to read the values, or they may read an incorrect value. In these circumstances, the LCD panel must be wiped clean and the values re-read. The if loop includes this condition because it's not possible for this to happen every time.
Using the time.sleep (1) condition, the device is forced to sleep for one second, allowing the sensor to once again provide the value needed for calculation. The LCD will be cleared and the string "Thank you for watching" will be displayed in the specified row and column whenever a keyboard interrupt is given. Afterward, the LCD screen will be cleansed of the string for roughly five seconds.
Before the temperature and humidity values are shown on the screen, the following text appears on the screen.
The LCD display will show the first reading from the sensor after a short delay.
It will take some time until a new set of values, dependent on the surrounding environment, is shown.
Any warm object placed near the sensor will alter the sensor's reading.
You'll see the following text if you provide a keyboard interrupt at any time while the program is running.
Advantages of temperature and humidity sensors
- It can operate in temperatures as low as -100°C without requiring any maintenance.
- It is not affected by atmospheric pressure; it works regardless of the applied pressure.
Disadvantages of temperature and humidity sensors
It's unstable for a long time, and it can be damaged by dew and other things.
Uses for the DHT11 Sensor
Networked IoT Environment Monitoring
Monitoring or controlling environmental quality is the goal of environmental monitoring. A temperature sensor can also be used to create an IoT temperature monitoring system. The DHT11 humidity and temperature sensors are used in this system, as well.
Since this temperature sensor is inexpensive, it can be used in low-cost but effective weather stations that show the temperature and humidity of the surrounding environment, which may be beneficial in a variety of businesses.
Climate control is a method of managing the temperature. So, the DHT11 temperature and humidity sensors may be useful in ensuring that the environmental readings are accurate. Temperature and humidity sensors are sent to the microcontroller, and the system will respond if the temperature exceeds a predetermined upper or lower limit. For example,
In this tutorial, we learned how to connect a DHT11 sensor for humidity and temperature readings using the Raspberry Pi's GPIO pins. The sensor's principles and applicability in the current world were also studied. In the following tutorial, we'll learn how to interface DS18B20 with the Raspberry Pi 4.