Author Topic: Sensing 12v line with 5v MCU  (Read 3971 times)

0 Members and 1 Guest are viewing this topic.

Offline vmalletTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: us
Sensing 12v line with 5v MCU
« on: April 22, 2019, 10:47:52 pm »
I have the following existing circuit (in a car, 12v battery):


I would like to know the state of the switch (open, closed) with a 5V MCU. The only place I can tap into the circuit is in A. What would be a good way to do it?

Would something like this work?


In this case:
  • Should I worry about the <2mA that are going to flow through the coil at all times now? Could it damage the relay over time?
  • Would I need a pull-up resistor to 5V in point C? (to prevent a "closed" read say in case the fuse blows?)
  • Do I need to worry about the relay coil's kickback?
  • (@Ian) would I need a small capacitor across the zener to ignore RMI (10nF)?
  • If I didn't want the <2mA to flow through the voltage divider while the car is turned off (no power to the MCU, but the +12V is always hot), would I / should I add a  switching transistor / mosfet between the divider and GND?
 

Offline Circlotron

  • Super Contributor
  • ***
  • Posts: 3180
  • Country: au
Re: Sensing 12v line with 5v MCU
« Reply #1 on: April 23, 2019, 01:54:30 am »
If C is going directly to the MCU port pin then you could increase the resistor values X100. That and put say 100nF in place of the zener and you're ready to go. Any high voltage transient from the relay coil switching off will not push any worthwhile current through to the port pin, and the ESD diode in the MCU should be just fine. Done this many times with HC08 micros, I expect yours should be much the same in this regard.
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2209
  • Country: au
    • DIGSYS
Re: Sensing 12v line with 5v MCU
« Reply #2 on: April 23, 2019, 03:18:46 am »
...  or for absolute safety / simplicity, just use an opto-coupler + resistor. Removes any pulse / fault / ground issues getting to the micro.
Hello <tap> <tap> .. is this thing on?
 

Offline vmalletTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: us
Re: Sensing 12v line with 5v MCU
« Reply #3 on: April 23, 2019, 06:42:00 pm »
If C is going directly to the MCU port pin then you could increase the resistor values X100. That and put say 100nF in place of the zener and you're ready to go. Any high voltage transient from the relay coil switching off will not push any worthwhile current through to the port pin, and the ESD diode in the MCU should be just fine. Done this many times with HC08 micros, I expect yours should be much the same in this regard.

You mean like this?


Wouldn't the circuit be super sensitive with such high value resistors?
Also with the 100nF there, wouldn't that introduce a delay from when the switch goes to GND to when the MCU can detect the drop while the capacitor slowly discharges through the resistors? (RC would be ~22ms with R=330K//680K)?

Thanks!
 

Offline vmalletTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: us
Re: Sensing 12v line with 5v MCU
« Reply #4 on: April 23, 2019, 06:43:35 pm »
...  or for absolute safety / simplicity, just use an opto-coupler + resistor. Removes any pulse / fault / ground issues getting to the micro.

How would you wire that up? I was trying to turn that around in my head but I'm not seeing it... Thanks.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Sensing 12v line with 5v MCU
« Reply #5 on: April 23, 2019, 07:25:44 pm »
Get an opto datasheet and see what the recommended VF is.  Let's say 1.5V at 10 mA, just for giggles.
You say your input is 12V so you need to drop (12-1.5)/0.01 so select a 1k resistor.  Connect the resistor in series with the LED of the opto and wire it across the relay coil.  Pay attention to polarity of the LED.

See "Forward Voltage" page 154:  https://www.vishay.com/docs/81181/4n35.pdf

Now, on the output, you have a transistor that can be tied to a digital input pin.  Connect the pin to the transistor collector along with a 1k resistor to Vcc and then tie the emitter to logic ground.

 

Offline vmalletTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: us
Re: Sensing 12v line with 5v MCU
« Reply #6 on: April 23, 2019, 07:50:27 pm »
... Connect the resistor in series with the LED of the opto and wire it across the relay coil.  Pay attention to polarity of the LED. ...

Right, yes makes sense; however my constraint at the moment is I can only tap the circuit in point A, so I can't go across the relay coil.

