bpm009
Posts: 10
Joined: Mon Feb 03, 2014 7:49 am

Pi in the Car - Accessory Power Monitoring

Fri May 24, 2019 1:44 am

I have a situation where I'm using the RasPi in a vehicle. I have it being powered off of constant power but I wish to have it sense the Ignition (Accessory) power being removed as a GPIO input to then initiate a delayed shutdown script. I already have a timed relay to switch power from constant power to accessory power after a set period (approximately 5 minutes after the scheduled shutdown) to remove power from the Pi.

My main question is how to detect/monitor the 12V accessory line within the Pi's allowable voltage range.

Bonus question, I'm using a 12V to 5V DC to DC converter to power the Pi in a car application, is there anything else I need inline? I've seen some threads about the less than "clean" power output from a car but wanted some input on whether this is simply a "less than ideal situation" or truly necessary.

Thanks in advance.

pcmanbob
Posts: 7060
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Pi in the Car - Accessory Power Monitoring

Fri May 24, 2019 9:21 am

I would suggest using an opto isolator that way you keep the 12v away from the pi gpio as 12v would kill a pi dead instantly should it get to the gpio.

wired like this

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Andyroo

Re: Pi in the Car - Accessory Power Monitoring

Fri May 24, 2019 2:54 pm

Car power is horrible to computers (not as bad a boats but still nasty). Yes you need more than a step down converter. They may be fine for charging phones but they are not a power supply :D

Have a look at https://mausberry-circuits.myshopify.com/ and see their power units.


bpm009
Posts: 10
Joined: Mon Feb 03, 2014 7:49 am

Re: Pi in the Car - Accessory Power Monitoring

Fri May 24, 2019 5:55 pm

All three responses are awesome options. The Mausberry seems like the most complete unit, will probably fall back on that if I don't make any progress. I would like to explore the opto isolator a little more.

My difficulty with the opto isolator is that I can't seem to grasp the specs on them.
- The current transfer ratio leads me to stare at the screen for a while :shock: .
- Why is the 10k resistor on the 3V3 side necessary? I've read that it's ~50mA off the GPIO pin, is that excessive for the photo transistor?
- Finally, does pin 6 have a purpose? I've only been looking at the 4 pin (photo transistor) style.

pcmanbob
Posts: 7060
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Pi in the Car - Accessory Power Monitoring

Fri May 24, 2019 7:20 pm

bpm009 wrote:
Fri May 24, 2019 5:55 pm
- Why is the 10k resistor on the 3V3 side necessary? I've read that it's ~50mA off the GPIO pin, is that excessive for the photo transistor?
- Finally, does pin 6 have a purpose? I've only been looking at the 4 pin (photo transistor) style.
So when the opto isolator is off ( IE led is off ) then the gpio pin is supplied 3.3v via the 10K resistor and the 1K resistor so you have a high on the gpio pin.

when the opto isolator is on ( IE led is on ) the transistor connects the gpio pin to ground but also connects the 10K resistor to ground , so the gpio pin sees a low, but the 3.3v is dropped across the 10K resistor , if you did not have the 10K resistor then the gpio pin would be left floating so it could have any value , if you replace the 10K resistor with wire when the opto isolator was on you would short the 3.3v to ground and destroy your pi.

there is another way to wire this that gives a high on the gpio when the opto isolator is on, if you want that version just ask.

A 4 pin opto isolator does not have pin 3 which is unused or the pin 6 which is the transistor base which is not use in this circuit.

The maximum you can draw from any gpio pin is 16mA and a maximum of 50mA for all used gpio pins this circuit will draw 0.33mA from the pi gpio pin.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

bpm009
Posts: 10
Joined: Mon Feb 03, 2014 7:49 am

Re: Pi in the Car - Accessory Power Monitoring

Tue Jun 04, 2019 7:01 pm

I wanted to follow up with an update and also a semi-related question. So I created the circuit as you showed and it was doing exactly as expected. I was embarrassed with my custom board :? so I found an optically isolated relay with triggers to perform the same task.

Got the code working and the relay working however I've run into one final challenge...
1. When ACC power is on, RPI is on accessory power
2. When ACC power is switched to off, RPI switches to constant battery on a timed relay that returns to ACC after set time.
Problem: the fraction of a second when ACC power is removed and relay switches to battery is causing a RPI reboot.
Solution: Capacitor??? I have not messed with these before and how to properly size.

