Archive for the 'Bricolage' Category

DIY: Clean Raspberry shutdown for RuneAudio

Wednesday, January 16th, 2019

I made a second RuneAudio player this weekend.. The main difference, apart from the physical box, is power management.

On my first setup, toggling the switch abruptly cuts power to the Raspberry, which can cause filesystem corruptions on the MicroSD card (and experience shows, in just ten days, that the MPD database sometimes ends up corrupted).

So I wanted a system that would allow me to turn the Raspberry on by switching the switch, and turn it off cleanly by re-switching the same switch, and consume zero watt when off.

The simplest solution I have found is a switch capable of controlling two separate circuits, such as this one or that one (look for DPST, “Double pole single throw”).

One of the circuits of this switch will cut power to the power supply and will be setup in parallel with a Raspberry-controlled relay, while the state of the second circuit, connected to one of the Raspberry’s GPIO pins, will be monitored in software, and trigger a clean shutdown.

The wiring diagram

In the diagram above, the main power is on the blue and brown wires; the 5V on red and black; the relay control wire is the orange one, and the second switch’s circuit is in violet.

We’re going to write a small Python script that will start by setting GPIO 5 to output mode, then pull it high, which will close the relay. Then, we’ll configure GPIO 27 as input, then read its state in an infinite loop. Depending on the state, we’ll either continue to wait or start a proper shut down.

# Cat /root/
import RPi.GPIO as GPIO
import os
import time
import sys

GPIO.setmode (GPIO.BCM)

#setup the relay control pin to high, so the relay closes
GPIO.setup (5, GPIO. OUT)
GPIO.output (5, GPIO.HIGH)

#Setup the switch monitoring pin
GPIO.setup (27, GPIO.IN, pull_up_down = GPIO.PUD_UP)

#Monitoring loop
while true:
state = GPIO.input (27)
if state == True:
#Switch is still ON
time.sleep (0.5)
#Switch OFF, trigger shutdown
os.system ("/var/www/command/rune_shutdown poweroff")
os.system ("shutdown -h now")
sys.exit (0)

Two remarks:

  • Depending on your cabling and the type of relay, the GPIO.HIGH may be LOW. Adapt to your setup.
  • We never re-open the relay. This is done automatically, late in the process of the Pi shutdown, and this has the effect of cutting power immediately, so we don’t want to do it earlier.

We’ll now register this script as a Systemd service so that it starts automatically on boot:

# cat /usr/lib/systemd/system/power-control.service
Description=Power control

ExecStart=/usr/bin/python /root/


And finally, we activate and start it:

# systemctl enable power-control 
# systemctl power-control
# systemctl status power-control

The remaining software configuration is basically the same as in this first article: configuring RuneAudio, a network music player. There are also numerous good english-written howtos on the subject on Internet.

A few pictures of the physical setup, just because I like it:


My own homemade BB-8

Monday, February 8th, 2016

After having seen the latest Star Wars movie, I, like numerous other geeks, fell in awe with the BB-8 droid, and, like numerous other geeks, I wanted to try and build one.

So, I started by getting a 300mm plastic ball and another of 180mm diameter (which was more of a 160mm ball according to my ruler…), a pair of motors, and got the electronic parts box out of the cupboard, guessing I’d just start and solve problems as they come instead of spending a lot of time with plans, and getting demoralised half-way.


The start was rather easy: get the useless parts out of the balls, and sand them so the paint would adhere.


Then I made a few calculations to create the main board at the lowest possible place, and placed the motors and wheels at each side.


Going on with the battery holder, even lower, in order to help with stability:


Spoiler alert, this USB smart battery didn’t provide enough current, so later it got replaced with a S2 LiPo battery I use in RC cars.


The fun part began, with a bit of Arduino programming. I used a standard 2.4GHz receiver for control, added a 3-axis gyroscope in order to try and compensate wobbling with the motors.


About everything, laid out. Spoiler alert: the gyro is of no use, because the motors, which are DC motors with reductors, are far from precise enough to compensate anything. Also, even if they were precise, they could compensate pitch (front-to-back wobbling) but not roll (lateral).


After upgrading the battery, the first test was rather promising:

And here came the time for the difficulties: adding the mast:


And the head, with magnets to hold it in place, and caster balls to have it roll freely:


Spoiler alert: the very little caster balls did not roll freely at all, and too much approximation in the magnets’ placement made them touch the body’s ball, making very unpleasant sounds (and also the head had that tendancy to fall):

But still, it was promising. So I started painting.





And after hours of masking, painting, re-masking, re-painting, I arrived at an unperfect, but not too shameful result:


Went on with the head…



But with just the added weight of the two “eyes”, it couldn’t sit on BB-8 more than a couple meters… So I ditched it and re-made it from a styrofoam ball (this time of the correct size), and while I was at it, re-made the magnetic mechanism with better magnets, better caster balls and better measurements.



And the result looks pretty good in my opinion!


It also rolls much better, although there are still a few noises and it can look like BB-8 had a few beers:

It took my three weeks to get to this point. I knew I could make it a month, so I’ve upgraded a few things, mostly so I don’t have to open the body each time I want to power BB-8 on: first, a magnetic switch (don’t use a bicycle odometer’s sensor, it won’t accept so much current and will solder itself the first time), which I can use to power on BB-8 from the outside (after a bit of searching each time):


It’s made out of a bendable piece of metal and a magnet.

You can also see there a pair of relays, which are used to auto-power-off the Arduino and its motor shield after two minutes of RC signal loss; this way, I don’t have to open the body to shut it down either.


And I’ve stuck in a little LiPo battery tester, so that it can scream at me when the LiPo’s starting to be too discharged (LiPos don’t like that, and when LiPos are unhappy, they tend to catch fire if you look at them wrongly, so.)

I now consider my BB-8 complete although it’s far from perfect ! I think it wasn’t the easiest robot to start building robots, but then, I enjoyed making it. Maybe next time I’ll do an R2D2, and although the body work will be harder, the mechanics will be a walk in the park.

After building it I’ve watched other videos from other BB-8 makers, and it is really fun to see how many different designs there are. Some are very simple with an RC car at the bottom of the ball, some roll by direct transmission and have turning performed by a fly wheel; some are holonomous robots with multi-directional wheels, some use indirect transmission like mine, but a flywheel to turn (this guy is good, but also has access to great resources, so I do think I could do much better if I had CNC mills, 3D printers and ACTUALLY GOT PAID TO DO IT), and this 17 years old’s model works exactly like mine, but damn this kid is skilled.

news for few, stuff no-one cares about