Switching a 12v voltage divider on/off with a transistor & an Arduino

After a full day of fumbling around with this simple circuit, I've come to the conclusion that I have a fundamental lack of understanding about transistors.

Here's what I'm trying to do:
Create a power distribution board with a 12 volt and 24 volt input/output. Both 12 and 24 volt rails need to have a battery monitoring circuit, which I need to have switchable via a transistor so that the voltage divider isn't always on and wasting battery charge.

I thought I could do this with a single PNP just cutting off voltage to the divider circuit, but I couldn't get that working (PNP wouldn't switch off when I cranked the power supply up to 12v).

So I played around with a PFET (IRF4905) and NPN (BC547B) and finally got something working (see diagram below), but I don't know if it's the best solution. This will also have to be applied to the 24 volt rail, but I don't have a 24 volt supply to test it so it's all guess work at this point.

Any tips would be appreciated, thank you.

You'll want to high side the mosfet to make the idea work properly.

I used 2n2700's on the low side, it did not work as well as on the high side.

The single PNP didn't work because there is a protection diode from the Atmel's GPIO pin to the Vcc pin. That provided a path for the transistor's base current even when the GPIO pin is output high, or even an input. So the transistor would always be on.

Also, for future reference, the datasheet will show an "Absolute Maximum" voltage that can be applied to a GPIO pin, and it's usually something like Vcc + 0.3V. So to switch, or read, voltages higher than Vcc, you typically need two transistors, as you have found in this case, one of which protects the GPIO pin from excessive voltage.

I think your diagram looks ok except that the resistor values won't work if the supply is 24V - that would put A0 at 8V, which is too high. I'm assuming your Arduino is running at 5V. If it's 3.3V, the resistors won't work at 12V either.

In picking the mosfet, you need to be careful that its gate/source voltage limit is enough to handle 24V. What mosfet are you using now?

A side note about drawing circuits. Typically, the upper rail is shown at the top and ground at the bottom. So your drawing will look upside down to most people here. That's why Idahowalker thought you had the mosfet on the low side.

Thank you for the info, that's helpful.

I'm a little lost on your first statement, as I don't really understand how current can flow from base-to-pin when the GPIO pin is high. Is this because the emitter voltage is 12v, so it overpowers the 5v out from the pin? As you can probably tell, I'm still trying to wrap my head around this stuff. During the course of troubleshooting, I managed to fry at least one analog pin and one digital pin.

The PFET is an IRF4905 and NPN is a BC547B. The IRF4906 datasheet says Vgs of +-20, so am I correct in assuming this will not work with the 24 volt rail?

I forgot to mention that I have different divider resistor values for the 24 volt side, 6.2k and 1.5k. Another thing I am curious about is whether or not I should keep those resistances as-is or if I should make them much higher in order to decrease current draw. As long as I keep the ratio, it should work right?

Also thanks for the tips on the schematic, I will do it top-bottom left-right in the future.

I assume that with the single PNP transistor version you had the emitter connected to 12V, the collector to the top of the divider network, with the network's bottom at ground. Then you had the base connected through a resistor to the I/O pin. You expected that with the I/O set to output, high, no current could sink through the I/O to ground. That would be true if the emitter were connected to 5V.

The problem is that completely independent of how you've programmed the port pin, there is a forward-biased diode from that pin to the Vcc pin. So if any voltage more than one diode drop above the Vcc voltage is applied to the I/O pin, current will flow from the pin into the Vcc power rail. The purpose of the diode is to protect the I/O pin from being damaged by high voltage.

So in addion to the base-resistor-I/O pin path, there is another parallel path after the resistor which goes through a diode to Vcc. With any emitter voltage above about 5.6V, base current will flow, and the transistor will be at least partially turned on. You can try this yourself by disconnecting the I/O pin completely, and connecting the PNP base through a resistor and a forward-biased diode to the 5V rail. The transistor will be on.

The IRF4905 gate voltage limit might well be a problem in the 24V supply. But you could add a resistor between its gate and the collector of the NPN, so the gate would be at the midpoint of a voltage divider when the NPN turns on. So it might only go half way to ground, which is still enough of a drop to turn it on. Or, traditionally, a Zener diode might be used instead.

Yes, your resistors can be much higher values. At some level things might not work right, but I would think anything up to 100K would be ok, including R13. Things will slow down a bit, but you aren't switching anything rapidly, and current involved is very low. You might need to insert a little delay before actually reading the voltage on A0 to make sure the mosfet has turned on, but you can test to see if such a delay makes any difference.

