Arduino - TM1637 4-Digit 7-Segment Display

A standard 4-digit 7-segment display is needed for clock, timer and counter projects, but it usually requires 12 connections. The TM1637 module makes it easier by only requiring 4 connections: 2 for power and 2 for controlling the segments.

This tutorial will not overload you by deep driving into hardware. Instead, We will learn how to connect the 4-digit 7-segment display to Arduino, how to program it do display what we want.

Arduino TM1637 4-digit 7-segment display

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×TM1637 4-digit 7-segment Display
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About TM1637 4-digit 7-segment Display

A TM1637 module is typically included four 7-segment LEDs and a colon-formed LED at the center. It is perfect for displaying time in hour and minute, or minute and seconds. It is also usfult for display temperature.

Pinout

TM1637 includes 4 pins:

  • CLK is a clock input pin. Connect to any digital pin on Arduino.
  • DIO is a Data I/O pin. Connect to any digital pin on Arduino.
  • VCC pin supplies power to the module. Connect it to the 3.3V to 5V power supply.
  • GND is a ground pin.
TM1637 Module Pinout

Wiring Diagram

To connect a TM1637 to an Arduino, connect four wires: two for power and two for controlling the display. The module can be powered from the 5-volt output of the Arduino. Connect the CLK and DIO pins to any digital pins of Arduino. For example, 2 and 3 on the Arduino. The pin numbers in the code should be changed if different pins are used.

Arduino TM1637 Module Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Library Installation

To program easily for TM1637 4-digit 7-segment Display, we need to install TM1637Display library by Avishay Orpaz. Follow the below steps to install the library:

  • On Arduino IDE, Go to Tools Manage Libraries
Arduino add library
  • Search “TM1637”, then find the TM1637Display library by Avishay Orpaz
  • Click Install button.
Arduino TM1637 4-digit 7-segment display library

How To Program For TM1637 4-digit 7-segment using Arduino

  • Include the library
#include <TM1637Display.h>
  • Define Arduino's pins that connects to CLK and DIO of the display module. For example, pin D9 and D10
#define CLK 9 #define DIO 10
  • Create a display object of type TM1637Display
TM1637Display display = TM1637Display(CLK, DIO);
  • Then you can display number, number with decimal, number with minus sign, or letter. In the case of leter, you need to define the letter form. Let's see one by one.
  • Display number: see below examples, '_' in below description represents for a digit that does not display anything in pratice:
display.showNumberDec(-12); // displayed _-12 display.showNumberDec(-999); // displayed -999 display.showNumberDec(42); // displayed __42 display.showNumberDec(42, false); // displayed __42 display.showNumberDec(42, false, 2, 0); // displayed 42__ => display 2 digit at position 0 display.showNumberDec(42, true); // displayed 0042 => zero padding display.showNumberDec(14, false, 2, 1); // displayed _14_ display.showNumberDec(-5, false, 3, 0); // displayed _-5_ display.showNumberDec(1234); // displayed 1234
  • Display number with colon:
// displayed 15:30 display.showNumberDecEx(1530, 0b11100000, false, 4, 0);

You can see more detail in the function references at the end of this tutorial

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-tm1637-4-digit-7-segment-display */ #include <TM1637Display.h> // define the connections pins #define CLK 9 #define DIO 10 // create a display object of type TM1637Display TM1637Display display = TM1637Display(CLK, DIO); // an array that sets individual segments per digit to display the word "dOnE" const uint8_t 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 }; // degree celsius symbol const uint8_t celsius[] = { SEG_A | SEG_B | SEG_F | SEG_G, // Degree symbol SEG_A | SEG_D | SEG_E | SEG_F // C }; void setup() { display.clear(); display.setBrightness(7); // set the brightness to 7 (0:dimmest, 7:brightest) } void loop() { // show counter 0-9 int i; for (i = 0; i < 10; i++) { display.showNumberDec(i); delay(500); display.clear(); } display.showNumberDec(-91); // displayed _-91 delay(2000); display.clear(); display.showNumberDec(-109); // displayed -109 delay(2000); display.clear(); display.showNumberDec(21, false); // displayed __21 delay(2000); display.clear(); display.showNumberDec(21, true); // displayed 0021 delay(2000); display.clear(); display.showNumberDec(28, false, 2, 1); // displayed _28_ delay(2000); display.clear(); display.showNumberDec(-9, false, 3, 0); // displayed _-9_ delay(2000); display.clear(); // displayed 15:30 display.showNumberDecEx(1530, 0b11100000, false, 4, 0); delay(2000); display.clear(); // displayed 23°C int temperature = 23; // or read from temperature sensor display.showNumberDec(temperature, false, 2, 0); display.setSegments(celsius, 2, 2); delay(2000); display.clear(); // displayed letters: dOnE display.setSegments(done); delay(2000); display.clear(); }

