IOT

How to use TM1637 4-digit 7-segment LED display with Arduino

In this blog, we’ll learn how to use the TM1637 4-digit 7-segment LED display with Arduino. These displays are less expensive and are ideal for displaying sensor data, time, stopwatch, random numbers, and other information. It looks like other 4-digit 7-segment displays but contains a TM1637 LED Driver IC. This eliminates unnecessary wiring and allows the display to be controlled with only two wires.

In this post, I’ve covered three TM1637 Arduino examples. We’ll look at the basic operations of the TM1637 Display library and display some random numbers and letters in the first example. In the second example, we’ll use an Arduino and the TM1637 to make a simple temperature display with the DHT11. The third example will make use of the DS3231 Real Time Clock Module to display the Time on TM1637 Display.

Hardware Required:

  • Arduino Nano Board Atmega328
  • TM1637-4-Digit 7-Segment Display
  • DS3231-Real Time Clock Module
  • DHT22/11-Humidity & Temperature Sensor
  • Male-to-Male Jumper Wires
  • Breadboard

TM1637 4-digit 7-segment LED display

TM1637 4-digit 7-segment LED display usually requires 12 connection pins. Since the TM1637 IC is positioned on the rear of the display module, the total wire count is reduced to four. The power connections require two pins, while the other two pins are utilized to control the segments.

Four 0.36′′ segment 7-segment displays are included in the TM1637 module. A ‘colon’ in the center of the module is used to develop clock or time-based projects. The TM1637 serial LED driver from Titan MicroElectronics can be found at the back of the display. Many functions are supported by the TM1637, including ON/OFF and brightness control of the LEDs, as well as access to each section.

The module operates between 3.3V to 5V with a current consumption of 80mA. sing the two data pins, we may connect the TM1637 to an Arduino or any other microcontroller. There are several TM1637 Libraries for Arduino that simplify the process and make communicating with the display easier.

TM1637 Module Pinout

The module has a 4-pin male header for creating connections.

TM1637 Pinout
  • GND: Ground Pin
  • VCC: 3.3V to 5V power supply Pin
  • DIO: Data Input/Output Pin
  • CLK: Clock Input Pin

Interfacing TM1637 4-digits LED Display with Arduino

Let’s connect the TM1637 4-digit LED Display to the Arduino Nano Board now. The connection is simple. Connect the TM1637 to the Arduino Nano board as shown in the image.

Connect the TM1637 Module’s VCC and GND pins to the Arduino 5V and GND pins. Connect the CLK and DIO pins of the Module to Arduino digital pins 2 and 3, respectively.

TM1637 Arduino Library Installation

For the TM1637 Display, there are numerous libraries available. The library created by Avishay Orpaz is the most popular of all of them. Several built-in functions in this library make managing the display simple. We must indicate the number to display, and it will take care of the rest.

You can download the TM1637 Library from the Github Link.

You can alternatively go to the Library Manager and manually install the library. Type ‘tm1637’ into the search box and look for the library by Avishay Orpaz. Select Install after clicking that.

This library includes various built-in functions for controlling the display.

  • 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 sample code from the TM1637 Library example. Copy and paste the following code into your Arduino IDE. The code can then be compiled and uploaded.

#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 turn on immediately and begin displaying random numbers and characters according to the code.

Code Explanation

#include <Arduino.h>
#include <TM1637Display.h>

The code begins with the library being included. If you don’t install the necessary library, you’ll get an error notice when you try to compile the code.

#define CLK 2
#define DIO 3

Next we need to specify the connection pins for CLK & DIO.

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
};

Arrays can be used to spell words. A | separates each section, whereas a comma separates the display’s numerals. On the LED Display, this array will say “done.”

#define TEST_DELAY 2000

The time between testing is expressed in milliseconds. This implies that after 200 milliseconds, the next item will be presented.

TM1637Display display(CLK, DIO);

Then, using the function TM1637Display, we must create a new instance of the TM1637Display class (). The CLK pin and the DIO pin are both required parameters for this function.

uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
uint8_t blank[] = { 0x00, 0x00, 0x00, 0x00 };

Hexadecimal numbers are the first choice for setting individual segments. In binary, hexadecimal 0xFF equals 11111111, and it turns all segments ON, whereas 0x00 sets all segments OFF.

display.setBrightness(7, false); // Turn off
display.setBrightness(7, true); // Turn on

This function is used to set the display’s brightness using the setBrightness syntax (brightness,on). You can set the brightness level anywhere between 0 and 7. (highest brightness). You can set it to true (display ON) or false (display OFF) (display OFF).

display.setSegments(data);
display.setSegments(data+2, 2, 2);

This function can be used to customize specific display segments. The array containing the segment information is the first argument. The second input indicates the number of digits between 0 and 4 that should be changed. The third option specifies the starting point for printing (0-leftmost, 3-rightmost).

Displaying Temperature Value on TM1637 using DHT11 Sensor

We can create our own Digital Thermometer with the DHT22/11 Sensor, TM1637 Display, and Arduino. The connection diagram is simple. The circuit can be assembled on a breadboard.

The DHT11/22 is a basic digital temperature and humidity sensor at a low price. It measures the ambient air with a capacitive humidity sensor and a thermistor and outputs a digital signal on the data pin. Follow the DHT11 Arduino Interfacing tutorial to learn more about this sensor. I used a breadboard to connect a DHT22 sensor to an Arduino and a TM1637 display in this project.

Source Code/Program

This code will just display the ambient temperature on the TM1637 Display.

Copy and paste the following code into your Arduino IDE.

Before compiling this code, you need to add DHT Library to the library folder.

// 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 also make a Real-Time Clock with Arduino and the TM1637 Display and DS3231 RTC Module. The diagram of the connection is shown below.

The Precise Real-Time Clock Module (DS3231 RTC) is a low-cost, very accurate I2C real-time clock (RTC) with an inbuilt temperature-compensated crystal oscillator (TCXO) and crystal. The module can be used to create a Real-Time Clock using Arduino or any other controller.

Source Code/Program

On the TM1637 Display, this code will display the time in minutes and hours.

Copy the code below and paste it into the Arduino board. But before that add the DS3231 RTC Library to the Library Folder.


// 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.

Conclusion:I hope all of you understand how to use the TM1637 4-digit 7-segment LED display with Arduino. We MATHA ELECTRONICS will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.