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.
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
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
Another pro of the optocoupler: the output automatically fits all controller operating voltages (3.3V, 5V...) and is independent of the car battery voltage.
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.
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...
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...