Using 12v Vehicle signal to trigger gnd on an arduino pin ( as input)

Hi all,

I am designing a project and have the whole thing done but last minute decided to add a headlight input so that when the headlight switch goes to 12v on the vehicle then the arduino will see this and can react.

gnd on arduino is connected to gnd on vehicle and 5v regulator used to power arduino so im assuing optocoupler wouldnt be much different for filtering out issues as gnd is conencted anyway?

Ideally I want to code it using a pullup resistor so I want to switch gnd.

Would the best practice be a NPN transistor for this?

Or am I best to use an optocoupler or something else completely even reed relay, ssr etc.

If so some values for resistors on Base etc or even how to calculate them would be greatly appreciated.

I was thinking along the lines of a 2n3904 with 12v going through a 10k resistor to base and then arduino pin connected to emitter and gnd on collector obviously with the arduino internal pullup assuming around 20-50k on the emitter pin to high it all works on a test circuit but im yet to try real world and dont really want to get this pcb circuit made and find out i should have done something else.

Thanks for the suggestions in advance.

That's the safest approach in car electronics, highly recommended.

I know its recommended but if they share the same gnd then isnt any isolation basically thrown out the window? so is it actually beneficial to suing an optocoupler vs transistor in this case?

There is no need for either, a voltage divider (2 resistors) is already sufficient. An optocoupler will withstand many problems (spikes...) off the car, and if you pick one in a DIL case it's easy to replace in case it was accidentally killed. It's all your choice and (later) experience :slight_smile:

Thanks, Sounds like i have my option now to try and choose the right optocoupler haha

A couple resistor is more than enough I think. 68K to +12V and 47K to Gnd should be all right. You can also add a small capacitor in parallel to the 47K to filter noise out. 10nF should eliminate 600Hz and beyond

1 Like

sorry i'm not following, A couple of resistors?

Do you have a diagram of what you mean at all?

how do you pull the arduino pin low from 12v + when on?

Another pro of the optocoupler: the output automatically fits all controller operating voltages (3.3V, 5V...) and is independent of the car battery voltage.

Simple Voltage Divider

so instead of using the internal pullup you're just saying use a 47k pulldown?

so when 12v is on then arduino pin will go high? and then when off will be pulled to gnd with the 47k resistor

The voltage divider might be OK but you need to take into consideration that a lead acid car battery nominal voltage is 12.6V and a fully charged battery is 13.8V and the 12V electrical system may be as high at 14V when the battery is being charged.

With the 47K/68K divider the voltage to to arduino could be as high as 5.72V.

If you want to make sure that you never blow-up your arduino, then use an Optocoupler.

so when 12v is on then arduino pin will go high? and then when off will be pulled to gnd with the 47k resistor

Yes

With the 47K/68K divider the voltage to to arduino could be as high as 5.72V

The equivalent (series) resistor is about 28k so the clamping diode (internal to Arduino) will manage that.

If you want to pull the pin low when +12V, you need a transistor or an optocoupler :
Transistor

Im fine with using a pulldown scenario instead of the internal pullup.

Just seeing what sort of resistor values would work for values between 12v and 16v as that's typical automotive use.

Most automotive electronics work around the 11v - 16v range so I'd like to be able to implement that same voltage range

As I said, if you want to be 100% certain that you never blow-up your arduino, then use an optocoupler.

Might go that way but i was under the impression if the arduino gnd and opto input gnd share the same gnd then any isolation characteristics are no longer valid?

@ 11V, the resistors/capacitor only will output 4.5V which is considered HIGH (> 0.6 x VCC = 3.3V)
@ 16V the resistors/capacitor only will output 6.5V though 27k. Clamping diode will get 0.004 mA : not significant

The protection the opto adds even if common ground is that any spike on the 12V rail won't pass beyond. But 100% safety does not exist : a spike can come to the outputs as well and Arduinos are so cheap...

How do you power the Arduino?
A common GND is not really bad. More problematic are positive and even negative spikes on whatever cable.

Yes and no. With a common ground the two systems are no longer totally isolated but you don't need total isolation. You just need to make sure that you never apply more than 5V to your arduino. With an optoisolator, even with a common ground, that would only happen if you exceeded the optoisolator's isolation voltage which are typically several thousand volts.

As a general rule, i'd say that if you're developing for Mercedes-Benz, go the safe way, they have a reputation to defend and customers can afford the extra, but if you're developing for yourself and want to learn what's possible and what's not, go the (reasonably) cheap way and elaborate on that.
As for skipkes, they can come from the power supply anyway.
Frying an Arduino is not the end of the world... Well, as long as it doesn't fry as soon as it's plugged in...

Blockquote How do you power the Arduino?

Its powered through a TSR_2-2450 5v regulator