I have habit of setting up alarm clock on my smartphone just before I go to bed. While setting up alarm I usually get distracted by tasks like taking daily notes or recording daily expenses on my smartphone which is not good for my sleep cycle. Well, analog alarm clocks are great substitute and I needed bigger motive to get my hands soldery. Then one day while I was watching Jack Ma's talk which contained few motivational liners on getting up early, I thought it would be great to hear such talk as alarm tone. Well that was it.
This miniProject is the result.
Step 1: Video
A comprehensive video.
Step 2: Setting Up Raspberry Pi Zero W and First Access
My speaking alarm clock is build around beautiful Raspberry pi zero W. Lets set it up.
First I downloaded raspbian lite from raspberrypi.org, Installed etcher on my laptop and burned raspbian on an SD card. I don't have a monitor/keyboard/mouse to which I can connect raspberry pi but a laptop hence I enabled remote access to Raspi. In order to get remote access I opened up interfaces file in /etc/network directory of root and used 'vim /etc/network/interfaces' command to edit it and made it look like one shown in image attached at #3. Replace black stripes with your router's SSID and password. Once that was done I created an empty file named ssh in boot directory to enable ssh.
Once configurations were done I took put SD card from my laptop and inserted it in to raspberry pi, followed by powering it up. I found IP address of raspberry pi using network scanning application named angry IP scanner. Following that I used obtained IP address to access raspberry pi using 'ssh pi@IP_ADDRESS'. If you are on windows machine use software like putty to perform ssh.
Step 3: Giving Raspberry Pi Zero W Audio Capabilities (software Config)
On my first login using default username and password, I opened up /boot/config.txt file and added highlighted line to it as shown in image #1. This configures raspberry pi zero W to output audio on GPIO 18 and GPIO 13. A reboot is required to activate this configuration.
Following reboot, I forced audio to 3.5mm jack using
sudo raspi-config --> advanced options --> audio --> 3.5mm jack
Step 4: Giving Raspberry Pi Zero W Audio Capabilities (hardware)
Once Raspberry pi zero W's software audio configurations were done, I connected GPIO 18 to PAM8403 3W audio amplifier, gave it independent 5V supply and connected its output to a speaker.
but there was a problem.
I was hearing a bit of noise in audio produced by speaker (If you want to here what kind of noise I was getting, checkout video). We can reduce this noise using a band-pass filter constructed in next step.
Step 5: Band-pass Filter
We can construct basic band-pass filter using
- Three 270 ohm resistors
- One 10 uF electrolytic capacitor and
- One 10 nF ceramic capacitor.
and connecting them as shown in attached schematic.
On connecting GPIO 18 to input of this filter and output to speaker, there was a noticeable reduction in the noise amplitude. I played some music to make sure that everything was fine. Once audio was sorted out I moved on to coding.
Step 6: Coding- Installing Dependencies
I used python to code up speaking alarm clock. First step was to install dependencies on Raspberry pi. I installed espeak package that converts text to speech using
sudo apt-get install espeak
then I installed python package manager pip using
sudo apt-get install python-pip
followed by installing python schedule package using
pip install schedule
Step 7: Coding- Main Code
First I imported all required packages. I used 4 GPIO pins as user interface to get minutes in alarm time. I decided to have resolution of alarm time to 5 minutes so we need only 12 binary digits to cover full 60 minute span and hence 4 GPIO pins (12 out of 16 max binary digits) for minutes. Going by similar logic I set aside 5 GPIO (24 out of 32 max binary digits) pins for hour input. I reserved two more GPIO pins, one for alarm start/stop and another for alarm snooz/reset.
Following GPIO declarations, I created two functions. In main part of code, I created an infinite while loop which reads minute and hour data on pressing alarm set button and speaks it out for confirmation. Once alarm is set I schedule check_time function to execute every minute. check_time function checks if it is time for alarm and if so it starts a thread to play alarm tone by running alarm_tone function. while alarm_tone thread is running, if user presses stop alarm or snooz button, I kill alarm_tone thread and in later case schedule alarm after 5 minutes. well this was top level view of code.
I have attached code. Let me know if you have any doubts or suggestions to improve this code.
Step 8: Schematic - Assembly - Finish
Once coding was finished. I connected buttons to raspberry pi using a breadboard and bunch of wires. To get minute and hour data I used DIP switched and to get alarm/snooz data I used a bit bigger push buttons.
Once I got full system working. It was time to assemble it on a perf board. To help me in that task I created attached schematic. Finally on connecting a speaker to output of audio amplifier on perf board, my speaking alarm clock was done. Take a look at quick demo towards end of the video attached in step 1.
Thanks for reading.