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
(NB: as of Feb 2020 Fip is at http://icecast.radiofrance.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")
Hi, I follow your tutorial and saved the mp3 files in Mpc playlist.
I ran the code and every thing is good except there is one bug.
If we just do nothing and let the song finish playing, mpc will jump to the next song.
The problem is the number of song on microbic will not change if we let the song finish, it will remain the same number on the led screen.
I tried to change code but it did not work.
can you give me solution, thanks
I tried to create one variable to store the variable ‘stationNumber’
If the stationNumber changes(means the position of songs on mpc changes), the variable will also set equal to stationNumber.
But the problem is that the python does not know the stationNumber changes on Mpc. So it can’t work.
Hello Giles,
Firstly, well done with the radio!
I was wondering, can the bitio work with bluetooth, instead of cable?
Thomas
Hello Giles,
I’m messaging again, since the bluetooth question. The reason it came up, it it was a workaround for an adapter not working (Pi-Dock microbit to pi adaptor using GPIO pins, https://thepihut.com/products/micro-bit-to-raspberry-pi-adaptor-horizontal). Have you tried this adaptor? I get power to microbit and io appears, but it’s not recognised as a new device.
Thanks!
Thomas
Hi Thomas – thank you for getting in touch. I’ve not tried that Raspberry Pi adaptor, though it’s an intriguing idea adding a Raspberry Pi as an accessory to a micro:bit ;)
I have no idea if bitio can work with Bluetooth – its author David Whale may be able to help! https://github.com/whaleygeek
best wishes and good luck
Giles
hi please i connect my microbit v2 to my mp3 player and i want to listen to my mp3 file by the microbit speaker and not an extern speaker
(in fact i succeeded to listen by extern speaker connected to my mp3 player and microbit)
help please
Hi Naim – the speaker in the micro:bit V2 can only play sounds from the micro:bit itself, but not from an external MP3 player.
best wishes and good luck,
Giles