technology, scones, nonsense






Subscribe to my RSS Feed



Welcome to!

Pi Music Box

Posted on Wed 18 May 2016

In this project, I describe how I transformed an inexpensive amplified speaker unit into a network-connected music player by embedding a Raspberry Pi Zero.


Components used:

  • An off-the-shelf amplified speaker unit - I used this one from B&M stores. Not the best audio quality, but I chose it due to the aesthetics (the unit was destined for a children's playroom).

  • Raspberry Pi Zero (hopefully they're in stock!).

  • pHAT DAC from Pimoroni, which gives the Pi Zero a high-quality audio output.

  • WiFi dongle compatible with Raspberry Pi - I used the Official Raspberry Pi one.

  • USB Micro-B adapter (often sold with the Pi Zero) - I used a right-angled one like this since it made it easier to fit in the box.

  • Small length of stereo screened cable. The speaker unit I bought came with an audio lead, which I butchered. If not, you could recycle the cable from a broken pair of headphones.

The speaker unit I chose didn't have a volume control, and I found the software volume control didn't quite give enough, well, control, so I added a hardware volume control using the following:


The following schematic shows how I connected everything together:Schematic


The speaker unit I chose could be powered either by a Mini-USB socket, or 3 AA batteries. AA batteries give 1.5 volts (or 1.2 volts for rechargeable types), so 3 of these connected serially isn't going to power the Pi Zero, since it requires 5 volts. I therefore chose to use the Mini-USB to supply power to the device, which obviously should give 5 volts when connected to an appropriate USB power supply. Remember to check the current requirements when choosing a power supply - the speaker unit should state how much current it takes on it's label, and add an additional 500mA or so for the Pi Zero and WiFi dongle.

If you don't want to be restricted by having the device plugged into a wall outlet, you could look at powering it from a rechargeable USB power bank. This could either be plugged into the back via a USB cable, or embedded inside the device (though you would need to be able to access the power input for recharging).

I disconnected the cables leading to the battery compartment, and found these to be a convenient way to power the Pi Zero since they were also connected to the USB power supply on the board. I soldered these cables directly to the Pi Zero using the pads underneath the Micro-B USB power socket. The positive (red wire) to the pad labelled PP1, and the negative (black wire) to the pad labelled PP6.

Power connections


The audio input to the speaker unit is via a 3.5mm jack socket on the amplifier board. I traced these connections from the socket on the PCB and located convenient points at which to solder the left, right and ground connections to the stereo screened cable. This cable was then routed internally, leading to the audio output connections on the pHAT DAC board. If, like me, you're adding a hardware volume control, these should go via the 10K logarithmic potentiometer (referring to the schematic above).

I connected the pHAT DAC onto the Pi Zero via the GPIO connector (I had to solder the supplied connectors onto each first), and stuck the Pi Zero onto the back of the battery compartment, where it sat just above the amplifier board. I plugged the WiFi dongle into the Pi Zero via the Micro-USB cable, and made sure it all fitted inside the speaker unit.

Audio connections

Volume connections

For the volume control, I drilled a suitable hole in the front of the box to fit the potentiometer shaft, and locked this in place with the supplied nut. The shaft was then cut to length and the knob applied.

Detail of volume knob


I prepared a Micro SD card with the latest Raspbian image - I actually used the "Jessie light" version since the device would be used as a server and didn't require any UI or desktop environment. I found a spare 4GB card I had gave plenty of room, in fact I think 2GB would be OK too, assuming that, like me, you aren't going to be storing any audio files on the device itself, but accessing them from a server via the network. If you intend to store audio files on the device, then you probably need a bigger card.

I booted from the SD card using another Raspberry Pi and entered the required settings to connect to my WiFi network in /etc/wpa_supplicant/wpa_supplicant.conf. Alternatively, you could temporarily plug a monitor and keyboard into the Pi Zero itself to do this. I also changed the hostname from the default "raspberrypi" (using the raspi-config program) so I could identify it easily when it connected to my network.

I then inserted the Micro SD card into the Pi Zero before screwing on the back of the speaker unit and switching it on. After the Pi has booted, you should be able to find the Pi on your network (I use my WiFi router's admin interface which lists connected devices and IP addresses), and connect to it using SSH (username "pi" and password "raspberry" by default - but I'd recommend you change this).

I followed the excellent instructions on the Pimoroni website to setup the pHAT DAC (ignoring their advice and setting my speaker volume to maximum!)

I added a line in /etc/fstab to mount my music collection on a server machine. I used NFS4 for this, which required the package "nfs-common". You could also use samba, if your server is of the Windows variety.

I then installed Mopidy, which is a music server which supports the protocol used by MPD (Music Player Daemon), which has a wide range of client support.

Refer to the Mopidy website for configuration details. For reference, my /etc/mopidy/mopidy.conf file looks like:

config_file = /etc/mopidy/logging.conf
debug_file = /var/log/mopidy/mopidy-debug.log

enabled = true
data_dir = /var/lib/mopidy/local
media_dir = /mnt/server/music
playlists_dir = /var/lib/mopidy/playlists

mixer = software
mixer_volume = 5
output = autoaudiosink

enabled = true
hostname = ::
port = 6600

(where /mnt/server/music is the location of my music on my mounted server)

Then enable and start the Mopidy server with:

$ sudo systemctl enable mopidy
$ sudo systemctl start mopidy

Which should start it now, and cause it to start automatically on boot.

I then used the command:

$ sudo mopidyctl local scan

To perform a scan of the available music. This can take a while, depending on how many files you have (and how fast your WiFi network is).

Now to actually play something. For testing, I installed ncmpcpp on the Pi itself. ncmpcpp stands for NCurses Music Player Client (Plus Plus) - a name that I find almost impossible to remember, let alone type! This presents a console mode UI (perfect for use over SSH).

For something a bit more user friendly, my favourite client is MPDroid which runs on Android devices, and has an interface which is quite like the default Android music player, so most people should find it fairly intuitive.

Music box in use with Android tablet

I'm still experimenting with the software really - there's certainly more that Mopidy can do that I haven't yet explored, such as streaming from online music services and podcasts, and there's plenty more software available to try. The possibilities are endless!

Hedgehog camera

Posted on Mon 09 May 2016

I recently built a Raspberry Pi-powered hedgehog camera to view hedgehogs and other wildlife in my garden.



Raspberry Pi

I actually tried a Pi 1 model A+ and a Pi 3 model B, and found no great difference in performance in this role, so I stuck with the A+ since it consumes less power and is smaller. You should be able to use any model which has the camera connector (all except the original Pi Zero).


I used a Raspberry Pi night vision camera - a 5MP infra-red camera module with IR LEDs - perfect for taking pictures at night, and also works well during the day.


WiFi dongle

I used a Long-range WiFi dongle with antenna - this allows the device to get a WiFi signal at the end of my garden so I can connect in.


After looking at various (very expensive) waterproof boxes, I opted for one from the supermarket - the kind used to store food. The ones with the rubber seal are very waterproof and should protect the electronics against adverse weather conditions.

I drilled a hole at the front of the box to allow the camera lens to fit through (although I left the LEDs inside). I placed blu-tack around the hole on the inside to prevent water ingress.

I also made an adapter which allows the box to be mounted on a tripod, although this probably isn't really necessary since it is fairly easy to position the box without this. I made this with a sheet of aluminium that fit into the bottom of the box, onto which I glued (using epoxy) a nut with the appropriate thread for a standard tripod (1/4-20 UNC). I drilled a hole in the aluminium sheet under the nut, and in the bottom of the box to allow the tripod thread to enter.


I had originally intended to power the device using a large rechargeable power pack I had (10400mAh). This was able to power the Pi A+ and the WiFi dongle just fine for over a day. However, I found that the IR LEDs on the camera module were particularly power-hungry, and the battery would only last around 4-5 hours with those plugged in. I therefore decided to allow the device to be powered from the mains if required, so I made a small hole in the lip of the box near the plastic seal, to allow a USB cable to enter. I then ran this to a mains adapter placed in an appropriate water-resistant location.


I started off by installing Raspbian on a 8GB MicroSD card, and enabled the camera from raspi-config.


I installed pi-timolo - which stands for Pi Time lapse, Motion detect, Low light. This is an excellent and very configurable program which allows you to take pictures automatically when motion is detected, and/or at regular intervals.

VNC Server

I found the preview version of RealVNC Server for Raspberry Pi to be very useful in setting up the system, since it allows you to connect in and view the live camera image (by running the raspivid command). This allowed me to connect to the device in-situ from a tablet and focus the camera (by turning the lens on the front). This is shown in the following screenshot, which is actually taken on my desktop machine running VNC Viewer:

VNC Screenshot

Here I'm using the raspivid command to show a live preview from the camera:

$ sudo raspivid -t 0 -rot 180 -f

Run "raspivid --help" for a full list of the available options here. The "-t 0" option means to show the preview indefinitely, so use Ctrl-c to stop it when done. Only one application can access the camera at one time, so you must stop raspivid before running pi-timolo, and vice-versa. Note that I used "-rot 180" to rotate the image since I actually mounted the camera upside down due to the cable orientation! Pi-timolo has a similar option (flip horizontal and vertical) to achieve this.


I found ffmpeg very useful for converting series of timelapse or motion-detection images into a single video file. An example command to do this is:

$ ffmpeg -f image2 -pattern_type glob -i '*.jpg' out.mp4

Use it

I placed the device in a suitable position at the end of my garden, where I had seen hedgehogs in the past. I then powered up the device and made sure the camera was focussed. I also put down some hedgehog food and a dish of water to try and draw in any wandering hedgehogs. I then stated pi-timolo and left it to run overnight!


The following video shows the result of my first attempt at filming hedgehogs:

Please visit the excellent Hedgehog Street website for more information about hedgehogs in the UK and how you can help to support them in your garden.

Introducing Calcul8r

Posted on Fri 12 Feb 2016 is a new website I've been developing.

It began as an experiment in HTML5 and compiled JavaScript, but has evolved into something vaguely useful.

It's basically a calculator. It has a text box into which you type mathematical expressions in a fairly intuitive form, press enter and it gives you the answer - what could be simpler?


The calculations are performed on your local machine, using JavaScript. But it's not quite as simple as that, because it actually uses the GNU Multiple Precision Arithmetic Library compiled into JavaScript, to perform the calculations. This allows it to support high precision floating point numbers, and very large integers.

The expression engine uses SconeScript, from my Sconeserver project, also compiled into JavaScript.

You can optionally sign up for an account, in which case it will save your history, allowing you to refer back to previous calculations, across multiple devices. It should even work on mobile devices, so long as they use a web browser that supports HTML5.

So go on, give it a go, and let me know what you think!

Let's Encrypt

Posted on Mon 11 Jan 2016

Yes, let's.

This site now has a valid SSL certificate, thanks to Let's Encrypt.

So you can now use if you want to access this site using a secure connection.

Raspberry Pi Clock

Posted on Mon 05 Oct 2015

I've been trying to come up with an interesting use for the Pimoroni Unicorn Hat I bought earlier this year.

The Unicorn Hat is an extremely bright 8x8 RGB LED matrix, which fits neatly onto a Raspberry Pi. It's actually only supposed to work with the newer model Raspberry Pi's (B+ and newer), which support the new HAT Specification. However, I managed to get it to work with an old model B I had lying around by simply removing the composite socket.

I wrote some Python code using Pimoroni's Unicorn Hat library and the Python Imaging Library, which assembles scrolling messages using text and images in a simple text format. Then I developed a series of scripts to generate various messages for the system to scroll. These include the time, date, weather conditions and forecast, moon phase, sunrise and sunset times (with a bit of help from the excellent PyEphem library).

My code is available in github as Unicorn Scroller.

Here's a video of it in action:

Robot obstacle course

Posted on Mon 02 Mar 2015

So, I finally got around to making my robot move, and allow the motor speeds to be controlled from a Raspberry Pi via I2C.

What better way to test it out than attempting to obstacle course at the Raspberry Pi 3rd birthday party event at Cambridge Computer Lab:

It didn't quite make it over the lumpy blocks in the middle, so I had to give it a helping nudge at that point!

Wall-mounted Christmas tree

Posted on Wed 17 Dec 2014

SketchUp rendering and finished tree with and without decorations

This year, I really needed a child-proof Christmas tree, so I had the idea of making one out of cardboard and mounting it on the wall.

Traditional free-standing Christmas trees are often unstable, and are particularly dangerous for children, who can end up toppling them over quite easily. They can also take up a lot of room and drop needles everywhere!

This design solves these problems by essentially having half a tree (with a regular tree, you can't see the 'back-bit' anyway), and fixing it to the wall, providing stability. Being made of cardboard, it doesn't have needles to drop, and it's also fun to make! If I had more time (and cardboard), I'd like to make more of these and put them all around the room.

Making the tree

It was relatively simple to make, using pieces of cardboard cut from a large sturdy cardboard box. I drew up some plans using SketchUp, drawing out the pieces I needed and worked out how big I could make these to maximize the use of the available cardboard.

My tree ended up at approximately 60cm wide and 120cm tall, and it used the following pieces:

I painted the pieces appropriately, and fitted them together. The three half-tree pieces slot into the two semi-circular pieces, and then they are held together with two cable ties, each passing through the back piece and through all three half-tree pieces. The trunk/pot section is stuck onto the bottom of the back piece, and the whole thing can then be stuck on the wall (I used removable sticky pads for this to avoid ruining the wall).

You can download the SketchUp File here.

Fruit Loaf

Posted on Sun 09 Feb 2014

Today I made my first fruit loaf. It was really just an experiment as I had some bread dough left over so I thought I'd simply try adding some fruit (dates and raisins). It came out really well and tastes great. Perhaps I will try adding some nuts next time.

Updated on 2017-04-13 13:17:26 +0000 | Requested on 2017-04-30 20:30:00 +0000