Arduino Mega - TM1637 4-Digit 7-Segment Display

For projects like clocks, timers, and counters, you use a normal 4-digit 7-segment display. Usually, it needs 12 wires. But the TM1637 module makes it easier by using only 4 wires: two wires for power and two wires to control the segments.

This guide won't go into hardware details. We'll show how to connect a 4-digit seven-segment display to the Arduino Mega and how to program it to show what we want.

Arduino Mega TM1637 4-digit 7-segment display

This guide uses a 4-digit, colon-separated 7-segment display. If you need to show decimal numbers, please use the 74HC30 4-digit 7-segment Display Module.

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×TM1637 4-digit 7-segment Display (colon-separated)
1×Jumper Wires

Or you can buy the following kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of TM1637 4-digit 7-segment Display

A TM1637 module usually has four 7-segment LED displays and a colon LED between them. It can show time as hours and minutes, or minutes and seconds, or the scores of two teams.

Pinout

The TM1636 four-digit seven-segment display module has four pins.

  • CLK pin: Clock pin. You can connect it to any digital pin on the Arduino Mega.
  • DIO pin: Data pin (input/output). Connect it to any digital pin on the Arduino Mega.
  • VCC pin: Power pin. It gives power to the module. Connect it to a 3.3V or 5V supply.
  • GND pin: Ground pin. Connect it to the ground.
TM1637 module pinout

Wiring Diagram

To connect a TM1637 display to an Arduino Mega, you need four wires: two for power and two for control. Power the module from the Mega’s 5V pin. Connect the CLK and DIO pins to any digital pins on the Mega, like 2 and 3. If you use different pins, remember to update the pin numbers in the code.

The wiring diagram between Arduino Mega TM1637 Module

This image is created using Fritzing. Click to enlarge image

Library Installation

To use the TM1637 4-digit 7-segment display easily, you need to install the TM1637Display library by Avishay Orpaz. Here are the steps to install the library:

  • Open the Libraries icon on the left side of the Arduino IDE.
  • Type TM1637 in the search box and find the TM1637Display library by Avishay Orpaz.
  • Click the Install button.
Arduino Mega TM1637 4-digit 7-segment display library

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

  • Add the library
#include <TM1637Display.h>
  • Connect the Arduino Mega to the display using D9 as CLK and D10 as DIO.
#define CLK 9 #define DIO 10
  • Create a display item named TM1637Display.
TM1637Display display = TM1637Display(CLK, DIO);
  • You can show:
  • A number
  • A decimal number
  • A negative number
  • Or a letter
  • If you use a letter, say how it looks. Here are examples for each case:
  • To show a number, see the examples below.
  • The symbol _ (underscore) means a digit that is not shown.
display.showNumberDec(-12); // Displays -12 using default width and alignment display.showNumberDec(-999); // Displays -999 with default width and alignment display.showNumberDec(42); // Displays 42 using default width and alignment display.showNumberDec(42, false); // Displays 42 with leading zeros disabled display.showNumberDec(42, false, 2, 0); // Right-aligned in a 2-digit field display.showNumberDec(42, true); // Displays 42 with leading zeros to fill a 4-digit field display.showNumberDec(14, false, 2, 1); // Centers the value in a 4-digit display display.showNumberDec(-5, false, 3, 0); // Displays -5 in a 3-digit field with a leading space display.showNumberDec(1234); // Displays 1234
  • Show the number using a colon or a period.
// Renders the value 1530 as 15.30 on a 4-digit 7-segment display using decimal point flags 0b11100000 display.showNumberDecEx(1530, 0b11100000, false, 4, 0);

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-tm1637-4-digit-7-segment-display */ #include <TM1637Display.h> #define CLK 9 // The Arduino Mega pin connected CLK pin of 7-segment module #define DIO 10 // The Arduino Mega pin connected DIO pin of 7-segment module // 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(); }

Detailed Instructions

Follow these steps one by one.

  • Connect a TM1637 4-digit 7-segment display to the Arduino Mega using the provided diagram.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the right board (Arduino Mega) and the correct COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to upload the code to the Arduino Mega.
  • Watch the states of the 7-segment display.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!