UniRemoteLab عربي Deutsch Open the lab

Dim a 230 V Lamp with ESP32: Zero-Cross Detection and Phase Control

You cannot dim a mains lamp with ordinary PWM. The ESP32 detects each zero crossing of the mains voltage, waits, then fires the TRIAC: the shorter the wait, the brighter the lamp. In the lab only the supervisor runs this one, for safety.

⚠️ In the lab only the supervisor runs this experiment, for safety.

AC dimmer

Wiring on the lab board

PartPin
Zero-cross Z-CGPIO33
Trigger PSMGPIO32

Full code

// Experiment 7: AC lamp dimming with the Dimmer (admin only) ⚠️ 220V
// Relays: K7 (PSM on GPIO32) and K8 (zero-cross on GPIO33)
// Phase-angle control: after each zero crossing we wait, then fire the triac with a short pulse
// Shorter delay = brighter. Type a percentage 0 to 100 in the send box
const int ZC = 33;
const int PSM = 32;
const int HALF_CYCLE_US = 10000;   // half a cycle on 50 Hz mains
volatile bool crossed = false;
volatile unsigned long crossings = 0;
int power = 0;                      // 0 = off ... 100 = full brightness

void IRAM_ATTR onZero() { crossed = true; crossings++; }

void setup() {
  Serial.begin(115200);
  pinMode(PSM, OUTPUT);
  digitalWrite(PSM, LOW);           // start with the lamp off
  pinMode(ZC, INPUT);
  attachInterrupt(digitalPinToInterrupt(ZC), onZero, RISING);
  Serial.println("Send power 0..100");
}

void loop() {
  static unsigned long lastReport = 0;
  if (Serial.available()) {
    power = constrain(Serial.parseInt(), 0, 100);
    while (Serial.available()) Serial.read();
    Serial.print("Power: "); Serial.println(power);
  }
  if (crossed) {
    crossed = false;
    if (power > 0) {
      int wait = map(power, 1, 100, 9000, 1000);   // from 9 ms (dim) to 1 ms (bright)
      delayMicroseconds(wait);
      digitalWrite(PSM, HIGH);
      delayMicroseconds(20);
      digitalWrite(PSM, LOW);
    }
  }
  if (millis() - lastReport > 2000) {               // every 2 s: number of zero crossings
    lastReport = millis();
    Serial.print("zero-crossings/s: ");
    Serial.println(crossings / 2);
    crossings = 0;
  }
}

The libraries are already installed in the lab.

How it works

  1. 50 Hz mains crosses zero 100 times a second, every 10 ms. The module raises Z-C at each crossing and attachInterrupt() catches it at once.
  2. After each crossing the code waits 1 to 9 ms depending on the power you set, then sends a short pulse on PSM that keeps the TRIAC on until the end of the half cycle.
  3. Every two seconds the code prints zero crossings per second, about 50 on European mains, so you know detection works.

Common mistakes and fixes

⚠️ Safety first
Mains voltage can kill. Never touch the module or lamp while connected, and use a ready-made isolated module.
The crossing counter is zero
Z-C is not connected, or the module has no mains supply.
The lamp flickers
Many LED bulbs are not dimmable. Use one marked dimmable, or an incandescent bulb.

No ESP32 at hand?

Run this code now on a real board in our remote lab: write, upload, and watch the result live on camera.

Open the lab

More tutorials