Making some modifications to some nice Python examples found on the Internet (Fuzzy Logic Robots, python-mpd documentation, raspi-hd44780 by Irvick) I was able to make a very basic, but fully working, standalone RPI Web Radio receiver.

Here is the complete wiring sketch:

In the above sketch, pin 1 of the LCD is the leftmost one. The rightmost 2 pins (15 and 16) are for the backlight connections. The LCD works at 5V. A 2 lines x 40 characters display was employed, because the titles and the station-ID are generally long. If a smaller display is being used (e.g. 16/20 characters), some modifications to the LCD program are necessary. Adjust the small 10 kOhm variable resistor to get the best LCD characters contrast.

S1 and S2 are used to move up and down through the channels, S3 and S4 to adjust the volume level. The pull-up resistors are all connected to the 3.3V power source. Idea: the four pushbuttons may be connected to a simple remote control circuit.

Take care not to short out each-other the 5V and the 3.3V outputs coming from the RPI and double-check the wiring before powering-up the RPI.

For simplicity, two independent programs have been made: one starts mpc and handles the four pushbuttons, while the other one handles the LCD (updated every 3 seconds). Both programs run in background.

The name of the playlist used by mpc is embedded into the program.  The playlist is named “mymy” (see below).


MPD/MPC must be already be installed as described in Part 1.

1. download the python programs and change the file permissions:

mkdir /home/pi/rpi_web_radio
cd /home/pi/rpi_web_radio
sudo chmod +x radio*

2. download the sample playlist “mymy.m3u” (stations may be obsolete at the time you’re reading this article, just update the contents as needed) then check/change the file permissions and the owner:

sudo cd /var/lib/mpd/playlists/
sudo wget
sudo chown mpd:audio /var/lib/mpd/playlists/mymy.m3u
sudo chmod 644 /var/lib/mpd/playlists/mymy.m3u

3. download the following programs into /etc/init.d/ then change the files permissions:

sudo wget -O /etc/init.d/radio_btn
sudo wget -O /etc/init.d/radio_lcd
sudo chmod +x /etc/init.d/radio_btn
sudo chmod +x /etc/init.d/radio_lcd

4. make the programs run automatically at boot:

sudo update-rc.d radio_btn defaults
sudo update-rc.d radio_lcd defaults

If needed, the two control programs can be manually stopped/started:

sudo /etc/init.d/radio_btn stop
sudo /etc/init.d/radio_btn start
sudo /etc/init.d/radio_lcd stop
sudo /etc/init.d/radio_lcd start

To stop the automatic program execution at boot:

sudo update-rc.d -f radio_btn remove
sudo update-rc.d -f radio_lcd remove

The Raspberry, wired to the network (ethernet or wifi), should automatically start playing and the LCD should show the song title and the station name at every reboot.


Along with their conventional RF transmitters, nearly all major radio stations now broadcast their programs through the Internet (Streaming Media). Also, many web-radios are exclusively Internet-based.

Being cheap and small, transforming the Raspberry PI into an Internet radio player was really tempting. Adding a wireless adapter, plus a handful of cheap components, the RPI may easily be transformed into a standalone “receiver”.

This first article deals with the installation of some command-line programs: MPD, Music Player Daemon, MPC (Music Daemon control-program) and python-mpd. These programs can easily interact with any user-written programs.

The following setup was tested under a fresh installation of the latest Raspbian distro (do sudo apt-get update and sudo apt-get upgrade before starting).

Download and install the required packages from the repository:

sudo apt-get install mpd mpc python-mpd alsa-utils

Make sure that the sound module is enabled:

sudo cat /etc/modules

snd-bcm2835 must be present in the list. Otherwise add it.

The setup is done, so let’s test the “receiver” and make it play. Execute MPC:

sudo mpc

it should reply:

volume: 80%   repeat: off   random: off   single: off   consume: off

Good, it should work.

MPC holds the list of “stations” on a playlist. It’s time to add the first “radio” to the playlist, so enter:

sudo mpc add

Inside the playlist, each station is identified by a number, starting from 1 and increasing as we enter the radios one after another. Having only one station, make it play entering:

sudo mpc play 1

MPC should reply with:
[playing] #1/1   0:00/0:00 (0%)
volume:100%   repeat: off   random: off   single: off   consume: off

