Built-in project · Arduino Uno R3

LED and key on an Arduino Uno

A TM1638 LED&KEY board with the TM1638plus library: each button lights its LED, and the display counts the presses. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate Runs in your browser. Free, and no account needed.

The parts retail for about $30. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino TM1638: LED&KEY Display, LEDs and Buttons

LED and key · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// LED and key: a TM1638 "LED&KEY" board with the TM1638plus library. Each
// button lights the LED above it while held, and the display counts the
// presses and names the last button.
//
//   TM1638  STB -> 4, CLK -> 6, DIO -> 7, VCC -> 5 V, GND -> GND
//
// The TM1638 holds the picture itself and scans the buttons itself, so the
// sketch only sends bytes: STB low, then each byte least significant bit
// first, clocked on CLK's rising edge. readButtons() sends 0x42 and the chip
// answers with four bytes on DIO, one bit per button once they are folded
// together.
//
// Things to try. Press two buttons at once. Then change the brightness in
// setup() from 2 to 7, and to 0.

#include <TM1638plus.h>

const int STROBE_PIN = 4;
const int CLOCK_PIN = 6;
const int DATA_PIN = 7;

TM1638plus tm(STROBE_PIN, CLOCK_PIN, DATA_PIN);

uint8_t lastButtons = 0;
unsigned long presses = 0;

void show(int button) {
  // "P" and the count on the left, a gap, the button's number on the right.
  char text[9] = "P       ";
  unsigned long n = presses % 100000UL;
  int at = 5;
  do {
    text[at--] = (char)('0' + n % 10);
    n /= 10;
  } while (n > 0 && at > 0);
  text[7] = (char)('0' + button);
  tm.displayText(text);
}

void setup() {
  Serial.begin(9600);
  tm.displayBegin();
  tm.brightness(2);
  tm.displayText("LED KEY ");
  Serial.println("TM1638 ready");
}

void loop() {
  uint8_t buttons = tm.readButtons();
  if (buttons != lastButtons) {
    tm.setLEDs((uint16_t)buttons << 8);
    uint8_t pressed = buttons & ~lastButtons;
    for (int i = 0; i < 8; i++) {
      if (pressed & (1 << i)) {
        presses++;
        show(i + 1);
        Serial.print("S");
        Serial.print(i + 1);
        Serial.print(" pressed, ");
        Serial.print(presses);
        Serial.println(" in all");
      }
    }
    lastButtons = buttons;
  }
  delay(20);
}

Parts list

3 parts, plus the jumper wires. They retail for about $30; on Mokxi they're free.

How it is wired

5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; LED and key board, TM1638 pin GND
  • Arduino Uno R3 pin 7; LED and key board, TM1638 pin DIO
  • Arduino Uno R3 pin 6; LED and key board, TM1638 pin CLK
  • Arduino Uno R3 pin 4; LED and key board, TM1638 pin STB
  • 5 V: LED and key board, TM1638 pin VCC

Change it and keep it

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