Hello friends, I hope you all are doing great. Today, I am going to share the TRIAC Lamp Dimmer with ESP32 - Proteus Simulation, the ninety-first project of our IoT Projects series. If you have searched for a TRIAC dimmer with an ESP32, how phase-angle control works, a zero-crossing detector with a PC817, or how to dim an AC lamp from a web page, here is a complete one in a Proteus simulation. Every rotary wall dimmer you have ever turned works the same way: it does not lower the voltage, it switches the lamp on part-way through each half-cycle of the AC.

This project builds that dimmer around an ESP32. A PC817 optocoupler tells the ESP32 when the AC crosses zero; the ESP32 waits, then gives a BT136 triac a short gate pulse; the triac conducts until the next zero. The later the pulse, the less power reaches the lamp. But there is a catch: the power does not follow the delay in a straight line. So this ESP32 does it properly: you ask for 25 % of the power, and it solves the RMS formula of a phase-cut sine for the firing angle that gives exactly that. In this tutorial, we will go through the circuit, how a triac works and why it switches itself off at every zero, the zero-crossing detector and its small offset, the firing angle and the power formula, why a cold filament needs a soft start, and why this sketch has no display. Then we dim a 21 W lamp in the simulation, compare a soft start with a hard one, and run a sunrise. At the end, you can download the project with the sketch and every library file.

NOTICE: Our team has put a lot of effort into this project: the circuit, the code and every part in the simulation are our own work, and you get them all free. Your feedback is the fuel that keeps us going, so please tell us what you think in the comments below. And if our free projects help you, you can buy us a coffee. So, let's get started with the TRIAC lamp dimmer with ESP32 in Proteus simulation:

Figure: The TRIAC lamp dimmer in a Proteus simulation.
Figure: The project working in the Proteus simulation (an animation of the main steps).

What Does This TRIAC Lamp Dimmer Do?

  • Dims an AC lamp: phase-angle control of a 12 V 21 W lamp on 12 V AC with a BT136 triac.
  • Finds every zero: a PC817 zero-cross detector on an interrupt pin, with a correction for its offset.
  • Sets the power, not the delay: 25, 50, 75 or 100 % of the lamp's power through the RMS formula.
  • Protects the filament: a 0.5 s soft start; HARD ON shows what a cold filament does without it.
  • Wakes you gently: a sunrise ramp from 0 to 100 % (30 s here, 30 minutes in a bedroom).
  • Watches the mains: its frequency from the zero crossings, NO MAINS when they stop.

It is a great project for learning AC power control, interrupts and precise timing in one go. In the simulation, the triac's pop-up shows the voltage across it and its current over the last two cycles, its firing angle and the share of full power, so we can check every number the ESP32 claims.

Components Used in the Proteus Simulation

All the parts come from our Proteus libraries, each with its own tutorial; the AC source is Proteus's own ALTERNATOR. The project zip at the end includes every LIB and DLL file.

The parts of the TRIAC lamp dimmer and their library posts
PartProteus deviceLibrary postConnection
ESP32 DevKit V1ESP32 DEVKIT V1ESP32 DevKit V1 Library for ProteusU1, ESP32_Triac_Dimmer.hex, Wi-Fi 192.168.1.101
BT136 triacTRIACBT136TEPSCR and TRIAC Library for ProteusQ1: T1 to ground, T2 = MT2, gate from D26 through R1
P21W 12 V 21 W lampBULBP21WTEPFilament Bulb Library for ProteusL1 from AC to MT2
PC817 optocouplerPC817TEPPC817 Optocoupler Library for ProteusU2, the zero-cross detector, output on D27
1N4007 diodeDIODE1N4007TEP1N4007 Diode Library for ProteusD1 across the PC817's LED
Resistors 100, 2k2, 10kRESISTOR4BANDTEPResistor Library for ProteusR1 the gate, R2 the LED, R3 the pull-up
12 V AC sourceALTERNATORProteusV1, 17 V peak (12 V rms), 50 Hz
Laptop WiFi MonitorLAPTOP WIFI MONITORLaptop WiFi MonitorPC1
Mobile WiFi MonitorMOBILE WIFI MONITORMobile WiFi MonitorMOB1

Proteus Simulation of the TRIAC Dimmer: the Circuit

Figure: The complete circuit of the TRIAC lamp dimmer on an A3 Proteus sheet.
The connections of the TRIAC lamp dimmer
SignalESP32Goes to
AC-V1's live: the lamp and R2
MT2-the lamp's other end and the triac's T2
GATED26R1 100 ohm to the triac's gate (G1)
ZC-R2 2k2 from AC, the PC817's LED, D1 across it
ZC_OUTD27the PC817's collector, R3 10k to 3.3 V

