Skip to content

Commit f99da00

Browse files
committed
Add ESP8266 support, optimize for speed and add Doxygen.
1 parent 018289f commit f99da00

5 files changed

Lines changed: 327 additions & 208 deletions

File tree

README.md

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,42 @@
1-
# TM1638 library for Arduino
1+
# Optimized TM1638 library for Arduino
22

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

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

77
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).
88

9-
10-
119
## Hardware
1210

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

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

2422

23+
### Pins
2524

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

27-
## Supported Arduino boards
28-
29-
* All ATMega328P MCU:
30-
* Arduino UNO
31-
* Arduino Nano
32-
* All ATMega32U4 MCU's:
33-
* Arduino Leonardo
34-
* Pro Micro
35-
* All ATMega2560 MCU's:
36-
* Arduino Mega2560
37-
38-
39-
* Other Arduino AVR MCU's may work, but are not tested.
40-
* Other targets such as ESP8266/Lolin32 are not tested.
41-
* The chip requires a 5V power supply and does not work at 3.3V.
4233
* Check maximum regulator / diode current to prevent a burnout when using lots of LED's. Some boards can provide only 100mA, others 800mA max.
43-
* The DIO data pin requires a bi-directional level converter when connecting to 3.3V digital pins.
44-
45-
46-
47-
## Library dependencies
48-
49-
- None
50-
51-
52-
53-
## Documentation
54-
55-
[TM1638 Datasheet](https://github.com/Erriez/ErriezTM1638/blob/master/extras/TM1638_datasheet.pdf)
56-
5734

5835

5936
## Example
6037

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

63-
64-
6540
## Usage
6641

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

7348
// Connect display pins to the Arduino DIGITAL pins
74-
#define DIO_PIN 2
75-
#define SCL_PIN 3
76-
#define STB_PIN 4
77-
78-
// Create TM1638 object
79-
TM1638 tm1638(DIO_PIN, SCL_PIN, STB_PIN);
49+
#define TM1638_SCL_PIN 2
50+
#define TM1638_DIO_PIN 3
51+
#define TM1638_STB0_PIN 4
52+
53+
// Create tm1638 object
54+
TM1638 tm1638(TM1638_SCL_PIN, TM1638_DIO_PIN, TM1638_STB0_PIN);
55+
56+
void setup()
57+
{
58+
// Initialize TM1638
59+
tm1638.begin();
60+
}
8061
```
8162
82-
83-
8463
**Display on/off**
8564
8665
```c++
@@ -91,34 +70,43 @@ tm1638.displayOff();
9170
tm1638.displayOn();
9271
```
9372

94-
95-
9673
**Turn all LED's off**
9774

9875
```c++
9976
// Turn all LED's off
10077
tm1638.clear();
10178
```
10279

103-
104-
105-
**Get key-scan**
80+
**Get keys**
10681

10782
```c++
10883
// Get 32-bit key-scan
109-
uint32_t keys = tm1638.getKeyScan();
84+
uint32_t keys = tm1638.getKeys();
11085
```
11186

87+
**Write Byte to display register**
11288

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

114-
**Write display register**
95+
**Write buffer to display registers**
11596

11697
```c++
117-
// Write segment LED's to the first display register
118-
// The LED's turned on depends on your hardware SEG/GRID connections
119-
// Experiment with the registers 0x00..0x0F value 0x00..0xff to display numbers
120-
// and characters, for example:
121-
tm1638.writeDisplayRegister(0x01, 0x01);
98+
// Creat buffer with LED's
99+
uint8_t buf[] = { 0b10000110, 0b00111111, 0b00111111, 0b00111111, 0b00111111, 0b00111111};
100+
101+
// Write buffer to TM1638
102+
tm1638.writeData(0x00, buf, sizeof(buf));
122103
```
123104
105+
## Library dependencies
106+
107+
- None
108+
109+
## Documentation
110+
111+
[TM1638 Datasheet](https://github.com/Erriez/ErriezTM1638/blob/master/extras/TM1638_datasheet.pdf)
124112

examples/Example/Example.ino

Lines changed: 58 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,61 +30,76 @@
3030
#include <TM1638.h>
3131

3232
// Connect display pins to the Arduino DIGITAL pins
33-
#define DIO_PIN 2
34-
#define SCL_PIN 3
35-
#define STB_PIN 4
33+
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MICRO) || \
34+
defined(ARDUINO_AVR_PRO) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_AVR_LEONARDO)
35+
#define TM1638_SCL_PIN 2
36+
#define TM1638_DIO_PIN 3
37+
#define TM1638_STB0_PIN 4
38+
#elif defined(ARDUINO_ESP8266_WEMOS_D1MINI) || defined(ARDUINO_ESP8266_NODEMCU)
39+
#define TM1638_SCL_PIN D2
40+
#define TM1638_DIO_PIN D3
41+
#define TM1638_STB0_PIN D4
42+
#elif defined(ARDUINO_LOLIN32)
43+
#define TM1638_SCL_PIN 0
44+
#define TM1638_DIO_PIN 4
45+
#define TM1638_STB0_PIN 5
46+
#else
47+
#error "May work, but not tested on this target"
48+
#endif
49+
50+
// Create tm1638 object
51+
TM1638 tm1638(TM1638_SCL_PIN, TM1638_DIO_PIN, TM1638_STB0_PIN);
3652

37-
TM1638 tm1638(DIO_PIN, SCL_PIN, STB_PIN);
38-
39-
uint32_t keysLast = 0;
4053

4154
void setup()
4255
{
43-
Serial.begin(115200);
44-
while (!Serial) {
45-
;
46-
}
47-
Serial.println(F("TM1638 example"));
56+
Serial.begin(115200);
57+
while (!Serial) {
58+
;
59+
}
60+
Serial.println(F("TM1638 example"));
4861

49-
// Turn display off (All LED's off)
50-
tm1638.displayOff();
62+
// Initialize TM1638
63+
tm1638.begin();
5164

52-
// Set brightness 0..7
53-
tm1638.setBrightness(3);
65+
// Turn display off (All LED's off)
66+
tm1638.displayOff();
5467

55-
// Turn display on
56-
tm1638.displayOn();
68+
// Clear display
69+
tm1638.clear();
5770

58-
// Clear display
59-
tm1638.clear();
71+
// Set brightness 0..7
72+
tm1638.setBrightness(3);
6073

61-
// Write segment LED's to the first display register
62-
// The LED's turned on depends on the connection of your board
63-
// Experiment with the registers 0x00..0x0F value 0x00..0xff to display
64-
// numbers and characters
65-
tm1638.writeDisplayRegister(0x01, 0x01);
74+
// Turn display on
75+
tm1638.displayOn();
76+
77+
// Write segment LED's to the first display registers 0x00..0x0F with value 0x00..0xff to
78+
// display numbers and characters. Just an example which depends on the hardware:
79+
tm1638.writeData(0x01, 0x01);
6680
}
6781

6882
void loop()
6983
{
70-
uint32_t keys;
71-
72-
// Read 32-bit keys
73-
keys = tm1638.getKeyScan();
74-
75-
// Check key down
76-
if (keysLast != keys) {
77-
keysLast = keys;
78-
79-
Serial.print(F("Keys: 0x"));
80-
Serial.println(keys, HEX);
81-
82-
if (keys) {
83-
// Write segment LED's to first display register
84-
tm1638.writeDisplayRegister(0, 0b00111111);
85-
} else {
86-
// Write segment LED's to first display register
87-
tm1638.writeDisplayRegister(0, 0b00000110);
84+
static uint32_t keysLast = 0;
85+
uint32_t keys;
86+
87+
// Read 32-bit keys
88+
keys = tm1638.getKeys();
89+
90+
// Check key down
91+
if (keysLast != keys) {
92+
keysLast = keys;
93+
94+
Serial.print(F("Keys: 0x"));
95+
Serial.println(keys, HEX);
96+
97+
if (keys) {
98+
// Write segment LED's to first display register
99+
tm1638.writeData(0, 0b00111111);
100+
} else {
101+
// Write segment LED's to first display register
102+
tm1638.writeData(0, 0b00000110);
103+
}
88104
}
89-
}
90105
}

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Erriez TM1638
2-
version=1.0.0
2+
version=1.1.0
33
author=Erriez <Erriez@users.noreply.github.com>
44
maintainer=Erriez <Erriez@users.noreply.github.com>
55
sentence=TM1638 button and LED controller library for Arduino
6-
paragraph=This is a TM1638 LED driver controller with key-scan interface library for Arduino.
6+
paragraph=This is an optimized TM1638 LED driver controller with key-scan interface library for Arduino.
77
category=Display
88
url=https://github.com/Erriez/ArduinoLibraryTM1638
99
architecture=*

0 commit comments

Comments
 (0)