Lesson 28: DHT11 Temperature Sensor with TM1637 Display | Arduino Step-by-Step Course


In this lesson from the Arduino Step-by-Step Course, we explore how to create a compact, standalone temperature display by combining a DHT11 temperature and humidity sensor with a TM1637 4-digit seven-segment display. Instead of relying on a serial monitor to view readings, this project builds a small, self-contained unit that visually reports the ambient temperature in both Celsius and Fahrenheit. This is a fundamental skill for creating user-friendly interfaces for your electronics projects, moving data from the digital world to a physical display.
This project is a practical building block for a variety of applications where a quick, at-a-glance temperature readout is needed. Here are a few ideas to inspire you:
- Desktop Weather Station: Build a small, always-on display to monitor the temperature in your office or hobby room.
- Thermostat Monitor: Create a dedicated display to verify the temperature near your home's thermostat or HVAC vent.
- Incubator or Terrarium Gauge: Monitor the temperature inside a reptile terrarium, plant propagation chamber, or DIY incubator without needing to open it.
- Workshop or Garage Thermometer: A clear, bright display is perfect for checking the temperature in a potentially dusty or dimly lit workshop.
- Educational Kit: An excellent starter project for learning how to interface different types of sensors and output modules with an Arduino.
Hardware Required
To follow along with this project, you will need the following components:
- Arduino Uno (or any compatible board)
- DHT11 Temperature and Humidity Sensor (both 3-pin and 4-pin PCB versions are shown)
- TM1637 4-Digit Seven-Segment Display Module
- Breadboard
- Jumper Wires
Wiring Guide
The wiring for this project is straightforward, requiring connections for both the sensor and the display. The video demonstrates the setup with two different versions of the DHT11 sensor and two sizes of the TM1637 display, but the core connections remain the same.
Here is the wiring for the DHT11 sensor and the TM1637 display, as described in the video (in video at 05:06).