The lamp and the triac are in series across the 12 V AC: the triac is the switch. The zero-cross detector is a separate branch: R2 limits the current into the PC817's LED in the positive half-cycles, and D1 carries the negative half-cycles past the LED so it is never reverse-biased. On the other side of the optocoupler, the transistor pulls D27 low while the LED is lit. This simulation uses 12 V AC so that it can safely sit next to the ESP32; on 230 V mains the same idea needs an opto-triac between the ESP32 and the triac.

How the TRIAC Dimmer Works

A Triac Switches Itself Off

A triac is two thyristors back to back in one package. It blocks in both directions until a small current flows into its gate; then it conducts, and keeps conducting with no gate current at all, until the current through it falls to zero. On AC that happens by itself at the end of every half-cycle. So one short gate pulse per half-cycle is enough: the ESP32 decides only when each half-cycle starts conducting, and the AC itself switches the triac off. The BT136 can be fired with a positive gate pulse in both half-cycles (quadrants 1 and 4); a sensitive part needs only 2.5 to 11 mA. Here 3.3 V through 100 ohm gives about 20 mA.

Finding the Zero

Everything is timed from the zero crossings, so the ESP32 must know exactly when they happen. The PC817's LED lights only once the AC is above about 1 V, which on a 17 V peak sine is about 250 microseconds after the zero. So D27 falls 250 us after each upward zero and rises 250 us before each downward one. The interrupt routine corrects for that: on a falling edge the zero was 250 us ago, on a rising edge it comes 250 us later. The same edges give the frequency: two zeros per cycle, so 50 Hz means a zero every 10 ms.

The Firing Angle and the Power

A half-cycle is 180 degrees. If the triac fires at an angle a, the lamp gets the part of each half-sine from a to 180 degrees. For a resistive load, the share of full power is P / Pfull = 1 - a / pi + sin(2a) / (2 pi). Fired at 90 degrees (5 ms after the zero at 50 Hz), the lamp gets exactly half the power. But the relation is not a straight line: the middle of the half-cycle carries most of the energy. A naive dimmer that sets the delay in proportion, 7.5 ms for "25 %", gives only 9 % of the power. This sketch solves the formula for the angle by bisection: for 25 % it fires at 114 degrees (6.33 ms), for 75 % at 66 degrees (3.67 ms).

Why a Soft Start

A tungsten filament's resistance grows about ten times as it heats from room temperature to 2700 K. Cold, the P21W measures about 2 ohm instead of 7; switched on at full power, it takes several times its normal current for the first few cycles. That surge is why lamps often fail at the moment they are switched on, and why this sketch reaches every new level over half a second. HARD ON skips that, so we can see the surge in the lamp's pop-up.

Why This Dimmer Has No Display

The gate pulse is timed in loop(): it watches micros() and fires when the delay has passed. So loop() must never wait. A full frame of an I2C OLED takes about 25 ms at 400 kHz, two and a half half-cycles in which no pulse could be given; the lamp would flicker twice a second. Even the web server only runs right after a pulse, when the next zero is at least 0.6 ms away. A commercial dimmer fires from a hardware timer, which would free loop() completely.

The Arduino Code Explained

The sketch is ESP32_Triac_Dimmer.ino, built for our ESP32 DevKit V1 with the WiFi and WebServer libraries. The zero-cross interrupt corrects the PC817's offset and keeps a running mean of the half-cycle:

const long ZC_US = 250;                                             // the PC817's edges from the true zero
  const unsigned long z = digitalRead(ZERO) == LOW ? now - ZC_US : now + ZC_US;
  if (zeroAt) period2 = (period2 * 7 + (z - zeroAt)) / 8;           // (a running mean of the half-cycle)

The power at a firing angle, and the bisection that finds the angle for a power:

float powerAt(float a) { return 1 - a / PI + sin(2 * a) / (2 * PI); }   // the share of full power at firing angle a
  for (byte k = 0; k < 24; k++) { const float m = (lo + hi) / 2; if (powerAt(m) > p) lo = m; else hi = m; }
  fireDelay = (unsigned long)(a / PI * period2);

The gate pulse, timed in loop(), and the web server only right after it:

      digitalWrite(GATE, HIGH); delayMicroseconds(PULSE_US); digitalWrite(GATE, LOW);
  if (!zeroSeen) web.handleClient();

