The first number
Four wires, one library from the Library Manager, and eight lines of sketch. 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
Upload the sketch. You should get 1234 for two seconds, then a count.
Three things in eight 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. Uncomment the pin numbers for your board. Nothing here is board-specific except those two numbers — the display behaves identically on all three.
// The first number: 1234, then a count from 0 to 60.
//
// Wiring, display to board:
// Display GND -> board GND
// Display VCC -> board 5V on an Uno, 3V3 on an ESP32 or ESP32-S3
// Display CLOCK -> Uno D2, ESP32 GPIO 18, ESP32-S3 GPIO 4
// Display DATA -> Uno D3, ESP32 GPIO 19, ESP32-S3 GPIO 5
//
// Arduino IDE: install "TM1637" by Avishay Orpaz from the Library Manager
// (Tools -> Manage Libraries, search TM1637). No special Tools settings on
// any of the three boards.
#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. Getting these the wrong way round is the most
// common reason a display stays dark.
TM1637Display display(CLOCK_PIN, DATA_PIN);
void setup() {
display.setBrightness(4); // 0 to 7; only takes effect on the next write
display.showNumberDec(1234); // and it stays there, with no further help
delay(2000);
}
void loop() {
for (int i = 0; i <= 60; i++) {
display.showNumberDec(i); // leading zeros off: 7 shows as " 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.
When 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 rated for 3.3 V, and that damage is cumulative rather than immediate.
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.