TM1638 LED and key board

The TM1638 "LED&KEY" board: eight seven-segment digits, eight LEDs and eight buttons on three wires, STB, CLK and DIO.

Aliases: LED&KEY, TM1638 module, 8-digit display with buttons. The chip holds the picture and scans the buttons itself, so a sketch only sends bytes, which makes it the usual front panel for clocks, counters and scoreboards.

Pins

Pin What it does
VCC, GND Supply, 5 V.
STB Strobe: low for the length of each command.
CLK The clock; data is taken on its rising edge.
DIO Data, both ways: the board drives it to send, the chip pulls it low to answer a key read.

Driving it from an Arduino

With Gavin Lyons' TM1638plus library, which Mokxi ships as TM1638plus.h:

#include <TM1638plus.h>
TM1638plus tm(4, 6, 7);   // STB, CLK, DIO

void setup() {
  tm.displayBegin();
  tm.displayText("hello");
}

void loop() {
  uint8_t buttons = tm.readButtons();     // bit 0 is S1
  tm.setLEDs((uint16_t)buttons << 8);     // the LED over each button
}

While it runs

Press the buttons with the pointer. The digits and LEDs are what the chip's display RAM holds.

What the model gets right

From the TM1638 datasheet: the data, address and display control commands; bytes least significant bit first on the rising edge of CLK; auto-increment and fixed addressing; sixteen bytes of display RAM, the even ones the digits and bit 0 of the odd ones the LEDs on this board; the eight pulse widths; display off keeping the RAM. A key read returns four bytes on DIO, open drain against the board's pull-up, with the board's buttons wired so the usual byte_i << i fold gives one bit per button.

What it does not model

The multiplexing (all eight digits are drawn lit at once), the 1 MHz clock limit, test mode, and the bicolor LEDs of the other TM1638 boards.

Common mistakes

Sending bytes most significant bit first, as SPI does: the segments come out scrambled. Leaving STB low between commands: each command needs its own strobe.

See it in action

LED and key counts button presses on it. Open it at /templates.