Sensing 12v line with 5v MCU - Page 1
Forum section: electronics / beginners

author: vmallet
author_rank: Regular Contributor
timestamp: April 22, 2019, 10:47:52 pm
content: 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?

author: Circlotron
author_rank: Super Contributor
timestamp: April 23, 2019, 01:54:30 am
content: 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.

author: digsys
author_rank: Unknown
timestamp: April 23, 2019, 03:18:46 am
content: ... or for absolute safety / simplicity, just use an opto-coupler + resistor. Removes any pulse / fault / ground issues getting to the micro.

author: vmallet
author_rank: Regular Contributor
timestamp: April 23, 2019, 06:42:00 pm
content: Quote from: Circlotron on April 23, 2019, 01:54:30 amYou 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!

author: vmallet
author_rank: Regular Contributor
timestamp: April 23, 2019, 06:43:35 pm
content: Quote from: digsys on April 23, 2019, 03:18:46 amHow would you wire that up? I was trying to turn that around in my head but I'm not seeing it... Thanks.

author: rstofer
author_rank: Super Contributor
timestamp: April 23, 2019, 07:25:44 pm
content: 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.pdfNow, 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.

author: vmallet
author_rank: Regular Contributor
timestamp: April 23, 2019, 07:50:27 pm
content: Quote from: rstofer on April 23, 2019, 07:25:44 pmRight, 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!

author: Circlotron
author_rank: Super Contributor
timestamp: April 24, 2019, 01:11:13 am
content: Quote from: vmallet on April 23, 2019, 06:42:00 pmYep, 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.

author: Tek Tech
author_rank: Contributor
timestamp: April 24, 2019, 03:26:04 am
content: Quote from: vmallet on April 23, 2019, 06:43:35 pmThe 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.

author: agehall
author_rank: Frequent Contributor
timestamp: April 24, 2019, 06:27:08 am
content: 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?

author: vmallet
author_rank: Regular Contributor
timestamp: April 24, 2019, 07:25:01 am
content: 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).

author: digsys
author_rank: Unknown
timestamp: April 24, 2019, 08:19:51 am
content: Quote from: agehallThere 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 nevera 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

author: GeorgeOfTheJungle
author_rank: Super Contributor
timestamp: April 24, 2019, 08:52:14 am
content: Quote from: vmallet on April 24, 2019, 07:25:01 amNote that that, unlike the other circuit, will sense the relay as ON even if there's no relay.

author: agehall
author_rank: Frequent Contributor
timestamp: April 24, 2019, 08:58:15 am
content: Quote from: digsys on April 24, 2019, 08:19:51 amThat does make perfect sense.

author: magic
author_rank: Super Contributor
timestamp: April 24, 2019, 09:23:33 am
content: Quote from: digsys on April 24, 2019, 08:19:51 amSure, 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.

author: digsys
author_rank: Unknown
timestamp: April 24, 2019, 11:43:21 am
content: Quote from: magicSure ... what you can "get away with" and what is ADR compliant (ADR=Australian vehicle design rules) are completely "different' animals !! All countries havevehicle 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 themains !! 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 repairsover 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.

author: Ian.M
author_rank: Super Contributor
timestamp: April 24, 2019, 12:32:40 pm
content: 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. seeMicrochip TB3013for 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 ondebouncing.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!

author: mariush
author_rank: Super Contributor
timestamp: April 24, 2019, 12:54:58 pm
content: 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 diodeThis 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)

author: David Hess
author_rank: Super Contributor
timestamp: April 24, 2019, 04:51:40 pm
content: 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.

author: floobydust
author_rank: Super Contributor
timestamp: April 24, 2019, 07:04:19 pm
content: 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.

author: David Hess
author_rank: Super Contributor
timestamp: April 24, 2019, 09:39:29 pm
content: Quote from: floobydust on April 24, 2019, 07:04:19 pmOpto-couplers are not very environmentally robust for whatever reason. I have occasionally seen photo-interrupters used in their place.

author: MadScientist
author_rank: Frequent Contributor
timestamp: April 24, 2019, 11:04:01 pm
content: 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 anywaySurely 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 .

author: vmallet
author_rank: Regular Contributor
timestamp: April 25, 2019, 06:55:27 am
content: What an interesting thread! From a seemingly innocuous question came a very informative discussionFloobydust'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:Quote from: floobydust on April 24, 2019, 07:04:19 pmI 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.

author: digsys
author_rank: Unknown
timestamp: April 25, 2019, 07:41:04 am
content: Quote from: floobydustIn 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: MadScientistSimilar reason we usestar groundsin vehicles / building etc etc The FEWER Inputs / outputs thatcanpass through a single device, especiallysomething like a CPU, greatly reducesrisk. Similar to only "earthing" 1 end of a shield etc etcIt 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.

author: magic
author_rank: Super Contributor
timestamp: April 25, 2019, 08:29:00 am
content: Quote from: digsys on April 24, 2019, 11:43:21 amWell, 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.Quote from: digsys on April 24, 2019, 11:43:21 amI 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.

author: floobydust
author_rank: Super Contributor
timestamp: April 25, 2019, 09:29:08 pm
content: Quote from: vmallet on April 25, 2019, 06:55:27 amThe 600V 1A S1J diode is a jellybean part, lowest cost. Smaller diodes are to 400V max. and not much for popular parts <500mA.Using the PC817 opto-coupler will work too. You have to design for CTR dropping due to age (-50%) and at high temperatures.