7 Segment Dengan TM1637
In this tutorial, we will learn how to use the TM1637 4-digit 7-segment LED display with Arduino. These displays are cheaper & best for displaying sensor data, time, stopwatch, random numbers, etc. It is similar to other 4-digit 7-segment displays but has a TM1637 LED Driver IC embedded in it. This removes the extra wiring and using only 2 wires the display can be controlled.
I have explained three TM1637 Arduino example in this tutorial. In the first example, we will look at the basic functions of the TM1637 Display library & display some random numbers and letters. In the second example, we will use TM1637 with Arduino to create a simple temperature display in combination with the DHT11. In the third example, we will use the DS3231 Real Time Clock Module to display the Time on TM1637 Display.
Bill of erials
We need the following components for this tutorial. You can purchase all these components from Amazon.
S.N. | Components | Quantity | Purchase Links |
---|---|---|---|
1 | Arduino Nano Board | 1 | Amazon | AliExpress |
2 | TM1637 4-Digit 7-Segment Display | 1 | Amazon | AliExpress |
3 | DS3231 RTC Module | 1 | Amazon | AliExpress |
4 | DHT22/11 Sensor | 1 | Amazon | AliExpress |
5 | Jumper Wires | 20 | Amazon | AliExpress |
6 | Breadboard | 1 | Amazon | AliExpress |
TM1637 4-digit 7-segment LED display
The Bare 4-digit 7-segment displays usually require 12 connection pins. But the TM1637 IC is mounted on the back of the display module, which reduces the total wire to 4. Two pins are required for the power connections and the other two pins are used to control the segments.
The TM1637 module includes four 0.36″ segment 7-segment displays. The module has a ‘colon’ at the center useed to create clock or time-based projects. At the back of the display, there is an inexpensive serial LED driver from Titan MicroElectronics called TM1637. The TM1637 supports many functions – including ON/OFF and brightness control of the LEDs as well as accessing each of the segments.
The module operates between 3.3V to 5V with a current consumption of 80mA. We can interface TM1637 with Arduino or any other microcontroller using the two data pins. There is multiple TM1637 Library for Arduino that removes the complexities and makes it easier to communicate with the display.
TM1637 Module Pinout
There is a 4-pin male header on the module for making connections.
1. GND: Ground Pin
2. VCC: 3.3V to 5V power supply Pin
3. DIO: Data Input/Output Pin
4. CLK: Clock Input Pin
Interfacing TM1637 4-digits LED Display with Arduino
Now, let us interface TM1637 4-digits LED Display with Arduino Nano Board. The connection is fairly simple. Make a connection between TM1637 & Arduino Nano Board as shown in image.
Connect the VCC & GND Pin of TM1637 Module to Arduino 5V & GND Pin. Similarly, connect the Module CLK & DIO Pin to Arduino digital pin 2 & 3 respectively.
TM1637 Arduino Library Installation
There are several libraries for TM1637 Display. Out of all those libraries, the library from Avishay Orpaz is the most popular one. This library has several built-in functions that make controlling the display simple. We need to specify which number to display and it will handle.
You can download the TM1637 Library from the Github Link.
You can also visit the Library manager and install the library directly. Search by typing ‘tm1637‘ & check for the library by Avishay Orpaz. Click on that and then select Install.
This library has several built-in functions that make controlling the display fairly easy.
- setSegments() – Set the raw value of the segments of each digit
- showNumberDec() – Display a decimal number
- showNumberDecEx() – Display a decimal number with decimal points or colon
- setBrightness() – Set the brightness of the display
- clear() – Clear the display
Basic TM1637 Arduino Example Code
This is the basic example code taken from the example of TM1637 Library. Copy the following code and paste it on your Arduino IDE. Then you can compile and upload the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #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 2000 const uint8_t SEG_DONE[] = { SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O SEG_C | SEG_E | SEG_G, // n SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E }; TM1637Display display(CLK, DIO); void setup() { } void loop() { int k; uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 }; display.setBrightness(0x0f); // All segments on display.setSegments(data); delay(TEST_DELAY); // Selectively set different digits data[0] = display.encodeDigit(0); data[1] = display.encodeDigit(1); data[2] = display.encodeDigit(2); data[3] = display.encodeDigit(3); display.setSegments(data); delay(TEST_DELAY); /* for(k = 3; k >= 0; k--) { display.setSegments(data, 1, k); delay(TEST_DELAY); } */ display.clear(); display.setSegments(data+2, 2, 2); delay(TEST_DELAY); display.clear(); display.setSegments(data+2, 2, 1); delay(TEST_DELAY); display.clear(); display.setSegments(data+1, 3, 1); delay(TEST_DELAY); // Show decimal numbers with/without leading zeros display.showNumberDec(0, false); // Expect: ___0 delay(TEST_DELAY); display.showNumberDec(0, true); // Expect: 0000 delay(TEST_DELAY); display.showNumberDec(1, false); // Expect: ___1 delay(TEST_DELAY); display.showNumberDec(1, true); // Expect: 0001 delay(TEST_DELAY); display.showNumberDec(301, false); // Expect: _301 delay(TEST_DELAY); display.showNumberDec(301, true); // Expect: 0301 delay(TEST_DELAY); display.clear(); display.showNumberDec(14, false, 2, 1); // Expect: _14_ delay(TEST_DELAY); display.clear(); display.showNumberDec(4, true, 2, 2); // Expect: 04__ delay(TEST_DELAY); display.showNumberDec(-1, false); // Expect: __-1 delay(TEST_DELAY); display.showNumberDec(-12); // Expect: _-12 delay(TEST_DELAY); display.showNumberDec(-999); // Expect: -999 delay(TEST_DELAY); display.clear(); display.showNumberDec(-5, false, 3, 0); // Expect: _-5_ delay(TEST_DELAY); display.showNumberHexEx(0xf1af); // Expect: f1Af delay(TEST_DELAY); display.showNumberHexEx(0x2c); // Expect: __2C delay(TEST_DELAY); display.showNumberHexEx(0xd1, 0, true); // Expect: 00d1 delay(TEST_DELAY); display.clear(); display.showNumberHexEx(0xd1, 0, true, 2); // Expect: d1__ delay(TEST_DELAY); // Run through all the dots for(k=0; k <= 4; k++) { display.showNumberDecEx(0, (0x80 >> k), true); delay(TEST_DELAY); } // Brightness Test for(k = 0; k < 4; k++) data[k] = 0xff; for(k = 0; k < 7; k++) { display.setBrightness(k); display.setSegments(data); delay(TEST_DELAY); } // On/Off test for(k = 0; k < 4; k++) { display.setBrightness(7, false); // Turn off display.setSegments(data); delay(TEST_DELAY); display.setBrightness(7, true); // Turn on display.setSegments(data); delay(TEST_DELAY); } // Done! display.setSegments(SEG_DONE); while(1); } |
The TM1637 Display will immediately turn ON and starts displaying the random numbers and characters as per the code.
Code Explanation
1 2 | #include <Arduino.h> #include <TM1637Display.h> |
The code starts with including the library. Install the correct library else you will get an error message while compiling the code.
1 2 | #define CLK 2 #define DIO 3 |
Next we need to specify the connection pins for CLK & DIO.
1 2 3 4 5 6 | const uint8_t SEG_DONE[] = { SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, // d SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O SEG_C | SEG_E | SEG_G, // n SEG_A | SEG_D | SEG_E | SEG_F | SEG_G // E }; |
You can create arrays to spell words. Each segment is separated by a | and digits of the display are separated by a comma. This array will display “done” on LED Display.
1 | #define TEST_DELAY 2000 |
This is the amount of time in milliseconds between the tests. This means the next thing will be displayed after an interval of 200 milliseconds.
1 | TM1637Display display(CLK, DIO); |
Then we need to create a new instance of the TM1637Display class with the function TM1637Display(). This function requires two parameters, first the CLK pin and the second the DIO pin.
1 2 | uint8_t data[] = { 0xff, 0xff, 0xff, 0xff }; uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 }; |
The first option to set individual segments is to use hexadecimal numbers. Hexadecimal 0xFF translates to 11111111 in binary, and it turns all segments ON while 0x00 will turn OFF all segments.
1 2 | display.setBrightness(7, false); // Turn off display.setBrightness(7, true); // Turn on |
This function is used to set the brightness of the display using the syntax setBrightness(brightness,on). You can specify a brightness level from 0 (lowest brightness) to 7 (highest brightness). You can pass it true (display ON) or false (display OFF).
1 2 | display.setSegments(data); display.setSegments(data+2, 2, 2); |
This function can be used to set individual segments of a display. The first argument is the array that contains the segment information. The second argument specifies the number of digits to be updated between 0 to 4. The third argument determines the position from which you want to print (0-leftmost, 3-rightmost).
Displaying Temperature Value on TM1637 using DHT11 Sensor
We can make our own Digital Thermometer using DHT22/11 Sensor along with the TM1637 Display & Arduino. The connection diagram is fairly simple. You can use a breadboard to assemble the circuit.
The DHT11/22 is a basic, ultra low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and spits out a digital signal on the data pin. To learn more about this sensor follow up the DHT11 Arduino Interfacing tutorial. In this project, I used breadboard to assemble DHT22 Sensor with Arduino & TM1637 Display.
Source Code/Program
This code will simply display the temperature value of the surrounding on TM1637 Display.
Copy the following code and paste it on your Arduino IDE. Before compiling this code, you need to add DHT Library to the library folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | // Include the libraries #include <TM1637Display.h> #include <Adafruit_Sensor.h> #include <DHT.h> // Define the connections pins #define CLK 2 #define DIO 3 #define DHTPIN 5 // Create variable int temperature_celsius; int temperature_fahrenheit; // Create °C symbol const uint8_t celsius[] = { SEG_A | SEG_B | SEG_F | SEG_G, // Circle SEG_A | SEG_D | SEG_E | SEG_F // C }; // Create °F symbol const uint8_t fahrenheit[] = { SEG_A | SEG_B | SEG_F | SEG_G, // Circle SEG_A | SEG_E | SEG_F | SEG_G // F }; //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302) // Create display object of type TM1637Display TM1637Display display = TM1637Display(CLK, DIO); // Create dht object of type DHT: DHT dht = DHT(DHTPIN, DHTTYPE); void setup() { // Set the display brightness (0-7) display.setBrightness(5); // Clear the display display.clear(); // Setup sensor dht.begin(); } void loop() { // Read the temperature as Celsius and Fahrenheit temperature_celsius = dht.readTemperature(); temperature_fahrenheit = dht.readTemperature(true); // Display the temperature in celsius format display.showNumberDec(temperature_celsius, false, 2, 0); display.setSegments(celsius, 2, 2); delay(1000); // Display the temperature in fahrenheit format display.showNumberDec(temperature_fahrenheit, false, 2, 0); display.setSegments(fahrenheit, 2, 2); delay(1000); } |
The display will start displaying the temperature both in degrees Celcius and Fahrenheit.
Creating a Clock with TM1637 and DS3231 RTC Module
We can use the TM1637 Display & DS3231 RTC Module with Arduino to create a Real Time Clock as well. The connection diagram is given below.
The DS3231 RTC module Precise Real-Time Clock Module is a low-cost, extremely accurate I²C real-time clock (RTC) with an integrated temperature-compensated crystal oscillator (TCXO) and crystal. The module can be interfaced with Arduino & any other controller to design a Real-Time Clock. Check our previous DS3231 & Arduino Interfacing tutorial.
Source Code/Program
This code will display the time in minute and hour on TM1637 Display.
Copy the following code and upload it to the Arduino Board. But before that add the DS3231 RTC Library to the Library Folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | // Include the libraries #include "RTClib.h" #include <TM1637Display.h> // Define the connections pins #define CLK 2 #define DIO 3 // Create rtc and display object RTC_DS3231 rtc; TM1637Display display = TM1637Display(CLK, DIO); void setup() { // Begin serial communication Serial.begin(9600); // Check if RTC is connected correctly if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } // Check if the RTC lost power and if so, set the time if (rtc.lostPower()) { Serial.println("RTC lost power, lets set the time!"); // The following line sets the RTC to the date & time this sketch was compiled: rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: //rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); } // Set the display brightness (0-7) display.setBrightness(5); // Clear the display display.clear(); } void loop() { // Get current date and time DateTime now = rtc.now(); // Create time format to display int displaytime = (now.hour() * 100) + now.minute(); // Display the current time in 24 hour format with leading zeros and a center colon enabled display.showNumberDecEx(displaytime, 0b11100000, true); delay(1000); } |
The TM1637 Display will start showing the Time immediately after you load the code.
Video Tutorial & Guide
You can also use TM1637 with MicroPython Code on Raspberry Pi Pico Board.
No comments