logo elektroda
logo elektroda
X
logo elektroda

Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor

petrykcom 2730 46

TL;DR

  • A 5V MCU can drive a 12V or 24V relay directly using a PNP high-side switch and one Zener diode.
  • The Zener sits between the PNP base and MCU pin, so a LOW output turns the transistor on and a HIGH output keeps it off.
  • For 12V, a 9.1V Zener fits; for 24V, 20V or 22V works, with the condition VCC-5.7V < Vz < VCC-0.7V.
  • The circuit tolerated ±5% Zener variation and supply changes, but it still needs a 1N4148 flyback diode and a sensible base resistor, such as 1kΩ.
Generated by the language model.
ADVERTISEMENT
This content has been translated flag-pl » flag-en View the original version here
📢 Listen (AI):
  • In engineering practice, there is often a need to control a relay or other receiver connected to a higher voltage (12V or 24V) using a signal from a microcontroller operating with 5V logic. The standard solution is to use an additional NPN transistor or optoisolator, which increases the number of components and cost.

    This article presents an alternative, industry-proven solution that requires only a single Zener diode selected to suit the supply voltage. The circuit has been used since the early 2000s in industrial controllers for the production of Europallets and still works reliably today.
    Assumptions

    Microcontroller supplied with 5V (e.g. AT89C2051, AVR, PIC)

    PNP transistor (e.g. BC557, 2N2907) working as a high-side switch

    Receiver supply voltage: V_CC = 12V or 24V

    Receiver: relay, coil, lamp, etc. connected between collector and ground

    Commercially available Zener diodes with ±5% tolerance

    Schematic diagram
    
    
                             V_CC (12V lub 24V)
                                  │
                                  │
                                  ├───────────┐
                                  │                         ┌─┴─┐
                                  │                         │         │
                            ┌─┴─┐                    │    R  │ Rezystor podciągający przy braku przewodzenia diody zenera
                             Emiter                     │         │   
                                  │                        └─┬─┘
                PNP          │                               |
            (np. BC557) │  Baza ───────┐
                                  │                        ┌─┴─┐ Katoda                 
                                  │                        │         │
                             Kolektor                 │  Dz  │
                            └─┬─┘                   │        │
                                  │                        └─┬─┘ Anoda
                                  │                              │
                                  │                              │
                                  │                              │
                                  │                        ┌─┴─┐
                                  │                        │         │
                                  │                        │  Rb  │  rezystor bazy
                                  │                        │         │  (1kΩ)
                                  │                        └─┬─┘
                                  │                              │
                                  │                              │
                                  │                do pinu wyjściowego           
                                  │                   mikrokontrolera
                            ┌─┴─┐
                            │         │                        
                            Cewka                 
                            └─┬─┘
                                  │                   
                                  │                   
                                  │                   
                               GND                
                                                 

    The receiver (relay coil) is connected between the PNP collector and GND. A surge diode (1N4148) in parallel to the coil is necessary but not shown for readability.
    Operating principle

    A PNP transistor conducts when its base is about 0.7V lower than its emitter, ie:

    V_B ≈ V_CC - 0.7V

    For V_CC = 24V: V_B ≈ 23.3V
    For V_CC = 12V: V_B ≈ 11,3V

    A Zener diode connected in series between the base and the microcontroller pin conducts in the reverse direction (as a Zener) only if the reverse voltage on it exceeds its Zener voltage Vz.
    Case 1: Microcontroller pin in LOW state (0V)

    The reverse voltage on the Zener is:
    V_rev = V_B - V_LOW ≈ V_CC - 0.7V - 0V

    For V_CC=24V: V_rev ≈ 23.3V
    For V_CC=12V: V_rev ≈ 11,3V

    If Vz is appropriately selected (see table), the Zener will break through, the base current can flow and the transistor turns on. The receiver is switched on.
    Case 2: Microcontroller pin in HIGH state (5V)

    The reverse voltage on the Zener is:
    V_rev = V_B - V_HIGH ≈ V_CC - 0.7V - 5V

    For V_CC=24V: V_rev ≈ 18.3V
    For V_CC=12V: V_rev ≈ 6,3V

    If Vz is higher than this value, the Zener does not punch through, the base current does not flow and the transistor remains off. The receiver is switched off.
    Selection of Zener voltage Vz

    For the circuit to operate reliably, the Zener voltage must meet the condition:

    V_CC - 0.7V - 5V < Vz < V_CC - 0.7V - 0V

    Simplifying:

    V_CC - 5.7V < Vz < V_CC - 0.7V
    Table of practical values
    Supply voltage V_CC Theoretical range Vz Recommended Vz nominal Available Zener Notes
    12V 6.3V < Vz < 11.3V 9.1V BZX55C9V1 Wide margin. Works with 8.2V or 10V
    24V 18.3V < Vz < 23.3V 20V or 22V BZX55C20 or BZX55C22 For 20V, more reliable switching; for 22V, more reliable switching
    Influence of tolerance

    For V_CC=24V and Vz=20V (±5% = 19V..21V):

    Minimum V_rev at LOW state (23.1V) > 21V → Zener always conducts

    Maximum V_rev at HIGH state (18,3V) < 19V → Zener never conducts

    The circuit is immune to Zener production tolerance and supply voltage variations.
    Practical considerations

    The base resistor Rb: 1kΩ is a universal value. For higher collector currents (above 100mA) it can be reduced to 470Ω.

    Microcontroller pin current: In the LOW state, the pin is shorted to ground by the internal N-MOS transistor. The current flowing through the pin is approximately (V_CC - 0.7V - V_LOW) / (Rb + R_zener). For V_CC=24V, Rb=1kΩ, ignoring the Zener resistance, the current is about 23mA - which is within the typical 20-40mA limit for most microcontrollers. For safety, Rb can be increased to 2.2kΩ.

    Zener diode: Use 0.5W or 1W types. It is always switched on in the ZENER (breakdown) direction, never in the conduction direction.

    Surge protection: When controlling the relay coil, it is essential to use a 1N4148 diode in parallel to the coil (cathode to collector, anode to GND).

    History and authorship

    This circuit was first developed and used by myself (Wojciech Petrykowski) in the early 2000s in industrial machine controllers for manufacturing .... [unfortunately I cannot disclose what ;) ]. It still works reliably today. It reappeared in the English-language literature in 2023 on circuitlab.com as "DrivingAPNPfromMCU", but with no author cited.

    He hereby documents his independent discovery and many years of industrial use of this solution.

    Cool? Ranking DIY
    About Author
    petrykcom
    Level 13  
    Offline 
    petrykcom wrote 61 posts with rating 19. Live in city Kalisz. Been with us since 2007 year.
  • ADVERTISEMENT
  • #2 21893012
    tos18
    Level 42  
    Posts: 7031
    Help: 1093
    Rate: 1816
    petrykcom wrote:
    This article presents an alternative, industry-proven solution that requires only a single Zener diode sized appropriately for the supply voltage.

    Not true - it also requires a PNP transistor and a resistor

    You will forgive me, but in practice your colleague's solution contains one more element than the classic solution with the NPN transistor and is more susceptible to fluctuations in the supply voltage.Just a jump of a few V and all the relays will switch on.
    While with a 12V supply a few volts is quite a lot, with 24V (the voltage used in industry) it is no longer so rosy.

    Two relay driver schematics: NPN BC547 and PNP BC557 with 1N4004 diodes and base resistors
  • #3 21893053
    Andrzej_Tomaszewski
    Level 13  
    Posts: 270
    Help: 2
    Rate: 338
    petrykcom wrote:
    This article presents an alternative, industry-proven solution that requires only a single Zener diode selected appropriately for the supply voltage.

    This text encouraged me to read further, mainly hoping that a transistor would not be needed,.... but no. It only got worse further on. Where did the idea come from to present the circuit diagram in this way? In every respect the circuit is inferior to the well-known solution using an NPN transistor.

    petrykcom wrote:
    This documents his independent discovery
    A colleague exposes himself to ridicule.
  • #4 21893068
    gulson
    System Administrator
    Posts: 29274
    Help: 148
    Rate: 5994
    Thanks for sharing the article.
    The schematic is a bit off, I drew using the GPT Image 2 model

    Diagram: controlling a 12/24 V load from a 5 V MCU using a PNP transistor and Zener diode
    (I hope the model is not wrong)
  • #5 21893100
    viayner
    Level 43  
    Posts: 10575
    Help: 1557
    Rate: 2009
    Hello,
    gulson wrote:
    Thanks for sharing the article.
    The schematic is a bit off, I drew using the GPT Image 2 model

    Diagram: controlling a 12/24 V load from a 5 V MCU using a PNP transistor and Zener diode
    (I hope the model is not wrong)

    i take it that this is a rhetorical question or did the fellow not look at the picture before inserting?
    I have simulated this for myself in LTspice and I have 11 volts on the output pin (GPIO), which is hardly the point.
    Greetings
  • #6 21893102
    krzbor
    Level 29  
    Posts: 1735
    Help: 40
    Rate: 1046
    Just why such an arrangement? What are its advantages over the classic emitter on ground - i.e. OC? I can only see the disadvantages - an additional resistor and Zener diode and the hassle of changing the voltage. Plus NPN transistors are more popular.
  • #7 21893110
    goose-berry
    Level 15  
    Posts: 91
    Help: 10
    Rate: 27
    You can see by the diagram that it is a product of artificial intelligence. Specifically a copilot...
  • #8 21893158
    petrykcom
    Level 13  
    Posts: 61
    Rate: 19
    >>21893110 Well, as if someone had been in electronics for a long time, they would know what it is for and why .....
    First of all, it is not a product of artificial intelligence, it is mine and why is it simple.... for someone who has been working with uC for a long time. When the microcontroller (the one I just needed and many others) is switched on, there is a logic 1 at its output, so when the power is switched on for a short while until the IO is initialised, the relay controlled by the NPN transistor would be switched on. If the uC hangs up when switched on in some cases this could cause a disaster. Of course you could use an inverter e.g. x06 or add a PNP transistor behind the NPN but the idea was to keep the number of components as small as possible and simplify. As for the stability of the power supply, yes it is required, but not so drastically. In fact, in this circuit (as it used to be done), the 7812 was used and it provided the required level of stability without any problems.
  • ADVERTISEMENT
  • #9 21893163
    kulmar
    Level 33  
    Posts: 1897
    Help: 184
    Rate: 368
    petrykcom wrote:
    After switching on the microcontroller (the one I just needed and many others) there is a logical 1 at its output

    Could you specify the type of this microcontroller?
  • #10 21893165
    petrykcom
    Level 13  
    Posts: 61
    Rate: 19
    viayner wrote:
    Hello,
    gulson wrote:
    Thanks for sharing the article.
    The schematic is a bit off, I drew using the GPT Image 2 model

    Diagram: controlling a 12/24 V load from a 5 V MCU using a PNP transistor and Zener diode
    (I hope the model is not wrong)

    i take it this is a rhetorical question or did the fellow not look at the picture before inserting?
    I have simulated this for myself in LTspice and I have 11 volts on the output pin (GPIO), which is hardly the point.
    Greetings


    Please study the schematic and voltages carefully - that's what the zener diode is for, so that voltages above 5V don't appear on the uC ..... and they don't.... this is where the brilliance of simplicity is revealed !!!

    Added after 5 [minutes]:

    aND here is one "quibble" to the value of the diode at 24V. because ideally you would give a 20.8V diode then you would have to assemble it from two 19V and a 1V8.... Also, it's not that the solution is better or worse..... just that I've come across many attempts to control with a PNP transistor and in all cases the authors gave up because they found it impossible.... and yet...

    Added after 1 [minute]:

    e.g. AT89S8252, AT89C2051 etc, etc... i have dozens (if not hundreds) more of them backlogged.....

    Added after 6 [minutes]:

    and lest it be that it wasn't me or that it wasn't there :)
    Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor

    Added after 1 [minute]:


    Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor

    Added after 4 [minutes]:

    i even managed to find a diagram of the whole thing which I attached....


    Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor

    Added after 1 [minute]:


    Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor
  • #11 21893236
    Andrzej_Tomaszewski
    Level 13  
    Posts: 270
    Help: 2
    Rate: 338
    kulmar wrote:
    petrykcom wrote:
    When the microcontroller (the one I just needed and many others) is turned on, there is a logical 1 at its output

    Could you specify the type of this microcontroller?


    This is the case with the 89C2051. Once in my project taking this into account I gave 2 NPN transistors in the EC circuit. Plus two resistors for the base currents, and a standard rectifier diode + relay. The whole thing is powered the same as the uC, i.e. +5Vdc, and the relay coil from +12Vdc and it works great, although I wouldn't call it some kind of discovery.
  • #12 21893324
    tos18
    Level 42  
    Posts: 7031
    Help: 1093
    Rate: 1816
    The common base level shifter circuit has been used for a long time in broadcasting technology, e.g. as a video amplifier with operating voltages above 100V

    here as a stage for an "unruly" microcontroller

    Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor
  • #13 21893347
    petrykcom
    Level 13  
    Posts: 61
    Rate: 19
    >>21893324 I understand and know the solution perfectly well... but it was precisely the use of just one transistor !
  • #14 21893348
    pawlik118
    Level 33  
    Posts: 2410
    Help: 201
    Rate: 596
    In the case of the 8051, controlling the logical 'zero' in this way makes sense, because before the processor reset, in the circuit if the processor controlled the NPN transistor, all relays would switch on for a while.
  • ADVERTISEMENT
  • #15 21893366
    krzbor
    Level 29  
    Posts: 1735
    Help: 40
    Rate: 1046
    petrykcom wrote:
    e.g. AT89S8252, AT89C2051 etc, etc.... i still have dozens (if not hundreds) of them backlogged.....

    Except they don't have a "1" on the output, which at start-up is the input. They have a pull-up. All you need is a 2.2k resistor to ground to bring this below the transistor trip threshold.
  • #16 21893441
    Sam Sung
    Level 33  
    Posts: 2005
    Help: 227
    Rate: 574
    krzbor wrote:
    petrykcom wrote:
    e.g. AT89S8252, AT89C2051 etc, etc.... i still have dozens (if not hundreds) of them backlogged.....

    Except they don't have a "1" on the output, which at start-up is the input. They have a pull-up. You just need a 2.2k resistor to ground to bring this below the transistor trip threshold.
    OK, and then how do you programmatically exceed the transistor trip threshold?
  • ADVERTISEMENT
  • #17 21893462
    kulmar
    Level 33  
    Posts: 1897
    Help: 184
    Rate: 368
    Sam Sung wrote:
    OK, and then how to programmatically override the transistor trip threshold?

    Issue a logic one to the port.
  • #18 21893464
    Citizen75
    Level 30  
    Posts: 2389
    Help: 155
    Rate: 460
    Andrzej_Tomaszewski wrote:
    This text encouraged me to read further, mainly hoping that a transistor would not be needed,.... but no.
    Here you have a description of micro relays that can be driven directly from the microcontroller port, without using transistors and protection diodes.

    gulson wrote:
    (I hope the model isn't wrong)
    The transistor "looks weird". Especially its base.
  • #19 21893479
    Andrzej_Tomaszewski
    Level 13  
    Posts: 270
    Help: 2
    Rate: 338
    Citizen75 wrote:
    Here you have a description of the micro transmitters


    Yes, I read this article.
  • #20 21893519
    krzbor
    Level 29  
    Posts: 1735
    Help: 40
    Rate: 1046
    kulmar wrote:
    Sam Sung wrote:
    OK, and then how to programmatically override the transistor trip threshold?

    Issue a logic one to the port.

    I thought it was obvious :) This extra resistor will load the output with 2mA - with mains power it doesn't matter.
  • #21 21893524
    gulson
    System Administrator
    Posts: 29274
    Help: 148
    Rate: 5994
    viayner wrote:
    I understand that this is a rhetorical question or rather the colleague did not look at the picture before inserting?
    I simulated it in LTspice and I have 11 volts on the output pin (GPIO), which is rather not the point.

    Informative: I just redrew the schematic knocked down in ASCII from the first post when testing the pictorial model, the task was not to analyse the schematic or simulate it or correct the symbols, it was to be redrawn ASCII.
  • #22 21893578
    djfarad02
    Level 19  
    Posts: 509
    Help: 7
    Rate: 675
    kulmar wrote:
    Sam Sung wrote:
    OK, and then how to programmatically override the transistor trip threshold?

    Issue a logical single to the port.


    I see it poorly with these parameters:
    Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor
    These ports do not have complementary pairs at the output

    The author's solution is not very elegant but it works. I would use a stage with a common base, as suggested earlier. Or better still, I would make a hardware watchdog that applies power to the relays only when there is a square wave on a specific uC pin. This is implementable on several elements (common to all relays). You can then go back to controlling the relays by ground, which allows you to use one NPN transistor and a resistor. In sum, the circuit will be simpler than these zeners and resistors
  • #23 21893593
    Sam Sung
    Level 33  
    Posts: 2005
    Help: 227
    Rate: 574
    krzbor wrote:
    kulmar wrote:
    Sam Sung wrote:
    OK, and then how to programmatically override the transistor trip threshold?

    Issue a logic one to the port.

    I thought it was obvious :) This extra resistor will load the output with 2mA current - with mains power it doesn't matter.
    This is what I was worried about. I don't think you are aware of how the GPIO on the AT89CX051 works. Well, the logic single exposed to the port is no different from the high state during reset, which is the recommended pull-down:
    krzbor wrote:
    2.2k to ground
    .... nullifies transistor switching not only during reset :(
  • #24 21893605
    krzbor
    Level 29  
    Posts: 1735
    Help: 40
    Rate: 1046
    Sam Sung wrote:
    will nullify transistor switching not only during reset

    Then what is the current output of the pin in OUT mode? 2mA?
  • #25 21893613
    djfarad02
    Level 19  
    Posts: 509
    Help: 7
    Rate: 675
    krzbor wrote:
    What is the current output of the pin in OUT mode? 2mA?

    About 0.1mA (100uA)
  • #26 21893669
    Sam Sung
    Level 33  
    Posts: 2005
    Help: 227
    Rate: 574
    krzbor wrote:
    That is, what is the current output of the pin in OUT mode? 2mA?
    There is no such thing as "OUT mode"; there is only a weak one or a strong zero.

    And what is the most economical way to control a relay at the moment?
    Perhaps a circuit with an LTV817 optocoupler, exactly like the one used in Chinese modules with 1/2/4/6/8 relays at 5/12/24 V (e.g. HL-525)?
    In addition, it provides galvanic isolation and does not require a stabilised coil supply voltage.

    djfarad02 wrote:
    I would use a stage with a common base, as previously suggested. Or better still - make a hardware watchdog that only applies power to the relays when there is a square wave on a certain uC pin. This is implementable on several elements (common to all relays). You can then go back to controlling the relays by ground, which allows you to use one NPN transistor and a resistor. Overall the circuit will be simpler than these zeners and resistors
    Interesting idea. I just happened to use a MOSFET with a P-channel feeding the plus of all coils, controlled from one additional port via the LTV817, in a circuit with an AT89C2051 controlling the relays via the ULN2003 (that is also switched on by a single). A one on this port cuts power to all relays; after initialisation, the program puts a zero there and it works as before. Simple circuit, only the MOSFET a bit expensive :)
  • #27 21893697
    elukam
    Level 17  
    Posts: 379
    Help: 8
    Rate: 149
    The proposed solution is of 'amateur-partisan class'. It is suitable for a specific case. Apart from the fact that there is nothing revealing about it, it is electronically inelegant, unprofessional and just plain wrong. There is absolutely nothing to admire, let alone praise, in the context of using a Zener diode as a level shifter. And even this circuit itself could have been done better if the author had had a better idea of the components and circuits.

    If the task is: use at most one transistor by force, then the author gets half a point. He would get a whole point if he did not use a transistor at all, but used a relay that can be driven directly from 5V using the available current capacity of the processor output in the low state.
    On the other hand, if the task was: drive any relay low state, it should be done decently with two transistors. Two transistors and one resistor would suffice, resulting in fewer components and being absolutely reliable and independent of the supply voltage. Because the requirement (in principle) to stabilise the power supply to the relay coil is a sick assumption - what's the saving?
    It should also be noted that Zener diodes, can have unpleasantly high currents below the nominal voltage. Especially those diodes of slightly higher power. The result known in such a specifically poorly designed circuit.
  • #28 21893722
    Andrzej_Tomaszewski
    Level 13  
    Posts: 270
    Help: 2
    Rate: 338
    >>21893697

    You have summed it up very well. Regards.
  • #29 21893795
    pawlik118
    Level 33  
    Posts: 2410
    Help: 201
    Rate: 596
    elukam wrote:
    The proposed solution is of 'amateur-partisan class'. It is suitable for a specific case. Apart from the fact that there is nothing revealing about it, it is electronically inelegant, unprofessional and just plain wrong. There is absolutely nothing to admire, let alone praise, in the context of using a Zener diode as a level shifter. And even this circuit itself could have been done better if the author had a better idea of the components and circuits.


    I disagree. Indeed, the approach is unusual and may create resistance to the unknown. Nevertheless, from an electronic point of view it is correct and works in an established, predictable way. From an economic point of view, the redundant use of unnecessary components is a non-technical approach, where the aim should be to fulfil the intended functions at the lowest possible cost.
  • #30 21893850
    Andrzej_Tomaszewski
    Level 13  
    Posts: 270
    Help: 2
    Rate: 338
    pawlik118 wrote:
    the approach is unusual

    consent

    pawlik118 wrote:
    it may arouse resistance to the unknown

    And what is the unknown here? Unless the arrangement is unknown because it is unused because it is incorrect.

    pawlik118 wrote:
    from an electronic point of view it is correct

    The uC pin controls several components in series and there is a +12Vdc potential on the other side. In my opinion this is incorrect.
