Arduino Mega · TM1637 Display

Arduino Mega TM1637 Display Simulator: Wiring, Code and a Live Circuit

This is a TM1637 4-digit 7-segment display wired to an Arduino Mega 2560, running a minutes and seconds clock live in your browser with the TM1637Display library.

Four wires: power, GND, CLK to pin 2 and DIO to pin 3. Two signal pins drive all 32 segments.

  • 5 V logic
  • Library: TM1637Display.h
  • CLK 2, DIO 3
  • Colon for a clock
Arduino Mega · TM1637 Displaylive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the TM1637 Display to the Arduino Mega

Every connection in the circuit above, with the board’s own pin names.

TM1637 Display pinArduino Mega pinWhy
VCC5VThe TM1637 runs from 3.3 to 5 V; brighter at 5 V.
GNDGNDCommon ground.
CLKpin 2Clock. Any digital pin: it is not I2C, despite looking like it.
DIOpin 3Data, both directions.

The module runs from 5V and its CLK and DIO lines idle at 5 V through the module’s pull-ups, the Arduino Mega’s own level.

Pins to leave alone on the Arduino Mega

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

On a real bench

The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The TM1637 Display uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

TM1637 clock on Arduino Mega
#include <TM1637Display.h>

const int CLK_PIN = 2;   // pin 2
const int DIO_PIN = 3;   // pin 3

TM1637Display display(CLK_PIN, DIO_PIN);

void setup() {
  Serial.begin(9600);
  display.setBrightness(4);         // 0 to 7
  Serial.println("Counting");
}

void loop() {
  // Minutes and seconds since reset, with the colon on.
  unsigned long s = millis() / 1000;
  int mmss = (s / 60) % 100 * 100 + s % 60;
  display.showNumberDecEx(mmss, 0b01000000, true);
  delay(200);
}

TM1637Display display(CLK_PIN, DIO_PIN) names the two pins. The TM1637 chip on the module scans the four digits itself, so the board only sends it a number when something changes.

showNumberDecEx(number, dots, leadingZeros) shows a number up to 9999. The dots mask 0b01000000 lights the colon between the second and third digits, and true keeps the leading zeros, so 1:05 shows as 01:05.

Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.

Common mistakes with the TM1637 Display on the Arduino Mega

Wiring CLK and DIO to the I2C pins

The protocol looks like I2C but is not; it has no address and the Wire library cannot talk to it. Any two digital pins work, and leaving pin 20 and pin 21 free keeps them for a real I2C part.

Expecting decimal points

Most TM1637 clock modules wire only the colon, not the dots under each digit. The dots argument lights them on modules that have them, and the colon on these.

Taking the serial or I2C pins

Pins 0 and 1 are the USB serial port, 14 to 19 are Serial1 to Serial3 and 20 and 21 are I2C. CLK or DIO works on any of them until the sketch needs that port, so start on pin 2 and up.

Questions and answers

Can I simulate a TM1637 display with an Arduino Mega 2560?

Yes. The circuit at the top of this page is an Arduino Mega 2560 with a TM1637 display, running in your browser on a simulated ATmega2560. The simulated module decodes the real two-wire protocol, so the digits and the colon show exactly what the library sent. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

How do I show letters?

setSegments() takes a byte per digit, one bit per segment, so letters that fit a 7-segment shape (A, b, C, d, E, F and a few more) can be built by hand.

Can I show negative numbers?

showNumberDec() handles them down to -999, using the first digit for the minus sign.

What about a MAX7219 display?

It is a different chip on a three-wire SPI-style bus, driven with LedControl.h. The simulator has it too; the TM1637 is cheaper and needs one pin fewer.

Build your own TM1637 Display project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.