# Detect a power supply with PNP Transistor?

**URL:** <https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728>\
**Category:** Uncategorized\
**Created:** [26 January 2025 10:30 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728 "2025-01-26T10:30:51Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [26 January 2025 10:30 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/1 "2025-01-26T10:30:51Z")

</div>

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. 🙏  
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.

 ![474145763_3892365317705256_8431103058963447199_n](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/f/ff08deadb3863098183385772be5f571e81cce3e.jpeg)

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?

---

<div class="post-metadata">

**Author:** ![Robert93820](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@Robert93820](https://forum.core-electronics.com.au/u/Robert93820)\
**Post date:** [26 January 2025 10:51 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/2 "2025-01-26T10:51:15Z")

</div>

Hi Pix

> [@Pixmusix](#):
>
> plugging in the battery will cause the base to ground,

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

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [26 January 2025 11:21 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/3 "2025-01-26T11:21:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [26 January 2025 23:38 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/4 "2025-01-26T23:38:04Z")

</div>

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.

 ![20250127_103423](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/2/2384e0171f75b066e9aa3f20d6d11e9a2e23510e.jpeg)

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

 ![20250127_103439](https://canada1.discourse-cdn.com/flex028/uploads/core_electronics/original/2X/2/29bea735a2657068a3fddbcc7953dc09cc79a30d.jpeg)

---

<div class="post-metadata">

**Author:** ![Robert93820](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@Robert93820](https://forum.core-electronics.com.au/u/Robert93820)\
**Post date:** [26 January 2025 23:55 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/5 "2025-01-26T23:55:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pixmusix](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/pixmusix/32/9719_2.png) [@Pixmusix](https://forum.core-electronics.com.au/u/Pixmusix)\
**Post date:** [27 January 2025 00:21 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/6 "2025-01-27T00:21:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Robert93820](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@Robert93820](https://forum.core-electronics.com.au/u/Robert93820)\
**Post date:** [27 January 2025 00:22 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/7 "2025-01-27T00:22:48Z")

</div>

Won’t do any harm

Cheers Bob

---

<div class="post-metadata">

**Author:** ![Michael99645](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/michael99645/32/11893_2.png) [@Michael99645](https://forum.core-electronics.com.au/u/Michael99645)\
**Post date:** [27 January 2025 00:34 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/8 "2025-01-27T00:34:36Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![Robert93820](https://avatars.discourse-cdn.com/v4/letter/r/ac91a4/32.png) [@Robert93820](https://forum.core-electronics.com.au/u/Robert93820)\
**Post date:** [27 January 2025 01:05 UTC](https://forum.core-electronics.com.au/t/detect-a-power-supply-with-pnp-transistor/21728/9 "2025-01-27T01:05:07Z")

</div>

Hi Michael

> [@Michael99645](#):
>
> When its 0v, the GPIO will show “0”

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