Hmm, I guess I physically have access to a 12v supply close to point A and that would be technically the same as going across the coil.. Giving me ideas, I'll explore a bit more. Thanks!
 

Offline Circlotron

  • Super Contributor
  • ***
  • Posts: 3180
  • Country: au
Re: Sensing 12v line with 5v MCU
« Reply #7 on: April 24, 2019, 01:11:13 am »
If C is going directly to the MCU port pin then you could increase the resistor values X100. That and put say 100nF in place of the zener and you're ready to go. Any high voltage transient from the relay coil switching off will not push any worthwhile current through to the port pin, and the ESD diode in the MCU should be just fine. Done this many times with HC08 micros, I expect yours should be much the same in this regard.

You mean like this?


Wouldn't the circuit be super sensitive with such high value resistors?
Also with the 100nF there, wouldn't that introduce a delay from when the switch goes to GND to when the MCU can detect the drop while the capacitor slowly discharges through the resistors? (RC would be ~22ms with R=330K//680K)?

Thanks!
Yep, like that. Probably no need for 1k though.
Of course there is a delay, but if you are dealing with button press and relay operation type time scale  then 22mS is probably not a big deal.
 

Offline Tek Tech

  • Contributor
  • Posts: 21
  • Country: pl
Re: Sensing 12v line with 5v MCU
« Reply #8 on: April 24, 2019, 03:26:04 am »
...  or for absolute safety / simplicity, just use an opto-coupler + resistor. Removes any pulse / fault / ground issues getting to the micro.

How would you wire that up? I was trying to turn that around in my head but I'm not seeing it... Thanks.

The output transistor is isolated from the car's installation, so it can be hooked up to the MCU's 5V in any way that is required.
 

Offline agehall

  • Frequent Contributor
  • **
  • Posts: 383
  • Country: se
Re: Sensing 12v line with 5v MCU
« Reply #9 on: April 24, 2019, 06:27:08 am »
This is probably a stupid question, but I'm sure someone will explain why:

Can't you just sense 0V in the car via a diode and an internal pull-up on the MCU?
 

Offline vmalletTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: us
Re: Sensing 12v line with 5v MCU
« Reply #10 on: April 24, 2019, 07:25:01 am »
Thanks for the feedback. Looks like maybe something like this would work pretty safely:


And the nice thing here is that there would be no current draw when the switch is open (definitely good when the car is off).
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2209
  • Country: au
    • DIGSYS
Re: Sensing 12v line with 5v MCU
« Reply #11 on: April 24, 2019, 08:19:51 am »
Quote from: agehall
... Can't you just sense 0V in the car via a diode and an internal pull-up on the MCU?
There are over 100 ways to skin a cat :-) but to answer your question specifically -
Car electrics is notoriously "dangerous", mainly due to 0V ground faults / currents, HV spikes etc etc Wiring ANYTHING directly to a logic / CPUs etc is never
a good idea. You can do it, but the amount of protection required is extensive. Not worth it. Optos are by FAR the most preferable method. Perfect isolation.
IF you need analogue sense, usually you provide some sort of isolation barrier. ie a linear opto
Hello <tap> <tap> .. is this thing on?
 
The following users thanked this post: agehall

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: Sensing 12v line with 5v MCU
« Reply #12 on: April 24, 2019, 08:52:14 am »
Thanks for the feedback. Looks like maybe something like this would work pretty safely:


And the nice thing here is that there would be no current draw when the switch is open (definitely good when the car is off).

Note that that, unlike the other circuit, will sense the relay as ON even if there's no relay.
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline agehall

  • Frequent Contributor
  • **
  • Posts: 383
  • Country: se
Re: Sensing 12v line with 5v MCU
« Reply #13 on: April 24, 2019, 08:58:15 am »
Quote from: agehall
... Can't you just sense 0V in the car via a diode and an internal pull-up on the MCU?
There are over 100 ways to skin a cat :-) but to answer your question specifically -
Car electrics is notoriously "dangerous", mainly due to 0V ground faults / currents, HV spikes etc etc Wiring ANYTHING directly to a logic / CPUs etc is never
a good idea. You can do it, but the amount of protection required is extensive. Not worth it. Optos are by FAR the most preferable method. Perfect isolation.
IF you need analogue sense, usually you provide some sort of isolation barrier. ie a linear opto

