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  46 3225 Cool? (+4)
📢 Listen (AI):

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

About Author
petrykcom wrote 62 posts with rating 19 . Live in city Kalisz. Been with us since 2007 year.

Comments

tos18 29 Apr 2026 03:10

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... [Read more]

Andrzej_Tomaszewski 29 Apr 2026 08:11

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... [Read more]

gulson 29 Apr 2026 08:36

Thanks for sharing the article. The schematic is a bit off, I drew using the GPT Image 2 model https://obrazki.elektroda.pl/8921155300_1777444585_bigthumb.jpg (I hope the model is not wrong) [Read more]

viayner 29 Apr 2026 09:22

Hello, 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),... [Read more]

krzbor 29 Apr 2026 09:22

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... [Read more]

goose-berry 29 Apr 2026 09:35

You can see by the diagram that it is a product of artificial intelligence. Specifically a copilot... [Read more]

petrykcom 29 Apr 2026 10:47

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....... [Read more]

kulmar 29 Apr 2026 10:54

Could you specify the type of this microcontroller? [Read more]

petrykcom 29 Apr 2026 11:16

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... [Read more]

Andrzej_Tomaszewski 29 Apr 2026 12:46

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.... [Read more]

tos18 29 Apr 2026 14:38

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 ... [Read more]

petrykcom 29 Apr 2026 15:24

I understand and know the solution perfectly well... but it was precisely the use of just one transistor ! [Read more]

pawlik118 29 Apr 2026 15:25

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... [Read more]

krzbor 29 Apr 2026 16:08

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. [Read more]

Sam Sung 29 Apr 2026 18:22

OK, and then how do you programmatically exceed the transistor trip threshold? [Read more]

kulmar 29 Apr 2026 18:51

Issue a logic one to the port. [Read more]

Citizen75 29 Apr 2026 18:53

Here you have a description of micro relays that can be driven directly from the microcontroller port, without using transistors and protection diodes. The transistor "looks weird". Especially... [Read more]

Andrzej_Tomaszewski 29 Apr 2026 19:23

Yes, I read this article. [Read more]

krzbor 29 Apr 2026 20:14

I thought it was obvious :) This extra resistor will load the output with 2mA - with mains power it doesn't matter. [Read more]

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.
%}