Detect a power supply with PNP Transistor?

I’ve been working on an idea where, for the first time, I’ve got a MCU switching between two power sources.

I’ve got a USB source and a 5v3a supply.
Thanks to many of you here, I’ve now got lots of that working. :pray:
Today I got some code I’m nearly happy with.

Something I didn’t realize I would need until now is the ability to detect when the external power supply comes on and off. It’s possible that the 3a supply is added in later, and my MCU will need to detect that, and re-run a C++ function.

Here is a circuit I’ve come up with. I’m representing my external wall power supply as a battery and the idea is that plugging in the battery will cause the base to ground, saturating(?) the PNP transistor. I can than read @DAT4 to detect weather my power supply is connected.

I’m hoping that I have :

  1. a collector with a higher voltage than my emitter
  2. a 60kohm resistor to make sure there is always at least 0.7v difference.

I’ve used NPN a little bit now, but this is my first time using a PNP.

Does anyone spot any improvements or mistakes?

Hi Pix

Base is connected to ground anyway so I don’t see what plugging the battery is going to do.
Cheers Bob

1 Like

Ah yeah bummer!! I need a common ground so the base will always have a return path.
Bummer. Ill have to think of something else.
Thanks Bob.

The PNP doesnt work.
I actually think and NPN would work.
However while I was iterating over it I spent some time with voltage regulators.
Then I remembered that shunt regulators are a thing.

This is what I came up with.

Its not as good as the theoretical but 3V will be sufficient to be detected as HIGH logic level on any MCU.

Hi Pix
Your calculations are not quite correct.
There will be 1.7V dropped across R1 therefor the current will be 1.7/56 = 30mA
The same 30mA will flow through the Zener
Due to the very high input impedance of the MCU input there will be virtually no current (so no voltage drop) through R2. You can check this by measuring the voltage ACROSS R2. Should be zero very close depending on your meter resolution.
Cheers Bob
By the way this is as good a method as any to detect your 5V supply. simple and not too exotic.

1 Like

Oh.
Ive calculated 5v/65Ω which is why i got 3 times the actual value.
I think given the high imepedance of the pin and the much lower current I probably dont need R2 at all.

Won’t do any harm

Cheers Bob

1 Like

Given its 5v and you need it 3.3 volt logic, you could just use a logic level shifter.

So when 5V is on, it will drop that down to 3.3v and now safe for your GPIO read “1”.When its 0v, the GPIO will show “0”

Hi Michael

That is true BUT:
A logic level shifter is usually DRIVEN high and low and works pretty well. When this 5V is removed this input will “float”, that is have nothing connected so the output could go either way. maybe not and I could be wrong here but I am not going to experiment. The zener method would be a form of shifter but only one way. The logic level shifter works both ways but that is not needed here.
Cheers Bob