That does make perfect sense.
 

Offline magic

  • Super Contributor
  • ***
  • Posts: 6775
  • Country: pl
Re: Sensing 12v line with 5v MCU
« Reply #14 on: April 24, 2019, 09:23:33 am »
Wiring ANYTHING directly to a logic / CPUs etc is never a good idea. You can do it, but the amount of protection required is extensive. Not worth it. Optos are by FAR the most preferable method. Perfect isolation.
Sure, but a transient could blow the opto, couldn't it? Particularly one of reverse polarity. :-//
With a CMOS gate, I would think a 1M 1kV series resistor plus the IC's built-in protection diodes should be good enough. And with, say, 10pF input capacitance, that's still less than 1ms delay.
I have seen a car component which monitored engine RPM by sensing the ignition switch (peak voltage ~200V). In front of some IC there was only an RC filter with 300k series resistance and some unidentified capacitor. It worked for years and never failed.
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2209
  • Country: au
    • DIGSYS
Re: Sensing 12v line with 5v MCU
« Reply #15 on: April 24, 2019, 11:43:21 am »
Quote from: magic
Sure, but a transient could blow the opto, couldn't it? Particularly one of reverse polarity. :-//
With a CMOS gate, I would think a 1M 1kV series resistor plus the IC's built-in protection diodes should be good enough. And with, say, 10pF input capacitance, that's still less than 1ms delay.
I have seen a car component which monitored engine RPM by sensing the ignition switch (peak voltage ~200V). In front of some IC there was only an RC filter with 300k series resistance and some unidentified capacitor. It worked for years and never failed.
Sure ... what you can "get away with" and what is ADR compliant (ADR=Australian vehicle design rules) are completely "different' animals !! All countries have
vehicle safety / wiring standards that are similar, especially IF they want to export.
They don't have electrical safety standards for no reason. HECK, I've seen 100s of products eg that are just a diode, resistor and led, wired STRAIGHT off the
mains !! Yeah they work, and are sold everywhere, but would you seriously use one? Same goes for vehicle electronics.
"Because it works" is not good advice to present to beginners or anyone.
I've repaired / designed vehicle electronics for 30+ years, currently spending the last several weeks on EV electronic controls. I've also had 1,000s of repairs
over that time and seen even the "best practice" designs fail on not "dotting the last i". I send the reports to the manufacturers.
Sure, the opto could "blow up", but it will cause a LOT less damage than "blowing up" a micro, that "may" also be in charge of OTHER safety functions !!
I'm not in any way having a go .. this is a a sore point. We should aim to direct users to the "best path approach", at the very least.
Hello <tap> <tap> .. is this thing on?
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12856
Re: Sensing 12v line with 5v MCU
« Reply #16 on: April 24, 2019, 12:32:40 pm »
Unless you have it in writing from the MCU manufacturer that currents up to a specified limit may flow through the internal protection diodes during normal operation without adverse effects to other device parameters, on a modern MCU, its unwise to rely on the internal protection diodes for normal operation of your circuit.   e.g. see Microchip TB3013 for some of the issues that can occur.   This is *NOT* hypothetical: over on the Microchip forums, there have been several reports of internal oscillator frequency shifts of 40% or more with less than 1mA flowing through a protection diode. 

Long time constant capacitive filtering and an input that doesn't have a Schmitt trigger characteristic has its own problems.  Normally they aren't electrical, though in low power designs the increased current consumption of the input gates due to an input voltage that spends too long in the transition region may be an issue, and I would be very suspicious of their possible behaviour at the top end of the MCU's permitted temperature range.   However your code *MUST* cope with multiple random transitions due to noise at every actual signal transition.  It would be a really *BAD* idea to use the input for an edge triggered interrupt, unless the ISR deferred re-enabling the interrupt for a time greater than the RC time constant.    Its well worth reading Jack Ganssle's mini-series on debouncing.

