Controlling a 12V relay directly from a 5V MCU using a Zener diode and a PNP transistor
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Ω.
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
He hereby documents his independent discovery and many years of industrial use of this solution.
Comments
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]
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]
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]
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]
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]
You can see by the diagram that it is a product of artificial intelligence. Specifically a copilot... [Read more]
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]
Could you specify the type of this microcontroller? [Read more]
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]
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]
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]
I understand and know the solution perfectly well... but it was precisely the use of just one transistor ! [Read more]
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]
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]
OK, and then how do you programmatically exceed the transistor trip threshold? [Read more]
Issue a logic one to the port. [Read more]
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]
Yes, I read this article. [Read more]
I thought it was obvious :) This extra resistor will load the output with 2mA - with mains power it doesn't matter. [Read more]