Powering a Raspberrypi in the car

Posted on Jun 18, 2023 Software Network

Operating a RaspberryPi in your car can be fun. My main reason to have a little computer running in the car, is to learn about setting up a mobile hotspot using hostapd, post car location data into the cloud using OwnTracks, track the car using LoraWAN and The Things Network and more.

This post discusses one of the basic requirements for all of this: powering the Pi

Power challenges

An automotive environment poses several challenges to powering electronics. The device should be powered up when needed and also be powered down at the right time. And most importantly it should not draw any power when powered down to avoid a flat car battery. 

A Pi has additional requirements, as you cannot simply unplug the power supply to power it down. A safe shutdown procedure is needed to ensure the SD card doesn't break. For this purpose, the  Carberry HAT provides a solution for that meets the requirements for an automotive environment. The Carberry HAT can also be connected to the CAN bus of the car.

The Carberry shield needs a constant 12V power supply and it needs a signal to indicate that the Pi needs to power up. This signal can be either an ignition signal or activity on the CAN bus. In this case I'm using the ignition signal, which is a 12V signal set to high when the car is started.

Finding the ignition signal can be challenging. The image below is a snapshot of the wiring diagram of my car. It shows that in the trunk of the car there is a Yellow-Blue wire which provides the 12V ignition signal. This wire is used to power the trailer module and can also provide a good ignition signal to the carberry.

wiring towbar xc60

Adding an extra wire that connects the ignition signal to the Carberry shield, works as expected: the Carberry provides power to the Pi when the car is started. When turning off the car, it takes a configurable number of seconds and then the Carberry signals the Pi it needs to shutdown.

The picture below shows Pi in hidden behind one of the panels in the back of the car. It also has a Waveshare 4G dongle.

Wired carberry

A smarter shutdown

The Carberry HAT communicates with the Pi using serial communication. The standard procedure for a graceful shutdown is that the HAT raises a pin (GPIO 27) to indicate the Rpi needs to gracefully shutdown. A small program checks the status of GPIO27 and when it is high it shuts down the Pi.

However, you can also talk to the Carberry HAT and subscribe to these events, so you can do some smarter things. To shutdown the Pi that also acts as a hotspot within a few minutes after the car is turned down, is not appreciated by the passengers. Wouldn't it be better to:

  1. Gracefully shutdown the Pi using systemctl halt.
  2. Postpone shutdown if there are still Wifi clients connected
  3. Do not postpone the shutdown when Wifi clients could better connect to the home wifi.

The second requirement is mainly there to keep providing Wifi to the passengers when the car is parked for a while and the engine is turned off. The third requirement comes from the scenario where the car arrives at home, and one or more Wifi devices remain connected to the hotspot, while it would be better to use the home Wifi. It basically overrides the second requirement when parking the car in front of the house.

CarberryCommander

The CarberryCommander python script that is available on GitHub implements the smarter shutdown.