Operating the TM1637 four-digit digital tube on Raspberry Pi – Only Learning for a Long Time https://blog.whwtf.com/index.php/2022011324.html General Technology Raspberry Pi Display Time https://testerhome.com/topics/12130
Raspberry Pi 3B+ TM1637 4-digit digital tube Raspberry Pi GPIO T-type expansion version connection wire Dupont wire (new) Raspberry Pi 4B system image and WIFI remote configuration information link: https://pan.baidu.com/s/1Jan7v6DuHxY6VmYcLNfTHQ Extraction code: tmsr
# Check kernel version $ uname -a Linux raspberrypi 6.1.21-v7+ #1642 SMP Mon Apr 3 17:20:52 BST 2023 armv7l GNU/Linux $ hostnamectl Static hostname: raspberrypi Icon name: computer Machine ID: 9f23c440839a43c6940e8a22518fc258 Boot ID: 791efb43849248e8b73d79ebd3ccf4ae Operating System: Raspbian GNU/Linux 11 (bullseye) Kernel: Linux 6.1.21-v7+ Architecture: arm
TM1637 Digital Tube | Function | RPI Pin | Raspberry Pi |
GND | Ground | 14 | GND |
VCC | +5V Power | 4 |
3.3-5V |
DIO | Data In | 18 | GPIO 24 |
CLK | Clock | 16 | GPIO 23 |
The CLK clock pin helps to keep the clock pulse synchronized with the module and the microcontroller. The DIO data pin helps to send and receive data from the microcontroller. GND ground (GND) is used to establish a common ground with external devices. VCC power (VCC) input pin, used to power the module. Accepts 3.3-5V VCC. DIO (serial data) and CLK (clock control)
Program Driver Code
# main.py import tm1637 import RPi.GPIO as GPIO import time CLK = 23 DIO = 24 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) lastTime = None HHMMFORMAT = '%H:%M' digital1637 = tm1637.TM1637(CLK,DIO,tm1637.BRIGHT_TYPICAL) digital1637.showDoublePoint(1) while(True): timenow = time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) curTime = time.strftime(HHMMFORMAT, time.localtime(time.time())) if curTime != lastTime: timer = time.localtime() number = [timer.tm_hour//10, timer.tm_hour%10, timer.tm_min//10, timer.tm_min%10] digital1637.showData(number) lastTime = curTime print("lastTime >>>", lastTime) time.sleep(0.1) GPIO.cleanup()
# tm1637py import time import RPi.GPIO as GPIO # 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 #HexDigits = [0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71]; HexDigits = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f] ADDR_AUTO = 0x40 ADDR_FIXED = 0x44 START_ADDR = 0xC0 BRIGHT_DARKEST = 0 BRIGHT_TYPICAL = 2 BRIGHT_HIGHEST = 7 OUTPUT = GPIO.OUT INPUT = GPIO.IN LOW = GPIO.LOW HIGH = GPIO.HIGH class TM1637: doublePoint = False clkPin = 0 dataPin = 0 brightnes = BRIGHT_TYPICAL curData = [0, 0, 0, 0] def __init__(self, clkPin, dataPin, brightnes=BRIGHT_TYPICAL): self.clkPin = clkPin self.dataPin = dataPin self.brightnes = brightnes GPIO.setup(self.clkPin, OUTPUT) GPIO.setup(self.dataPin, OUTPUT) # end __init__ def clear(self): point = self.doublePoint self.doublePoint = False data = [0, 0, 0, 0] self.showData(data) self.doublePoint = point # end clear def showData(self, data): #print 'show data'; for i in range(0, 4): self.curData[i] = data[i] #print 'data[', i, ']', data[i]; #end for self.start() self.writeByte(ADDR_AUTO) self.stop() self.start() self.writeByte(START_ADDR) for i in range(0, 4): self.writeByte(self.encode(data[i])) self.stop() self.start() self.writeByte(0x88 + self.brightnes) self.stop() # end showData def showDoublePoint(self, on): if(self.doublePoint != on): self.doublePoint = on self.showData(self.curData) # end if #end showDoublePoint def writeByte(self, data): for i in range(0, 8): GPIO.output(self.clkPin, LOW) if(data & 0x01): GPIO.output(self.dataPin, HIGH) else: GPIO.output(self.dataPin, LOW) data = data >> 1 GPIO.output(self.clkPin, HIGH) # end for # wait for ACK GPIO.output(self.clkPin, LOW) # set data gpio high, until slave ack, set low GPIO.output(self.dataPin, HIGH) GPIO.output(self.clkPin, HIGH) GPIO.setup(self.dataPin, INPUT) # set data gpio to input for slave #print 'self.dataPint = ', self.dataPin; while(GPIO.input(self.dataPin)): time.sleep(0.001) if(GPIO.input(self.dataPin)): GPIO.setup(self.dataPin, OUTPUT) GPIO.output(self.dataPin, LOW) GPIO.setup(self.dataPin, INPUT) # end if # end while #print 'ack is get' GPIO.setup(self.dataPin, OUTPUT) # end writeByte def start(self): GPIO.output(self.clkPin, HIGH) GPIO.output(self.dataPin, HIGH) GPIO.output(self.dataPin, LOW) GPIO.output(self.clkPin, LOW) # end start def stop(self): GPIO.output(self.clkPin, LOW) GPIO.output(self.dataPin, LOW) GPIO.output(self.clkPin, HIGH) GPIO.output(self.dataPin, HIGH) # end stop def encode(self, data): if(self.doublePoint): pointData = 0x80 else: pointData = 0 if(data == 0x7F): data = 0 else: data = HexDigits[data] + pointData return data # end encode # end class TM1637