Using DHT11, DHT22 with TM1637 display and Relay to control AC load
This project demonstrates how to build a temperature-controlled system using an Arduino, DHT11/DHT22 temperature and humidity sensor, a TM1637 display, and a relay to control an AC load. This setup is incredibly versatile and can be adapted for various applications. For example, you could build:
- A smart thermostat for a small greenhouse, automatically adjusting a heater or fan based on temperature readings.
- A system to monitor and control the temperature of a liquid in a chemical process.
- An automated temperature control system for a reptile enclosure.
- A temperature-activated alarm system, triggering an alert when a certain threshold is reached.
The system uses a DHT11 or DHT22 sensor to measure temperature and humidity (in video at 00:11). The TM1637 display shows the current temperature (in video at 00:17), and a relay switches an AC load (like a light, fan, or heater) on or off based on pre-defined temperature settings (in video at 00:23).
Hardware/Components
To build this project, you will need the following components:
- Arduino Uno (or compatible board)
- DHT11 or DHT22 Temperature and Humidity Sensor (in video at 00:11)
- TM1637 Display Module (in video at 00:17)
- Relay Module (in video at 00:19)
- Jumper Wires
- Breadboard
- AC Load (e.g., a small light bulb or fan)
Wiring Guide
The wiring is straightforward but crucial for the project's functionality. Refer to the video for a visual guide.
%%WIRING%%
Detailed wiring instructions and diagrams can be found in the video (in video at 02:03 to 05:18). Pay close attention to the connections for the DHT sensor (in video at 01:44), TM1637 display (in video at 02:25), and relay module (in video at 03:37).
Code Explanation
The Arduino code uses two libraries: TM1637Display and DHT sensor library. You'll need to install these libraries in the Arduino IDE. The most important configurable parts of the code are:
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000
// ****** end of TM1637 Display code
#define DHTPIN 9 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
#define RELAY 7 // the pin connected to relay
The #define statements allow you to easily change the pin assignments for the TM1637 display (CLK and DIO), DHT sensor (DHTPIN), and relay (RELAY) to match your wiring. The DHTTYPE defines which DHT sensor you are using (DHT11 or DHT22). The TEST_DELAY variable controls the time between readings.
The getTemp() function is a custom function used to read temperature and humidity data from the DHT sensor. It allows you to request different data types (Celsius, Fahrenheit, humidity, heat index) by passing a string argument (in video at 09:01 to 10:34). For example: getTemp("c") returns the temperature in Celsius, and getTemp("h") returns the humidity.
if(temp >50 )
{
digitalWrite(RELAY, LOW);
}else{
digitalWrite(RELAY, HIGH);
}
This code section controls the relay based on the temperature. Modify the threshold value (50 in this example) to adjust the trigger point (in video at 09:44 to 10:11).
Live Project/Demonstration
A full demonstration of the project is available in the video (in video at 00:51 to 01:07). The video shows the system in action, displaying the temperature on the TM1637 display and controlling the AC load based on the temperature readings.
Chapters
- [00:06] Introduction and Project Overview
- [01:12] Code Availability and Additional Resources
- [01:44] DHT Sensor Wiring
- [02:25] TM1637 Display Wiring
- [02:50] AC Load and Relay Wiring
- [04:33] Relay Pin and Power Connections
- [05:29] Library Installation
- [05:52] Code Explanation: TM1637 Section
- [06:47] Code Explanation: DHT Section
- [07:50] Code Explanation: Relay and Setup
- [08:26] Code Explanation: Loop and Temperature Control
- [10:12] getTemp() Function Explanation
/*
* Original code from TM1637 https://github.com/avishorp/TM1637
* Original code and library for DHT22 https://github.com/adafruit/DHT-sensor-library
* Watch the video for this code https://youtu.be/xD8wHXDzLkQ
* Other Arduino library and videos https://robojax.com
*/
/*
* Modified for Robojax video on January 10, 2018
* by Ahmad Nejrabi, in Ajax, Ontario, Canada
*/
// ****** Start of TM1637 Display code
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 1000
TM1637Display display(CLK, DIO);
// ****** end of TM1637 Display code
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
// updated by Ahmad for Robojax.com videos.
// on January 10, 2018 in Ajax, Ontario, Canada
// ****** Start of DHT code
#include "DHT.h"
#define DHTPIN 9 // what digital pin we're connected to
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
// ********** end of DHT22 code
#define RELAY 7 // the pin connected to relay
void setup()
{
Serial.begin(9600);
Serial.println("DHT22 Robojax Test with Display");
pinMode(RELAY,OUTPUT);// set RELAY pin as output
dht.begin();
}
void loop()
{
delay(TEST_DELAY);// wait
// **** TM1637 code start
display.setBrightness(0x0f);// set brightness
uint8_t data[] = { 0x0, 0x0, 0x0, 0x0 };// clear display values
display.setSegments(data);//clear display
// **** TM1637 code end
// Robojax.com test video
Serial.println(getTemp("c"));
int temp = round(getTemp("c"));
display.showNumberDec(temp, false, 3,1);
if(temp >50 )
{
digitalWrite(RELAY, LOW);
}else{
digitalWrite(RELAY, HIGH);
}
}// loop end
/*
* getTemp(String req)
* returns the temperature related parameters
* req is string request
* getTemp("c") will return temperature in Celsius
* getTemp("hic") will return heat index in Celsius
* getTemp("f") will return temperature in Fahrenheit
* getTemp("hif") will return temperature in Fahrenheit
* getTemp("h") will return humidity
*/
float getTemp(String req)
{
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return 0.0;
}
// Compute heat index in Kelvin
float k = t + 273.15;
if(req =="c"){
return t;//return Celsius
}else if(req =="f"){
return f;// return Fahrenheit
}else if(req =="h"){
return h;// return humidity
}else if(req =="hif"){
return hif;// return heat index in Fahrenheit
}else if(req =="hic"){
return hic;// return heat index in Celsius
}else if(req =="k"){
return k;// return temperature in Kelvin
}else{
return 0.000;// if no request found, return 0.000
}
}
资源与参考
尚无可用资源。
文件📁
没有可用的文件。