For voltage measurements you should use the internal 1.1volt reference (assuming Arduino Uno), not default Aref.
Then you must have a voltage divider to ~1volt.

You can switch that on/off with a single logic level N-channel mosfet.

10k resistor between source and ground, source also to an analogue pin.
gate to a digital pin (on/off).

Leo..

The drawing is OK, just upsidedown, +V should be at top, GND at bottom. :slight_smile:

Something doesn't look quite right about that diagram! :astonished:

Be careful with the connections ! If the ground connection failed you could put 11 volts on the analog input and destroy the processor.
I would up the divider resistors a bit to limit any fault current into the chip , say R5 around 22k and R3 to suit and put a zener diode across input to ground ( say 2v7 if you are’s using the 1.1v ref) .

Thanks everyone for the tips! All of this is extremely helpful. I don't completely understand the single N-channel mosfet method and the purpose of using 1.1v instead of 5v, I think I just need to do some more studying on this stuff. I know less than I thought I did.

Also here's a correctly oriented diagram for you sticklers!

Wawa:
For voltage measurements you should use the internal 1.1volt reference (assuming Arduino Uno), not default Aref.
Then you must have a voltage divider to ~1volt.

You can switch that on/off with a single logic level N-channel mosfet.

10k resistor between source and ground, source also to an analogue pin.
gate to a digital pin (on/off).
~22k resistor (for 24volt) between voltage source and drain.
Leo..

It seems for the 12V supply you could have a 100K drain resistor and a 10K source resistor, and a perfect mosfet would read 1.09V at the source. Change the 10K to 4.7K for 24V, and get 1.08V at the source.

Thanks for posting this suggestion. I hadn't thought of switching these dividers that way.

djm227:
Thanks everyone for the tips! All of this is extremely helpful. I don't completely understand the single N-channel mosfet method and the purpose of using 1.1v instead of 5v, I think I just need to do some more studying on this stuff. I know less than I thought I did.

He's saying you can stick a mosfet in the middle of the divider network. When you drive the gate low, the mosfet is off, and no current flows through the divider. But when you raise the gate to 5V, that's enough to turn on a logic level mosfet because the source only comes up to no more than 1.1V. Not sure about his 22K and 10K resistors though. Per my previous reply, I think you would use 100K and 10K for the 12V rail, and 100K and 4.7K for the 24V rail.

Note that this doesn't work with an NPN bipolar because the emitter voltage would be set by the base driver circuit, and would be the same no matter what voltage the power rail has.

ShermanP:
Not sure about his 22K and 10K resistors though.

Oops, should have been 220k:10k.
Corrected.

10/(220+10)= 1.0435volt.
Leo..

Also here's a correctly oriented diagram for you sticklers!

TNX for that but excuse me, I have to run and stickle. ;D

ShermanP:
I assume that with the single PNP transistor version you had the emitter connected to 12V, the collector to the top of the divider network, with the network's bottom at ground. Then you had the base connected through a resistor to the I/O pin. You expected that with the I/O set to output, high, no current could sink through the I/O to ground. That would be true if the emitter were connected to 5V.

The problem is that completely independent of how you've programmed the port pin, there is a forward-biased diode from that pin to the Vcc pin. So if any voltage more than one diode drop above the Vcc voltage is applied to the I/O pin, current will flow from the pin into the Vcc power rail. The purpose of the diode is to protect the I/O pin from being damaged by high voltage.

So in addion to the base-resistor-I/O pin path, there is another parallel path after the resistor which goes through a diode to Vcc. ** With any emitter voltage above about 5.6V, base current will flow, and the transistor will be at least partially turned on**. You can try this yourself by disconnecting the I/O pin completely, and connecting the PNP base through a resistor and a forward-biased diode to the 5V rail. The transistor will be on.

In all of the circuits shown, how can that happen, the emitter of the T4 is connected to GND.
Tom..... :slight_smile:

TomGeorge:
In all of the circuits shown, how can that happen, the emitter of the T4 is connected to GND.
Tom..... :slight_smile:

It was in response to this part of his original post:

"I thought I could do this with a single PNP just cutting off voltage to the divider circuit, but I couldn't get that working (PNP wouldn't switch off when I cranked the power supply up to 12v)."

and to further discussion in replies 2 and 3.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.