Thursday, January 31, 2013

Interfacing with Arduino

As I explained in my previous posts, the hardware of the radio is actually connected to an Arduino Uno.

Arduino is an open source hardware/software platform based on the Atmel AVR micro controllers.


My Arduino is of the Uno model:



My radio has an on/off button, a volume control pot and a channel selector (SW band selector). I have designated these as inputs to the Arduino. Then my program running on the micro processor reads the inputs and also communicated with the Raspberry Pi.

The link between the Arduino and the Raspberry Pi is based on the I2C protocol, as I explained in my previous post. The Arduino software library, http://arduino.cc/en/Reference/Libraries, contains calls for communicating using I2C protocol. In this configuration, Raspberry Pi is the master and Arduino is the slave. That is, Raspberry Pi always initiates the communication link.

As per I2C, Raspberry Pi sends the address of my Arduino program on the hardware interface and the Arduino library allows me to respond to this reqeuest and send some data back.

So the role of the Arduino program is to continuously gather data from the radio controls, volume, on/off switch etc, and provide their state to the Raspberry Pi whenever a request is made.


I am including the code for the Arduino at this site on Source Forge

https://sourceforge.net/projects/r-pi-radio/files/

There, Arduino.zip contains the C++ code that runs on the Arduino.

Notable details:
There is a data structure called HardwareState which represents the data made available from all sensors on the radio. I have split the volume control value to two bytes. Also the cookie Low and High contain my initials. This data structure is updated every 10ms.

