LCD Alarm Clock With Snooze Button
Componant :-
·
Raspberry Pi
·
Breadboard
·
16 x 2 LCD Screen
·
10kΩ Potentiometer
·
Active Buzzer
·
Pushbutton Switch
·
10 Female to Male Jumper Wires
·
9 Male to Male Jumper Wires
Instructions:
This
project is designed to use the Raspberry Pi to simulate a simple alarm clock
with a buzzer and a snooze button. Our project utilizes the same 16 x 2
LCD screen from our previous projects while incorporating the active piezo
buzzer from Project 10. Additionally, we have inserted a pushbutton switch
into our breadboard layout to act as a simple snooze button.
Because
this experiment builds upon Project 9 on this
website and requires the Adafruit Raspberry Pi Python Code Library, be sure to follow
the instructions in our Previous Project outlining
the installation procedures before you continue.
After
you have populated your breadboard and cloned the Adafruit library
mentioned above, use your favorite editor to create the Python script
below.
CODE :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#!/usr/bin/env
python
#
#
alarm_clock_LCD.py
#
# A simple alarm
clock using Adafruit_CharLCD module
# plus a buzzer and
16x2 LCD display. A button is connected
# to GPIO 27 to
simulate a snooze function.
#
#
# LCD Screen 16x2
Pins
#
--------------------
# Pin 1 to GND
# Pin 2 to 5v
# Pin 3 to
variable resistor then to GND 'Contrast Adjustment'
# Pin 4 to
GPIO 25 for 'Command or Data Mode Switching'
# Pin 5 to GND
for 'Read/Write Mode'
# Pin 6 to
GPIO 24 for 'Enable Pin'
# Pin 7 to Not
Connected
# Pin 8 to Not
Connected
# Pin 9 to Not
Connected
# Pin 10 to Not
Connected
# Pin 11 to GPIO 23
for 'Data 4'
# Pin 12 to GPIO 17
for 'Data 5'
# Pin 13 to GPIO 21
for 'Data 6'
# Pin 14 to GPIO 22
for 'Data 7'
# Pin 15 to 5v for
'Backlight Anode' - Could use variable resistor as a 'Brightness Control'
# Pin 16 to GND for
'LED Backlight Cathode'
#
# The positive side
of the two pin active buzzer is attached to pin 18
# and the other pin
goes to ground
#
# Import the
required libraries
from Adafruit_CharLCD
import Adafruit_CharLCD
import time
import RPi.GPIO
as GPIO
# Set some global
constants
lcd = Adafruit_CharLCD()
buzzer_pin = 18
snooze_button = 27
alarm_sounding = False
RUNNING = True
# Set buzzer pin as
a GPIO output and snooze button
# as an input with
an internal pull-down resistor
GPIO.setup(buzzer_pin,
GPIO.OUT)
GPIO.setup(snooze_button,
GPIO.IN, GPIO.PUD_DOWN)
# Make the function
to create a buzzing sound
# This function was
originally written by Simon Monk
def buzz(pitch,
duration):
period
= 1.0 / pitch
delay
= period / 2
cycles
= int(duration *
pitch)
for i
in range(cycles):
GPIO.output(buzzer_pin,
True)
time.sleep(delay)
GPIO.output(buzzer_pin,
False)
time.sleep(delay)
# Define a function
that returns a correct 24 hour result after
# snooze time has
been added
def snooze(alarm):
#
Add 9 minutes to the alarm time for the snooze
alarm
+= 9
#
Separate time into hours and minutes
hours
= int(alarm /
100)
minutes
= alarm - hours * 100
#
Make sure new alarm time fits 24 hour format
if (minutes
> 59):
plushrs
= int(minutes//60)
minutes
= minutes % 60
hours
= hours + plushrs
if (hours
> 23):
hours
= hours - 24
#
Reconstruct alarm time and return to main loop
alarm
= hours * 100
+ minutes
return alarm
try:
#
Get alarm time from user
response
= raw_input("Enter the alarm time in 24-Hour format
HHMM: ")
print("Alarm
time has been set for %s hrs"
% response)
buzz(500,0.1)
alarm
= int(response)
#
Clear LCD screen
lcd.clear()
while RUNNING:
#
Continually get the time as an integer
#
Output time in 24-Hour format to the LCD
curr_time
= int(time.strftime("%H%M"))
lcd.home()
lcd.message("
Current Time\n")
lcd.message(time.strftime("
%H:%M:%S\n"))
#
Trigger the buzzer function when the alarm time is reached
#
The buzzer will have two different tones just for fun
if curr_time
== alarm:
lcd.clear()
lcd.home()
lcd.message("
Wake Up!")
buzz(10,0.25)
time.sleep(0.1)
buzz(20,0.25)
time.sleep(0.1)
alarm_sounding
= True
#
Check for snooze button press
input_button
= GPIO.input(snooze_button)
if alarm_sounding
== True and input_button ==
True:
print('Snooze
Pressed!')
#
Call function to add snooze time
alarm
= snooze(alarm)
print('New
Alarm Time Is: %s' % alarm)
alarm_sounding
= False
#
Wait until the button is released to move on
while input_button
== True:
input_button
= GPIO.input(snooze_button)
#
Let the system have a brief break
time.sleep(0.01)
except KeyboardInterrupt:
RUNNING
= False
print "\nQuitting"
# Clear LCD screen
upon exit
# Don't forget to
clean up after so we
# can use the GPIO
next time
finally:
lcd.clear()
GPIO.cleanup()
|
Comments
Post a Comment