How to monitor a wire for 12v with a 3.3v microcontroller

Thread Starter

ba58smith

Joined Nov 18, 2018
62
When using zeners, you don't need to have a resistive voltage divider with two resistors and then the zener. All you need to do is to provide one resistor that will allow the zener's rated current thru the zener, then connect the I/O pin to the junction between the zener and the resistor.

For example: if the max voltage is 12v, the zener is 3v3 and the zener's current is 5 mA, they you need (12v-3v3)/5mA = 1740 ohm
Somewhere, I got the idea that the Zener could accept only up to about 15V, and someone pointed out that the voltage surge from the pump when it starts and/or stops could be up to 20V. So I put the voltage divider in there to bring that 20V spike down to under 15V. It's quite possible that I'm wrong about the 15V limit on the Zener.
 

Norfindel

Joined Mar 6, 2008
326
What's you zener? Did you take a look at the datasheet?
There's a resistor in series to the zener, so the diode should only see it's rated voltage, and the rest will fall on the resistor. Of course, with the additional voltage, it will flow more current, and the regulation won't be that good, but just take a look for worst case voltage, and try to keep the current as close to normal operating levels for the zener as possible. Or you can always use a CC source to drive the zener, but that could be overkill unless there's a serious issue with the noise.
 

PhilTilson

Joined Nov 29, 2009
156
The ground wire coming out of the pump has a diode in it with a 1V forward voltage. By putting my "tap" into the anode side of the diode, I get about 1V between there and ground. And that's enough to turn the GPIO to HIGH. The pull-down resistor will keep it LOW when the pump is off. So, all in all, this looks like a pretty good solution, and far simpler than many others that have been considered. Thanks, all, for your input. I've learned a LOT!
As usual, some of the more enthusiastic members on here have tried to get you to build a solar-powered, high-voltage, nuclear cigarette lighter when all you really wanted was a match!

You really don't need zeners or anything more than some simple small-signal diodes. Connect the anode of the diode to the input of the μC with a 10K resistor going to Vdd. Connect the cathode to the pump side of the switch. When the switch is off, the voltage at the cathode will be around 12V, so the diode is reverse-biased and the 10K resistor pulls the input high. When the switch is on, the voltage at the cathode is 0V, so the μC input sees a LOW. No worries about voltage dividers, zeners or anything else. You don't specify what μC you are using, but if it has a Vdd of 5V, then almost certainly the inputs are 5V tolerant. As usual, the simplest answer is usually the best - if not quite so exciting!

Anyway, good luck with the project. A boat with FOUR bilge pumps eh? Must be quite something!

Drawing1.png Drawing1.png
 

Phil-S

Joined Dec 4, 2015
241
As usual, some of the more enthusiastic members on here have tried to get you to build a solar-powered, high-voltage, nuclear cigarette lighter when all you really wanted was a match!

You really don't need zeners or anything more than some simple small-signal diodes. Connect the anode of the diode to the input of the μC with a 10K resistor going to Vdd. Connect the cathode to the pump side of the switch. When the switch is off, the voltage at the cathode will be around 12V, so the diode is reverse-biased and the 10K resistor pulls the input high. When the switch is on, the voltage at the cathode is 0V, so the μC input sees a LOW. No worries about voltage dividers, zeners or anything else. You don't specify what μC you are using, but if it has a Vdd of 5V, then almost certainly the inputs are 5V tolerant. As usual, the simplest answer is usually the best - if not quite so exciting!

Anyway, good luck with the project. A boat with FOUR bilge pumps eh? Must be quite something!

View attachment 170232 View attachment 170232
Or a lot of holes.
A 12-V relay with changeover contacts would provide all the options.
 

Reloadron

Joined Jan 15, 2015
7,980
As usual, some of the more enthusiastic members on here have tried to get you to build a solar-powered, high-voltage, nuclear cigarette lighter when all you really wanted was a match!

You really don't need zeners or anything more than some simple small-signal diodes. Connect the anode of the diode to the input of the μC with a 10K resistor going to Vdd. Connect the cathode to the pump side of the switch. When the switch is off, the voltage at the cathode will be around 12V, so the diode is reverse-biased and the 10K resistor pulls the input high. When the switch is on, the voltage at the cathode is 0V, so the μC input sees a LOW. No worries about voltage dividers, zeners or anything else. You don't specify what μC you are using, but if it has a Vdd of 5V, then almost certainly the inputs are 5V tolerant. As usual, the simplest answer is usually the best - if not quite so exciting!

Anyway, good luck with the project. A boat with FOUR bilge pumps eh? Must be quite something!

View attachment 170232 View attachment 170232
Well that is a nice elegant and simple solution. However, your drawing is using low side switching on the pump. The thread starter mentions " I'm trying to monitor a bilge pump on my boat, so I can get an alarm whenever it comes on. It's a 12V pump with a float switch that turns it on. I want to "tap into" the wire between the float switch and the pump, and when the float switch closes, and the pump comes on, I want to "see" that on a microcontroller. The microcontroller has 3.3v GPIO pins. I also want to power the microcontroller from the boat's 12V system so I don't have to worry about replacing little batteries periodically, and the microcontroller is powered by 5V". Most automatic bilge pumps I have been familiar with use a float switch to switch the high side. Getting in there and rearranging things may not be that simple. I believe most of the replies were based on a high side switching arrangement as that is how it was presented.

Ron
 

Thread Starter

ba58smith

Joined Nov 18, 2018
62
As usual, some of the more enthusiastic members on here have tried to get you to build a solar-powered, high-voltage, nuclear cigarette lighter when all you really wanted was a match!

