Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Headlight and Reverse Lights monitor for YouTube Commenters - UNTESTED but should work fine.
# A quick hack of my button monitor script to monitor headlights and reverse lights to
# Dim the screen and switch to the reverse camera.
# I have not tested this...
# Remember, the switch or opto is switching to ground, so the logic is inverted... 0 = on 1 = off
# Also, you would run only one of these "Monitor" scripts. you can't be running AND
import RPi.GPIO as GPIO
import time
import subprocess, os
import signal
RearView_Switch = 14 # pin 18 (You must use an optocoupler and resistor as shown in the video)
Brightness_Switch = 15 # pin 16 (You must use an optocoupler and resistor as shown in the video)
#Extra_Switch = 1 # pin 3
GPIO.setup(RearView_Switch,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(Brightness_Switch,GPIO.IN, pull_up_down=GPIO.PUD_UP)
print " Press Ctrl & C to Quit"
run = 0
bright = 0
while True :
time.sleep(0.25) #this restricts the script to check the lights every 1/4 second, There is no point in checking 10,000 times a second.
# If the reverse reverse lights come on, do this:
if GPIO.input(RearView_Switch)==0 and run == 0:
print "Switching Rearview Camera On"
rpistr = "raspivid -t 0 -vf -h 480 -w 800"
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
run = 1
# When the reverse lights go off, do this:
if GPIO.input(RearView_Switch)==1 and run == 1:
os.killpg(, signal.SIGTERM)
print "Killing the reverse camera feed"
run = 0
#These next Two blocks monitor the headlights or marker light and adjust the screen brightness settings.
if GPIO.input(Brightness_Switch)==0 and bright == 0:
print "Setting Brightness to 20" # 20 is about 10% ("/usr/local/bin/ 20", shell=True)
bright = 1
if GPIO.input(Brightness_Switch)==1 and bright == 1:
print "Setting Brightness back to 255" #255 is 100% ("/usr/local/bin/ 255", shell=True)
bright = 0
except KeyboardInterrupt:
print " Quit"
Copy link

tthenrie commented Jul 28, 2019

I plan to use your script on my setup to monitor backup lights to turn on camera view, however, I will have two backup cameras: one on the tailgate of the pickup to see the bumper hitch (usb cam feed) and one on the back of my horse trailer (usb cam feed to rpizw fed to the truck computer via wifi). The tailgate and trailer backup cameras will switch on with the reverse lights.

Any help with the code for making the tailgate and trailer backup cam views selectable via your script would be appreciated.

Copy link

Jbizo commented Aug 10, 2020

This works as deigned. I have a video of it running on youtube if you are interested.

Copy link

Jbizo commented Aug 13, 2020

Print "Killing the reverse camera feed" was giving an error. I removed it and all was well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment