One of the things I needed to address before installing a RaspberryPi in my car is how to switch it on and off in a controlled way.
Requirements:
- It must boot when the ignition turns on
- It must shutdown whenever the ignition turns off – to avoid draining the battery
- It must shutdown gracefully without any risk of damage
- There must be no power used when the Pi is switched off – to avoid draining the battery
- The above must be true throughout any ‘normal’ car electrical events. e.g. voltage dip during engine cranking and stalling.
Deciding on a Solution:
I did a fair amount of research on different approaches I could use. In the lists below I have highlighted the chosen solution first so you can skip the other info if you want to.
A – Booting Up
I needed to ensure that the Pi would boot consistently each time the key switched to the ignition position. I was able to find 4 ways to boot the RaspberryPi:
- [CHOSEN SOLUTION] Applying power to the Power USB port. If power is completely switched off to the Pi then applying power to the Power USB port will cause the Pi to immediately boot. This approach also means that no power can be consumed by the Pi in the off state as it is completely isolated.
- Apply power to the 5V rail via the GPIO pins. In the same was as option one, if power is applied directly to the 5V rail then the Pi will immediately boot. Whilst this solution is attractive due to its avoidance of needing a dedicated USB cable, it’s notable downside is that this method bypasses all the in-built protection circuitry. You can view the schematics on the RaspberryPi site.
- Driving GPIO3 (Physical pin 5) low. When the Pi is in a shutdown state with power applied to either the USB or 5V rail directly, driving GPIO3 low (momentarily connecting it to ground) causes the Pi to boot. I haven’t been able to find any official documentation for this feature. There are two notable limitations to this method. First, enabling the gpio-poweroff overlay, explained below, prevents this method from working. Secondly, GPIO is shared with the I2C Clock pin. This doesn’t directly represent a conflict as once the Pi is shutdown the I2C bus is not operational, however, it does represent a potential source of issues if for some reason the pin is driven low before the Pi is shutdown.
- Driving the RUN pin low. The Broadcom chips that power the Pi require a pin to be constantly held high in order to run. A rising edge on this pin is what causes the Pi to boot; this explains the behaviour of methods 1 and 2. There is an unpopulated header on the board which allows you to manipulate this pin directly, but it is a hard reset, so if you use this you need to be absolutely sure you only do the high-low-high pulse when the Pi is in a shutdown state or you risk corrupting your SD card.
B – Shutting down
The most important aspect of the shutdown is to ensure it is a controlled ‘safe’ shutdown procedure. I consider this to mean the issuing of the “shutdown -h” command.
- [CHOSEN SOLUTION] Using “gpio-shutdown”. There is a device tree overlay built into Raspbian specifically to allow a shutdown to be triggered via the GPIO pins. Official documentation is here or available in /boot/overlays/README , but in short you need to add the following line to /boot/config.txt.
dtoverlay=gpio-shutdown
With this line in place, a low signal on GPIO3 (Physical pin 5) will cause a linux shutdown command to be issued. There are parameters you can add to this command to use a different pin or operate on a high signal.
- Custom Linux script. As the Pi runs a version of the Debian Linux distrobution many users have written their own scripts to monitor GPIO pins and execute various arbitrary commands. This approach is infinitely flexible as it can recognise any pattern of inputs and can execute any command or set of commands. This was the approach I was going to take before I found the standardised device tree overlay.
- Console/SSH Connection. This is effectively the ‘normal’ way to shutdown a headless server. Connecting via a network or console connection and then using the command line interface to shutdown the Pi.
- PIC UART. It’s possible to automate the shutdown process using a simple microcontroller such as a PIC chip to send commands over the console UART connection. Whilst achievable (and implemented in the Amazon Dash buttons for instance) it is a fairly complex implementation.
C – Safely removing power
- [CHOSEN SOLUTION] Using “gpio-poweroff”. Similar to the shutdown solution, there is a ready made device tree overlay on the Pi to allow it signal that it has successfully completed its shutdown procedure. Official documentation is here or available in /boot/overlays/README , but in short you need to add the following line to /boot/config.txt.
dtoverlay=gpio-poweroff
With this line in place, GPIO26 (Physical pin 37) will go high once the Pi has completely shutdown. There are parameters you can add to this command to use a different pin or operate on a high signal.
- Status Light. When the Pi shuts down, the status LED flashes 10 times, then goes out. I have seen implementations which use a photodiode or LDR coupled to the status LED in order to trigger a power removal. To be effective this approach requires an enclosure which allows in no natural light which can cause issues.
- Manual switch. The simplest method is to simply use a physical power switch, but this opens up the opportunity for human error, and would require some kind of indicator to allow me to know when it was safe to remove the power
Final Implementation:
So, having considered all the above I designed myself a small electronic circuit control the power input to the Pi. The design is a modification of the soft latching power supply designed by Dave Jones of EEVBlog. Dave is incredibly knowledgeable and a fantastic YouTuber; if you’re interested in electronics you really should head over and take a look.