LCD Alarm Clock With Snooze

Project 11: 16×2 LCD Alarm Clock With Snooze Button

What you will need:

  • 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.

#!/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
#
#  Copyright 2015  Ken Powers
# 

# 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()

Code Analysis:

Our Raspberry Pi Python script begins by telling the interpreter we are using the Python language. We also include some descriptive comments that help to let readers know what the script will accomplish as well as indicating what Raspberry Pi GPIO pins we are using.

#!/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
#
#  Copyright 2015  Ken Powers
#

Next, we import all the required function libraries, instantiate an LCD using the Adafruit library, and define some necessary constants.

# 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

At this point in our script, we setup our GPIO so our buzzer pin is an OUTPUT and our snooze button is an INPUT that is utilizing an internal pull-down resistor.

# 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)

Our first function definition was written by Simon Monk and is designed to make a sound through our active buzzer.

# 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)

Our next function definition is used for our snooze button. If the alarm is sounding and the snooze button is depressed, this function is called.

Every alarm clock I have ever owned has had a snooze time of 9 minutes. I have no idea why 10 minutes isn’t used. This function sticks with that unusual convention and adds 9 minutes to the current alarm time.

Next, the time is broken into hours and minutes so we can ensure that the new alarm time fits standard 24-hour format conventions.

# 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

The main loop of our script prompts the user for their desired alarm time in 24-hour format. Our LCD screen is continually updated with the current time and the alarm buzzer is sounded if our alarm time is reached. While the alarm is sounding, a press of the snooze button calls our snooze() function and the new alarm time is returned. The entire process continues until Ctrl + C is pressed.

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()

The addition of a snooze button to our previous project turned out to be a bit of a challenge. Making sure the adjusted alarm time remained in proper 24-hour format required a bit of thought. Enjoy the project and have fun modifying it to suit your own needs. Above all, however, have fun!



Leave a Reply