class HardwareState
{
private :
unsigned char m_size;
unsigned char m_onOff;
unsigned char m_channelValue;
unsigned char m_volumeLow;
unsigned char m_volumeHigh;
unsigned char m_cookieLow;
unsigned char m_cookieHigh;


When the I2C master (the Raspberry Pi) requests data, the data structure is sent on the I2C bus:


void onI2cRequest()
{
    Wire.write((unsigned char*)&state, sizeof(state)); 
    lastConnect = masterConnected;
}


There is also an output pin which is connected to a transistor which switches a bright white LED that is used to illuminate the radio display. If the I2C master does not connect for more than a second, the Arduino program assumes that the Raspberry Pi is not active and blinks the LED as a visual clue to the user. For example, when powered up for the first time, the LED starts blinking until Linux boots up and starts running the program on the Raspberry Pi.






Saturday, January 26, 2013

Design of the Raspberry Pi Radio

Design of the Raspberry Pi Radio


In this installment I will try and outline the main components that take part in the Raspberry Pi radio.

Of course the most significant part is the Raspberry Pi. This is responsible for the actual radio playback, streaming down and so on. But there are some other components that make it all possible.

Hardware
One of my requirements was to use the existing controls on the radio. I wanted to use the existing volume control (a potentiometer), the on/off switch and the channel selection controls. In the future I may bring more and more radio controls into play.

On the left hand side, there is a band selection knob. This was used to switch between SW bands, 6 of them. I wanted to use this as a channel selection control.

SW Band Selection - also volume control in view

19m to 49m, short waves


When I examined the channel selector, I realised that I could represent the 6 states it has by using 3 bit digital inputs.

The second input I wanted to use was the On/Off button at the front of the radio. Rather than cutting the power supply, this will now function like a mute button. The circuits are not being shut down but this will stop the playback (and more).

The On Off Switch



So I have the following controls available to me:
  • An On/Off (Ein/Aus) switch
  • A 10K Potentiometer for Volume
  • The channel selection knob.


The On/Off is another single bit digital input.

I decided to A/D sample the volume control to control the actual volume of the radio. Later I will explain the attractiveness of this.

In order to achieve all of these objectives, I decided to use an Arduino Uno development board. Arduino is an excellent open source microcontroller development system. The microcontroller is Atmel AVR. More details can be found here: http://arduino.cc/en/

The board I used has sufficient I/O pins to input the channel selection, on/off switch and an A/D input for volume control.

Arduino Uno - very much recommended

Writing the C++ code for Arduino was a lot of fun. The Arduino system, both software and hardware, works really well and I can say that pretty much everything worked straight out.

The next hurdle was to connect the Ardunio to the Raspberry Pi. Both systems support the I2C protocol. I2C protocol is a serial two wire protocol which allows different chips talk to each other over short distances. Arduino platform supports I2C and Raspberry Pi supports that too, with some tweaks.

The main problem on the I2C front was the voltage levels. The IO pins of Raspberry Pi operate with 3.3V logic. The Ardiuno works on 5V. If I tried to connect them directly, that would most definitely fry the Raspberry Pi.

So I used a bi-directional level shifter. This is a very simple but very clever piece of circuit that can translate the logic levels of the systems back and forth.

You can make your own bi-directional level shifter but that might require eyeballs size of watermelons. The surface mounting tiny components were too small for me to work with so I actually purchased one that was made by some robot.

It was clear that I needed an audio power amplifier. Raspberry Pi has an audio output jack but that would not be able to drive a loudspeaker. So I decided to build a simple audio power amplifier. I used an LM386; a very popular low power audio amplifier. That, again, worked pretty well without much effort.

LM386 - It works


Finally, I designed a power supply to power the entire system using mains without any need for any power adaptors and so on.

Here is a view of the built radio:



Software
OK, I am using the official Raspberry Pi Raspbian as the Operating System.

First and foremost I needed a radio player program. The excellent Media Player Daemon (MPD) is an excellent choice. This supports a very large number of formats. Because it is a Linux daemon it runs in the background and you can write programs (MPD clients) to control the playback, playlists.
http://mpd.wikia.com/wiki/Music_Player_Daemon_Wiki


So I wrote a C++ program, the radio controller, which talks to Arduino on the I2C protocol and controls the MPD using the MPD protocol. MPD Protocol is a socket/text based protocol.

Typically, my program reads the inputs from the radio controller, such as volume level, channel selection and convert these to commands to issue to the Media Player Daemon. So when I twist the channel selection, this translates to a digital input to the Arduino and this is then picked up by my radio controller software and this issues the correct playlist control commands to MPD.

MPD starts up when the Raspberry Pi boots up and so does my radio controller program.

Conclusion

Well there is no conclusion yet. My aim today was to outline the top level design. I would like to go into more detail in all of the points discussed and hopefully this will be something I will be doing in the following days/weeks.







Thursday, January 24, 2013

My Raspberry Pi Radio Project

My Raspberry Pi Radio Project


When I was a kid, long before the advent of computers, digital media and so on, there was radio. When I was a child, we had a single channel television but I probably learned more things from the radio than the television.

I remember that when I was getting ready for school in the mornings, our radio would be on. I would listen to it when revising in the evenings.

The actual physical radio we had was a "Grundig Satellit Transistor 5000". This must have been an expensive piece of equipment at the time. This radio featured long wave, short wave, medium wave and even FM.

Here is a page that features some information on the exact model:

http://www.radiocorner.de/candies/satellit1/205/

And this is the picture from this chap's web site, featuring the "5000".

A pristine specimen of 1960s engineering from an enthusiast



Then things changed; my sisters and I moved out of our parents' home. I got involved with all sorts of things life had to throw at me and I fell out with our radio. The Grundig fell out of fashion and eventually ended up on a shelf (in my uncles home!) for at least 20-25 years.

But, I never forgot this radio. So last summer I asked my father and we found out where it was. I brought it to the UK. It is not light; it is a serious piece of equipment with a lot of wires, metal bits and a wooden case!

My aim was to replace the innards of the Grundig with a modern Internet radio. And what better hardware to construct a radio than the Raspberry Pi.

Here in the UK, there are hardly any medium wave stations left. The FM will be replaced with DAB in the future. Clearly the radio had no future pupose.

So the first thing I did was to open the case, admire the German engineering of 1960s for a while and remove almost all of the components. I left the knobs and the loudspeaker but everything had to go.

First day in the UK - A lovely summer's day

Some readers may think that I ruined the radio by removing everything inside and that it is no longer a Grundig. But my aim was not to conserve an antique radio sitting idle on a shelf getting more and more obsolete every year. I believe that by converting the radio to an internet radio, I gave it a new fresh lease of life and purpose. As the web site above proves, there are genuine enthusiasts around the world and I am sure those specimens will eventually find their ways to museums.

Innards...

Above, my radio when I first opened the case after bringing to the UK.

Side note: as I removed the bits in pieces, it became clear that there were at least 4 distinct receivers inside.Especially the short wave receiver was very delicate.


My New Lovely Radio

I spent the next 6 months rebuilding the radio. So where am I now? I am writing this whilst listening to my new radio.

Right now, I am listening to a stream from my country of birth. Probably similar sort of music that we have been accustomed to listen on this radio. You would not be able to tell if this radio was an internet radio just by looking at it.

I started this blog to talk about my Raspberry Pi adventures and my Grundig / Raspberry Pi radio is my first adventure. Hopefully over the next few weeks I will document all I did, how I used custom hardware to manage the radio controls and so on. Perhaps this can inspire others.



Wednesday, January 23, 2013

Beginning is half finishing...

Desperate House Geek
Why the title?
Well, I am a software developer, with a well respected IT job in London.

I commute to London one day a week and the rest of the week I work from home and some of you may already know this that working from home can be hard. Especially after the first five years.

I have a large number of responsibilities at my day job but in my heart I still think that I am a software developer.

Lately, I have been taking a great interest in the Raspberry Pi. The extremely affordable computer.

http://www.raspberrypi.org/faqs




There is something innocent about the Pi. It takes me to my younger days when computing was my greatest fascination ever. I remember how I would be completely lost in the detail. I remember my first computer, a Casio PB300.






Yes this was the first computer I actually owned. It is barely bigger than a hand calculator. I remember that my father was very disappointed with this tiny device. He thought a computer should be bulkier.

Over the years I have had many other computers, including Sinclairs, PCs. There are 12 PCs in this household right now. I spent most of my life working with PCs and I guess I enjoyed most of it :) And my fascination with computers was always there.

But over the years my relationship with my childhood fascination turned first into my craft then to my work. I am sure many professional software developers have similar experiences.

So when I heard about the Raspberry Pi, I was very anxious to get my hands on to one. Although I was one of the very first people to place an order I still ended up waiting for a long time. Now they are available for everyone and I already have 4 devices here anyway.

I love my Raspberry Pi. It takes me to my childhood. Amazingly I can still lose myself in that world. I still feel the excitement in running a compiler, starting an app I wrote, that sort of thing. The developers will understand.

Anyway, I am not sure if I am a good writer. Maybe it is too pretentious. Besides English is not my first language. My writings: to me, they sound like the mutterings of a geek. A desperate geek! If so, at least I am getting my message across :)

Over the next few days, weeks, whatever I will try and write about Raspberry Pi, feeding the geek within and general nonsense like that...