I’ve already created a slew of sound based Micro:bit projects:

BBC Micro:bit – Light Theremin

BBC Micro:bit – ADVANCED Light Theremin

BBC Micro:bit – Atari Punk Console

BBC Micro:bit – 8-Button Melody Synthesizer

BBC Micro:bit – 16-button True-Tone Sound Sequencer & Synthesizer

BBC Micro:bit – Accelerometer Tone Adjuster and MicroSynth Case

BBC Micro:bit – Accelerometer Theremin

 

One thought always popped up. How can I play a pre-recorded sound file? Use actual drum beats. Record voice. ETC. In this project, I set out to do just that, play back a pre-recorded sound file.

 

A surprise to me, there is about 30k of storage space within the file system. MicroPython allows for data storage inside that space. In my case, I wanted to store a sound file in there and play it back.

 

I chose WAV files for no particular reason. I suppose I thought they were a standard, license-free audio storage option. But, in retrospect, I should have picked something else. Something more compressed.

 

Due to the 30k limit, which seemed more like 20-25k, I couldn’t store that much sound. For 30k of file system size and 1 sample == 1 byte: 30*1024/7812.5 = 3.9s of sound. In the end, I think I was only able to get about 2.5s or less. But it did work!

 

The software works as follows:

- Load the playback code onto the Micro:bit

- Use a Raspberry Pi or a PC for the WAV file encoding and transfer of the WAV file into the file system storage.

- After it’s done copying, press the Micro:bit reset button and plug in some sort of speaker.

- Press the “A” button on the Micro:bit and the sound will play.

 

 

First, let’s gather all the necessary components you’ll need to build this project.

1x BBC Micro:bit

1x 3.5mm Audio Jack

1x 5v 2A power supply

 

Optional:

Laser cutter to cut the case

 

Video demonstration of the project:

 

STEP 1

Load the code onto the Micro:bit.

I am providing the hex file you need to drag and drop onto the Micro:bit, and the python code as two separate files. For the below process, just use the hex file.

Plug the MicroUSB cable into the Micro:bit, and plug the other end of the cable to a PC or MAC.

At this point, you are going to copy over the code to the Micro:bit. I am providing the program (code) in this post that needs to be copied over. When the Micro:bit plugs into a computer, it shows up as a USB flash drive. All you have to do is copy the file over to the Micro:bit, like it’s a USB Flash Drive, and the Micro:bit will reset, and the program is active.

You can remove the Micro:bit from the computer at this point. The program will start running, and without the rest of the circuit, it will not function.

A little bit about the code:

"import audio" is a module that allows the Micro:bit to play sounds from a speaker attached to it. It's key here in playing the file back.

After the WAV file is converted to raw 8-bit audio this bit of code allows the Micro:bit to play it back.

 

STEP 2

 

Wire up the Micro:bit as in this simple schematic.

If you built the previous two projects, then you are good to go!

 

 

STEP 3

Get a computer of some sort setup for interfacing with the Micro:bit.

 

In my case, I started with a Windows 7 based PC. I was able to just about everything with it. But I had trouble with processing the WAV file.

 

I switched to using a Raspberry Pi to do everything with the transfer of the WAV file. If you are curious which Raspberry Pi I used, I used an original Model B. The first one! I misplaced all my others. The last time I used my Raspberry Pi 3 was for the Scary Doll Halloween Project! It must be in storage.

 

I recommend a Pi 3 for the speed of bootup/use.

 

STEP 4

A little bit of setup for the computer or Pi you are using for file transfer. 

 

- To get access for the Micro:bit’s local file system, install the microFs module.

From the command prompt, type:   pip install microfs

 

- You must have “ffmpeg” installed. If you are using the Pi, then it should already be there.

Check to see what version ffmpeg you have installed.

From the command prompt, type:  ffmpeg --version

If it isn’t installed type from the prompt:   sudo apt-get install ffmpeg

 

STEP 5

On the PC or Raspberry Pi:

 

Take your WAV file and place in into a directory, call it “wav.” It isn’t necessary to call it anything in particular.

Also, place the python file “file_uploader.py” into the same directory. This way, you will not have to type the full path for the python file or WAV file.

From the prompt, move to that directory.

 

Once in there, you are ready to convert and copy over the WAV file. For practice, use the wav file in the zip called “microbit.wav.” That is me saying “Microbit!”

From the command prompt, type:   python file_uploader.py microbit.wav

 

You will see ffmpeg encoding and other processing steps. It is converting the WAV file to an 8-bit unsigned raw audio data sample. In this case, the file is called “output.raw.”

At the end of the program’s readout, you will see “uploading output.raw.”

 

If it worked, then it will say, “uploaded: output.raw.”

 

This is a screenshot from the Raspberry Pi after I loaded me saying "Microbit" into the file space. (Image credit: me!)

 

The two example WAV files in the zip should work just fine. But if you go on to copy over your own WAV files, you may experience some errors. Like I said, I found most audio samples under 2.5 seconds long copy over OK.

 

STEP 6

Playback

 

- Plug in powered speakers to the 3.5mm jack.

- Power it with a 5V microUSB cable from either a PC, outlet or battery. If you have it connected to the Pi, or whatever, it is already powered.

- Flip the Micro:bit over and press the “reset” button. Now the Micro:bit is ready to play the audio.

- On the front side, press the “A” button and you should hear the audio play.

 

 

Conclusion:

I was very happy when this worked. It’s amazing that an audio file can play back on a device not originally setup to do so.

 

However, the quality of sound isn’t the best. Or, perhaps it was the speakers I was using for playback. I used the speakers built into my monitor. I never used them for anything else before, so not sure of the fidelity. Perhaps an audio smoothing circuit could be used? When sound starts and stops playing, the speakers pop. So, some sort of conditioning circuit should be used.

 

I tried putting a single beat sound onto the Micro:bit. I think I could put a few different beat sound into the 30k and use a sequencer to make my own beats, I suppose.

 

Also, I would love to interface the Micro:bit to an SD card and store/playback much larger audio files. Perhaps I will look into this.

 

Message me at: cabe(at)element14(dot)com

http://twitter.com/Cabe_Atwell