In my last article , “Building a simple safe power shutoff switch.” I detail how to build a circuit to safely shut the power and disconnect it from a Raspberry Pi. This is part of my in the works Pi based car radio project. I needed a way to prevent the Pifrom draining my car battery when it was shutdown.
There are a number of car radio floating around the internet, but most of them depend on using the car ignition switch to activate and disconnect the power. This is not optimal, since the Raspberry Pi doesn’t yet have a sleep mode, and thus has to suffer the reboot process every time you turn the ignition on. I wanted to do something better.
While I was hacking the CAN bus on my Jeep, I spent some time reverse engineering the packets traveling on my Jeep internal CAN bus. I discovered that as long as power is applied there is constant CAN activity. Only after the key is removed and does the bus go quiet. But as soon as you open the door, or attempt to use the remote unlock, CAN bus activity starts up again. In fact this behavior was so repeatable that I could use it as a indicator of when I was going to access the vehicle and possibly make the Pi ready for use.
But thanks to my power circuit from the article, can’t use the CAN board in my Pi setup to detect activity. I wanted a lower power solution, that can hang on the bus and drain minimal power from my battery when not in use. This circuit needs to drive the POWER ON
signal going to the ULN2803A Darlington that drives the relay on my power board.
Since CAN is a differential bus, my first thoughts were something on the order of an opto-isolator on the CANH and CANL lines. But after some research, I discovered that there was a bit more to the CAN electrical characteristics than first suspected. The good news was since CAN was so prolific there is plenty of devices already available with the wake up functionality.
How CAN we do it?
Enter the TJA1441 CAN transceiver from NXP Semiconductors.
The TJA1441 has all the proper voltage protection needed to directly interface to an automotive CAN bus . But what is interesting about this device is that will enter silent mode
when the S
pin is set high. This will disable the transmitter completely, but will still indicate traffic on the RXD
pin. Best of all the part sells for about $1.50, much cheaper than any circuit I could create.
However the RXD signal duration isn’t long enough to trigger my latching relay. But a simple solution is to pass it trough the old reliable 555 setup as a monostable mode.
I calculated the R/C values so that it would produce roughly a 240 msec pulse that I could feed to a ULN2803A to drive the latching relay. Using the formula
Output Pulse Width (secs) = 1.1 x R x C
But we need power.
The TJA1441 is a 5v device, and we also need to power the 555. We will need to step down the 12v from the car battery. NO, we can’t use the DC-DC converter that goes to our Pi circuit, since this controls that power. Keep in mind though that automotive power needs some conditioning, so we need to pick an appropriate part.
I settled on the Infineon Technologies TLE4268, which is a 5v LDO voltage regulator specifically designed for the wild voltages in automotive environments. Available for less than $3.00. combine this with a Schottky diode to prevent reverse polarity issues and you have a simple but reliable power supply for the CAN activity detection circuit.
Combining the design from the last article, we now have a circuit that will go on when then CAN bus is active and disconnect power when the Pi completes a shutdown cycle.
Also to prevent the relay from constantly being energized once the circuit is activated, I attached the 555 RESET line (pin5) to the switched power (using a spare inverter on the ULN2803, to correct the logic). So that when power is applied, the flip-flop circuit in the 555 is effectively disabled.
Prototyping
I build the prototype on a perf-board, with the intent that this could be made into a PCB at some point. But alas my favorite macOS based PCB design app is no longer available. And KiCad has awful user experience, at least in my opinion. I don’t know anything (or really care) about the Windows versions. So I am content with good old solder and wires for now. It’s how I started in the 70s.
This is the first prototype of the power supply board.
Sitting on its hat? Wait What?
The complete assembly is shown below. If you look carefully you will notice something slightly unorthodox here. The Pi is sitting on its hats. Yes I had to do some creative hacking on the Pi itself, to get this card in place. I removed the Raspberry Pi 40 pin GPIO connector and replaced it with a socket header so I could stack the cards, such that the Pi sat directly under the fan board. This is how Pi’s should come from factory to begin with if you ask me.
The Fan hat is on top, followed by the Pi 4, the CAN adapter, and finally the power controller board. Oh I added a serial to USB converter, to allow me optional console access with a USB cable.
Success!
To test turn-on, I set up another Pi to send it CAN commands. This triggered the relay and the PI started up..
Recall from the previous article that I edited the /boot/config.txt
file to include the following line.
dtoverlay=gpio-poweroff,gpiopin=16
This will cause the Raspberry PI so set the GPIO-16 high (3.3v) when the shutdown process is complete.
I instructed the Pi under test to do a shutdown
$sudo shutdown now
Which resulted in setting GPIO-16 when the shutdown was complete, then triggering the relay to reset, and thus removing power from the Pi.
I did this a bunch of times with no unexpected results. And there you have it, a reliable power on and off circuit.
Incidentally is someone wants to volunteer to help move the design to a PCB, I am more than willing to work with you.