Saturday, 26 March 2016

Cheap solution for automatic shutdown PiDashCam

For the automatic shutdown I have used a 12V programmable timer in combination with an optocoupler.
I have used a CNY17-3 type because I had it lying around but I think other types will also work.

The timers you can buy very cheap on eBay or Banggood, 2 examples are given:-

http://www.ebay.nl/itm/12V-Digital-Programmable-Timer-Relay-Control-Module-IR-Remote-Controler-/131346807602?hash=item1e94e13b32:g:idsAAOSwEetWALBI

http://www.banggood.com/12V-DC-Multifunction-Self-lock-Relay-PLC-Cycle-Timer-Module-Delay-Time-Switch-p-1028337.html

The timer is getting a power supply that is always there from somewhere in the car. The trigger
signal comes from a switched 12V power supply, I used the power supply from the car radio.
The input for the optocoupler is also taken from the car radio power supply.

When the car is powered the the timer gets triggered and is starting the power supply for the Raspberry pi,
Dashcam pi will get started and will do its job. When the car is turned off the optocoupler triggers the
GPIO pin of the raspberry and starts a shutdown. After 15 seconds the timer is shutting down
and the raspberry pi is powerless. You can chose the length of time when the timer shuts down
the power supply. I have used 15 seconds and that is enough to shut down the raspberry.


I have written a script for the DashCamPi so it can properly shut down called pwr-dashcam.py
Create it with the following contents:-

import subprocess
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

# GPIO25 (pin 22) set up as input. It is pulled up to stop false signals
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# wait for the pin to be sorted with GND and, if so, halt the system
GPIO.wait_for_edge(25, GPIO.FALLING)
subprocess.call(['shutdown -h -P now "System halted by GPIO action"'], shell=True)

# clean up GPIO on normal exit
GPIO.cleanup()

(You can use other pins if you want to)

Then I have place the script file in the same place as the otherscript files from the dashcam in the map /root/scripts/
Then you have to make the scriptfile executable

# chmod +x /root/scripts/pwr-dashcam.py

Then create a service file so that the script to shutdown the raspberry pi is automatically started.

# nano /etc/systemd/system/pwr-dashcam.service

Then add the following text to the service file:

[Unit]
Description=Service to start the power detection for Dashcam
After=syslog.target network.target
[Service]
ExecStart=/root/scripts/pwr-dashcam.py
Restart=on-abort
[Install]
WantedBy=multi-user.target

To start the service file use the following commands.

# systemctl daemon-reload
# systemctl start pwr-dashcam.service

To let the service start automaticly after a reboot execute the following command:

# systemctl enable pwr-dashcam.service

Now everything should be working. In my car it is.

3 comments:

  1. I'm waiting for the parts to arrive to build this for my car pi (audio player rather than dashcam). Curious as to what is the best way to start the Pi up? Starting with the ignition key is great, but how is the Pi protected from power spikes or cut-outs when starting the engine?

    ReplyDelete
    Replies
    1. Speaking on behalf of the author Ronald, I believe the ignition being switched on supplies power to the radio which is the trigger to start supplying power to the Pi. It does not take it's power directly from the ignition source. When the power is removed, the Pi runs on this 'original' power source until the timer expires. I am not sure how sensitive the timer is though. For example what happens if ignition needs a few goes before the car will start?

      Delete
    2. I'm going to try reworking it so the Pi is powered by the switched live and add a USB power bank with passthrough in between the timer and the Pi. Hopefully that will supply power all the time the pi needs it. I'll let you know whether it works.

      Delete