In this lesson, we will use a 4-digit LED display to create a clock that displays the time of day. These clocks will use the tm1637 library to communicate
with the four-digit display. Some of these displays also have a "colon"
between the hour and minute digits that flashes every second.
You can purchase 4-digit LED displays on eBay for about $2 each.
Connections
These displays have four pins:
Ground
Power (3.2 v or 5 v)
Data
Clock
In our examples, we will connect the power to our 3.3 regulated output
of the Pico. We will connect Data to GP0 and Clock to GP1.
The following example
1 2 3 4 5 6 7 8 9101112
frommachineimportPinfromtimeimportsleepimporttm1637# data and clock pinsDIO_PIN=0CLK_PIN=1tm=tm1637.TM1637(clk=Pin(CLK_PIN),dio=Pin(DIO_PIN))# display "1234"tm.write([1,2,3,4])
The tm.write() function takes a sequence of numbers and will shifts them in from right to left.
Clock
We can create a simple clock by using the localtime() function when the
programs first starts up and then we just update the time after the sleep() functions run for a second. This also can updates the colon between the hours
and minutes.
localtime() returns an array of numbers for date, hour, minute and second. In our example here, we only need the hour and minutes.
# a simple clock that only grabs the time from the server on startupimporttm1637frommachineimportPinfromutimeimportsleep,localtimetm=tm1637.TM1637(clk=Pin(1),dio=Pin(0))now=localtime()hour=now[3]# use AM/PM 12 hour timeifhour>12:hour=hour-12minute=now[4]sec=now[5]print(hour,':',minute,' ',sec,sep='')# update from the first timewhileTrue:# turn the colon ontm.numbers(hour,minute,colon=True)sleep(0.5)# turn the colon offtm.numbers(hour,minute,colon=False)sleep(0.5)sec=sec+1ifsec==60:minute=minute+1sec=0ifminute==60:hour=hour+1minute=0ifhour==24:hour=0
A more accurate version will access the new time from the server every minute.
# a more accurate clock that only grabs the time from the server once per minuteimporttm1637frommachineimportPinfromutimeimportsleep,localtimehour=0minute=0sec=0defupdate_time():globalhour,minute,secondnow=localtime()hour=now[3]# use AM/PMifhour>12:hour=hour-12minute=now[4]sec=now[5]tm=tm1637.TM1637(clk=Pin(1),dio=Pin(0))update_time()# loop every secondwhileTrue:tm.numbers(hour,minute,colon=True)sleep(0.5)tm.numbers(hour,minute,colon=False)sleep(0.5)sec=sec+1ifsec==60:# get the new time from the hostupdate_time()print(hour,':',minute,' ',sec,sep='')minute=minute+1sec=0ifminute==60:hour=hour+1minute=0ifhour==24:hour=0