ryangd
Posts: 8
Joined: Tue Jul 09, 2013 5:07 am

Automatic shutdown for pi in a car

Fri Sep 02, 2016 9:23 pm

I'm trying install a pi in my car so I can use it as an AirPlay receiver.
I'd like to be able to run it through some sort of capacitor circuit so when it loses power when the car switches off it knows to shutdown properly.
Has anyone ever done this before? I've seen a few tutorials to make a shutdown button, but nothing that automatically shuts it down when the main power supply is pulled.

ejolson
Posts: 3224
Joined: Tue Mar 18, 2014 11:47 am

Re: Automatic shutdown for pi in a car

Fri Sep 02, 2016 11:19 pm

ryangd wrote:I'm trying install a pi in my car so I can use it as an AirPlay receiver.
I'd like to be able to run it through some sort of capacitor circuit so when it loses power when the car switches off it knows to shutdown properly.
Has anyone ever done this before? I've seen a few tutorials to make a shutdown button, but nothing that automatically shuts it down when the main power supply is pulled.
Would it be possible to connect the Pi to the battery before the ignition switch? Then it wouldn't suddenly lose power when the car is turned off. Shutdown could be on a timer like the car headlights when they are accidentally left on.

ryangd
Posts: 8
Joined: Tue Jul 09, 2013 5:07 am

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 1:03 am

I have thought about hardwiring to the battery of the car so it never turns off but I need it to re-search for the network of my phone whenever I get back in the car.
Plus eventually the battery would go flat

User avatar
Gavinmc42
Posts: 3412
Joined: Wed Aug 28, 2013 3:31 am

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 1:15 am

I would probably base a car Pi on another OS like piCore.
It loads from SD card then runs from ram, a power off does not corrupt the SD card.
piCore may not have every app you need, limited choices but there is usually at least one option.
Not sure about airplay, but there is a media OS based on piCore
https://sites.google.com/site/picoreplayer/home

Alternatively, figure out how to run Raspbian from ram.
Is there a Kodi version that runs from ram?
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

ryangd
Posts: 8
Joined: Tue Jul 09, 2013 5:07 am

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 1:17 am

That's a great idea. I didn't even know that was a thing.
I was hoping to just install libreelec as I don't know of any stand alone AirPlay apps

ElEscalador
Posts: 671
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 4:34 am

Simple solution the original question - power the pi via some big caps or a tiny battery use, and use the same "shut down with switch" code you've seen, except the switch is a the contacts from a relay - the coil powerd by some car wire thatbis only powered when the car is running.
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

User avatar
aTao
Posts: 1086
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 4:50 am

Cars have 3 power supplies:
Always on, accessory and run
Power the RPi from always on and monitor accessory to initiate shut down. You should include a debounce/delay since at power up (particularly in diesels) acessory power is very intermittent.