and… if an headphone or the speakers are plugged into the RPI’s audio socket, we should now hear the radio playing.

Deleting an entry from the playlist, the list is shifted backwards (so, if the 4th was deleted, the 5th will become the 4th, the 6th will become the 5th, an so on).

The volume can be adjusted with the following command:

sudo mpc volume (followed by any number from 0 to 100)

and again MPC will reply with some useful informations:


Let’s now add another nice station,  then query the status of MPC:

sudo mpc add
sudo mpc

The reply should be something similar to:
[playing] #2/2   0:00/0:00 (0%)
volume:100%   repeat: off   random: off   single: off   consume: off

Now we can save our playlist entering:

sudo mpc save mylist

The list, named “mylist” in the above example, is saved under /var/lib/mpd/playlists/ and the full-name is mylist.m3u. More than one list can be created and loaded when needed.

If the playlist exists, it must be deleted before saving it, using:

sudo mpc rm mylist

Among the many MPC commands, the most useful ones are:

sudo mpc load mylist
sudo mpc lsplaylists (show the available .m3u playlists)
sudo mpc playlist (show the playlist content)
sudo mpc clear (to empty the playlist)

To stop the player enter:

sudo mpc stop

Here is a very short and simple Python program that gets the Station Name and the Song Title from MPD and prints these informations onto the command-line:

import mpd

client = mpd.MPDClient()
client.connect("localhost", 6600)

cs = client.currentsong()

if 'title' in cs:
   SongTitle = cs['title']
elif 'file' in cs:
   SongTitle = cs['file']

print ('Song Title  : ' + SongTitle)
print ('Station Name: ' + cs['name'])

Download and run the above program:

chmod +x

The program works only when MPD is tuned-in and playing (through mpc play).

Looks quite simple and promising, isn’t it ?


To interface SWiTh to other electronic devices (small LEDs, switches and other basic components), some circuit is needed. This is necessary when we are coupling SWiTh to devices working at different voltage levels or when driving high-power loads (e.g. lamps, relays, etc.).

For safety, an optocoupled interface is mandatory if we need to drive loads connected to the 230V mains.

A series of simple, practical examples follows. These should cover the most common situations and can be used with any similar systems.

Driving a low-power relay using one N-CH MOSFET

Example 1: this circuit allows SWiTh to drive a low-power relay. Very few components are needed. The input resistor insures a stable logic L when the input is floating (the input impedance of the MOSFET is very high). The diode protects the MOSFET against the voltage spikes generated across the relay coil when this latter is powered on and off.

A bipolar transistor drives a low-power relay.

Example 2: a common NPN transistor is used instead of a MOSFET (see Ex. 1).
Both circuits turn on the relay when a high level (5V) is applied to the input.

A bipolar PNP transistor drives a low-power relay

Example 3: a PNP transistor drives a relay.
This circuit is useful when an inverted logic is needed: the relay is OFF when a 5V voltage is applied to the input and ON when the input is floating or logic zero.

Using an integrated darligton driver ULN-2003

Example 4: a circuit using a very useful and popular IC, the ULN2003.
This ICs can drive different loads of various types: relays, lamps, motors, even at high voltages and currents.
Each channel is rated at 500mA, that can be paralleled for higher demands.
Each driver has its own suppression diode for driving inductive loads. A very handy IC !

Optocoupled digital input

Example 5:  a digital input circuit using an optocoupler,
useful when the electrical separation of two interconnected circuits is needed for safety reasons.

For an effective isolation the two devices must not share any signal/voltage (including ground).

Optocoupled output

Example 6:  an optocoupled digital output circuit.
This circuit can be used to drive low-voltage devices.
This circuit is inverting: the output goes low when the input goes high.
If the interfacing circuit has its own pull-up resistor just remove the 4.7 kΩ collector resistor.
Do not share any signal or voltage between the left and the right circuits.

An optocupled output, driving a relay (for greater safety)

Example 7: this circuit can be used to drive devices connected to the mains or high-current switching (using a suitable relay).
At the cost of a little more complex circuit you get an interface that can safely drive high-voltage devices.
A medium- or high-power relay may need the use of a medium power NPN transistor.


, , ,