Home

Awesome

Optimized TM1637 library for Arduino

Build Status

This is a 2-pin serial TM1637 chip library for Arduino, optimized for size and speed. It supports a combined LED driver controller and key-scan interface to detect one key press.

TM1637 chip

Chip features

Hardware

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

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

Pins

PinTM1637Arduino 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

Two-wire serial interface

The TM1637 communicates with a MCU serial by using two wires:

Note: The serial interface is not compatible with I2C or TWI, because no device address with read/write bit is used.

Example

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

ErriezTM1637

Documentation

Usage

Initialization

// Include TM1637 library
#include <ErriezTM1637.h>
  
// Connect display pins to the Arduino DIGITAL pins
#define TM1637_CLK_PIN   2
#define TM1637_DIO_PIN   3

// Create tm1637 object
TM1637 tm1637(TM1637_CLK_PIN, TM1637_DIO_PIN);

void setup()
{
    // Initialize TM1637
    tm1637.begin();
}

Display on/off

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

Turn all LED's off

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

Get keys

// Get 8-bit key-scan
uint8_t keys = tm1637.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:
tm1637.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 TM1637
tm1637.writeData(0x00, buf, sizeof(buf));

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 8MHz84kHz352us344us1080us1072us
UNO 16MHz170kHz156us152us496us480us
WeMos D1 & R2 80MHz205kHz261us137us396us396us
WeMos D1 & R2 160MHz300kHz233us96us275us271us

Arduino Pro-Mini 8MHz

TM1637 Arduino Pro-Mini 8MHz timing

Arduino UNO 16MHz

TM1637 Arduino UNO 16MHz timing

WeMos D1 & R2 80MHz

TM1637 WeMos D1 & R2 40MHz timing

WeMos D1 & R2 160MHz

TM1637 WeMos D1 & R2 160MHz timing

Library dependencies

Library installation

Please refer to the Wiki page.

Other Arduino Libraries and Sketches from Erriez