The ramps (the soft start and the sunrise) move the level a little between pulses:

    setLevel(rampFrom + (target - rampFrom) * f);

And the mains watch. The last edge's time is copied with interrupts off and compared as a signed number; reading micros() first and the edge time second let an edge slip in between, and the unsigned difference wrapped round to a huge number, so our first run reported NO MAINS several times a second:

  const bool now = (long)(micros() - le) < 100000L;

The dashboard's links are Refresh, OFF, 25 %, 50 %, 75 %, 100 %, HARD ON and SUNRISE; /data gives everything as JSON.

How to Run the Proteus Simulation

  1. Download the project zip below and install the library files (the steps are under the download button).
  2. Open ESP32-Triac-Dimmer.pdsprj from the Proteus Simulation folder. Keep the HEX and the DLL files next to it.
  3. Press Play. The lamp comes up softly to 50 % of its power.
  4. Open the Laptop WiFi Monitor (press Go): 25 % to 100 %, OFF, HARD ON and SUNRISE.
  5. Click Q1 for the triac's pop-up and L1 for the lamp's.

Simulation Results: the Dimmer in Proteus

We pressed Play and let the sketch bring the lamp up to half power, then used the dashboard: 25 %, 100 %, OFF, HARD ON and SUNRISE. The triac's pop-up measures its own firing angle and the share of full power from the voltage across it, so every number the ESP32 gives can be checked. The times are the simulation's clock; the whole run took 56.1 simulated seconds.

Half Power: Fired at 90 Degrees

At 2.25 s the ESP32 saw the mains at 50.2 Hz and started at 50 % with its soft start. At 5 s it reported fired 5.01 ms after the zero (90 deg), and the triac's pop-up agreed: fired 5.0 ms after each zero, 90 degrees, about 50 % of full power. Its two cycles of voltage and current show the shape of phase control: the voltage across the triac follows the sine until the pulse, then drops to about 1 V while the current flows to the zero. The triac carried 2.51 A; with no heatsink its junction was heading for 81 C.

Figure: Half power: the lamp glows.
Figure: The triac: its own measurement of the firing angle.
Figure: The lamp at half power.
Figure: The dashboard.

25 %: Not a Quarter of the Delay

We clicked 25 %. The ESP32 solved the power formula and fired 6.34 ms after the zero, 114 degrees. The triac measured 6.4 ms, 115 degrees and about 24 % of full power. A naive dimmer would have waited 7.5 ms for "25 %" and given about 9 %. The filament cooled to 1888 K and gave 13 lumen, a warm glow.

Figure: 25 % clicked.
Figure: 25 %: a later firing.
Figure: The triac at 25 %.
Figure: The filament at 1888 K.

Full Power