You really don't need zeners or anything more than some simple small-signal diodes. Connect the anode of the diode to the input of the μC with a 10K resistor going to Vdd. Connect the cathode to the pump side of the switch. When the switch is off, the voltage at the cathode will be around 12V, so the diode is reverse-biased and the 10K resistor pulls the input high. When the switch is on, the voltage at the cathode is 0V, so the μC input sees a LOW. No worries about voltage dividers, zeners or anything else. You don't specify what μC you are using, but if it has a Vdd of 5V, then almost certainly the inputs are 5V tolerant. As usual, the simplest answer is usually the best - if not quite so exciting!

Anyway, good luck with the project. A boat with FOUR bilge pumps eh? Must be quite something!
PhilTilson - I like it! I'll give it a try. When choosing the diode, is it the 12V from the pump side trying to "crash through" the diode that I need to be concerned about? And if so, what, exactly, should I look for in the datasheet of the diode? Is it "Reverse voltage" or "Repetitive peak reverse voltage"? Maybe "Breakdown voltage"? Or something else? This is the diode I have handy that I will use if it's suitable.
https://www.vishay.com/docs/81857/1n4148.pdf

The boat isn't all that big (52' trawler), but I have two separate bilge areas, each with two bilge pumps - the little one for the very small amounts of water that only occasionally get into the bilge, and the big one for when all hell breaks loose.

Reloadron - I appreciate your concern, but I can implement PhilTilson's approach without any trouble.
 
Last edited:

Reloadron

Joined Jan 15, 2015
7,980
Reloadron - I appreciate your concern, but I can implement PhilTilson's approach without any trouble.
Then that is a great and simple solution. Just about any diode will work. The +5 Volts and 10K resistor act as a pull up. So your micro controller DIO pin is held normally high. When the pump is switched on that point will go Low and the diode will conduct so now your DIO becomes a Logic Low. Just about any 1N4000 series diode should work as a switching diode, 1N4001 or for that matter even a 1N4148 or 1N914 will work.

The uC will normally see a logic high on your chosen DIO pin and when the pump is switched On the DIO pin will see a logic low. Just write your code accordingly.

Ron
 
I think the simplest solution would just be a 12V reed relay with its coil connected across the pump. A pull up resistor to 3.3 volts at the input may be useful.
 

Thread Starter

ba58smith

Joined Nov 18, 2018
62
OK, the above circuit works great! Thanks @PhilTilson! Now... I have another pump I want to monitor, but this time, my only access to the wiring is AFTER the swtich and BEFORE the pump. Trying to keep it simple, I'm proposing this, below. When the switch is closed, approx. 3.3V will go through the Zener, then be divided in half by the voltage divider, and about 1.8V will make it to the GPIO. (I've tested the little Zeners and they don't always step voltage down as low as they're labeled, so to be safe, I'll just cut the output in half, which is still way more than enough to send the pin HIGH.)
When the switch is open, R2 will pull the GPIO to LOW.
Correct?
upload_2019-2-16_6-52-23.png
 

Attachments

Thread Starter

ba58smith

Joined Nov 18, 2018
62
@BobTPH - I sure did screw that up! Let me try again, below. You show a resistor between the switch and the Zener, which I've represented as R3. Is that necessary, with the two resistors (used as a voltage divider) AFTER the Zener? To recap, I want to take whatever voltage passes by the Zener (to the right) and reduce it by 50% to make sure I have a safe input for my 3.3V GPIO pin. I hope what this gives me is:
- Switch closed, pump running, approx. 3.3V just past the Zener, then 1/2 of that to the GPIO.
- Switch open, pump not running, GPIO pulled to LOW / GND by R2.
Thanks!

upload_2019-2-16_12-0-13.png
 

crutschow

Joined Mar 14, 2008
38,952
I see no reason for a more complex circuit or the need for a Zener.
Just use the same diode circuit that you used for the first pump.
In this case the low motor impedance when off will pull the input to ground, so the input low is motor OFF and high is ON.
 

Thread Starter

ba58smith

Joined Nov 18, 2018
62
@ba58smith yes you need R3. You do not need or want the other two resistors.

Bob
Sorry for being dense, but after the Zener (to the right of it), the voltage is in the range of 3.3V to 3.7V - I know this because I have tested several of my 3.3V Zeners. But I don't have a lower voltage Zener... and my GPIO pins take a max of 3.3V. (Min of 0.75 to be considered HIGH.) So, how can I ensure I don't damage my GPIO by giving it more than 3.3V, if the Zener will sometimes let more than 3.3V go by it?
 

Thread Starter

ba58smith

Joined Nov 18, 2018
62
I see no reason for a more complex circuit or the need for a Zener.
Just use the same diode circuit that you used for the first pump.
In this case the low motor impedance when off will pull the input to ground, so the input low is motor OFF and high is ON.
I'm sure you noticed that in the second circuit, the output to the GPIO is AFTER the switch and before the pump, which is the opposite of the first circuit. So it looks to me like, when the switch is closed, the GPIO pin will go HIGH, as you say, but with 12V, which will fry it, won't it? It's a 3.3V max input GPIO. But hey, I'm so new to this, I could easily be missing something. Thanks for any clarification you can offer.
 

JohnInTX

Joined Jun 26, 2012
4,787
I would consider an opto isolator with an AC (back to back LED) input with an appropriate series resistor to limit the LED current and a 100-300 ohm resistor across the LED terminals to set a current threshold. That way any and all electrical horribleness that occurs in the bilge, including effects of ham handed reverse-wiring installers, won't be transmitted to your delicate 3V3 uC. I've actually dealt with things like resistor dividers, zeners and back biased diodes and .... now I always use optos.

Also, consider using an automotive rated voltage regulator instead of the 7805. ST makes lots of them and they are specifically designed to withstand the other electrical horribleness - reverse polarity, alternator load dumps etc. - that auto/boat electrical systems can dish out. Again.. been there.

YMMV
 
Top