You also have to consider the sensing thresholds on the 12V side of the input protection network.   A typical CMOS input will have logic thresholds near 30% and 70% of Vcc, with the actual transition somewhere between them.   If you scale a 12V input down to logic levels with a potential divider, the percentage thresholds scale as well, so, assuming the divider ratio is chosen for 14V in, Vcc out, to best match the MCU logic levels to the 12V signal levels in a running vehicle, anything below 4.2V will be seen as logic '0', and anything above 9.8V will be seen as logic '1'.  This applies as long as your input protection network doesn't rely on clamping to reduce the signal to logic levels.

If you use the simple opto input circuit: Opto-LED + series resistor on the 12V side and Opto-phototransistor to ground and pullup resistor to Vcc on the logic level side, the thresholds are a *LOT* harder to predict.   They are determined mainly by the CTRR of the optocoupler, which is not a tightly controlled parameter and varies widely from device to device and with temperature and ageing.    One approach to fixing this is to add a Zener in series with the LED to 'hold off' any significant current through it below the Zener voltage to guarantee the worst case transition  wont be at an inconveniently low voltage.  Another less accurate method is to feed the LED with a potential divider so that the voltage across it is below Vf for inputs below 4V or so.

The problem of transients has already been touched on - most opto LEDs have a peak pulsed If of typically an order of magnitude greater than their continuous rating, and if one follows the typical design practice of not exceeding 50%  of the continuous rating in normal operation, that will permit the optocoupler to survive brief positive going transients up to around 250V if its setup for 14V input.  However the permissible reverse bias on the LED is typically quite small, and although catastrophic avalanche breakdown is fairly unlikely, repeated negative going transients  can degrade the LED's efficiency, reducing the CTRR.  Its therefore  advisable to protect it with an anti-parallel diode.

Protection against Load Dump transients is a whole different matter - worst case, your input circuit (and device PSU) can have to cope with a transient lasting half a second or so that peaks at over 100V.  Power dissipation in your protection network during the surge is a significant issue.   Fortunately modern vehicles tend to have load dump clamping at source to protect their ECU and other electronic systems, so unless your device is particularly high value, it may suffice to design it so it wont fail before the ECU does, and accept that you may get a few warranty claims from unlucky classic vehicle enthusiasts!
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5022
  • Country: ro
  • .
Re: Sensing 12v line with 5v MCU
« Reply #17 on: April 24, 2019, 12:54:58 pm »
You could do with it a resistor and a temperature sensor or a PTC/NTC resistor or even a diode ...  connect the resistor between voltage and ground with a value picked in such a way that the resistor would heat at some reasonable value like 80-100 degrees C (you probably want something above 60 degrees or so, whatever's high enough not to get false positives when car's parked for hours under the sun or whatever)
Measure temperature sensor, resistor value in case of ptc/ntc , voltage drop on the diode

This way you don't care about transients reverse voltage whatever, downside is constant small power consumption and heat on the resistor (but you have a mosfet or transistor or something to disconnect the resistor and then you'd have to deal with delay of a few seconds or so from connection time to when it would safe enough to assume resistor got hot enough to measure)
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16611
  • Country: us
  • DavidH
Re: Sensing 12v line with 5v MCU
« Reply #18 on: April 24, 2019, 04:51:40 pm »
An optocoupler is the safest way because it also removes the need for a ground connection preventing a ground loop.

Your first example with the low impedance divider and zener is good also but should include RF suppression capacitors across the zener diode and at the microcontroller input pin.

 

Offline floobydust

  • Super Contributor
  • ***
  • Posts: 6972
  • Country: ca
Re: Sensing 12v line with 5v MCU
« Reply #19 on: April 24, 2019, 07:04:19 pm »
I'm sensing the grounded switch contact. Then you don't have to worry about what voltage the input swings up to. This is a common technique in automotive, industrial PLC's etc.
It can withstand several 100's V surges and moderate ESD.

Use a Schottky diode for low-voltage MCU's and it ends up limited +ve voltage surge rating due to the diode's low PIV rating.
Typical levels are 12V vehicles +100V and -150V spikes max., 24V trucks are +200V and -600V spikes max.
OP may have relay-coil inductive spikes present which will stress the blocking diode.

Opto-couplers aren't considered great for automotive environments, I'm not sure exactly why. Very few achieve automotive qualifications like AEC-Q100, 101.
I recall it was aging and CTR degradation from temperature swings. I don't see them used in cars. Electric cars will surely change this.
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16611
  • Country: us
  • DavidH
