Home

Awesome

Optimized TM1638 library for Arduino

Build Status

This is a 3-pin serial TM1638 chip library for Arduino, optimized for size and speed. It supports a combined LED driver controller and key-scan interface to detect multiple key presses at the same time.

TM1638 chip

Displaying numbers, characters and reading keys depends on the hardware wiring and is not part of this library. A fully operational example for a board with 8 7-segment displays, 8 dual color LED's and 8 buttons which uses this library is available here: JY-LKM1638.

Hardware

Connect power and 3 data pins to an Arduino board DIGITAL pins:

The following TM1638 pins should be connected to LED's and buttons in a matrix:

Pins

PinTM1638Arduino UNO / Nano / Micro / Pro Micro / Leonardo / Mega2560WeMos D1 & R2 / Node MCUWeMos LOLIN32
1VCC5V (or 3.3V)3V33V3
2GNDGNDGNDGND
3CLK2 (DIGITAL pin)D20
4DIO3 (DIGITAL pin)D34
5STB04 (DIGITAL pin)D45

Example

Arduino IDE | Examples | Erriez TM1638 button and LED driver:

Documentation

Usage

Initialization

// Include TM1638 library
#include <ErriezTM1638.h>

// Connect display pins to the Arduino DIGITAL pins
#define TM1638_CLK_PIN   2
#define TM1638_DIO_PIN   3
#define TM1638_STB_PIN   4

// Create tm1638 object
TM1638 tm1638(TM1638_CLK_PIN, TM1638_DIO_PIN, TM1638_STB_PIN);

void setup()
{
    // Initialize TM1638
    tm1638.begin();
}

Display on/off

// Turn display off
tm1638.displayOff();
  
// Turn display on
tm1638.displayOn();

Turn all LED's off

// Turn all LED's off
tm1638.clear();

Get keys

// Get 32-bit key-scan
uint32_t keys = tm1638.getKeys();

Write Byte to display register

// Write segment LED's to the first display registers 0x00..0x0F with value 0x00..0xff to
// display numbers and characters. Just an example which depends on the hardware:
tm1638.writeData(0x01, 0x01);

Write buffer to display registers

// Creat buffer with LED's
uint8_t buf[] = { 0b10000110, 0b00111111, 0b00111111, 0b00111111, 0b00111111, 0b00111111};

// Write buffer to TM1638
tm1638.writeData(0x00, buf, sizeof(buf));

Small footprint

Measured with Arduino IDE v1.8.5 without any other peripherals, calling all library functions once:

BoardMCU flash sizeMCU RAM sizeTM1638 library flashTM1638 library RAM
Arduino UNO32kB2048kB1840 Bytes40 Bytes

Optimized timing

The library uses optimized pin control for AVR targets. Other targets uses the default digitalRead() and digitalWrite() pin control functions.

Output Benchmark example:

BoardCLKRead keysWrite ByteWrite 16 Bytes bufferClear display
Pro Mini 8MHz65kHz736us312us2448us2224us
UNO 16MHz125kHz340us152us1192us1176us
WeMos D1 & R2 80MHz200kHz284us116us683us682us
WeMos D1 & R2 160MHz300kHz223us66us474us469us

Arduino UNO 16MHz

TM1638 Arduino UNO 16MHz timing

WeMos D1 & R2 80MHz

TM1638 WeMos D1 & R2 40MHz timing

WeMos D1 & R2 160MHz

TM1638 WeMos D1 & R2 160MHz timing

Library dependencies

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and Sketches from Erriez