Introduction: Custom Alarm-Clock/Music Over Radio

About: One of my hobbies is taking stuff apart and seeing how I can customize it.

This a fairly simple Raspberry Pi project for beginner level Raspberry Pi enthusiasts. In this Instructable I will show you how to make an alarm-clock that will broadcast over the FM band. This will enable you to be able to listen to the radio and still have an alarm function unlike most alarm-clock/radios. You can also broadcast your own music with it! More importantly you will hopefully learn something new about the Raspberry Pi and how to use it.

DISCLAIMER: In some countries certain radio transmissions are illegal. I do not encourage the violation of any laws, and cannot be responsible for any violations of such laws.

DISCLAIMER 2: This is my first Instructable...please don't be too harsh :)

Features

  1. Alarm broadcasts over radio.with a 10 meter radius range
  2. Can play favorite music.
  3. Create custom alarms(Not constrained by traditional alarm clock tones)
  4. Learn how to use a Raspberry Pi in the process!
  5. Power Supply

Including the Pi, this project costs less than $50. One tip: Ebay is your friend, if you have some patience you can get some really good deals. I got my RTC there for less than $2. Lets get started!

Step 1: Materials

These are the materials you need. This project uses the Raspbian OS. If you have a screen then you can skip the first step in software materials. I will demonstrate how to set up the project via SSH(for those of you without screens available.

Hardware

  1. Raspberry Pi B+ and Mirco SD card(may work on other models but I haven't tested it.)
  2. Real Time Clock module ds1307 or ds3231(I am using the later)
  3. Female Jumper Wires x5
  4. Battery(depends on model of RTC but is usually 2032 or 2025)
  5. Power Supply with at least 700ma current

Software

  1. If you have a Windows computer you will need to install Putty.
  2. Raspbian OS
  3. If you have a Windows computer you can take advantage of Win32DiskImager.
  4. Pifm Software(The code we are borrowing to enable us to transmit over the Radio) If the link fails to work use the files bellow.(You will have to recompile if you do so)

When picking out your RTC ensure it can run on 3.3v. It might say in the product description or, you can always look at the data sheet!

Attachments

Step 2: Getting Started - Software

The first thing we want to do is flash the Raspbian image to the micro SD card. After you download it, use the Win32DiskImager mentioned before to flash it to your micro SD card. Make sure you select the correct drive! To verify hit Windows + e keys. In the window that comes up look for your SD card. Take note of the name. Then select that name in the program. Now plug the sd card into your Pi. Before you go to the next step, you should have the Pifm source, Putty installed for Windows users(If you are going to SSH), and the OS burned to your SD card.

Step 3: Getting Started - Hardware

Good news! Wiring is fairly simple. Of course, make sure your Pi is off while you wire it up. The first label is the RTC and the second is the pin on the Pi. Refer to the pictures above:

  1. VCC goes to 3.3v on Pi(look at the bottom of your pi, the square solder pad is the one)
  2. GND goes to Pin 6 on Pi
  3. SDA goes to Pin 3 on Pi
  4. SCL goes to Pin 5 on Pi

Look at the layout picture above and roll over to see where each goes. It should form an "L" if wired correctly. You are done wiring the RTC! Now for the Antenna, the hard part! Just add a wire to pin 7 of the Pi : - ). Refer to the numbering in the diagram. I do not have the antenna attached in the picture. Lastly, add a battery to the RTC.

You might be wondering what the SDA and SCL pins are. Those pins are protocol called I2C(pronounced I-squared-see). It allows the Pi to "talk to" multiple devices on the same two pins(such as an Arduino, RTC, etc.). The pi is called the master, because it controls the others. The RTC and anything else you add to the pins are called the slaves. To tell the slaves apart, each slave has an address. More on this later. If you want to learn more, take a look at this.

Before you proceed you should have the RTC and antenna wired and the battery in the RTC module.

Step 4: Let's Talk to the Pi - Setting Up SSH

If you have a screen and wish to use it, proceed to the next step. Otherwise, read on.

SSH is a way to access your Pi using its Ethernet port. Connect the Raspberry pi to the Router with a standard Ethernet cable. Then attach another Ethernet cable from the router to a PC. Now assuming you have no other way to access your Pi, you will need to install and IP scanning software, such as nmap for Linux users. Just search for "Free IP scanning Software" or for us Linux users, on a Debian based PC you can type sudo apt-get install nmap.

Now, power on your pi and give it 1 or 2 minutes to boot. Now use nmap or your Windows compatible ip scanner to search for your pi. For nmap you can type sudo nmap -sn 192.168.0.0/24 where 192.168.0. is the range of your network. If you don't know then assume that is the range.

You should see a listing that says an IP address and something about the Raspberry Pi Foundation. Mark down that IP address.

Finally, using Putty type in the IP address you wrote down in this format: pi@ipaddress. So for example if the ip of your pi was 192.168.0.4 then you would enter pi@192.168.0.4 into the window. For linux based OS's its even simpler. Just type ssh pi@ipaddress.

This will give you a security warning and unless you don't trust your pi then enter "yes" : -) Type in "raspberry"

At the end of this step you should have a working connection between a computer and your pi. You will not be able to access a Graphical User Interface(GUI) through SSH but we don't need to.

Step 5: Writting the Code - Python

The brains of our project are going to be in this code. Follow the steps:

  1. Type nano alarmclock.py into the terminal window(nano is a text editor and alarmclock.py is what we will call our file) The nano text editor will be used to make our code.
  2. Insert !# /usr/bin/env python as the first line. This tells Raspbian this is a python script.
  3. Here is the file with the code but I will explain the major parts.

We import the time module so we can access the system time. We import the os module so we can use bash commands from within python. Then we make a variable for the alarm time. The longest line in the file is the line that calls the pifm binary. This is because it is first converting the file to a form that pifm accepts, than the path of the binary is listed and then the station we wont to broadcast on is listed.

The while True function checks if its time to sound the alarm and if it is not, plays a music file of your choice. I could write a whole instructable on the Python part of this project; but the main functions have been covered.

Now download a custom alarm sound, and get some of your favorite music audio files, put them on a USB drive and put them in your home folder on your Pi. Also, put the pifm binary in your home folder. Lastly, execute the following command to make your file executable: sudo chmod 755 ./alarmclock.py

At the end of this step you step you should have your python code, some audio files, and the pifm binary in your home folder.

Step 6: Finish Setting Up Software

In this step we are going to do 2 things: configure the RTC and get our python script to run as soon as we power the pi on. First lets configure the RTC. To do this we need to install i2c-tools so go ahead and run sudo apt-get install i2c-tools. I found this tutorial by Adafruit very helpful.

First, we need to enable I2C. Enter the command raspi-config, select advanced options and allow I2C.

Basically, we need to run sudo i2cdetect -y 1. Then you should see a 68 somewhere in the chart that appears. Then we load the RTC module with this command: sudo modprobe rtc-ds1307. Then type sudo echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device.

Now check the time of the clock with sudo hwclock -r. If its way back in the 90's that OK. Run the date command. That will print your current system time. If you have a working internet connection then it will be correct. In that case write the time to the clock using sudo hwclock -w. Double check with the sudo hwclock -r command that it worked.

Now, all is fine and well...untill we reboot. So what we want to do is load the RTC modules on boot. So, run sudo nano /etc/modules and add rtc-ds1307 to the end. Then enter the command sudo nano /etc/rc.local. This is a file that will run whatever commands you put in it, at boot. Here we can kill two birds with one stone. First enter echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device and in a new line sudo hwclock -s for the rtc module and /usr/bin/python /home/pi/alarmclock.py for auto starting our python script. If you are confused, follow the link I gave.

At the end of this step you should have your RTC displaying the correct time even after a restart. And they python script should auto start at boot.

Step 7: Customizing

Now...the hard part is out of the way. Here is the fun part. Go back into your alarmclock script(sudo nano alarmclock.py) and configure it:

  1. Change the radio station - current station is 105.9
  2. Change the alarm File - make it whatever you want
  3. Change the song file path
  4. Change the time for the alarm.

Now if all is working correctly, when you reboot, and tune your radio to the preset station it should start playing your music file. If the time you set your alarm for is reached, then it will play the alarm.

You could delete the second "if clause" and just listen to the radio. Thus you can have radio and an alarm unlike most alarm clocks where you have to choose what you want.

Step 8: Weaknesses and Room for Improvement

Like most projects there is room for improvement. With the current code you cannot interrupt the music your playing to sound the alarm so you will miss the time if you have something playing while it is time for the alarm. Also, in the current code it can only play one music file.

One of the benefits is that if you play something and then it stops it will blank the radio until you reboot the pi. My original purpose for making this was to play audio files when I went to sleep. After a preset time I would have them stop(when I judged I would be asleep). Then the alarm would turn on in the morning. In the meantime the radio is blanked so it sounds like your radio is off when you are not broadcasting.

Feel free to make the code better and use it as a learning experience! You could make it sound like a Grandmother clock with some audio files and a little more code! The most important thing is that you learned something from this Instructable! If you liked it, please vote for me in the Raspberry Pi contest!

Raspberry Pi Contest

Participated in the
Raspberry Pi Contest