For the TM1637 display module, the connections are as follows:
- VCC to 5V on the Arduino.
- GND to GND on the Arduino.
- CLK (Clock) to a digital pin, for example, D2.
- DIO (Data Input/Output) to another digital pin, for example, D3.
For the DHT11 sensor, the connections are:
- VCC (or the + pin) to 5V.
- GND (or the - pin) to GND.
- Data (or the S pin) to a digital pin, for example, D2. (Note: In the video, the sensor's data pin is connected to pin 2, but the code snippet shows it on pin 2 as well. The CLK and DIO pins for the display are on pins 2 and 3 in the code snippet, which is a conflict. The video transcript mentions pin 5 and 6 for the display, so we will use those to avoid any overlap.)
Please note: The transcript and code snippet show a potential pin conflict. The code defines the DHT11 data pin and the TM1637 CLK pin both on digital pin 2. To avoid this, it is recommended to use separate pins, such as connecting the DHT11 to pin 2 and the TM1637 CLK to pin 5 and DIO to pin 6, as mentioned in the video.
Code Explanation
The code for this project combines the libraries for both the DHT11 sensor and the TM1637 display. The key user-configurable parts are the pin definitions and the display's brightness setting. You can adjust these to fit your specific wiring and preferences.
First, the DHT11 sensor's data pin is defined. You can change this number if you have connected the sensor to a different digital pin on your Arduino.
#define dhtpin 2 // set the pin to connect to DHT11
Next, the pins for the TM1637 display module are defined. The CLK pin is for the clock signal and the DIO pin is for data. These must match your physical wiring.
// Module connection pins (Digital Pins)
#define CLK 5
#define DIO 6
An object of the TM1637Display class is then created, which takes the CLK and DIO pin numbers as arguments. This object provides all the functions needed to control the display.
TM1637Display display(CLK, DIO);
The display's brightness is set in the setup() function. The value 0x0f represents the maximum brightness. You can use a value from 0x00 (off) to 0x0f (maximum) to adjust the display's intensity to suit your environment.
display.setBrightness(0x0f);
The main logic in the loop() function reads the temperature from the DHT11 sensor and displays it. The showNumberDec() function is used to send the temperature value to the display. Its parameters allow you to control how the number is shown. For example, the following line displays the temperature in Celsius, starting from the second digit (position 1).
display.showNumberDec(23, false, 2, 1);
In this example, 23 is the number to display, false means no leading zeros, 2 is the number of digits to use, and 1 is the starting position (0-indexed) on the display.
Live Project Demonstration
In the video, the project is demonstrated live. The presenter shows the temperature readings on both the larger and smaller TM1637 displays. To prove the sensor is working, heat is applied to the DHT11 using a heat gun.
Initially, the display shows the room temperature in both Celsius and Fahrenheit. As the heat gun warms the sensor, you can see the temperature readings climb rapidly. The demonstration shows the temperature rising from a normal room temperature to over 50°C (123°F), confirming that the sensor is accurately reading the change in temperature and the display is correctly showing the data. This test is a great way to verify that all your connections and code are functioning correctly.
Video Chapters
- [00:00] Introduction to the project
- [00:28] Understanding the code for the TM1637 display
- [03:04] Displaying temperature in Celsius and Fahrenheit
- [05:06] Wiring the DHT11 to the TM1637 display
- [06:42] Live demonstration of the temperature display
- [08:23] Testing with a smaller display module
- [08:35] Conclusion and project summary
/*
* Robojax Arduino Step-by-Step Course
* Lesson 28: DHT11 with TM1637
* This is the Arduino code for TM1637 4-digit display.
*
* Please watch video instructions here: https://youtu.be/C-1iaCPzNtU
* This code is available at: http://robojax.com/course1/?vid=lecture11
*
* With over 100 lectures free on YouTube. Watch it here: http://robojax.com/L/?id=338
* Get the code for the course: http://robojax.com/L/?id=339
*
* Date: December 6, 2017, in Ajax, Ontario, Canada
*
* Original code from: https://github.com/avishorp/TM1637
* Modified for Robojax video on December 6, 2017
*/
#include <dht11.h>
dht11 DHT11; // create object of DHT11
#define dhtpin 2 // set the pin to connect to DHT11
// start of settings of TM1637 LED display
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
TM1637Display display(CLK, DIO);
uint8_t blank[] = { 0x0, 0x0, 0x0, 0x0 };// blank value for all digits
// The amount of time (in milliseconds) between tests
#define TEST_DELAY 2000
// start of settings of TM1637 LED display
void setup()
{
//Robojax Step-by-Step Arduino Course YouTube Watch it here: http://robojax.com/L/?id=338
Serial.begin(9600);// setting up serial monitor
display.setBrightness(0x0f);
}
void loop()
{
//Robojax Step-by-Step Arduino Course YouTube Watch it here: http://robojax.com/L/?id=338
DHT11.read(dhtpin);// initialize the reading
int humidity = DHT11.humidity;// get humidity
//code for Robojax.com video
Serial.print(getTemp('C'));
Serial.print("C ");
Serial.print(getTemp('F'));
Serial.print("F ");
Serial.print(getTemp('K'));
Serial.print("K ");
Serial.print(" humidity:");
Serial.print (humidity);
Serial.print("% ");
Serial.println();
delay(500);
display.setSegments(blank);
display.showNumberDec(23, false, 2,1);
delay(TEST_DELAY);
display.setSegments(data);
display.showNumberDec(153, false, 3, 1);
delay(TEST_DELAY);
display.setSegments(data);
for(int i=0; i<=500; i++)
{
display.showNumberDec(i);
}
}
Cose di cui potresti avere bisogno
-
eBay
-
AliExpressTM1637 4-digit 7-segment display on AliExpresss.click.aliexpress.com
Risorse e riferimenti
-
EsternoLibreria DHT11 su GitHubgithub.com
-
EsternoManuale DHT11 (PDF)robojax.com
File📁
Librerie Arduino (zip)
-
Libreria TM1637 per Arduino
TM1637_library.zip1.36 MB
File Fritzing
-
Modulo a sette segmenti TM1637
TM1637.fzpz0.01 MB -
Display a sette segmenti a 4 cifre TM1637
TM1637-1.fzpz0.01 MB
Altri file
-
XY-T01 Relè Timer: cablaggio del carico in corrente continua
/download/datasheet/robojax_TM1637_datasheet.pdf