Re: Sensing 12v line with 5v MCU
« Reply #20 on: April 24, 2019, 09:39:29 pm »
Opto-couplers aren't considered great for automotive environments, I'm not sure exactly why. Very few achieve automotive qualifications like AEC-Q100, 101.
I recall it was aging and CTR degradation from temperature swings. I don't see them used in cars. Electric cars will surely change this.

Opto-couplers are not very environmentally robust for whatever reason.  I have occasionally seen photo-interrupters used in their place.
 

Offline MadScientist

  • Frequent Contributor
  • **
  • Posts: 439
  • Country: 00
Re: Sensing 12v line with 5v MCU
« Reply #21 on: April 24, 2019, 11:04:01 pm »
I don’t see the point of an opto if the two grounds are ultimately connected as all the issues from load dumping will be seen on the ground anyway

Surely a either a zener or a simple clamping diode to the micros Vdd along with a suitable series resistor is the simplest most effective solution .
EE's: We use silicon to make things  smaller!
 

Offline vmalletTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: us
Re: Sensing 12v line with 5v MCU
« Reply #22 on: April 25, 2019, 06:55:27 am »
What an interesting thread! From a seemingly innocuous question came a very informative discussion :)

Floobydust's suggestion looks like a winner: voltage-safe, draws no current in the common case (i.e. when the switch is open, even when the car is "off"), and bonus points for using components I already have in my bin :) (even though I now have PIC817s on order).

@Floobydust:
Typical levels are 12V vehicles +100V and -150V spikes max., 24V trucks are +200V and -600V spikes max.
I understand the 600v PIV in your design to be super safe on the spikes, but why the 1A rating?

Like @MadScientist suggested, my MCU power supply takes its ground from the battery so in the end I have a shared ground with the rest of the system.
 

Offline digsys

  • Supporter
  • ****
  • Posts: 2209
  • Country: au
    • DIGSYS
Re: Sensing 12v line with 5v MCU
« Reply #23 on: April 25, 2019, 07:41:04 am »
Quote from: floobydust
Opto-couplers aren't considered great for automotive environments, I'm not sure exactly why. Very few achieve automotive qualifications like AEC-Q100, 101 ....
In higher end vehicles / trucks etc, I see them used a lot. Often, it is a case of "minimize cost" and "if an alternative works ok, go with that".
Like in any field, not every design is "most optimal", works ok is acceptable. Just giving the OP options.
Quote from: MadScientist
I don’t see the point of an opto if the two grounds are ultimately connected as all the issues from load dumping will be seen on the ground anyway  ...
Similar reason we use star grounds in vehicles / building etc etc The FEWER Inputs / outputs that can pass through a single device, especially
something like a CPU, greatly reduces risk. Similar to only "earthing" 1 end of a shield etc etc
It completely eliminates that Input path via the CPU.
PS: I'd still go for the opto option, but that is up to you. You have options.
Hello <tap> <tap> .. is this thing on?
 

Offline magic

  • Super Contributor
  • ***
  • Posts: 6775
  • Country: pl
Re: Sensing 12v line with 5v MCU
« Reply #24 on: April 25, 2019, 08:29:00 am »
Sure ... what you can "get away with" and what is ADR compliant (ADR=Australian vehicle design rules) are completely "different' animals !! All countries have vehicle safety / wiring standards that are similar, especially IF they want to export.
They don't have electrical safety standards for no reason.
Well, the circuit I talked about was not an amateur job but a "smart" fuel pump relay from a pre-ECU Volkswagen car. I also peeked inside two LPG controllers for similar ECU-less engines and they too used nothing but resistors and diodes for input protection, going straight to some 40 series logic chips. All those components were built professionally and (hopefully) to standards, though standards may have been different 20-30 years ago. And they worked in practice.

Sure, the opto could "blow up", but it will cause a LOT less damage than "blowing up" a micro, that "may" also be in charge of OTHER safety functions !!
I think it likely will because LEDs have poor reverse voltage ratings and a series resistor helps nothing in that regard. At least I would add some series or antiparallel diode for protection.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf