# Best approach to sense a 12v switch going to GND in an automotive environment

Hi there

Im currently working on an arduino based car alarm.

I´m looking for the best and simple approach to sense when the door switch is put to GND (door opened)
and when the ignition is turned off (also switched to ground)

I was thinking of putting a simple diode between the arduino digital pin and the switch, but I understand that when the circuit to be sensed in the car is left opened there will be 12v at the cathode.

My options (from the top of my head) are, to put a diode like the drawing.
To put a PNP transistor, although i believe it would be the same approach
Make a voltage divider with resistors
or to use an optocoupler.

My idea is to make the circuit as clean as possible using the less parts necessary

How do you guys think it would be the best approach?

Thanks and happy new year

Your circuit will work, or you can make an [u]over-voltage protection circuit[/u] with a resistor and diode. I'd recommend increasing the resistance to 10K. The 2nd circuit uses "regular" diodes and since there probably isn't a negative voltage spike you can leave-out D2.

but I understand that when the circuit to be sensed in the car is left opened there will be 12v at the cathode.

That is correct. The diode is reverse-biased so no current flows and the Anode is held at 5V (via the pull-up resistor). When the switch is closed current flows and the Anode is pulled-down to about 0.7V. Anything less than 1.5V (0.3Vcc) is read as low. Anything greater than 3V (0.6Vcc) is read as high. In-between is unspecified.

Just FYI - It's "traditional" to draw schematics with the input on the left and the output on the right. Your schematic isn't "wrong", but usually the output of your circuit (going to the input on an Arduino) should be on the right.

That circuit is the proper way to go because it ensures the Arduino input can never be pulled higher than the Arduino wants it to, but it has the danger that somehow, the input from your car wiring might transiently be pulled down below -0.5 V. So you need a resistor - say 2k - in series with the line from the car wiring and a second diode with cathode to the junction between that resistor and the first diode cathode, anode to your (Arduino) ground so that if the input ever saw a negative impulse, the second diode would carry it.

I’d use an opto isolator

hammy:
I’d use an opto isolator

Actually, best idea. One 12k resistor to the opto LED, the output transistor between the Arduino pin and ground, pinMode as INPUT_PULLUP, no other components required.

Use a modern opto - not the obsolete 4N25 sort.

Thanks guys for all the input. Sorry I didn´t reply sooner had been busy at work.

I´m about to start making the PCB to solder all the components. Will see how it unfolds

Thanks again!

Paul__B:
Use a modern opto - not the obsolete 4N25 sort.

What would be a good part number to start from to find "modern"?

Oooh, maybe PC817.

Hey guys got some developments.

In the weekend I assembled the prototype perfboard to star testing the code inside the car. (it was already tested on a protoboard but using always 5V.

THe bad news is I already blew 2 arduino pro mini. At first it was a current limiting resistor problem with the BJT´s. When I solved that the second arduino blew the same way. They are not totaled, but the pins 9,8,7 are shorted. The first time could have been the bjt resistors (also a bjt blew) but the second attempt had me guessing

The problem was that I was trying to see if the ignition sensing part (voltage divider worked ok, and it did) then I moved to the door switch part (with the diode)

In the cathode part of the diode things worked as it should (0V door opened, 12V door closed)

But the arduino never registered the switch from high to low. Then was when I realized some pins blew over and the atmega chip started getting hot (on both pro mini)

They are not rendered unusable (still) the analog pin that I was using still work on both, I also have a HC 05 BT module attached, in the second pro mini used, the TX/RX pins must blew over, because I have no serial comm, but the HC 05 works fine.

Here is the schematic can anyone point out the error?

I realize that on the Pin7 (which uses input pull up configuration) there is no diode to +5V as suggested. I was just testing the pcb with the engine off so that no negative voltages were risen at the test.

The resistor values on the analog input are far too low for safety, need to be at least ten times. While my advice about the diode remains, there is a risk that that input could be driven negative if the grounding of the Arduino to the car frame is insufficient. This is a particular risk when you are using the same ground to switch your solenoids (or whatever they are).

You need a second diode with its cathode to the anode of the first and its anode to the microprocessor ground, and a 1k between the common point of the diodes to D7. The emitters of the transistors also need to connect separately to the car frame.

Design for automotive applications is not trivial!