Quick Steps

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • See the states of the 7-segment display

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Function References

The below are references for the following functions:

  • display.clear()
  • display.showNumberDec()
  • display.showNumberDecEx()
  • display.setSegments()
  • display.setBrightness()

display.clear()

Description

This function clear the display. It turns all LEDs off

display.showNumberDec()

Description

The function is used to display a decimal number on the 7-segment display.

Syntax

void showNumberDec(int num, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);

Parameter

  • num: This is the number to be displayed on the 7-segment display. It should be within the range of -9999 to 9999.
  • leading_zero: This is an optional parameter with a default value of false. If it is set to true, leading zeros will be displayed.
  • length: This is an optional parameter with a default value of 4. It sets the number of digits to be displayed on the 7-segment display.
  • pos: This is an optional parameter with a default value of 0. It sets the position of the most significant digit of the number.

Please note that, if the number is out of range or if the value of length is greater than 4, the function will not display anything.

showNumberDecEx()

Description

The function is used to display a decimal number on the 7-segment display with additional features compared to the showNumberDec() function. It is an advanced version of showNumberDec() that allows you to control the dot or colon segments of each digit individually.

Syntax

void showNumberDecEx(int num, uint8_t dots, bool leading_zero = false, uint8_t length = 4, uint8_t pos = 0);

Parameter

  • num1: This is the number to be displayed on the 7-segment display. It should be within the range of -9999 to 9999.
  • dots: This parameter is used to specify which segments of the display should be turned on as dots. Each bit of the value corresponds to a digit on the display: Valid value
    • 0b10000000: display the first dot: 0.000
    • 0b01000000: display the second dot: 00.00
    • 0b00100000: display the third dot: 000.0
    • 0b01000000: For displays with just a colon: 00:00
  • leading_zero: This is an optional parameter with a default value of false. If it is set to true, leading zeros will be displayed.
  • length: This is an optional parameter with a default value of 4. It sets the number of digits to be displayed on the 7-segment display.
  • pos: This is an optional parameter with a default value of 0. It sets the position of the most significant digit of the number.

For example, if you call display.showNumberDecEx(1530,0b01000000); it will display the number 15:30 on the 7-segment display.

Please note that, if the number is out of range or if the value of length is greater than 4, the function will not display anything.

setSegments()

Description

The function is used to set the segments of the 7-segment display directly. It can be used to dislay letters, special character, or turn all all LED segment.

Syntax

void setSegments(const uint8_t segments[], uint8_t length = 4, uint8_t pos = 0);

Parameter

  • segments: This parameter sets the segments of the 7-segment display, it's an array of bytes, where each byte represents the segments of each digit. Each segment is represented by a bit in the byte.
  • length: This is an optional parameter with a default value of 4. It sets the number of digits to be displayed on the 7-segment display.
  • pos: This is an optional parameter with a default value of 0. It sets the position of the most significant digit of the number.

This function is useful when you want to display characters or symbols that are not included in the basic 7-segment display. By setting the segments directly, you can display any pattern you want.

Please note that, if the number is out of range or if the value of length is greater than 4, the function will not display anything.

setBrightness()

Description

The function is used to set the brightness of the 7-segment display.

Syntax

void setBrightness(uint8_t brightness, bool on = true);

Parameter

  • brightness: This parameter sets the brightness level of the 7-segment display. The value should be in the range of 0 to 7. A higher value results in a brighter display.
  • on: This is an optional parameter with a default value of true. It's used to turn on or off the display. If it's set to false, the display will be turned off.

The Best Arduino Starter Kit

See Also

※ OUR MESSAGES