Tag Archives: python

(Yet Another) Raspberry Pi Retro Radio

8 Oct

Putting a raspberry pi into an old radio from the 60s had been on my project list for quite a while. Having a dedicated internet radio player inside a beautiful old case and with all the original controls connected would have been so awesome. I was hence very excited when I finally got around to buy a used 60s radio, settling for a “Löwe Opta Magnet 4725W“. Unfortunately, it turned out that the old radio I bought from a nice lady nearby still works, despite having been advertised as “broken”. Buggers, as I can’t really bring myself to destroy something that is still functioning perfectly fine.

The Löwe Opta Magnet 4725W – High Tech from the 60s.

However, there are alternatives to breaking it, especially as it features a line-in connector on the backside, which was originally used to connect record players or tape recorders. After some reading on the web and buying the right cable I tested connecting the radio to the audio jack of a Raspberry Pi 1, which worked well enough for me to stop investigating any further.

Next question was: how to control the radio? Some earlier experiments with potentiometers for controlling volume and selecting channels had already convinced me that this was the direction to take, but also showed that rotary encoders would probably the better choice due to their comparably greater range, and also have the advantage of (kind of) working out of the box with a Raspberry Pi (i.e. without involving any further chips that convert the analogue readings of the potentiometers to a digital signal).

An earlier prototype with potentiometers. Note the DAC chip to read the analogue signals on the RPi.

As rotary encoders come in different flavours and are often not well documented (at least for laypersons), I tried to find a model that reportedly worked and ended up getting some KY-040s that already had most of the circuitry on board. Apart of GND and power, only two pins need to be connected to the Raspberry Pi’s GPIO as inputs: one Clock pin, and one Data pin.

The read the rotary encoder from software, you basically need to do two things: 1) listen to the input of the Clock-Pin, and when you detect that it is getting from HIGH to LOW, 2) read the Data-Pin to figure out if the knob was turned clockwise (HIGH) or counter-clockwise (LOW). Here is a simple example on how this works in Python. You can read up further on KY-040s and how to connect them to a computer here (the example is about an Arduino).

For playing web radio streams, I used the popular Music Player Demon (MPD) which is easy to install (as in “apt-get install mpd”) and quite versatile with regard to its use. I then wrote some Python code that uses a library to control the MPD based on a playlist containing all the radio channels I like to listen to. After figuring out how to control the MPD from Python and combining that with the code for reading the rotary encoder, I can now use a knob to switch between channels on my radio; for volume, I can just use the controls of the original radio.

You can find the code for the radio here, in case you are interested: https://bitbucket.org/snippets/boden/ppRAdG. The principle of reading the rotary encoder is similar to the example above, with some small tweaks. The rest is just code for controlling the MPD (including making sure the button works even when another MPD client is used in parallel), and for cleaning up the GPIOs when the Raspberry Pi is shut down. The music playlist itself needs to be configured in MPD, for which several clients are available on a variety of platforms (so as a nice side-effect, the radio can also be controlled easily from the smartphone or any other computer on the network).

The control knob for switching the channels –  already covering dust like the radio it controls.

So far, the rotary encoder is sitting in a plain paper box next to the Raspberry Pi. Next steps would be to get a nicer case for it or potentially putting everything right into the radio itself. We will see.


Raspberry Pi 2 Weatherstation

10 Jul

I got a Raspberry Pi 2 for my birthday and, as a small evening project, ported my Arduino Weatherstation project to this new platform.


The setup is relatively simple: I used the Adafruit Python DHT Sensor Library to read the sensor values from the GPIO, logging the resulting values with a timestamp to a CSV file. Another python script then reads this files and creates a graph using the Matplotlib library (which can be installed easily on the RPi with sudo apt-get install python-matplotlib).


Both scripts are called every five minutes  by crontab, and the resulting image is displayed on a webserver (I used nginx, which is also quite easy to install).

Next steps would be to log to a database instead of a file, and implement different visualizations over different time intervals.