hero

Alarm Clock

Build your own alarm clock using a 4-digit-display and a buzzer to get you out of bed.

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.

Required Hardware

You will need the following to build this project:

Circuit

Assemble the components according to the circuit diagram below:

Circuit for the alarm clock
Circuit for the alarm clock

Code

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, RTC
2import time
3from time import sleep
4import tm1637
5import ntptime
6import network
7
8UTC_OFFSET = 0 * 60 * 60 # change the '0' according to your timezone
9actual_time = time.localtime(time.time() + UTC_OFFSET)
10
11# Frequency and duration of the sound
12FREQUENCY = 220 # Hz
13DURATION = 2 # seconds
14
15# Create a controllable Pin for the speaker
16speaker = 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 = ssid
24 password = password
25 if connection.isconnected() == True:
26 print("Already connected")
27 return
28 connection.active(True)
29 connection.connect(ssid, password)
30 while connection.isconnected() == False:
31 pass
32 print("Connection successful")
33 print(connection.ifconfig())
34
35# Function to play a sound
36def 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 password
44rtc = RTC()
45
46alarm = [08, 30, 1] #The time for the set alarm [hour, minutes, enabled status(1=true)]
47isPoint = True
48
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 sound
54
55 tm.numbers(printTimeH, printTimeM, isPoint)

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.