100 % fires the triac 0.30 ms after each zero, the earliest the sketch allows (the PC817's edge itself sits 0.25 ms from the zero). The triac measured 0.8 ms and 14 degrees, about 100 % of full power: the few degrees before it carry almost no energy. The filament ran at 2766 K and 355 lumen. The lamp's pop-up sometimes catches the moment between two conducting parts and calls it cooling; its temperature and its light tell the truth.

Figure: 100 % clicked.
Figure: Full power.
Figure: The triac at full power.
Figure: The filament at 2766 K.

Off, Then a Hard Switch-On

OFF stops the gate pulses; the triac blocks at the next zero. Within 2.5 s the filament had cooled to 375 K, almost room temperature, with its resistance back near its cold value. Then HARD ON: full power at once, no ramp. The lamp's graph of the first 400 ms shows the surge: about 15 A in the first half-cycles, twelve times its working current, falling as the filament heated, full light after 216 ms. With the soft start, single pulses into the cold filament can still be large when a pulse lands near the top of the sine, but they come one by one at low power and their average stays low.

Figure: OFF.
Figure: The lamp off.
Figure: The filament cold again.
Figure: HARD ON clicked.
Figure: A hard switch-on.
Figure: The surge of a cold filament.
Figure: The triac: full power at once.

A 30 Second Sunrise

Last, SUNRISE: the power ramps from 0 to 100 % over 30 seconds (a real wake-up light takes 30 minutes). Nine seconds in, the triac fired at 105 degrees, 34 % of full power, the filament at 2043 K: a dim orange glow. Eighteen seconds in, 73 degrees and 68 %, 2487 K. At 53.5 s the ESP32 reported SUNRISE done: 100 %. Because the ramp follows the power, not the angle, the brightness rises evenly instead of jumping at the end. The phone shows the same dashboard, and the serial monitor holds every event of the run.

Figure: SUNRISE clicked.
Figure: The sunrise, early.
Figure: The triac at 34 %.
Figure: A dim orange glow.
Figure: The sunrise, later.
Figure: The triac at 68 %.
Figure: Brighter.
Figure: The phone: the same dashboard.
Figure: The serial monitor: the whole run.

At 50 %, 25 % and 100 %, the triac's own measurement matched the power the ESP32 asked for within one or two percent: the formula, the zero-cross correction and the timing in loop() all did their job.

Download the Project Files

Click the button below to download TRIAC-Lamp-Dimmer-ESP32-Proteus-Simulation-v1.0.zip (about 4.3 MB, without the source code of the models):

TRIAC Lamp Dimmer with ESP32 - Proteus Simulation V1.0
  • Proteus Simulation: ESP32-Triac-Dimmer.pdsprj, ESP32_Triac_Dimmer.hex and copies of the models.
  • Arduino Code: ESP32_Triac_Dimmer.ino, the sketch.
  • Proteus Library Files: the LIB files of every part.
  • Proteus Model Files: the DLL models.
  • README.txt: the installation steps, the properties, the test results and the links to every library post.

No Proteus yet? Read How to Download and Install Proteus 7 and 8. New to libraries? Follow How to Install a New Library in Proteus.

How to Install the Project Files

  1. Close Proteus and extract the whole zip file, for example to your Desktop.
  2. Copy the files from Proteus Library Files into the LIBRARY folder of Proteus.
  3. Copy the files from Proteus Model Files into the MODELS folder. The ALTERNATOR is part of Proteus.
  4. Start Proteus and open the project from the Proteus Simulation folder.

Changing the Code

Open the sketch in the Arduino IDE with the TEP Boards for Proteus installed, select ESP32 Dev Module (TEP Proteus), and use Sketch > Export Compiled Binary. Put the new HEX next to the project. The offset, the pulse width, the soft start and the sunrise are near the top.

Ideas to Extend the Project

  • Fire the triac from a hardware timer, so the loop is free for a display and the web.
  • Add a rotary encoder for a knob that still works without Wi-Fi.
  • Schedule the sunrise from the time on an NTP server.
  • Measure the lamp's current with a current transformer and show its real power.
  • Correct for the filament: perceived brightness is not the same as power; add a gamma curve.
  • Dim a fan instead of a lamp, and see why inductive loads need a snubber.

From Simulation to Real Hardware

  • Mains is dangerous: on 230 V the triac, the lamp and the detector sit on the mains; only experienced builders should do it, in a closed, insulated box.
  • Isolate the ESP32: drive the triac through an opto-triac (MOC3021, without zero-crossing) and use a mains-rated zero-cross module.
  • Snubber and fuse: an RC snubber across the triac for inductive loads, and a fuse in the live wire.
  • Heatsink: a BT136 drops about 1 V; at 1 A that is 1 W, more than a bare TO-220 should carry for long.
  • LED bulbs: many LED bulbs do not dim on phase control; use dimmable ones.

Troubleshooting

  • The lamp never lights: the gate from D26 through R1 to G, T1 to ground; NO MAINS on the dashboard means no zero-cross edges.
  • The lamp flickers: something in loop() waits: a display, a long print, a delay().
  • The power is a little off at the extremes: the PC817's offset is not exactly 250 us at every voltage; adjust ZC_US.
  • No dashboard: the ESP32 joins TEP-WiFi a moment after the start; press Go again.

Limitations of the Simulation

  • The triac, the lamp and the optocoupler are our libraries' models, built from their datasheets, not every detail of real parts.
  • The AC is a perfect 12 V sine; real mains carries noise that can trigger a zero-cross detector falsely.
  • The power formula is for a resistive load; a filament's resistance changes with its temperature, so its power is close to, not exactly, the formula.
  • ESP32 is a product of Espressif Systems; this project is not made or endorsed by Espressif or by Labcenter.

So, that was all about the TRIAC Lamp Dimmer with ESP32 - Proteus Simulation. I hope it helps you understand how a triac switches itself off at every zero, how a zero-cross detector times everything, why the power is not proportional to the delay, and why a cold filament needs a gentle start, all tested in a simulation before you go anywhere near the mains. More projects with the ESP32 and the Raspberry Pi 5 are coming. If you have any questions, ask in the comments and I will help you out. Till the next tutorial, take care and have fun!