PyRaspBeta

tm1637

The TM1637 is the controller on cheap 4-digit seven-segment modules: a clock-style display with four digits and a colon. It is not I²C — there is no address and no standard register model. It is bit-banged over two wires, which is why any two GPIOs will do. The shop carries it as the 4-digit display.

PyRasp provides the API of the popular raspberrypi-tm1637 library:

circuit.py
from wiring import *

pi = Pi()
display = TM1637()
pi.V3_3 >> display.vcc
display.gnd >> pi.GND
pi.GPIO23 >> display.clk
pi.GPIO24 >> display.dio
main.py
import tm1637
from time import sleep

display = tm1637.TM1637(clk=23, dio=24)

display.show("12:34")      # text: digits, a few letters, '-' and ' '
sleep(1)
display.numbers(9, 30)     # clock style, colon on by default
sleep(1)
display.number(42)         # right-aligned integer
sleep(1)
display.clear()

The constructor also takes brightness=7 (0 is dimmest, 7 brightest). If your program has not set a GPIO numbering mode, the driver calls GPIO.setmode(GPIO.BCM) itself, so pin numbers are BCM by default; set a mode first if you want to pass physical pin numbers instead.

What the display can show

show(text) right-aligns the text and keeps the last four characters; if the text contains a colon it is preserved between the second and third digit:

display.show("12:34")         # colon after the second digit
display.show("HELP")
display.show("88:88")
display.show("1234", colon=True)

Supported characters: 0–9, the letters A B C D E F H L P U O N, -, _ and space. Anything else renders blank. The colon lights as the high bit of the second digit.

Raw segment bytes go through write():

display.write([0x3F, 0x06, 0x5B, 0x4F])   # "0123"
display.write([0x00, 0x00, 0x00, 0x00])   # blank
MethodBehaviour
TM1637(clk, dio, brightness=7)creates the display; wires up both GPIOs
show(text, colon=False)text, right-aligned (or colon-aware as above)
number(value)integer, clamped to −999 … 9999
numbers(left, right, colon=True)two 2-digit numbers, each taken mod 100
write(segments, pos=0)raw segment bytes from a digit position
encode_string(text)returns the four segment bytes without writing
brightness(value=None)sets (0–7) or returns the brightness
clear()blanks all four digits

Not implemented in the simulator, though the real library has them: hex(), temperature(), time(), scroll() and the single-character helpers encode_char()/encode_digit(). Calling one raises AttributeError.

Going deeper

Colon-only updates: display.show("12:34"), then display.show("1234") (no colon) turns it off, because the colon bit belongs to the second digit's segment byte.

The part decodes the protocol at the wire level: start condition (DIO falls while CLK is high), eight data bits sampled on each rising CLK edge, LSB first, an ACK clock, and a stop condition. The driver pulls DIO low, or releases it to the module's 10 kΩ pull-up — it never drives it high, so the chip can pull it low to acknowledge without a fight. That means a driver other than this That means a driver other than PyRasp's also works, as long as it speaks TM1637.

Simulator vs real hardware

  • Any two GPIOs work. The real module is bit-banged on arbitrary pins too, so there is no surprise here; the simulator only insists that CLK and DIO reach the part.
  • No timing limits. The simulator's _delay() is a no-op — real drivers sleep microseconds between edges, and the chip has a 250 kHz maximum clock; the simulation does not care how fast you send.
  • Brightness is stored, not PWM. The module sets its own LED duty; here the value 0–7 is kept with the display control byte, and the board view dims the lit segments to match (the control byte's on/off bit blanks the whole display). A real module's brightness steps may not be exactly linear, but the shape is the same.
  • Repeated identical writes are skipped. The simulator remembers the last frame and does nothing if the segments, position and brightness are unchanged, so the bus sniffer will not show a write you repeat. On hardware the bytes would be sent again (harmlessly).
  • Letters outside the table are blank. The real raspberrypi-tm1637 has a larger font; here anything outside the list above shows as an empty digit.