📢 Listen (AI):

FAQ

TL;DR: At 24V with Rb = 1kΩ, the MCU pin may sink about 23mA, and one commenter called the idea “not very elegant but it works.” This FAQ is for engineers deciding whether a Zener-plus-PNP relay driver solves startup-safe 5V MCU control of 12V or 24V relays better than NPN, level-shifter, or optocoupler alternatives. [#21893578]

Why it matters: If a legacy 8051-family MCU briefly presents a logic-high-like state at reset, the wrong relay driver can energize loads at power-up.

Approach Parts discussed Main benefit Main drawback
PNP high-side + Zener transistor + Zener + base resistor Can keep relay off during certain 8051 startup states Sensitive to rail tolerance and GPIO behavior
Classic NPN low-side NPN + resistor Familiar, simple, widely used May turn relay on during reset in some 8051 cases
Common-base level shifter 2 transistors + resistor(s) More robust level shifting More parts than the Zener trick
Optocoupler / module optocoupler + driver stage Isolation, less dependence on coil rail stability More components and cost

Key insight: The thread’s consensus is not that the Zener-plus-PNP circuit never works. The real takeaway is that it only makes sense for narrow startup-behavior problems in older MCU families, and even then several participants prefer more robust alternatives.

Quick Facts

  • The proposed Zener window is 6.3V < Vz < 11.3V for a 12V rail and 18.3V < Vz < 23.3V for a 24V rail, derived from keeping the PNP off at 5V and on at 0V. [#21893011]
  • Suggested nominal Zeners were 9.1V for 12V rails and 20V or 22V for 24V rails; the post names BZX55C9V1, BZX55C20, and BZX55C22. [#21893011]
  • The original sizing advice was Rb = 1kΩ as a default, 2.2kΩ to reduce MCU pin current, and 470Ω for collector currents above about 100mA. [#21893011]
  • One practical warning is that at 24V with Rb = 1kΩ, the author estimated about 23mA into the MCU pin in LOW state, which sits near the lower edge of the cited 20–40mA typical GPIO range. [#21893011]
  • The flyback diode was specified as 1N4148, connected across the relay coil with cathode to collector and anode to GND, even though it was omitted from the ASCII sketch for readability. [#21893011]

How do I choose the Zener diode voltage for driving a 12V or 24V relay from a 5V MCU with a PNP transistor?

Choose Vz so it conducts when the MCU outputs 0V and stops conducting at 5V. The thread gives this range: VCC - 5.7V < Vz < VCC - 0.7V. For 12V, that means 6.3V to 11.3V, with 9.1V recommended. For 24V, that means 18.3V to 23.3V, with 20V or 22V suggested. A 20V Zener gives more turn-on margin, while 22V gives more turn-off margin. [#21893011]

Why would someone use a PNP high-side relay driver with a Zener diode instead of the classic NPN low-side transistor circuit?

They use it to avoid relay activation during power-up on some older MCUs. The author says certain controllers, including AT89C2051-class parts, can present a startup condition that would turn on a classic NPN low-side relay stage before I/O initialization. The Zener-plus-PNP approach makes the relay active on MCU LOW, not HIGH, so the default startup behavior can be safer for that narrow use case. Several participants still judged the classic NPN or two-transistor alternatives better overall. [#21893158]

What base resistor value should I use in this Zener-plus-PNP relay driver, and when should Rb be changed from 1kΩ to 2.2kΩ or 470Ω?

Start with 1kΩ. The original post calls 1kΩ a universal value, suggests 2.2kΩ to reduce the current the MCU must sink, and suggests 470Ω when collector current exceeds about 100mA. In practice: 1. Use 1kΩ for ordinary relay coils. 2. Raise to 2.2kΩ if GPIO current margin is tight. 3. Drop to 470Ω only when the PNP needs more base drive for heavier loads. [#21893011]

Why does this relay control method matter with AT89C2051 or AT89S8252 microcontrollers during power-up and reset?

It matters because these older 8051-family parts were discussed as problematic during reset and early startup. Multiple posters linked the idea specifically to AT89C2051 and AT89S8252, where a classic NPN relay stage can briefly energize the relay before firmware initializes the port. That makes the topology relevant when a short unwanted relay pulse could cause a fault, especially in legacy industrial controllers. [#21893165]

What is a common-base level shifter circuit, and how could it replace the Zener diode approach in relay control?

“Common-base level shifter” is a transistor stage that translates one logic voltage to another, keeping the base at a fixed reference while the signal passes through the emitter-collector path. In the thread, it was proposed as a long-used alternative that handles “unruly” MCU outputs better than the Zener trick. Several users preferred it because it avoids depending on a narrowly chosen Zener voltage and is less sensitive to supply variation. [#21893324]

What is an open-collector (OC) or open-drain (OD) output, and why was it mentioned in the discussion about GPIO safety?

“Open-collector” or “open-drain” output is a switching output stage that actively pulls low but does not actively drive high, so an external pull-up defines the HIGH level. It was mentioned because one poster argued that if the GPIO is effectively high-impedance in HIGH state, the Zener anode can float upward toward 12V, which raises safety concerns for the MCU pin. That criticism targeted the schematic’s behavior, not just its math. [#21893897]

How should the flyback diode be connected across a relay coil in this PNP high-side circuit, and why was 1N4148 suggested?

Connect the flyback diode directly across the relay coil, with cathode to the transistor collector and anode to GND. The original post explicitly recommends 1N4148 and says the diode is essential, even though it was omitted from the ASCII drawing for readability. Its job is to clamp the coil’s inductive kick when the relay turns off and protect the switching transistor. [#21893011]

PNP with Zener diode vs NPN transistor vs optocoupler module like LTV817 or HL-525: which relay driver approach is better for 5V-to-12V or 24V control?

The better approach depends on the failure you must prevent. The thread treats the Zener+PNP stage as workable but niche, the NPN stage as the normal simple option, and the LTV817/HL-525 optocoupler style as better when you want galvanic isolation and less dependence on a stabilized coil rail. One participant also preferred a shared P-channel MOSFET power gate for all coils over changing every single relay driver. [#21893669]

Why do some users report getting about 11V on the GPIO in LTspice when simulating this circuit, and what does that say about the schematic?

They report it because the schematic representation was disputed and likely misleading. One user said LTspice showed about 11V on the GPIO, while another clarified that the image posted later was only a redraw of the original ASCII art and was not analyzed or corrected. That means the simulation result says as much about the ambiguous drawing as about the intended circuit. [#21893524]

Which 8051-family MCU pin behavior during reset is relevant here, and how do the AT89C2051 ports actually behave at startup?

The relevant behavior is that some 8051-family ports do not act like modern push-pull GPIO at reset. In the thread, users say the AT89C2051 startup state is effectively an input with a pull-up, not a strong logic-high output, and another poster says there is “only a weak one or a strong zero.” That distinction matters because startup relay behavior depends on weak pull-up current, not just voltage level. [#21893669]

How much current does the microcontroller pin really sink in this circuit at 12V or 24V, and is that safe for typical AVR, PIC, or 8051 GPIO limits?

The original estimate is about 23mA at 24V with Rb = 1kΩ, ignoring Zener dynamic resistance. The same post says this sits within a typical 20–40mA GPIO limit for many MCUs, including AVR, PIC, and 8051-class devices, but recommends 2.2kΩ if you want more safety margin. That makes the circuit plausible, but it leaves little headroom on weak or legacy ports. [#21893011]

What happens in the real circuit, not just in ideal calculations, if I disconnect the MCU pin and measure the Zener anode voltage with a voltmeter?

In the real circuit, the node may not stay at a safe logic-level voltage. One participant challenged the idealized math and said to physically disconnect the MCU pin and measure the Zener anode to ground, implying the node can rise due to leakage and non-ideal Zener behavior. Another poster separately warned that the anode can effectively “hang in air,” which is the core practical objection to the design. [#21893971]

How can I stop relays from briefly turning on during MCU reset without using this Zener diode trick?

Use a driver topology that blocks relay power until firmware is alive. The thread suggests three practical alternatives: 1. Use a two-transistor stage instead of the Zener trick. 2. Use a common-base level shifter. 3. Gate the relay supply with a shared circuit, such as a watchdog-controlled stage or a P-channel MOSFET, then enable it only after initialization. Those methods target the reset glitch directly. [#21893578]

When is it better to use a hardware watchdog or a P-channel MOSFET relay power gate instead of changing the individual relay driver topology?

Use them when many relays share the same risky startup condition. One poster recommended a hardware watchdog that applies power to all relays only when it sees a square wave on one MCU pin. Another described using a P-channel MOSFET plus LTV817 so one control line disconnects power to every coil until initialization finishes. That can simplify a multi-relay design more than redesigning each channel. [#21893669]

What supply-voltage tolerance problems can make all relays switch on in this design, and how stable does the 12V or 24V rail need to be?

If the supply rises enough, the Zener threshold window can shift from “relay off” to “relay on.” One critic warned that “just a jump of a few V” could switch on all relays, especially on a 24V industrial rail. The author answered that the rail must be stabilized and cited using a 7812 regulator in practice. So this design needs a controlled coil supply more than a classic low-side NPN stage does. [#21893158]
Generated by the language model.
ADVERTISEMENT