You can find both these suppiies at the back of the clock.
>)))'><'(((<

fdr4prez
Posts: 17
Joined: Fri May 13, 2016 8:06 pm

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 7:19 am

Here is what you can do -

These relays are used often in cars for various aftermarket applications:
https://www.amazon.com/gp/r.html?C=2KVQ ... BEG2AJRUIA

You would connect the relay's coil to the car's ground (wire 85) and the accessory (wire 86).

Then you would connect the relay's common (wire 30) to Pi's GPIO ground pin (not car ground), and the NC (normally closed) (wire 87a) to one of the GPIO I/O pin.

When the relay has power, then the this will open the relay and the GPIO I/O is open.

When the car turns off, then the relay has no power and the contact will close and this will pass the Pi ground into the GPIO I/O.

Now you need a Python script running in the background monitoring the GPIO pin, so that when the pin goes to ground, then the shutdown command will be issued.

A Python script like this would work:

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if(GPIO.input(5) == False):
        os.system(“sudo shutdown -h now”)
        break
    time.sleep(1)

AJB2K3
Posts: 215
Joined: Sun Aug 11, 2013 3:25 pm

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 8:10 am

Kodi handles communication with iphones over Airplay but only audio. There is a commercial plugin that boasts screen mirror and video playback, but it costs.

One way is to power the pi is to use the Bat+ign supplies like a normal stereo and have a script that sends the shutdown signal when ign goes off but you will still need an adapter to produce a "clean, constant" 5V supply.

My own (not installed yet) uses a 130000 charger battery and will work somewhat like fdr4prez's proposition but cutting both the batteries outputs via a timer just to make sure that the pi actually has cleanly shut down.
However, this also has an issue that the pi does not always take the same amount of time to shut down.
Back in the old days, we were smarter then the technology.
Nowadays technology makes us look like villages fools in front of the children!

mosespi
Posts: 508
Joined: Mon May 12, 2014 3:35 pm
Location: 34,-118
Contact: Website

Re: Automatic shutdown for pi in a car

Sat Sep 03, 2016 8:33 pm

Your description is basically a UPS. I have one that works well for automotive use, see:

http://www.allspectrum.com/mopower/

It has an onboard ADC that can read voltages from the input.. which allows it to shutdown and start your Pi based on these voltages. I use a 9v block battery on one in a car, works well. I have been meaning to try it out with some supercaps, since the power fed to my Pi/UPS combo is always on and I just need a small bit of power reserve to account for the cranking droop.

BTW, shutdown on the UPS actually cuts the power to the Pi, which is needed if you want to get really low power draw from this setup. It also has a TVS diode, works well for 12v automotive use.

Regards,
-Moses
Power problems? MoPower UPS for the Pi
http://www.allspectrum.com/mopower/

ejolson
Posts: 3224
Joined: Tue Mar 18, 2014 11:47 am

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 6:13 am

ryangd wrote:I have thought about hardwiring to the battery of the car so it never turns off but I need it to re-search for the network of my phone whenever I get back in the car.
Plus eventually the battery would go flat
Assuming that a below average car battery can provide 50 amp hours at 12 volts or 600 watt hours and that the Pi takes 5 watts of power, a Pi could run for 120 hours before draining the battery. When shutdown the Pi uses about 1/2 watt, which would then take 50 days to drain the battery.

ryangd
Posts: 8
Joined: Tue Jul 09, 2013 5:07 am

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 8:05 am

ejolson wrote:
ryangd wrote:I have thought about hardwiring to the battery of the car so it never turns off but I need it to re-search for the network of my phone whenever I get back in the car.
Plus eventually the battery would go flat
Assuming that a below average car battery can provide 50 amp hours at 12 volts or 600 watt hours and that the Pi takes 5 watts of power, a Pi could run for 120 hours before draining the battery. When shutdown the Pi uses about 1/2 watt, which would then take 50 days to drain the battery.


120 hours probably isn't worth the risk, thats only 5 days, I could easily leave my car that long from time to time. Plus I need it to start up everytime I get in the car so it looks for the wifi network and discovers my phone again

ryangd
Posts: 8
Joined: Tue Jul 09, 2013 5:07 am

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 8:18 am

mosespi wrote:Your description is basically a UPS. I have one that works well for automotive use, see:

http://www.allspectrum.com/mopower/

It has an onboard ADC that can read voltages from the input.. which allows it to shutdown and start your Pi based on these voltages. I use a 9v block battery on one in a car, works well. I have been meaning to try it out with some supercaps, since the power fed to my Pi/UPS combo is always on and I just need a small bit of power reserve to account for the cranking droop.

BTW, shutdown on the UPS actually cuts the power to the Pi, which is needed if you want to get really low power draw from this setup. It also has a TVS diode, works well for 12v automotive use.

Regards,
-Moses

That is awesome. It is exactly what I'm looking for.

Do you have a script that goes with it to enable shut down once the power is pulled? Will it start back up once power is reapplied?
Super caps would be awesome, much tidier- would that just be a matter of getting the right ones and soldering them in place of the battery pack, or is there more complex circuitry involved?
Does it work with any pi?

Ryan

mosespi
Posts: 508
Joined: Mon May 12, 2014 3:35 pm
Location: 34,-118
Contact: Website

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 7:08 pm

Hi Ryan,

I'm glad you like it!

There is client software on the Pi that talks to the UPS via serial, and in combination you can start/shutdown/configure options/read pins and a whole bunch of other stuff. The user manual details a lot of this. For example.. page 11 of the user manual details using the INPUT_CONTROL variable to start and shutdown an automotive Pi. There is a small error in docs on the example variable, so I will give you the one from my own car..

INPUT_CONTROL[1]=2,1,1,13.20,3,1,0,2,1
INPUT_CONTROL[2]=3,3,1,12.50,10,0,1,1,1

The variables for this command are separated by comas, but basically greater then 3 seconds at 13.20 volts input and the Pi starts up and less then 12.50 volts input for 10 seconds and the Pi shuts down. You can also turn off or on the Pi in either state using the onboard power button (or wire your own?), the last 3 commands make this possible.

I have a separate TIMER_CONTROL that starts up the Pi every half hour to send telemetry data up.. the shutdown on the timer startup is controlled by the data upload script on the Pi itself since the time varies by a few minutes usually.

TIMER_CONTROL[1]=5,1,130,24

You should be fine replacing the battery pack with supercaps. I did a brief test a while back on a breadboard.. six caps of 2.7v in series I think it was.. gives you 16.2v max cap voltage, should be good for a 12v system, I didn't bother with equalizing resistors on the caps, but you should probably put them on for a permanent application. Adding supercaps off the battery connector on the UPS also puts the supercaps behind the current limiting charger circuit.. which you can setup to limit it to 52ma (the default UPS current for AA batteries. It works great to avoid the initial current surge that large supercaps have.

I've tested every Pi except a Zero and it works fine. The output for the Pi is current limited by a polyfuse of 1.1 amps. The power supply is capable of a bit more if you are willing to bypass the polyfuse. I have several Pi 3s running as well. You just have to be aware of how much power you really need.

Regards,
-Moses
Power problems? MoPower UPS for the Pi
http://www.allspectrum.com/mopower/

mosespi
Posts: 508
Joined: Mon May 12, 2014 3:35 pm
Location: 34,-118
Contact: Website

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 7:15 pm

ryangd wrote:
ejolson wrote:
ryangd wrote:I have thought about hardwiring to the battery of the car so it never turns off but I need it to re-search for the network of my phone whenever I get back in the car.
Plus eventually the battery would go flat
Assuming that a below average car battery can provide 50 amp hours at 12 volts or 600 watt hours and that the Pi takes 5 watts of power, a Pi could run for 120 hours before draining the battery. When shutdown the Pi uses about 1/2 watt, which would then take 50 days to drain the battery.


120 hours probably isn't worth the risk, thats only 5 days, I could easily leave my car that long from time to time. Plus I need it to start up everytime I get in the car so it looks for the wifi network and discovers my phone again
You also have to remember that most stock car batteries do not like to be cycled that deep. Deep discharge a normal car starter battery 5-10 times like that and you will likely destroy it. Keep the discharge about 5-10% of total capacity between recharges/starts and you should be OK.

You may also find that starting becomes difficult or impossible past a certain discharge.. maybe 50%, maybe 30%? All depends on the battery, car, temperature, etc..

Regards,
-Moses
Power problems? MoPower UPS for the Pi
http://www.allspectrum.com/mopower/

MarkR
Posts: 154
Joined: Fri Jan 25, 2013 1:55 pm

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 9:57 pm

Probably the most robust solution would be to use a system which doesn't need to be shut down cleanly.

A system which runs completely out of ram would work. Or one which doesn't need a filesystem on the sdcard mounted read-write (readonly is ok).

But actually in practice, just turning the power off (e.g. in Raspbian) is ok. It's even better if you can contrive a system where there are no files open for writing (hint: replace rsyslogd with one which runs from ram, or configure it to only write logs into ram)

ryangd
Posts: 8
Joined: Tue Jul 09, 2013 5:07 am

Re: Automatic shutdown for pi in a car

Sun Sep 04, 2016 10:14 pm

MarkR wrote:Probably the most robust solution would be to use a system which doesn't need to be shut down cleanly.

A system which runs completely out of ram would work. Or one which doesn't need a filesystem on the sdcard mounted read-write (readonly is ok).

But actually in practice, just turning the power off (e.g. in Raspbian) is ok. It's even better if you can contrive a system where there are no files open for writing (hint: replace rsyslogd with one which runs from ram, or configure it to only write logs into ram)
I want to run libreelec on it, so I'm not sure if there's any way of running it in ram. And I'm pretty sure it wouldn't enjoy just being switched off

danopia
Posts: 1
Joined: Thu Feb 02, 2017 8:08 pm

Re: Automatic shutdown for pi in a car

Thu Feb 02, 2017 8:20 pm

I made a £2.50 solution (other than the bits you'd need anyway like a cable and cig lighter transformer); use two relays 5V (£1ish each), powered off the pi, and switched from the GPIOs.

Hook the NC (normally closed) side of the relays up to the switched live, and the perm live up to the NO (normally open) side. The middle terminals should go to the live end of the transformer, and reduce the 12V down to 5V, then into the pi via the USB.

When you switch the car on, the pi will power up, running off the switched live. Run a script to activate each of the relays one at a time (leave at least a few milliseconds between otherwise the power drops out during the switch, just leave it 5 seconds); the pi is now running off the perm live, and switching the car off doesn't cause the pi to stop suddenly.

It's an easy step to monitor the switched live, and when it drops out, safely shutdown the pi (it'll be on perm live, so you have plenty of time to do it cleanly).

User avatar
aTao
Posts: 1086
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Automatic shutdown for pi in a car

Fri Feb 03, 2017 7:09 am

danopia wrote:I made a £2.50 solution (other than the bits you'd need anyway like a cable and cig lighter transformer); use two relays 5V (£1ish each), powered off the pi, and switched from the GPIOs.

Hook the NC (normally closed) side of the relays up to the switched live, and the perm live up to the NO (normally open) side. The middle terminals should go to the live end of the transformer, and reduce the 12V down to 5V, then into the pi via the USB.

When you switch the car on, the pi will power up, running off the switched live. Run a script to activate each of the relays one at a time (leave at least a few milliseconds between otherwise the power drops out during the switch, just leave it 5 seconds); the pi is now running off the perm live, and switching the car off doesn't cause the pi to stop suddenly.

It's an easy step to monitor the switched live, and when it drops out, safely shutdown the pi (it'll be on perm live, so you have plenty of time to do it cleanly).
Interesting, but....
On power up, you stagger the relays energising to prevent a power drop, good. This will briefly connect perm power to switched power, not a problem since both are on at this time. However, at switch off, the same will happen (best will in the world, 2 separate relays wont operate simultaneously). This will put a short power up to all the car systems running off switched power. Probably not a problem for them, but it may impact your choice of relay.
>)))'><'(((<

User avatar
davidcoton
Posts: 3934
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Automatic shutdown for pi in a car

Fri Feb 03, 2017 9:54 am

aTao wrote:
danopia wrote:I made a £2.50 solution (other than the bits you'd need anyway like a cable and cig lighter transformer); use two relays 5V (£1ish each), powered off the pi, and switched from the GPIOs.

Hook the NC (normally closed) side of the relays up to the switched live, and the perm live up to the NO (normally open) side. The middle terminals should go to the live end of the transformer, and reduce the 12V down to 5V, then into the pi via the USB.

When you switch the car on, the pi will power up, running off the switched live. Run a script to activate each of the relays one at a time (leave at least a few milliseconds between otherwise the power drops out during the switch, just leave it 5 seconds); the pi is now running off the perm live, and switching the car off doesn't cause the pi to stop suddenly.

It's an easy step to monitor the switched live, and when it drops out, safely shutdown the pi (it'll be on perm live, so you have plenty of time to do it cleanly).
Interesting, but....
On power up, you stagger the relays energising to prevent a power drop, good. This will briefly connect perm power to switched power, not a problem since both are on at this time. However, at switch off, the same will happen (best will in the world, 2 separate relays wont operate simultaneously). This will put a short power up to all the car systems running off switched power. Probably not a problem for them, but it may impact your choice of relay.
Feed the switched 12V through a chunky (3A) diode. Then you only need one relay on permanent 12V, no problem with glitches, timing, or feeding permanent back to switched. Reduced cost, increased reliability.
Signature retired

Karathos
Posts: 2
Joined: Thu Sep 20, 2018 12:44 am

Re: Automatic shutdown for pi in a car

Thu Sep 20, 2018 12:47 am

I know I’m bringing a post up from the dead, but did you ever find a cheap and easy solution for this? I have went through 3 mausberry car switches and I refuse to install another. So something more simple would be great

mayukh.sroy
Posts: 2
Joined: Fri May 31, 2019 3:22 pm

Re: Automatic shutdown for pi in a car

Fri May 31, 2019 3:30 pm

fdr4prez wrote:
Sat Sep 03, 2016 7:19 am
Here is what you can do -

These relays are used often in cars for various aftermarket applications:
https://www.amazon.com/gp/r.html?C=2KVQ ... BEG2AJRUIA

You would connect the relay's coil to the car's ground (wire 85) and the accessory (wire 86).

Then you would connect the relay's common (wire 30) to Pi's GPIO ground pin (not car ground), and the NC (normally closed) (wire 87a) to one of the GPIO I/O pin.

When the relay has power, then the this will open the relay and the GPIO I/O is open.

When the car turns off, then the relay has no power and the contact will close and this will pass the Pi ground into the GPIO I/O.

Now you need a Python script running in the background monitoring the GPIO pin, so that when the pin goes to ground, then the shutdown command will be issued.

A Python script like this would work:

Code: Select all

import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
    if(GPIO.input(5) == False):
        os.system(“sudo shutdown -h now”)
        break
    time.sleep(1)
Will this work? If this works,can I use any 12V single channel relay module?

energyi
Posts: 141
Joined: Tue Mar 24, 2015 9:39 pm

Re: Automatic shutdown for pi in a car

Thu Jun 06, 2019 4:44 pm

Assuming you want to shut down properly to save the microSD card. I did a rpi ZeroW car unit for about a year on a cheap microSD card which just shutdown when ever the car was turned off, no problems. YMMV

Another option is a USB boot, which might be less detrimental to a power off scenario.
energyi

Return to “General discussion”