Arduino library for modules based on the TM1638 controller.
The library provides simple functions for
- 8 seven-segment displays
- 8 LEDs
- 8 push buttons
- Display brightness control
The interface uses only three GPIO pins.
https://fr.aliexpress.com/wholesale?SearchText=TM1638
- Read individual buttons or all buttons at once
- Control individual LEDs or all LEDs
- Display hexadecimal digits (0-F)
- Display raw segment bitmaps
- Adjustable brightness
- Display on/off control
- Reset and hardware test functions
| TM1638 | Arduino |
|---|---|
| STB | Any digital output |
| CLK | Any digital output |
| DIO | Any digital I/O |
Example:
TM1638 display(2, 3, 4);
// CLK DIO STB#include <TM1638.h>
TM1638 display(2,3,4);Constructor
TM1638(clkPin, dioPin, stbPin);| Parameter | Description |
|---|---|
| clkPin | Clock pin |
| dioPin | Data pin |
| stbPin | Strobe (Chip Select) pin |
Clears everything.
- All digits off
- All LEDs off
display.reset();Displays one hexadecimal digit.
display.displayVal(position, value);Parameters
| Parameter | Range |
|---|---|
| position | 0-7 |
| value | 0-15 |
Example
display.displayVal(0,10); // A
display.displayVal(1,15); // F
display.displayVal(2,5); // 5Displays raw segment data.
display.displayDig(position, bitmap);The bitmap uses
bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
DP G F E D C B A
Example
display.displayDig(0,0b01111111);Lights every segment except the decimal point.
Turns off all seven-segment displays.
display.displayClear();LEDs are not affected.
Turns the display back on.
display.displayTurnOn();Turns off all display outputs without losing display memory.
display.displayTurnOff();Sets display brightness.
display.displaySetBrightness(PULSE10_16);Available brightness levels
| Constant |
|---|
| PULSE1_16 |
| PULSE2_16 |
| PULSE4_16 |
| PULSE10_16 |
| PULSE11_16 |
| PULSE12_16 |
| PULSE13_16 |
| PULSE14_16 |
Higher values produce a brighter display.
Controls one LED.
display.writeLed(1, ON);
display.writeLed(5, OFF);Parameters
| Parameter | Range |
|---|---|
| LED | 1-8 |
| State | ON/OFF or true/false |
Controls all LEDs simultaneously.
display.writeLeds(0b10101010);Bit 0 controls LED1.
Bit 7 controls LED8.
Reads one button.
if(display.getButton(S3))
{
...
}Available button constants
S1
S2
S3
S4
S5
S6
S7
S8
Returns
true
false
Reads all eight buttons.
uint8_t keys = display.getButtons();Bit mapping
Bit0 -> S1
Bit1 -> S2
Bit2 -> S3
Bit3 -> S4
Bit4 -> S5
Bit5 -> S6
Bit6 -> S7
Bit7 -> S8
Hardware test.
display.test();The function repeatedly flashes every LED and every segment.
Useful for verifying wiring.
S1
S2
S3
S4
S5
S6
S7
S8ON
OFFPULSE1_16
PULSE2_16
PULSE4_16
PULSE10_16
PULSE11_16
PULSE12_16
PULSE13_16
PULSE14_16#include <TM1638.h>
TM1638 display(2,3,4);
void setup()
{
display.reset();
display.displaySetBrightness(PULSE14_16);
display.displayVal(0,1);
display.displayVal(1,2);
display.displayVal(2,3);
display.displayVal(3,4);
display.writeLed(1,ON);
}
void loop()
{
if(display.getButton(S1))
{
display.writeLed(8,ON);
}
else
{
display.writeLed(8,OFF);
}
}MIT License.
