LCD Alarm Clock With Buzzer

Project 10: 16×2 LCD Alarm Clock With Buzzer

What you will need:

  • Raspberry Pi
  • Breadboard
  • 16 x 2 LCD Screen
  • 10kΩ Potentiometer
  • Active Buzzer
  • 9 Female to Male Jumper Wires
  • 8 Male to Male Jumper Wires

Instructions:

This project is designed to use the Raspberry Pi to simulate a simple alarm clock. Our project utilizes the same 16 x 2 LCD screen from our previous projects while incorporating an active piezo buzzer. A buzzer that is similar to what my son and I used for this project can be found through Adafruit Industries.

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. Our breadboard layout is identical to our previous 16 x 2 LCD screen projects except for the addition of an active buzzer. Notice that we have connected the anode (+) of the buzzer to GPIO 18 of our Raspberry Pi and the cathode is attached to our ground () rail.

#!/usr/bin/env python
#
#  alarm_clock_LCD.py
#
# A simple alarm clock using Adafruit_CharLCD module
# plus a buzzer and 16x2 LCD display
#
#
# 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 GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

# Set some global constants
lcd = Adafruit_CharLCD()
buzzer_pin = 18
RUNNING = True

# Set buzzer pin as a GPIO output
GPIO.setup(buzzer_pin, GPIO.OUT)

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

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.5)
			time.sleep(0.25)
			buzz(20,0.5)
			time.sleep(0.25)

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’s Python script begins by letting the interpreter know what language we are using. It also provides some simple comments that let readers understand the script’s purpose as well as indicating which GPIO pins are being utilized.

#!/usr/bin/env python
#
#  alarm_clock_LCD.py
#
# A simple alarm clock using Adafruit_CharLCD module
# plus a buzzer and 16x2 LCD display
#
#
# 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, the required function libraries are imported, some constants are defined, and an LCD is instantiated by utilizing the Adafruit function library. Finally, the Raspberry Pi GPIO pin we are using for our buzzer is set to output mode.

# Import the required libraries
from Adafruit_CharLCD import Adafruit_CharLCD
import time
import RPi.GPIO as GPIO

# Set GPIO pins to Broadcom numbering system
GPIO.setmode(GPIO.BCM)

# Set some global constants
lcd = Adafruit_CharLCD()
buzzer_pin = 18
RUNNING = True

# Set buzzer pin as a GPIO output
GPIO.setup(buzzer_pin, GPIO.OUT)

Our Python script next defines a function which was written by Simon Monk to cycle the buzzer on and off to create a specific pitch. This will be used to sound our alarm.

# 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 Raspberry Pi’s main Python script loop begins by asking the user to enter the alarm time in 24 hour format (HHMM). The user’s response is printed to our main screen and a beep is sounded to acknowledge the receipt of the information before continuing.

Next, we create a loop to continually update the current time displayed on our LCD screen. Once our alarm time has been reached, we display the message “Wake Up!” on our LCD screen, and trigger our buzzer.

The alarm clock script 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.5)
			time.sleep(0.25)
			buzz(20,0.5)
			time.sleep(0.25)

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 Raspberry Pi Python script above demonstrates how easy it is to build upon previous projects in order to create more complex experiments. Try building upon this simple script by changing the information that is displayed on the LCD screen or add a snooze button to make things more functional. Above all, however, have fun!



Leave a Reply