Use the table above and diagram reference to connect your i2c LCD screen to the Raspberry Pi. Note: I'm using a backpack module to make the process a little easier. You can connect the LED to either the LCD itself or the Pi for custom control options.
To use our LCD screen, we'll need to enable i2c. Access the Raspberry Pi configuration menu:
Under Interfacing Options, select the option to enable I2C. Confirm the change and restart your Raspberry Pi.
We need to see which I2C address our LCD is using. To do this, we'll be installing a package called I2C Tools. Run the following command:
sudo apt-get install i2c-tools
Install the following SMBUS Python library:
sudo apt-get install python-smbus
Restart the Pi and run this command to find the I2C address.
i2cdetect -y 1
This will return a table full of addresses. Jot down the number used by your LCD screen, mine happens to be 27. Update i2c_driver.py with the address number your screen is using on line 6.
Next we'll need to install this Python i2c driver. On the GitHub repository for this project, we've added a driver that you can use! It's basically just a refactor of a driver provided by DenisFromHR on GitHub.
Installing the driver is pretty simple. Just make sure you're in your home directory and use wget.
cd ~ wget https://raw.githubusercontent.com/Howchoo/smart-alarm-clock/master/i2c_driver.py
Now it's time to set up the RPLCD library. Begin by installing PIP:
sudo apt-get install python-pip
Once complete, install the RPLCD library package. Run the following:
sudo pip install RPLCD
It's time for the fun part! We need our LCD screen to give us some kind out output. The following script will display the time and date on our LCD screen. You can get creative with output settings.
#!/usr/bin/env python import I2C_LCD_driver import time mylcd = I2C_LCD_driver.lcd() while True: mylcd.lcd_display_string(time.strftime('%I:%M:%S %p'), 1) mylcd.lcd_display_string(time.strftime('%a %b %d, 20%y'), 2)
Save this python script to a file and drop it in
/home/pi. I've named my file
Note: I'm from America so I prefer my date with the Month in front. However, you can use this opportunity to adjust the output however you like. Visit this Ubuntu page for more information on clock output customization.
display.py script, Python's time module will output the current time in whatever timezone is set on the Raspberry Pi. So, in order to see the time in our local timezone, we'll need to configure the timezone.
We've written a detailed guide showing you how to set the timezone on your Raspberry Pi (tl;dr
Test your script by running the following code:
sudo python display.py
Note: Replace display.py with the file name you chose for your python script.
Find a sound to use for your alarm clock. I'm using a file labeled alarm.wav. Drop it in the /home/pi folder.
The alarm script requires a scheduler to work properly. Run the following script to install the scheduler:
pip install schedule
Create a python script to initiate our sound file at a specific time. Below is the script I used to schedule my alarm (for 7:00AM).
#!/usr/bin/env python import schedule import subprocess import time def job(): subprocess.call(['aplay /home/pi/alarm.wav'], shell=True) schedule.every().day.at('7:00').do(job) while True: schedule.run_pending() time.sleep(1)
Make sure the alarm script is running the alarm when scheduled. Plug in your speaker and run the following script. Replace alarm.py with the name of your alarm script file.
sudo python alarm.py
Note: If you have trouble when testing your alarm, try scheduling it for at least 5-10 minutes into the future. Sometimes the Pi needs a few minutes before will work.
Open the crontab file with the following command
At the end of the file, add the following two lines of code. Be sure to replace the 'display.py' and 'alarm.py' with your custom display and alarm scripts.
@reboot nohup python display.py & @reboot nohup python alarm.py &
Save and close the file. Congratulations! Every time your Pi restarts, it’s going to tell you the time and schedule your custom alarm.
Now let’s get to the good stuff. To make our alarm clock a smart alarm clock, we’ll be using AVS. Visit our guide here to learn how to set up AVS on the Raspberry Pi. Using Alexa, you can create commands and even trigger custom python scripts!
Here are a few example ideas to get you started:
- Set alarms using voice commands
- Check the weather
- Program reminders for future events
If your Pi has an internet connection (see our guide on setting up WiFi on your Pi), the time should remain perfectly in sync. Where you take the project from here is up to you. Congratulations! Your Raspberry Pi is now a completely functional alarm clock.
If you want to get really creative, incorporate it with our awesome Vinyl Record Clock project.
Nothing can slow down the Raspberry Pi Foundation, as even in 2021, the team managed to serve up a selection of new Raspberry Pi boards, modules, and fun accessories.