以前曾實做過一篇有關時間顯示的文章:Arduino筆記(17):即時時鐘 RTC 與 TM1637 四位數顯示器,因為Arduino 需借助 RTC才有即時時鐘的功能,透過 TM1637顯示出來。今天要試著用樹莓派透過 Python 顯示時間在 TM1637四位數顯示器上。
Raspberry Pi 與4位數7段顯示器的接腳對應關係:
程式會用到tm1637相關的應用函式,執行以下三個程式前,需先到下列網址下載 tm1637 程式:
• Display. Clear () :清除所有 LED的顯示。
• Display.SetBrightnes(x) :設定LED顯示的亮度,介於0與7之間的值。
• Display. Show(x,x,x,x) :顯示四位數字,每個數字可以是0 到 9。
• Display.ShowDoublepoint (status) :控制四位數第二與第三數字間的冒號 ':' 是否為亮或暗,設定為 true(1) 時會亮,反之設定為 false (0) 為暗。
• stdscr.getkey():等待輸入按鍵。

• Github:raspberrypi-tm1637
除了時間顯示外[程式一],希望也能顯示日期,有兩個方式可實做,一是固定每30秒顯示日期5秒鐘[程式二]。另一個方式是用鍵盤控制顯示日期或時間,按下特定的按鍵,再顯示日期或時間[程式三]。
Raspberry Pi 與4位數7段顯示器的接腳對應關係:
| Pi 接腳 | TM1637四位數顯示器 |
|---|---|
| Pin 4(+5V) | VCC |
| Pin 6(GND) | GND |
| Pin 18 GPIO 24 | DI0 |
| Pin 16 GPIO 23 | CLK |
程式會用到tm1637相關的應用函式,執行以下三個程式前,需先到下列網址下載 tm1637 程式:
$ wget https://raspberrytips.nl/files/tm1637.py
[程式一]
顯示目前時間。import sys import time import datetime import RPi.GPIO as GPIO import tm1637 # GPIO23 (Pin 16) <-> CLK # GPIO24 (Pin 18) <-> DI0 Display = tm1637.TM1637(23,24,tm1637.BRIGHT_TYPICAL) Display.Clear() Display.SetBrightnes(1) while(True): now = datetime.datetime.now() hour = now.hour minute = now.minute second = now.second currenttime = [ int(hour / 10), hour % 10, int(minute / 10), minute % 10 ] Display.Show(currenttime) Display.ShowDoublepoint(second % 2) time.sleep(1)[程式說明]
• Display. Clear () :清除所有 LED的顯示。
• Display.SetBrightnes(x) :設定LED顯示的亮度,介於0與7之間的值。
• Display. Show(x,x,x,x) :顯示四位數字,每個數字可以是0 到 9。
• Display.ShowDoublepoint (status) :控制四位數第二與第三數字間的冒號 ':' 是否為亮或暗,設定為 true(1) 時會亮,反之設定為 false (0) 為暗。
[程式二]
顯示時間[時:分],每分鐘的 25-30 或 55-60 秒鐘顯示當日日期。import sys
import time
import datetime
import RPi.GPIO as GPIO
import tm1637
# GPIO23 (Pin 16) <-> CLK
# GPIO24 (Pin 18) <-> DI0
Display = tm1637.TM1637(23,24,tm1637.BRIGHT_TYPICAL)
Display.Clear()
Display.SetBrightnes(1)
while(True):
now = datetime.datetime.now()
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
currenttime = [ int(hour / 10), hour % 10, int(minute / 10), minute % 10 ]
currentdate = [ int(month / 10), month % 10, int(day / 10), day % 10]
if (second > 25 and second <=30) or (second > 55 and second <= 60):
Display.Clear()
Display.Show(currentdate)
Display.ShowDoublepoint(0)
else:
Display.Show(currenttime)
Display.ShowDoublepoint(second % 2)
time.sleep(1)
[程式三]
增加鍵盤控制功能,按下[d]鍵顯示日期,按下[t]鍵顯示時間,按下[q]鍵結束程式,因需等待鍵盤輸入,時間中間的冒號不會閃爍。import sys
import time
import datetime
import RPi.GPIO as GPIO
import tm1637
import curses
from curses import wrapper
# GPIO23 (Pin 16) <-> CLK
# GPIO24 (Pin 18) <-> DI0
Display = tm1637.TM1637(23,24,tm1637.BRIGHT_TYPICAL)
Display.Clear()
Display.SetBrightnes(1)
stdscr = curses.initscr()
stdscr.clear()
while(True):
ch = stdscr.getkey()
if len(ch) > 0:
now = datetime.datetime.now()
month = now.month
day = now.day
hour = now.hour
minute = now.minute
second = now.second
currenttime = [ int(hour / 10), hour % 10, int(minute / 10), minute % 10 ]
currentdate = [ int(month / 10), month % 10, int(day / 10), day % 10]
Display.Show(currenttime)
Display.ShowDoublepoint(second % 2)
if ch == 't':
Display.Show(currenttime)
Display.ShowDoublepoint(1)
time.sleep(1)
if ch == 'd':
Display.Clear()
Display.Show(currentdate)
Display.ShowDoublepoint(0)
time.sleep(1)
if ch == 'q':
Display.Clear()
stdscr.clear()
curses.endwin()
break
[程式說明]• stdscr.getkey():等待輸入按鍵。
[執行結果]
以下是顯示日期的畫面:[參考資料]
• Youtube:Displaying Time over 4-Digit 7-Segment Display Using Raspberry Pi• Github:raspberrypi-tm1637

作者已經移除這則留言。
回覆刪除作者已經移除這則留言。
回覆刪除請問這個元件在rpi上可以顯示溫度嗎? 因為您之前的文章是用rpi pico來顯示溫度
回覆刪除可以, 但是要先從 rpi 取得溫度, 可以使用指令 vcgencmd measure_temp 取得, 得到的結果再顯示到這個元件即可。
刪除張貼留言