Hi again,
So I have been working on a way to cut power to my Raspberry Pi after it shuts down properly, and to do this I thought I’d be sneaky and use a MOSFET or two.
I learnt from work that MOSFETs are actually sensitive to ESD, so I have been grounding myself frequently when working with them, just for good measure.
Hold on hold on… I should probably give you some background on where this Pi is, then it’ll make a bit more sense as to why I’m doing this.
This Raspberry Pi (version 2, with the quad core : D, or should I say, : D : D : D : D) is installed in my car, and is running an installation of OpenCarPC by Andrei Istodorescu which is working quite nicely, with a few modifications to get the USB ports to the front of the car (more on this in a future post).
I trialled just straight running the Pi off of a 12-5V DC-DC converter from the car’s battery, permanently on. This drained the car’s main battery enough to need a push start in 3 short days. I give many thanks to my good friend that gave it a push in the pouring rain to get me going.
Then I put the Pi on the accessory circuit of the car (the one that turns on when you turn the key one click), this worked well, the battery was not draining 24/7, but the Pi was getting a brown out as soon as I tried to start the car (the crank drained the volts enough for the Pi to turn off and on again).
This was not good for the OS, and became corrupt within a week, that poor OS… So then I though to plug it into a USB power bank, and charge said bank when the car was on. This meant the car’s battery would not be draining and the Pi would be on and not getting corrupt.
Nope, USB bank ran out of power within three hours of running the Pi, it takes a lot more power than I originally thought! So I decommissioned it (unplugged the power) until I could implement a better way of doing things.
I quite recently built a little circuit that switches the output (5V) from different sources, depending on if said source had enough power. The three sources are:
– Small solar panel (connected through 12-5V converter)
This (I thought) would work a treat, and it did! In theory anyway.
So what would happen, is that when the car is turned on, the relay would switch the output from USB power to 12-5V converter, and charge the USB power bank at the same time. If the car was off, and there was sufficient sunlight, the solar panel would generate enough power to trigger the 12-5 volt converter and switch the relay, charging the USB power bank.
That works awesomely! So I go to test it and when I switch from USB power to 12 volt supply… The Pi restarts. What? Why?
Well, it’s because I’m using a relay to switch Pi power, and in the time it takes the gate to switch from USB power to 12-5v converter power, there is nothing going to the pi. Mind you, this is around 50ms of time, very little amount of time to you and me, but to something that lives off of power, this is enough to shut off.
So, I’m at work, and I go to the boss man (experienced electrical engineer) with my idea of using a super capacitor to hold up the voltage when the relay is switching. He agreed and said that’s a good idea, and I asked how to implement it in the circuit He drew up a quick circuit with a resistor for charging and a schottky diode for a fast reaction time and a low volt drop, I went home that night and built it, and it works flawlessly.
My testing went like so:
Ok, plugged in, voltage in the cap is rising… rising… rising…
2 minutes later: rising… rising… yay! 5v!
Plug in the pi on USB bank power, let it boot.
Unplug the USB power so that the Pi was running on the discharge of the supercap only, go.
Ok, good Pi is still running… still running… still running…
Yeah that went on for a solid 20 seconds before I was baffled enough to plug it back in and say “Good god I like supercaps”. Pi was still running, all is well.
Alright, implementation time. I mounted the Power bank in the back of the car near the Pi, plugged it in and it booted right away… That’s when I realised how silly I was. Dang it man, this is going to drain the power bank in three ish hours.
Which brings me back to the start of this post, cut the power to the Pi when it is turned off.
How do I do it? oh, easy man! Use a relay, turned on when the Pi is on, and off when the Pi is off. Ok, good, but how do implement that? Well you just uh… um… hmmmm…
Yeah that ensued for about 2 minutes, and I remembered playing with the GPIO a while back in python. If I find a pin on the Pi’s GPIO that is off by default, then turn it on once booted, it’ll turn back off again when the Pi is safely shut down. Good, that’s the off part, but what about the on bit, how do I get the Pi to start?
This problem is trickier than it led on to be, you see, when you get in the car to start it, you get in, put the key in, turn it (accessory circuit), then within a few seconds (less than 10) start the car. As said previously, this dims the voltage from the battery enough to turn off the pi for a few seconds (that’s what the supercap/relay is for! Woot!).
Cool, so, connect the relay to the accessory circuit, and to the power on the Pi, that will turn it on, and when the voltage from Acc (accessory) dims, the USB bank will take over for a few seconds, and keep the relay closed, keeping the Pi turned on.
Ok, now to turn it off, I said I would use the GPIO to give me a 3.3v signal when it was on, and 0v when it was off. However, when the Pi is first switched on, said GPIO pin is 0v, kinda fundamental to the whole thing. Now this brings about a problem.
If I start the car, the Acc starts the Pi, and the GPIO pin has to turn the power back off when it’s dead. But the pin is dead, so the power turns off straight away… Dang.
New thought pattern:
3v3: “Pi is on, keep alive”
GPIO: “I’m on, now listen to me (disregard 3v3)”
GPIO: “I’m off now, so cut power”
I needed a way to implement this as an analogue circuit, and I found the way through some leftovers from another project.
I have a little DIL DPDT 12V latching relay, and another normally open SPST relay, and a few MOSFETs as said at the start of the post.
After much deliberation, a design was decided upon, it included three N channel MOSFETs, a 9v battery to switch the relay when 12v power was not available, and both relays. I haven’t actually soldered the circuit together yet, but I plan to very soon.
Here we have come to the reason for this post, as my desk holds on it the sprawled remains of projects past and future, some APA102 LEDs, tools, multimeter, soldering iron, aforementioned 9V battery, an Arduino UNO, a 100m roll of wire, 4 notebooks, document wallet, and various other electronic things; I can’t help but be wary to not touch some of it, as the ESD might kill it.
On a different note, I was shuffling through my music collection, and the Goo Goo Dolls played, and damn now I can’t get them out of my head, not that I’m complaining or anything.
Until next time,
Travers.