Microbit-controlled radio / MP3 player

Here’s a quick project showing the power of David Whale’s bitio Python library. This gives Python on a Raspberry Pi, Mac or Windows PC access to the physical sensors on the BBC micro:bit. You can control a program on your computer using the buttons, touch and accelerometer sensors on the micro:bit, and use the micro:bit’s display too.

My default position with any bit of tech is: can I make a radio with it? And here the answer was yes I can, and in only about 10 minutes too!

This program allows me to change stations up and down using the A and B buttons on the micro:bit, increase the volume by titling to the right, lower the volume by tilting to the left and you can exit the program by touching the GND and 0 pins on the micro:bit. It could just as easily work as an mp3 player, it doesn’t have to play internet radio streams. The micro:bit displays the playlist number on the screen.

If you fancy trying this out, here’s what you need:

  • A computer such as a Raspberry Pi with Python installed.
  • Also the computer needs an audio player installing and configuring with some radio station URLs or mp3 files. I use mpc/mpd on the Raspberry Pi – see below.
  • Download bitio on the computer.
  • A BBC micro:bit and USB lead.
  • Some way of hearing the audio, through a TV or headphones or powered speakers plugged into the computer.

First install mpc and mpd which I use on the Raspberry Pi to play radio streams or mp3 files:

sudo apt-get install mpc mpd

(On a Mac you could use the afplay command or VLC on any computer to do the same thing).

If you’ve not used mpc before, open the mpd config file with nano like this:
sudo nano /etc/mpd.conf

Then edit these lines to remove some # signs (uncomment) and change the mixer_type from hardware to software so it looks like this:

audio_output {
type "alsa"
name "My ALSA Device"
device "hw:0,0" # optional
mixer_type "software" # optional
mixer_device "default" # optional
mixer_control "PCM" # optional
mixer_index "0" # optional
}

Now add some radio station URLs using the mpc add command. I added BBC 6music, BBC World Service News, FIP and Radio 4. For example to add FIP I typed
mpc add http://chai5she.cdn.dvmr.fr/fip-midfi.mp3

You can find BBC radio URLs on my big list here.

You can add audio files instead of radio stations if you like, to make an MP3 player.

I added just 4 stations but you can add more or fewer, just tweak the if stationNumber < and > lines accordingly. They get added in order but you can check your playlist of stations or MP3 files by typing
mpc playlist

And you can change the order using the move command, for example to move station 4 to preset 2 just type:
mpc move 4 2

I then downloaded the bitio files as a ZIP, extracted them and put the Python program below in the same folder as the examples and ran it using IDLE - you could also run it from the command line. It will display 'radio rocks!' on the micro:bit display and then start playing station number 1. Use the A and B buttons to change channel, tilt left and right to control volume, touch pin 0 to exit the program.

The first time you use a micro:bit with bitio you need to drag a special .HEX file to the micro:bit and follow some steps to locate the device - this just involves unplugging and replugging it, and then it remembers your device while you are using it.

If you use this or have ideas for improving it, please let me know!

# micro:bit-controlled radio by Giles Booth @blogmywiki
# Requires bitio https://github.com/whaleygeek/bitio
# Also requires mpc/mpd installing and some audio files or URLs adding

import microbit
import time
import os

stationNumber = 1

running = True

microbit.display.scroll("radio rocks!")
os.system("mpc play 1")
microbit.display.show("1")

while running:
    x = microbit.accelerometer.get_x()

    if x < -300:
        print("left - volume down")
        os.system("mpc volume -10")
    elif x > 300:
        os.system("mpc volume +10")
        print("right - volume up")

    if microbit.button_a.was_pressed():
        print("Button A pressed - station down")
        stationNumber -= 1
        if stationNumber <1:
            stationNumber = 4
        os.system("mpc play "+str(stationNumber))
        microbit.display.show(str(stationNumber))     

    if microbit.button_b.was_pressed():
        print("Button B pressed - station up")
        stationNumber += 1
        if stationNumber >4:
            stationNumber = 1
        os.system("mpc play "+str(stationNumber))
        # you could use 'mpc next' if you don't know how many tracks/URLs are loaded
        microbit.display.show(str(stationNumber))

    if microbit.pin0.is_touched():
        print("Pin 0 touched")
        microbit.display.scroll("bye")
        running = False

    time.sleep(0.5)

os.system("mpc stop")
This entry was posted in Raspberry Pi and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>