Skip to content

Commit

Permalink
Add ESP8266 support, optimize for speed and add Doxygen.
Browse files Browse the repository at this point in the history
  • Loading branch information
Erriez committed May 20, 2018
1 parent 018289f commit f99da00
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 208 deletions.
102 changes: 45 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,67 +1,42 @@
# TM1638 library for Arduino
# Optimized TM1638 library for Arduino

This is a 3-pin serial TM1638 chip library for Arduino. It supports a combined LED driver controller and key-scan interface.
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.

![TM1638 chip](https://raw.githubusercontent.com/Erriez/ErriezTM1638/master/extras/TM1638_pins.jpg)

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](https://github.com/Erriez/ErriezLKM1638).



## Hardware

Connect power and 3 data pins to an Arduino board DIGITAL pins:
* VDD (Power 5V +/- 10%)
* VDD (Power 3.3V - 5V)
* GND (Ground)
* DIO (Bi-directional data input/output)
* STB (Chip select)
* CLK (Clock)

The following TM1638 pins should be connected to LED's and buttons in a matrix:
* K1~K3 (Key-scan data input)
* K1~K3 (Key-scan data input to read multiple key presses at the same time)
* SEG/GRID (Output for LED matrix)


### Pins

| Pin | TM1638 | Arduino UNO / Nano / Mega2560 / Leonardo / Pro Micro | Node MCU | LOLIN32 |
| :--: | :----: | :--------------------------------------------------: | :------: | :-----: |
| 1 | VCC | 5V (or 3.3V) | GND | GND |
| 2 | GND | GND | 3V3 | 3V3 |
| 3 | CLK | 2 (Digital pin) | D2 | 0 |
| 4 | DIO | 3 (Digital pin) | D3 | 4 |
| 5 | STB0 | 4 (Digital pin) | D4 | 5 |

## Supported Arduino boards

* All ATMega328P MCU:
* Arduino UNO
* Arduino Nano
* All ATMega32U4 MCU's:
* Arduino Leonardo
* Pro Micro
* All ATMega2560 MCU's:
* Arduino Mega2560


* Other Arduino AVR MCU's may work, but are not tested.
* Other targets such as ESP8266/Lolin32 are not tested.
* The chip requires a 5V power supply and does not work at 3.3V.
* Check maximum regulator / diode current to prevent a burnout when using lots of LED's. Some boards can provide only 100mA, others 800mA max.
* The DIO data pin requires a bi-directional level converter when connecting to 3.3V digital pins.



## Library dependencies

- None



## Documentation

[TM1638 Datasheet](https://github.com/Erriez/ErriezTM1638/blob/master/extras/TM1638_datasheet.pdf)



## Example

Examples | TM1638 | [Example](https://github.com/Erriez/ErriezTM1638/blob/master/examples/Example/Example.ino)



## Usage

**Initialization**
Expand All @@ -71,16 +46,20 @@ Examples | TM1638 | [Example](https://github.com/Erriez/ErriezTM1638/blob/master
#include "TM1638.h"

// Connect display pins to the Arduino DIGITAL pins
#define DIO_PIN 2
#define SCL_PIN 3
#define STB_PIN 4

// Create TM1638 object
TM1638 tm1638(DIO_PIN, SCL_PIN, STB_PIN);
#define TM1638_SCL_PIN 2
#define TM1638_DIO_PIN 3
#define TM1638_STB0_PIN 4

// Create tm1638 object
TM1638 tm1638(TM1638_SCL_PIN, TM1638_DIO_PIN, TM1638_STB0_PIN);

void setup()
{
// Initialize TM1638
tm1638.begin();
}
```
**Display on/off**
```c++
Expand All @@ -91,34 +70,43 @@ tm1638.displayOff();
tm1638.displayOn();
```



**Turn all LED's off**

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



**Get key-scan**
**Get keys**

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

**Write Byte to display register**

```c++
// 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 display register**
**Write buffer to display registers**

```c++
// Write segment LED's to the first display register
// The LED's turned on depends on your hardware SEG/GRID connections
// Experiment with the registers 0x00..0x0F value 0x00..0xff to display numbers
// and characters, for example:
tm1638.writeDisplayRegister(0x01, 0x01);
// Creat buffer with LED's
uint8_t buf[] = { 0b10000110, 0b00111111, 0b00111111, 0b00111111, 0b00111111, 0b00111111};

// Write buffer to TM1638
tm1638.writeData(0x00, buf, sizeof(buf));
```
## Library dependencies
- None
## Documentation
[TM1638 Datasheet](https://github.com/Erriez/ErriezTM1638/blob/master/extras/TM1638_datasheet.pdf)
101 changes: 58 additions & 43 deletions examples/Example/Example.ino
Original file line number Diff line number Diff line change
Expand Up @@ -30,61 +30,76 @@
#include <TM1638.h>

// Connect display pins to the Arduino DIGITAL pins
#define DIO_PIN 2
#define SCL_PIN 3
#define STB_PIN 4
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MICRO) || \
defined(ARDUINO_AVR_PRO) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_LEONARDO)
#define TM1638_SCL_PIN 2
#define TM1638_DIO_PIN 3
#define TM1638_STB0_PIN 4
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) || defined(ARDUINO_ESP8266_NODEMCU)
#define TM1638_SCL_PIN D2
#define TM1638_DIO_PIN D3
#define TM1638_STB0_PIN D4
#elif defined(ARDUINO_LOLIN32)
#define TM1638_SCL_PIN 0
#define TM1638_DIO_PIN 4
#define TM1638_STB0_PIN 5
#else
#error "May work, but not tested on this target"
#endif

// Create tm1638 object
TM1638 tm1638(TM1638_SCL_PIN, TM1638_DIO_PIN, TM1638_STB0_PIN);

TM1638 tm1638(DIO_PIN, SCL_PIN, STB_PIN);

uint32_t keysLast = 0;

void setup()
{
Serial.begin(115200);
while (!Serial) {
;
}
Serial.println(F("TM1638 example"));
Serial.begin(115200);
while (!Serial) {
;
}
Serial.println(F("TM1638 example"));

// Turn display off (All LED's off)
tm1638.displayOff();
// Initialize TM1638
tm1638.begin();

// Set brightness 0..7
tm1638.setBrightness(3);
// Turn display off (All LED's off)
tm1638.displayOff();

// Turn display on
tm1638.displayOn();
// Clear display
tm1638.clear();

// Clear display
tm1638.clear();
// Set brightness 0..7
tm1638.setBrightness(3);

// Write segment LED's to the first display register
// The LED's turned on depends on the connection of your board
// Experiment with the registers 0x00..0x0F value 0x00..0xff to display
// numbers and characters
tm1638.writeDisplayRegister(0x01, 0x01);
// Turn display on
tm1638.displayOn();

// 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);
}

void loop()
{
uint32_t keys;

// Read 32-bit keys
keys = tm1638.getKeyScan();

// Check key down
if (keysLast != keys) {
keysLast = keys;

Serial.print(F("Keys: 0x"));
Serial.println(keys, HEX);

if (keys) {
// Write segment LED's to first display register
tm1638.writeDisplayRegister(0, 0b00111111);
} else {
// Write segment LED's to first display register
tm1638.writeDisplayRegister(0, 0b00000110);
static uint32_t keysLast = 0;
uint32_t keys;

// Read 32-bit keys
keys = tm1638.getKeys();

// Check key down
if (keysLast != keys) {
keysLast = keys;

Serial.print(F("Keys: 0x"));
Serial.println(keys, HEX);

if (keys) {
// Write segment LED's to first display register
tm1638.writeData(0, 0b00111111);
} else {
// Write segment LED's to first display register
tm1638.writeData(0, 0b00000110);
}
}
}
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Erriez TM1638
version=1.0.0
version=1.1.0
author=Erriez <Erriez@users.noreply.github.com>
maintainer=Erriez <Erriez@users.noreply.github.com>
sentence=TM1638 button and LED controller library for Arduino
paragraph=This is a TM1638 LED driver controller with key-scan interface library for Arduino.
paragraph=This is an optimized TM1638 LED driver controller with key-scan interface library for Arduino.
category=Display
url=https://github.com/Erriez/ArduinoLibraryTM1638
architecture=*
Loading

0 comments on commit f99da00

Please sign in to comment.