Arduino Code and Video for DHT22 Temperature and Humidity Sensor with TM1637 Display and Relay

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.

DHT22 sensor with PCB-1

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

ManufacturerAdafruit
Part numberDHT22
Logic/IO voltage3.3 V - 5.5 V
Supply voltage3.3 V - 6 V
Output current (per channel)0.5 mA
Peak current (per channel)2.5 mA
PWM frequency guidanceN/A
Input logic thresholds0.3 V - 0.7 V
Voltage drop / RDS(on) / saturationN/A
Thermal limits-40°C to 80°C
Package3-pin package
Notes / variantsAlso 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

Wiring DHT11 DHT22 with TM1637 dispaly and relay
Wiring DHT11 DHT22 with TM1637 dispaly and relay

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.

Wirig relay module to AC load

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

Images

DHT11 Module
DHT11 Module
DHT22 sensor with PCB-1
DHT22 sensor with PCB-1
DHT22 sensor no PCB
DHT22 sensor no PCB
Wirig relay module to AC load
Wirig relay module to AC load
Wiring DHT11 DHT22 with TM1637 dispaly and relay
Wiring DHT11 DHT22 with TM1637 dispaly and relay
57-Arduino code for a DHT11 DHT22 temperature and humidity sensor with a TM1637 display.
Language: C++
Copied!

Things you might need

Resources & references

Files📁

Arduino Libraries (zip)

Fritzing File

User’s Manual