Arduino Code and Video for DHT22 Temperature and Humidity Sensor with TM1637 Display and Relay
In this tutorial, we will explore how to use a DHT22 temperature and humidity sensor in conjunction with a TM1637 display and a relay to control an AC load, such as a heater or fan. The objective is to turn on the load when the temperature reaches 50 degrees Celsius and turn it off when it drops below that threshold. This setup is ideal for maintaining desired temperature levels in a controlled environment.

To achieve this, we will write an Arduino program that reads temperature and humidity data from the DHT22 sensor, displays the temperature on the TM1637 display, and controls the relay based on the temperature readings. For further clarity, you can check the video at (in video at 10:00).
Hardware Explained
The main components of this project include the DHT22 sensor, TM1637 display, relay module, and an Arduino board. The DHT22 sensor is responsible for measuring the temperature and humidity, providing digital output that the Arduino can read easily. The TM1637 display is used to show the temperature readings in a user-friendly format, while the relay module allows us to control high-voltage devices like heaters and fans safely.
The DHT22 sensor has three pins: VCC (power), GND (ground), and DATA (output). The TM1637 display uses four pins for power, ground, clock, and data communication. The relay module connects to the Arduino and acts as a switch for the AC load, ensuring safe operation when controlling devices that require higher voltage.
Datasheet Details
| Manufacturer | Adafruit |
|---|---|
| Part number | DHT22 |
| Logic/IO voltage | 3.3 V - 5.5 V |
| Supply voltage | 3.3 V - 6 V |
| Output current (per channel) | 0.5 mA |
| Peak current (per channel) | 2.5 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3 V - 0.7 V |
| Voltage drop / RDS(on) / saturation | N/A |
| Thermal limits | -40°C to 80°C |
| Package | 3-pin package |
| Notes / variants | Also known as AM2302 |
- Ensure proper power supply (3.3 V - 6 V) to the DHT22.
- Use pull-up resistors on the data line if necessary.
- Allow sensor readings to stabilize for accurate data.
- Double-check pin connections to avoid miscommunication.
- Be cautious with AC loads; ensure proper insulation and safety.
- Implement heat-sinking for the relay if controlling high loads.
- Use debounce techniques if needed when reading sensor data.
- Keep the DHT22 sensor shielded from direct sunlight for accurate readings.
Wiring Instructions

To wire the components, start by connecting the DHT22 sensor. Connect the VCC pin to the 5V pin on the Arduino, the GND pin to the ground (GND), and the DATA pin to digital pin 9 on the Arduino.
Next, wire the TM1637 display. Connect the VCC pin to the 5V pin on the Arduino, the GND pin to ground, the CLK pin (clock) to digital pin 2, and the DIO pin (data) to digital pin 3. For the relay module, connect the VCC pin to the 5V pin, the GND pin to ground, and the control pin (signal) to digital pin 7. Finally, ensure that the AC load is safely connected to the relay module following the manufacturer's guidelines.
Code Examples & Walkthrough
The following code initializes the DHT22 sensor and the TM1637 display. It sets up the necessary pins and prepares to read temperature data.
#include
#define CLK 2
#define DIO 3
#define DHTPIN 9
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
pinMode(RELAY, OUTPUT);
dht.begin();
}
In this snippet, we include the necessary libraries and define the pins for the TM1637 display and the DHT22 sensor. The setup function initializes the serial communication and sets the relay pin as an output.

The loop function retrieves the temperature and controls the relay based on the temperature reading. If the temperature exceeds 50 degrees, the relay is activated.
void loop() {
delay(TEST_DELAY);
int temp = round(getTemp("c"));
display.showNumberDec(temp, false, 3, 1);
if(temp > 50) {
digitalWrite(RELAY, LOW);
} else {
digitalWrite(RELAY, HIGH);
}
}
This code snippet demonstrates how to read the temperature, display it, and control the relay. The temperature is rounded and displayed on the TM1637, and the relay is turned on or off based on the temperature threshold.
For a more comprehensive understanding, please refer to the full code loaded below the article.
Demonstration / What to Expect
As you run the program, you should see the temperature displayed on the TM1637. When the temperature exceeds 50 degrees Celsius, the relay will activate, turning the connected AC load on. Conversely, when the temperature drops below 50 degrees, the relay will deactivate, turning the load off. Be cautious of reversed polarity and ensure proper connections to avoid damage to components (in video at 12:30).
Video Timestamps
- 00:00 - Introduction
- 02:15 - Wiring Explanation
- 05:00 - Code Walkthrough
- 10:00 - Demonstration
- 12:30 - Common Issues
Things you might need
-
Amazon4 Channel Relay module on Amazonamzn.to
-
AliExpressPurchase 5v 12v 1 2 4 6 8 channel channel relay moduls.click.aliexpress.com
Resources & references
-
External
Files📁
Arduino Libraries (zip)
-
DHT22 PCB module red
application/zip0.01 MB
Fritzing File
-
Temperature Sensor DHT11
application/zip0.01 MB -
DHT22 Humidity and Temperature Sensor
application/zip0.01 MB -
DHT22 PCB module red
application/zip0.01 MB -
DHT22 module white
application/zip0.01 MB -
TM1637 Seven Segment module
application/zip0.01 MB -
DHT11 Humitidy and Temperature Sensor (3 pins)
application/zip0.20 MB
User’s Manual
-
DHT22 Temperature and Humidity sensor user's manual
application/pdf0.36 MB