markost
Posts: 17
Joined: Mon Sep 17, 2018 4:45 am

Re: Pi in the Car - Accessory Power Monitoring

Tue Jun 04, 2019 7:36 pm

Relay switching time is too long for sensitive electronics like Pi.
Look at https://www.tindie.com/products/picotou ... w-version/, special designed for car.

User avatar
thagrol
Posts: 1885
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pi in the Car - Accessory Power Monitoring

Wed Jun 05, 2019 12:04 am

I'd suggest not powering the pi from the ACC feed.

Instead power it directly from the battery by use a couple of relays to control the switching.

The circuit below is theoritical but did simulate OK. Ignore component values and part numbers they're probably sub optimal. And the relays could probably use some back EMF supression diodes. The gpio input should proably have some current limiting and, maybe, an external pullup.

I never did get around to building this :(

Power into the first relay is routed out its NO contact and into a second relay. The NC output of the second relay feeds the voltage conversion/PSU for the Pi, back into the first relay's coil to keep it "on", and into one side of the coil of the second relay.

When the ACC feed is switched on, the first relay switches sending power onwards.

Once the Pi detects, via the opto coupler, that the ACC feed has switched off the Pi can perform a safe shutdown and the gpio-poweroff device tree overlay is used to switch the second relay via the transistor cutting power.

Switch S1 is there to provide a manual kill switch in the event of a software crash preventing the Pi from shuting down.
car psu B_schem.jpg
car psu B_schem.jpg (239.59 KiB) Viewed 604 times
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

bpm009
Posts: 10
Joined: Mon Feb 03, 2014 7:49 am

Re: Pi in the Car - Accessory Power Monitoring

Wed Jun 05, 2019 3:08 am

I like the sound/look of the layout, I have everything I'd need except a base transistor to put it together. Quick question, forgive my lack of diagram knowledge, but what is being shown with the 2 triangles pointing at each other as the Accessory feed goes to Relay 1?

Also:

1 - Not very familiar with transistors and how they differ from optocouplers (just learned about those). Is the GPIO pin to the transistor base isolated from the 12v power going through the transistor? Anything needed on the GPIO feed wire?
2 - Am I understanding right that RPI initiates shutdown script and when it shuts down the loss of power to the GPIO out to transistor is what flips Relay 2 to cut power?

This is really helpful, I hope I can put your theory to the test.

drgeoff
Posts: 9874
Joined: Wed Jan 25, 2012 6:39 pm

Re: Pi in the Car - Accessory Power Monitoring

Wed Jun 05, 2019 7:42 am

bpm009 wrote:
Wed Jun 05, 2019 3:08 am
Quick question, forgive my lack of diagram knowledge, but what is being shown with the 2 triangles pointing at each other as the Accessory feed goes to Relay 1?
Diodes.

User avatar
thagrol
Posts: 1885
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Pi in the Car - Accessory Power Monitoring

Wed Jun 05, 2019 12:57 pm

bpm009 wrote:
Wed Jun 05, 2019 3:08 am
I like the sound/look of the layout, I have everything I'd need except a base transistor to put it together. Quick question, forgive my lack of diagram knowledge, but what is being shown with the 2 triangles pointing at each other as the Accessory feed goes to Relay 1?
They're diodes. They're being used to block current flow in the wrong direction.
Also:

1 - Not very familiar with transistors and how they differ from optocouplers (just learned about those). Is the GPIO pin to the transistor base isolated from the 12v power going through the transistor? Anything needed on the GPIO feed wire?
Yes.
A current limiting resistor might be advisable.
2 - Am I understanding right that RPI initiates shutdown script and when it shuts down the loss of power to the GPIO out to transistor is what flips Relay 2 to cut power?
Not quite. The GPIO should be held low. The gpio-poweroff dtoverlay is used to drive the pin high which, via the transistor, allows current to flow through the relay's coil which flips cutting power to the rest of the circuit.
This is really helpful, I hope I can put your theory to the test.
I'd like to see what results you get.
Attempts to contact me outside of thes forums will be ignored unless signed in triplicate, sent in, sent back, queried, lost, found, subjected to public enquiry, lost again, and finally buried in soft peat for three months and recycled as firelighters

Return to “Automation, sensing and robotics”