Wall's Corners Wall's Corners Author
Title: Build a Raspberry Pi Alarm Clock Part 1
Author: Wall's Corners
Rating 5 of 5 Des:
I’ve started playing with Raspberry Pi almost one year ago. Not only I’m following the news about Raspberry Pi, I took the habit of checkin...

I’ve started playing with Pi almost one year ago. Not only I’m following the news about Raspberry Pi, I took the habit of checking the other small computers, and I’ve been steadily becoming addicted to electronic kits.
By the way, I recently really got into a single-board computer called “CHIP“.

CHIP can be anything of the selling price $ 9 ultra-small computer | TechCrunch

$9 is cheaper than buying a pie! I’m happy to see that it seems possible to install a lighter version of Debian, OS that I’ve learned using with Raspberry Pi. I’m impatient to buy it!

So, let’s get back to the Raspberry Pi.
Since we’ve learned about the Raspberry Pi basic content, this time let’s change  a little. I’d like to create a project divided in several parts. Let start an “alarm clock” small series!

In the first part, we will see the essential functions of the alarm clock, and how to implement a stop button. Clicking on a button to stop the alarm clock seems to be simple to create, however, it may be considerably difficult to make with an electronic kit.

Below is the process. Let’s first start by creating a single-function alarm clock!

raspberrypi19_main2

About the Switch

Seal type Tactile switch type B3W | OMRON Electronic Components Web

raspberrypi19_img01

Figure 1 (information from the Omron Electronic Components Website)

This time, I have been using a tactile switch (also called tact switch). When pressing the button, it will connect to the internal circuit, and the current will flow.
It’s a little difficult to understand. When using this component, the pin port will curve a little inside. The pin on the opposite side will connect internally when the button is pressed, and all the pins will be connected.

raspberrypi19_photo01

Picture 1

Picture 1 is the breadboard we set up. Do not push strongly to insert it. The legs just need to be hooked in the breadboard holes. Try to press lightly, until you feel it clicked.

Wiring

raspberrypi19_photo02
Picture 2

Parts needed

  • Tactile switch × 1
  • Resistor × 1
  • Jumper wire × 3

raspberrypi19_img02
Figure 2

The components are wired as above.
We’re using the 3.3V pin 1 for the power (green jumper wire). For the GPIO to perceive the switch input, this time we’re using the GPIO2’s pin 3 (blue jumper wire).

Incidentally, when it was tried to test it actually omit the pull-down resistor, the value of “1” regardless of the state of the switch, it has been acquired.

I feel that it could work just like that, but there is still one important thing. It’s the resistor placed between the switch and the GPIO2. This is called a “pull-down resistor” (black jumper wires). It should be connected to the GND as it’s used to stabilize the current. It’s the opposite of the “pull-up resistor” we used previously.
By the way, when I was running some tests without using the pull-down resistor, regardless of the switch situation, I got the value “1”.

Sensing the Raspberry Pie Switch State

We learned about the GPIO “output” in Making an LED Light up with Raspberry Pi Before Making It Blink. This time, we’ll see the opposite with the Raspberry Pi “input”. The basic way of operating is the same for the input mode and the output mode. First, we’ll prepare the GPIO port we’ll use. We’ll use the port of the GPIO2 pin3, and we’ll assign  “2”.

echo 2 > /sys/class/gpio/export

Next, we’ll set up both the “input” and the “output” of the GPIO Port. We set “out” for the “output”, so this time we’ll set “in” for the “input”.

echo in > /sys/class/gpio/gpio2/direction

We’re done with the settings. The difference of the output is only specifying the “in” and “out”.
To sense the switch input state, we access the file by using the “cat” command to get the variable value.

cat /sys/class/gpio/gpio2/value

raspberrypi19_img03
Figure 3

The output will be “0” when the button isn’t pressed. If the output shows “1”, something may be wrong with your wiring. If it’s not working, check the board if the tact switch is in the right position or if the pulldown resistor is working.

Finally, end the process. You can end it by writing the GPIO port number in “unexport”.

echo 2 > /sys/class/gpio/unexport

We finished entering the required commands to sense the switch. By including them in the program, we’re done with the alarm clock.

Program Preparation

Let’s prepare to ring the alarm clock program, and to stop the alarm by pressing the switch. We’ll use PHP for that.
We’ll start first with the program that will ring the alarm sound. We’ll use the “mpg321” package to playback the alarm clock sound file.

