Introduction: Raspberry Pi Dedicated Internet Radio and Music Player
Have you ever wanted something to fill your day with lovely sounds but didn't want to dedicate a PC to this purpose? The Raspberry Pi provides exactly what we need, and with a few component parts laying around, you can control it with a few buttons and a knob.
Everything should work fine without the additional hardware, but I this hasn't been tested as rigorously.
This project will be able to play Internet mp3 radio streams and save them to your Pi's SD card. Ambiant sound will be included with the code that I provide later in this Instructable. You can add your own music or sounds to it as much as you wish. The code rips music with Streamripper and plays back the tracks. When a track is playing that you wish to save, you can simply press a button.
The volume automatically goes to 25% when the lights go off, but you can edit the code to change this.
What you'll need:
1) A Raspberry Pi with Raspbian installed, connected to the internet, and (optionally) running an SSH server
3) Coding experience helps
For the hardware, this project will use:
1) A Breadboard
3) 100k ohm linear potentiometer
4) Three LEDs
5) Jumper wires
6) Toggle switch
8) 1uF electrolytic capacitor
9) 100uF electrolytic capacitor
11) Three 100 ohm resistorsamixer set PCM -- 1000
Also, it's good to make sure your Raspberry Pi's volume output is at 100%.
you can do this with:
"amixer set PCM -- 900"
Step 1: Optionally Setting Up SSH
In a shell, type:
A menu will come up in which you can choose to enable SSH.
In Linux, to make sure that you can access your pi remotely, after you have your SHH server started, type:
Where 'user' is a user name on the pi and hostname is the pi's address.
For example, to login to an account on the pi named "bob," you would type:
To figure out what your Pi's address is, there's an awesome android app called Fling that will scan your network.
Step 2: Dependencies
This project runs mainly from a python script, which can be found in latter steps.
First, you need to make sure that you have all the required software installed on your Raspberry Pi.
Your Pi almost definitely has this installed already, but to make sure, type:
"sudo apt-get install python"
I used version 2.7.4
I used pygame to manage audio playback. Install this with:
"sudo apt-get install python-pygame"
This is needed to control the Pi's GPIO pins with Python
Download it with:
and install with:
"sudo dpkg -i python-rpi.gpio_0.3.1a-1_armhf.deb"sudo apt-get install streamripper"
This is needed to rip music from internet radio stations.
install it with:
"sudo apt-get install streamripper"
Step 3: Getting the Code
Create a directory on your Pi and extract the attached archive into it.
I extracted mine to
Step 4: Start Building the Hardware (optional)
This code is fully functional without any hardware (other than a speaker) attached to the Raspberry Pi.
To change the ouput volume or mute it, you could simply use the controls on the speakers you have hooked up to it or start the python code in a shell over SSH and input your commands into that.
However, if you want a quick way to control the volume and have the code react to the level of light in its environment, follow these steps:
The hardware will consist of three LEDs for monitoring the volume of the pi, a volume knob, a photocell for monitoring the light level, a button for saving the currently playing song to the disk, and a switch for muting the pi.
The hardware will be attached to the Raspberry Pi with jumper cables.
The green picture is a reference sheet for the Pi's GPIO pins. I would recommend putting a piece of insulation over the two 5 volt pins, because if you accidentally short 5 volts into another pin, it could brick your pi.
1) Get out your trusty breadboard and the parts for hardware mentioned in step 1.
2) First, hook up your LED's. Place a jumper wire between pin 11, 12, and 13 on your pi (labeled on the reference sheet above as GPIO 17, GPIO 18, and GPIO 21/27, respectively) and your 100 ohm resistors. Don't forget to hook up the 3 volt and ground pins to the power and ground buses on your breadboard. Then put an LED between each of the resistors and ground.
It should look like this:
PIN 11 -----resistor------LED----ground
PIN 12 -----resistor------LED----ground
PIN 13 -----resistor------LED----ground
3) We have 4 inputs in total: mute, save, volume, and light.
Since the mute switch and save button are digital(and the code enables pull down resistors on the pi itself), all we have to do is connect the power bus on the breadboard to pin 7(for the mute switch) and pin 18(for the save song button) on the pi.
4) The photocell and volume knob aren't digital. This is a problem, because the raspberry pi doesn't have analogue inputs. I didn't have any analogue to digital converters, so I hacked around that.
For the volume knob, hook up the middle pin to 3 volts + and hook up the left pin to both the positive a 1uF capacitor and pin 16 on the pi. Then, connect the other side of the capacitor to ground.
Similar steps are used with the photocell. First, connect one side of the photocell to 3 volts and the other side to both the positive side of a 100 uF capacitor and pin 15 on the pi. Then connect the negative side of the capacitor to ground.
The code times how long it takes for the capacitor to discharge, which is affected by the resistance of the volume knob(potentiometer) and the photocell.
More information can be found here:
If you wish to keep this hardware permanently, transfer it from your breadboard to some perfboard and solder it.
NOTE: If you wish to use other pins for any of these components, open the file named "gpioInput.py" and change their values. For example, the mute pin is set to 7 by default. You can change 7 to be any pin you want.
The actual time it takes for the Pi to measure capacitor discharge may
vary when you build your own version of this. The volume is calculated as the time it took for the volume pin to read LOW divided by the maximum time it would take(if the volume knob was at full resistance). You can change the maximum value in the file "gpioInput.py". The variable "max" in the method "poll_vol" can be changed to what you want it to be. To monitor the time it takes, uncomment the line "print self.vol_knob" and run the program in a terminal.
Step 5: The Finishing Touches
I attached the Pi and my hardware to a platform with Velcro to prevent the jumpers attaching them from being pulled and also to keep everything organized and easy to carry.
You should now probably attach some speakers to your pi to hear your beautiful musics. You can add type of audio you like. To add your own songs or sounds, just place them in /presence/soundMusic or /presence/soundLong
To add more internet radio stations to rip, simply figure out the IP address of the stations you want and add them to the file "station.txt" Make sure each station's IP is on its own line.
To figure out the IP of a station( on shoutcast.com) right click on a station and click save it. Then, open it up in a text editor and you'll see a line like
Just copy and paste that line into station.txt and you're all set.
Now, whenever you want to start the code, just go into its directory and type:
sudo python presence.py
You NEED to be root for this code to work.
I'm going to start it up with SSH whenever I want to run it, but for convenience, you could set that script to run when the Pi starts up.
And now you're finished. The code is, of course, buggy, So feel free to fix or edit it however you like.