12 - 14v to switch Arduino digital pin.

Good evening,

I'm working on a project for the car and need to detect when various signals switch from ground to 12v or vice versa (I appreciate that this voltage will change depending on load and engine running or not, but I'll call it 12v for the purpose of this discussion).

Currently I'm using an optoisolator (4N35) which is working fine, but I'm looking to reduce the size of my circuit and was wondering if I could use something like a 2N7002 Mosfet to do a similar job ?

I can find plenty of discussion about switching the Mosfet with the Arduino, but not much about using a Mosfet to switch a digital pin. I've not used a Mosfet before, but can't see any particular reason why it won't work.

Thanks,

Ian.

Its best to show us a schematic of the current circuit you are using so we can make informed comments.

I like the idea of using opto isolators as they physically remove one part of the circuit from the other, hence, ground noise is removed.
If you have more than one i/o consider a MCT6 dual opto isolator to help keep you size down.

I like the opto-isolator method. You can get an O/I array in an IC package which would probably use less space than individual transistors.

This is pretty much what I have now. The switch on the LED side of the 4N35 is just to show the signal can switch from 12v to ground. It's actually a telephone mute signal that I'm detecting.

Arduino D3 is configured as an input with the pullup enabled.

This is a good setup. You get a clean nice signal for your Arduino to trigger on.

Tobbera:
This is a good setup. You get a clean nice signal for your Arduino to trigger on.

It works very well, but I'd like to achieve the same result but with a smaller footprint on the board.

Ian.

but not much about using a Mosfet to switch a digital pin.

That is because it is no difference than switching anything else. Source to ground, drain to input pin. Enable pull ups and put a 10K between gate and ground. The ground of the arduino has to be connected to the ground of the car.

The opto-isolator is only meaningful if the grounds you illustrate are not the same, otherwise the function is the same as as a simple NPN transistor.
If the ground on the source side of your opto (or indeed all opto-isolators) is returned separately to the car chassis and the ground on the transistor side is returned directly to the Arduino board, then it does help with noise rejection.

Tobbera:
This is a good setup. You get a clean nice signal for your Arduino to trigger on.

Not necessarily. You still have to provide for switch debouncing - and some spurious impulses which the opto may faithfully reproduce. What it will not do is to permit spikes to be conveyed to the Arduino which may cause an execution crash.

In fact, a common (NPN) transistor connected to the Arduino input, with its emitter returned to the Arduino ground rather than any other, a capacitor of 0.1µF or more from base to emitter and a resistor of something like 47k isolating the base from your switched circuit will provide quite adequate isolation.

Mind you, I am a trifle curious as to from where in the wiring this telephone mute signal is derived and what its present function is?

Paul__B:
Mind you, I am a trifle curious as to from where in the wiring this telephone mute signal is derived and what its present function is?

It's derived from the cars Bluetooth telephone module and is used to mute the radio/CD when a call comes in (or goes out). As far as I'm aware, It's all solid state, so I don't think there will be any bounce to worry about. I'm also using an identical circuit to detect when the radio/CD is switched on.

The Arduino is connected to the vehicle ground, so the optoisolator is not doing any isolation. It was just a way to interface a 12v signal to an Arduino digital input.

Ian.

ian332isport:
The Arduino is connected to the vehicle ground, so the optoisolator is not doing any isolation. It was just a way to interface a 12v signal to an Arduino digital input.

Grounds are not necessarily grounds. The fact that your Arduino is connected somewhere to the vehicle ground is not the point. The opto-isolator would have some advantage if the arrangement were as I described.

Grumpy_Mike:

but not much about using a Mosfet to switch a digital pin.

That is because it is no difference than switching anything else. Source to ground, drain to input pin. Enable pull ups and put a 10K between gate and ground. The ground of the arduino has to be connected to the ground of the car.

Thanks Mike. I'll give that a try.

Just out of interest, what's the purpose of the 10K resistor ?

The opto-isolator is only meaningful if the grounds you illustrate are not the same, otherwise the function is the same as as a simple NPN transistor.

No, not the same as a transistor. You will benefit from noise immunity because it takes Voltage AND current to turn on the opto. I'ts also a nice way to prevent your Arduino from letting magic smoke out if someone connects too high a voltage to the sensor line. The 2504 part also has two photodiodes in it, so if you connect an AC signal to your input, it will still turn on like it should, converting the AC to DC for you. Then if you have to separate the grounds due to ground loops, it's a no brainer.

I've use these in my prior life, had people hand me I/O boards with cinders where 2504's used to be. The device on the other side of the barrier survived! Also a great way to level-shift especially if you're using one of the new 3.3v parts such as the Due.

Just out of interest, what's the purpose of the 10K resistor ?

It is to make sure the FET is off in the event of no signal present, as opposed to a path to ground in the gate. You could remove it if you want but I would recommend you keep it in.

Grumpy_Mike:

Just out of interest, what's the purpose of the 10K resistor ?

It is to make sure the FET is off in the event of no signal present, as opposed to a path to ground in the gate. You could remove it if you want but I would recommend you keep it in.

Ah, that makes perfect sense.

Thanks for the explanation.

Ian.