Arduino - DHT22 - Relay
In this tutorial, we are going to learn how to use Arduino to control the relay based on the temperature read from DHT22 sensor.
Or you can buy the following sensor kit:
|1||×||DIYables Sensor Kit 30 types, 69 units|
About Relay and DHT22 Sensor
If you do not know about DHT22 temperature sensor and relay (pinout, how it works, how to program ...), learn about them in the following tutorials:
This image is created using Fritzing. Click to enlarge image
How System Works
- Arduino reads the temperature from the DHT22 sensor
- If the temperature exceeds an upper threshold, Arduino turn on the relay
- If the temperature falls below a lower threshold, Arduino turn off the relay
The above process is repeated infinitely in the loop.
If you want to turn on and turn off the relay when the temperature is above and below a specific value respectively, you just need to set the upper threshold and lower threshold to the same value.
In the above codes, the Arduino turn on the relay when the temperature exceeds 25°C, and keep the relay on until the temperature is below 20°C
- Connect Arduino to PC via USB cable
- Open Arduino IDE, select the right board and port
- On Arduino IDE, Go to Sketch Include Library Manage Libraries
- Search “DHT”, then find the DHT sensor library by Adafruit
- Click Install button to install the library.
- You will be ased for intall some other library dependancies
- Click Install All button all library dependancies.
- Copy the above code corresponding to the sensor you have and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Make enviroment around sensor hotter or colder
- See the state of the relay
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.