Home

Awesome

MicroPython TM1638 LED Driver

A MicroPython library for LED&KEY modules with 8x 7-segment decimal LED modules, 8x individual LEDs and 8x switches using the TM1738 LED driver.

demo

Examples

Copy the file to your device, using ampy, rshell, webrepl or compiling and deploying. eg.

$ ampy put tm1638.py

Basic usage

# TinyPICO / ESP32
import tm1638
from machine import Pin
tm = tm1638.TM1638(stb=Pin(5), clk=Pin(18), dio=Pin(23))

# Wemos D1 Mini / ESP8266
import tm1638
from machine import Pin
tm = tm1638.TM1638(stb=Pin(13), clk=Pin(14), dio=Pin(12))

# STM32F407VET6
import tm1638
from machine import Pin
tm = tm1638.TM1638(stb=Pin('B4'), clk=Pin('B5'), dio=Pin('B6'))

# every 2nd LED on
tm.leds(0b01010101)

# all LEDs off
tm.leds(0)

# segments
tm.show('cool')
tm.show('abcdefgh')
tm.number(-1234567)
tm.number(1234)
tm.number(5678, 4)
tm.hex(0xdeadbeef)

# dim both LEDs and segments
tm.brightness(0)

# all LEDs and segments off
tm.clear()

# get which buttons are pressed on LED&KEY module
tm.keys()

# get which buttons are pressed on QYF-TM1638 module
tm.qyf_keys()

For more detailed examples, see examples.

Seven Segment Font

They are called 7-segment displays as there are 7 LEDs for each digit (segment). One byte (7 lower bits) for each segment. The 8th bit (MSB) is for the decimal point on each segment.

      A
     ---
  F |   | B
     -G-
  E |   | C
     ---  * H
      D

  HGFEDCBA
0b01101101 = 0x6D = 109 = show "5"
DisplayBinHexDec
00b001111110x3F63
10b000001100x066
20b010110110x5B91
30b010011110x4F79
40b011001100x66102
50b011011010x6D109
60b011111010x7D125
70b000001110x077
80b011111110x7F127
90b011011110x6F111
A0b011101110x77119
b0b011111000x7C124
C0b001110010x3957
d0b010111100x5E94
E0b011110010x79121
F0b011100010x71113
G0b001111010x3D61
H0b011101100x76118
I0b000001100x066
J0b000111100x1E30
K0b011101100x76118
L0b001110000x3856
M0b010101010x5585
n0b010101000x5484
O0b001111110x3F63
P0b011100110x73115
q0b011001110x67103
r0b010100000x5080
S0b011011010x6D109
t0b011110000x78120
U0b001111100x3E62
v0b000111000x1C28
W0b001010100x2A42
X0b011101100x76118
y0b011011100x6E110
Z0b010110110x5B91
blank0b000000000x000
-0b010000000x4064
*0b011000110x6399

Methods

Power up, power down or check status.

power(val=None)

Get or set brightness.

brightness(val=None)

Write zeros to each address.

clear()

Write to all 16 addresses from a given position. Order is left to right, 1st segment, 1st LED, 2nd segment, 2nd LED etc.

write(data, pos=0)

Set the value of a single LED.

led(pos, val)

Set all LEDs at once. LSB is left most LED. Only writes to the LED positions (every 2nd starting from 1).

leds(val)

Set one or more segments at a relative position. Only writes to the segment positions (every 2nd starting from 0).

segments(segments, pos=0)

Return a byte representing which keys are pressed. LSB is SW1.

keys()

Return a 16-bit value representing which keys are pressed. LSB is SW1.

qyf_keys()

Convert a single hex digit (0x00-0x0f) to a segment.

encode_digit(digit)

Convert a string to a list of segments. Dots are merged with the previous character.

encode_string(string)

Convert a single character to a segment.

encode_char(char)

Display a number in hexadecimal format 00000000 through FFFFFFFF, right aligned, leading zeros.

hex(val)

Display a numeric value -9999999 through 99999999, right aligned.

number(num)

Display a temperature -9 through 99 followed by degrees C.

temperature(num, pos=0)

Displays a relative humidity -9 through 99 followed by RH.

humidity(num, pos=4)

Displays as much of a string as will fit.

show(string, pos=0)

Display a string, scrolling from the right to left, speed adjustable. String starts off-screen right and scrolls until off-screen left at 4 FPS by default.

scroll(string, delay=250)

Parts

Connections

WeMos D1 MiniLED&KEY TM1638 Module
3V3 (or 5V)VCC
GGND
D7 (GPIO13)STB
D5 (GPIO14)CLK
D6 (GPIO12)DIO
STM32F407VET6LED&KEY TM1638 Module
3V3VCC
GGND
B4STB
B5CLK
B6DIO

Links

License

Licensed under the MIT License.