/var/www/start.php

Code-Example
1
<?php exec('mpg321 -l 0 -q /var/www/alerm1.mp3 > /dev/null &');

I use some free sound file for the MP3 file. Since the audio file is short, we’ll use the “- l” option to play it in a loop. The “> /dev/null &” at the end is to make this process work in the background.
Next, let’s work on the program that will stop the alarm. By observing the condition of the button, when it’s pressed the MP3 playback will stop.

/var/www/stop.php

Code-Example
1
2
3
<?php $interval_sec = 0.25; $wait_sec = 60; exec('echo 2 > /sys/class/gpio/export');
exec('echo in > /sys/class/gpio/gpio2/direction');
for($i=0; $i<($wait_sec / $interval_sec); $i++){ if(exec('cat /sys/class/gpio/gpio2/value')==1){ break; } usleep($interval_sec * 1000000); echo $i.PHP_EOL; } exec('killall mpg321'); exec('echo 2 > /sys/class/gpio/unexport');

In the above program, the button condition is observed every 0.25 seconds during one minute (0.25s x 240 times). When the button is pressed, or when the button isn’t pressed for 1 minute, “mpg321” will stop.

Let’s test!

You can see the execution of “start.php” in the left side of the screen, and “stop.php” in the right side. The “mpg321” command used in start.php to stop the playback has been tested in the LX terminal 2nd screen to monopolize the console.
The “stop.php” right screen displays the counter variables in the console screen. If the “swith press condition” is sensed by the 12 times loop process, then the alarm playback will be stopped. If it’s not sensed, adjust the usleep variable or try to release your finger slowly.

If the test is successful, let’s try to set up the alarm clock by registering the cron.

Cron Configuration

In a previous article, we already did the cron setting, but this time let’s look at it in more details.
Cron specifies the date and time as well as the command to be executed. The date and time are described in 5 variables (see below).

Min hour day of month month day of week command to execute

It might be convenient to use special symbols if you want to use multiple values. When writing a list of specified values “,” you should separate them using “-“. If you want to specify all the values, you should use “*”.

Let try to execute the command “weekday (Monday to Friday) 6:45” to set up our alarm clock.
Let’s start with the following.

* * * * * command

Let’s start with the day of the week, the 5th value. The value used are from 0 to 6. The numerical value stats from 0, which is Sunday. To set the weekday, let’s assign “1-5”. Because it’s numbers, we’re using the hyphen “-” to specify the range. Using an enumeration with the comma “,” would work the same.

* * * * 1-5 command
* * * * 1,2,3,4,5 command

Because the timing will be set up with the day of the week, you will need to specify “every day”. “day of the month” and “month” won’t be needed. Let’s use the “*”.
Finally, let’s specify the time. “hour” is the second value, and “min” is the first one. For minute, the value is going from 0 to 59.

45 6 * * 1-5 command

Date and time settings are now complete! After that, we only need to specify the command!
Because this time we want to call two programs, we need to include both in shell script.

/root/morning.sh

php /var/www/start.php
php /var/www/stop.php

In the script file, either the “chmod” command or a property setting file can grant the execution permission
Before registering the cron, let’s check the execution.
When the shell script is ready, the cron is complete. We will replace the “command” we temporarily set as follow.

45 6 * * 1-5 /root/morning.sh

Now, the cron is registered.

crontab -e
45 6 * * 1-5 /root/morning.sh

raspberrypi19_img04_2
Figure4

In the Figure 4, there are still some instructions from the thermometer we made previously. In that way, it’s much easier to understand for the person who put together the shell script.
Finally, press Ctrl+O to save it. We have now completed the alarm clock that will wake you up at 6:45!

Summary

Our simple Raspberry alarm is done.
Here’s my impression after using it. I feel that it might be better to set the waiting time of the stop button a little longer as it may take some time before the MP3 playback starts. Because you need to press softly a small button at the top of the breadboard, it might welcome your morning with a certain stress!

Next time, we’ll continue to work on the customization of our alarm clock.
Because I’m not really satisfied ending just with the alarm stopping, I want to add another function after it stops. I want it to tell us the time.
This is why I want to implement a function that will read aloud the current time.

Share This:

View more at: http://yoursmart.mobi

About Author

Advertisement

Post a Comment

 
Top