# Raspberry pi in the car

Hoping someone might be able to help me. I am looking to connect an RPI to a car and use the ignition voltage to signal to the RPI to shutdown. I have the constant voltage part sorted but I just need guidance with the GPIO configuration and sensing the drop in voltage from the car. Basically I have purchased optocouplers (PC817X2NSZ0F) and have a selection of resistors. I understand the voltage is going to be somewhere around 7 - 18 volts so I was going to use a high enough resistor to adjust for this over the diode. Do you think this should work?

• There is nothing specific to the Pi. This is an electrical engineering question, which incidentally seriously underestimates the difficulty of working in the hostile automotive environment. Commented Dec 9, 2015 at 10:02
• @Milliways, yes, I've made that underestimate in the past (turns out 555s don't like their supply voltage changing while running). Commented Dec 9, 2015 at 15:54

I suspect your range of 7--18V is too broad, but taking it on trust for the moment:

At 18V you'd need (approximately) a 330Ω resistor for the rated 50mA of current. You'd dissipate over 800mW of heat in the resistor, so you'd be looking for a fairly large resistor (rated 1W). Alternatively 3 resistors in parallel, each 1/2W 1KΩ would do the trick. At 7V this resistor would only allow about 17mA to flow. The data sheet doesn't give a minimum current but it might be marginal - you should test on the bench before you get too far. And note that the success of switching with a low input current may depend on the output current.

The upper limit of 18V, if presented to the car battery, would wreck the battery as soon as it was fully charged. A more realistic upper limit is 14.4V, which we could round up to 15V. At the lower end, I'd be surprised to see less than around 10V even when cranking, if the battery is good. So you might find a resitor that would serve your purposes.

As for the GPIO, you could set it as an input with a pull-up resistor and use the opto-coupler to switch to 0V.

Car electronics get interesting because of the voltage ranges, as you've rightly spotted. I recommend from experience that you test everything under realistic conditions before making it too inaccessible.

• I have a 12v power supply in the house I used for a CB back in the day so I could test with that and than try in the car. I just had a thought, what about using a 12v relay switch the 3.3v rail on the RPI. Similar to the optocouplers but would keep the 12v / 3.3 away from each other. I could still pull-up or pull down resistor to signal to the RPI. What you think, would that work ? Commented Dec 9, 2015 at 10:53
• There are 2 difficulties with switching th epower using a relay: (i) the switching voltage isn't very well defined - it may switch on when you turn the ignition to the accessory position, then off again when the starter motor is running, then on when the engine fires. (ii) Do you really the Pi to boot based on the ignition, or just to sense the ignition? Commented Dec 9, 2015 at 12:08
• I'm just thinking of using the ignition to sense the drop. The RPI will be powered directly with a different source and also with a shutdown power timer. The relay would only be used to signal to the RPI that the car has shutdown. Of course I'm thinking i will also have a small problem when the car turns over that I might reset the RPI but I might be able to use a cap or something Commented Dec 9, 2015 at 12:22
• @AlanR Oh OK, I though you meant switch all the 3.3V on and off. If you're just switching a signal with the relay there's no real advantage over the optocoupler you've got. Commented Dec 9, 2015 at 12:24
• Yep basically I just want to sense the presence of the 12v but am now thinking of just using a standard 12v relay for a car. The 3.3v will be switched by using the 12v standard relay and the 3.3v drawn from the RPI itself. What you think would that work ? It's much more simply. Commented Dec 9, 2015 at 17:08