Please complete the basic installation-chapters before starting a project.
This project will use the
ntptime
module to keep track of the time and display it on a 4-digit-display. An alarm can then be set in the code, which will turn on the buzzer at a specified time. To keep track of time accurately the board will also connect to a Wi-Fi® network.You will need the following to build this project:
Assemble the components according to the circuit diagram below:
Read the comments in the code and change the variables as necessary then upload it to your board.
In order to use the 4-digit-display you will need to install the following module:
1mip.install("https://raw.githubusercontent.com/mcauser/micropython-tm1637/master/tm1637.py")
If you are unsure how to install external modules you can read up on it here
1from machine import Pin, PWM, RTC2import time3from time import sleep4import tm16375import ntptime6import network7
8UTC_OFFSET = 0 * 60 * 60 # change the '0' according to your timezone9actual_time = time.localtime(time.time() + UTC_OFFSET)10
11# Frequency and duration of the sound12FREQUENCY = 220 # Hz13DURATION = 2 # seconds14
15# Create a controllable Pin for the speaker16speaker = PWM(Pin(5))17
18tm = tm1637.TM1637(clk=Pin(9), dio=Pin(8))19
20connection = network.WLAN(network.STA_IF)21
22def connect(ssid, password):23 ssid = ssid24 password = password25 if connection.isconnected() == True:26 print("Already connected")27 return28 connection.active(True)29 connection.connect(ssid, password)30 while connection.isconnected() == False:31 pass32 print("Connection successful")33 print(connection.ifconfig())34
35# Function to play a sound36def play_sound(frequency, duration):37 speaker.freq(frequency)38 speaker.duty(512)39 time.sleep(duration)40 speaker.duty(0)41
42
43connect("SSID", "password") #Replace the SSID with the name of your network and password with the networks password44rtc = RTC()45
46alarm = [08, 30, 1] #The time for the set alarm [hour, minutes, enabled status(1=true)]47isPoint = True48
49while(1):50 printTimeH = int("{3:02d}".format(*actual_time))51 printTimeM = int("{4:02d}".format(*actual_time))52 if alarm[2] == 1 and alarm[0] == printTimeH and alarm[1] == printTimeM:53 play_sound(FREQUENCY, DURATION) # Play the sound54 55 tm.numbers(printTimeH, printTimeM, isPoint)