The first number
Four wires, one library from the Library Manager, and a dozen lines of sketch that put 1234 on the display and then count. On an Uno feed it 5V; on an ESP32 feed it 3V3, because the board's pull-ups put VCC on the two signal wires.
Install the library
In the Arduino IDE, open Tools → Manage Libraries, search for TM1637
and install the one by Avishay Orpaz. The header it gives you is
TM1637Display.h.
There are at least six libraries called some variation of TM1637, and they do
not share function names. Sketches from elsewhere that call display.show() or
tm.displayStr() belong to different ones and will not compile against this.
Four wires
TM1637Display display(2, 3); — CLOCK first. VCC is the one worth a thought: it is 5V here, and it decides what voltage the two signal wires sit at, because the board’s 10 kΩ resistors pull them up to it.| Display | Arduino Uno | ESP32 | ESP32-S3 |
|---|---|---|---|
| GND | GND | GND | GND |
| VCC | 5V | 3V3 | 3V3 |
| CLOCK | D2 | GPIO 18 | GPIO 4 |
| DATA | D3 | GPIO 19 | GPIO 5 |
Any two digital pins work for CLOCK and DATA; these avoid the pins each board needs for itself. VCC is the one that is not free choice: the board's pull-ups tie both signal wires to it, so on a 3.3 V board it goes to 3V3.
Run it
Set Tools > Board to your board and Tools > Port to the one that appears when you plug it in; on an ESP32-S3 also set Tools > USB CDC On Boot to Enabled. The sketch's opening comment lists the same rows, so a copy that travels without this page still carries them.
Upload it. You should get 1234 for two seconds, then a count.
Three lines are worth pointing at.
TM1637Display display(CLOCK_PIN, DATA_PIN) takes clock first. Both are
plain digital pins, so the wrong way round is not an error anybody's compiler
can catch — it is a dark display.
setBrightness(4) does not send anything on its own. The brightness rides
along with the next lot of digits, which is why it is called before
showNumberDec and not after. Called on its own in a sketch that never shows
anything again, it appears to do nothing at all.
showNumberDec(i) writes all four digits and blanks the ones it does not need,
so 7 appears as three blanks and a 7 rather than 0007. Passing true as the
second argument gives you the leading zeros instead — and for a clock you will
want them, for
a reason that only shows up at midnight.
Then unplug the board
With the count running, pull the USB cable and plug it back in. The display
comes up blank and stays blank until setup() runs again — nothing is stored
in the display across a power cut.
Now try it the other way: while the count is running, press the reset button and watch. The number on the display at the moment of the reset stays there until the sketch writes a new one. The chip does not know or care that the board driving it has restarted, which is the whole point of the previous two articles and something to remember when a display looks perfectly alive and the sketch behind it is not.
The code
Shows 1234, waits, then counts 0 to 60 twice a second. Change the two pin numbers for your board; nothing else in it is board-specific, and the display behaves the same on all three.
/*
TM1637 4-Digit Display - the first number TK51 / /p/tk51
Wiring. No pad on this board is square, so go by the names
printed on the back: GND, VCC, clock, data. Seen from the
display side with the header on the left, GND is the top pin.
GND -> GND
VCC -> your board's logic supply: 5V on an Uno, 3V3 on an
ESP32 or ESP32-S3. Never 5V beside a 3.3 V board:
the pull-ups would put 5 V on your pins.
CLOCK -> D2 on an Uno, GPIO 18 on an ESP32, GPIO 4 on an
ESP32-S3
DATA -> D3 on an Uno, GPIO 19 on an ESP32, GPIO 5 on an
ESP32-S3
Arduino IDE
Tools > Board your board, e.g. Arduino Uno
Tools > Port the one that appears when you plug in
Tools > USB CDC On Boot Enabled (ESP32-S3 only)
Library: "TM1637" by Avishay Orpaz, from Tools > Manage
Libraries. It installs TM1637Display.h.
*/
#include <TM1637Display.h>
#define CLOCK_PIN 2 // Uno D2. ESP32: 18. ESP32-S3: 4.
#define DATA_PIN 3 // Uno D3. ESP32: 19. ESP32-S3: 5.
// CLOCK first, then DATA. The wrong way round is a dark display.
TM1637Display display(CLOCK_PIN, DATA_PIN);
void setup() {
display.setBrightness(4); // 0 to 7, sent with the next write
display.showNumberDec(1234); // stays there with no further help
delay(2000);
}
void loop() {
for (int i = 0; i <= 60; i++) {
display.showNumberDec(i); // leading zeros off: 7 is " 7"
delay(500);
}
}If it stays dark, swap CLOCK and DATA: crossed over, they look exactly like a dead board and damage nothing. If the count runs but the first digit stays blank on 0 to 9, that is showNumberDec blanking the digits it does not need, which is what it is meant to do.
View on GitHub · blocks/tk51-tm1637-display/arduino/first_number/first_number.ino @ v1.13The same thing in MicroPython, for an ESP32, an ESP32-S3 or a Pico. MicroPython has no TM1637 driver built in, so save tm1637.py from the same folder to the board first; it drives the two wires by hand, the way the Arduino library does.
"""
TM1637 4-Digit Display - the first number TK51 / /p/tk51
Wiring. No pad on this board is square, so go by the names
printed on the back: GND, VCC, clock, data. Seen from the
display side with the header on the left, GND is the top pin.
GND -> GND
VCC -> 3V3 on an ESP32 or ESP32-S3, 3V3(OUT) on a Pico.
Never 5V: the pull-ups would put 5 V on your pins.
CLOCK -> GPIO 18 on an ESP32, GPIO 4 on an ESP32-S3,
GP2 on a Raspberry Pi Pico
DATA -> GPIO 19 on an ESP32, GPIO 5 on an ESP32-S3,
GP3 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save tm1637.py from this folder to the board first.
"""
import time
from tm1637 import TM1637
# CLOCK, DATA. ESP32: 18, 19. ESP32-S3: 4, 5. Pico: 2, 3.
display = TM1637(18, 19)
display.brightness(4) # sent with the next write
display.number(1234) # stays there with no further help
time.sleep(2)
while True:
for i in range(61):
display.number(i) # leading zeros off: 7 is " 7"
time.sleep_ms(500)There is no Uno here: an Uno cannot run MicroPython. The driver's number() takes the same arguments as the Arduino library's showNumberDecEx, minus the zero bug: a colon on the number 0 stays lit.
View on GitHub · blocks/tk51-tm1637-display/micropython/first_number.py @ v1.13When it does not work
In order: is GND connected, is VCC connected, and are CLOCK and DATA the right way round? Crossed signal wires are the usual answer and are harmless — the chip never sees a start condition, so it ignores everything. Then check the two pin numbers in the sketch match the two wires.
Check VCC is on 3V3 and not 5V. It will often appear to work from 5V, which is the problem: the board's pull-ups then hold CLOCK and DATA at 5 V against pins Espressif rates for 3.3 V. Nothing may fail at once, which is why it goes unnoticed.
Set Tools > USB CDC On Boot to Enabled and upload again. This sketch prints nothing, but the next ones do, and without that setting the S3's USB port brings up no serial port at boot.
The library is not installed, or a different TM1637 library is. Search the Library Manager for TM1637 and install the one by Avishay Orpaz — several others exist with different function names, and code written for one will not compile against another.
Either the brightness is low — try setBrightness(7) — or the panel is a blue or white one on 3.3 V, which is a supply problem rather than a code one. Red, orange and yellow panels are the bright ones at 3.3 V.
How a digit is stored, and how to write the things that are not digits.
Seven segments, one byte →Edit this page — content/books/tm1637-display/the-first-number.mdx
Questions about this product
See what other owners have asked, and read their solutions.
This page covers several products. Choose yours to see the right discussions.
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.