Create a Dimmer using the ESP32

Hello everyone!
Im working on my final projec, which consists in create a dimmer using the esp32, this will be controlled using the wifi and bluetooth functions those two are easy. But the main problem is that i don't have idea how to create a zero-crossing detector and how to do that the esp32 makes an interruption, i've been watching some codes to use them as example but simply i don't understand how to translate them to the esp32, here's the codes that im using as a reference and to save time, first im testing the code and the circuit using a potentiometer:

code1:

int detectado = 0;
int valor=0;
int last_CH1_state = 0;
void setup() {
/*

  • Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board
  • The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports:
    -B (digital pin 8 to 13)
    -C (analog input pins)
    -D (digital pins 0 to 7)
    //All Arduino (Atmega) digital pins are inputs when you begin...
    */
    PCICR |= (1 << PCIE0); //enable PCMSK0 scan
    PCMSK0 |= (1 << PCINT0); //Set pin D8 trigger an interrupt on state change. Input from optocoupler
    pinMode(23,OUTPUT); //Define D3 as output for the DIAC pulse
    }
    void loop() {
    //Read the value of the pot and map it from 10 to 10.000 us. AC frequency is 50Hz, so period is 20ms. We want to control
    //of each half period, so the maximum is 10ms or 10.000us. In my case I've maped it up to 7.200us since 10.000 was too mu
    valor = map(analogRead(36),0,1024,7200,10);
    if (detectado)
    {
    delayMicroseconds(valor); //This delay controls the power
    digitalWrite(23,HIGH);
    delayMicroseconds(100);
    digitalWrite(23,LOW);
    detectado=0;
    }
    }

//This is the interruption routine
//----------------------------------------------
ISR(PCINT0_vect){
///////////////////////////////////// //Input from optocoupler
if(PINB & B00000001){ //We make an AND with the pin state register, We verify if pin 8 is HI
if(last_CH1_state == 0){ //If the last state was 0, then we have a state change...
detectado=1; //We haev detected a state change!
}
}
else if(last_CH1_state == 1){ //If pin 8 is LOW and the last state was HIGH then we have a state cha
detectado=1; //We haev detected a state change!
last_CH1_state = 0; //Store the current state into the last state for the next loop
}
}


code2:

const int pulsePin = 23; //pin to pulse the triac

const int zcPin = 22; //pin to zero cross detection

int tempo = 0; //time to wait to send the pulse in microseconds

void zeroCrossing() {
if (tempo == 0) {
digitalWrite(pulsePin, LOW);
}
else {
delayMicroseconds(tempo);
digitalWrite(pulsePin, HIGH);
delayMicroseconds(20);
digitalWrite(pulsePin, LOW);
}
}

void setup() {
Serial.begin(115200);
pinMode(pulsePin, OUTPUT);
pinMode(zcPin, INPUT);
attachInterrupt(digitalPinToInterrupt(zcPin), zeroCrossing, FALLING);
}

void loop () {
int temp;
if (Serial.available()) {
temp = Serial.parseInt();
if (temp != 0) {
tempo = temp;
}
}
}


code3:
int AC_LOAD = 36; // Output to Opto Triac pin
int CRUCE = 23;
int dimming = 128; // Dimming level (0-128) 0 = ON, 128 = OFF

void setup()
{
Serial.begin(115200);
delay(1000);
pinMode(CRUCE, INPUT);
pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
attachInterrupt(CRUCE, zero_crosss_int, RISING); // Choose the zero cross interrupt # from the table above
}

//the interrupt function must take no parameters and return nothing
void zero_crosss_int() //function to be fired at the zero crossing to dim the light
{
// Firing angle calculation : 1 full 50Hz wave =1/50=20ms
// Every zerocrossing thus: (50Hz)-> 10ms (1/2 Cycle)
// For 60Hz => 8.33ms (10.000/120)
// 10ms=10000us
// (10000us - 10us) / 128 = 75 (Approx) For 60Hz =>65

int dimtime = (75*dimming); // For 60Hz =>65
delayMicroseconds(dimtime); // Wait till firing the TRIAC
digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC
delayMicroseconds(8.33); // triac On propogation delay
// (for 60Hz use 8.33) Some Triacs need a longer period
digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

void loop() {
for (int i=5; i <= 128; i++){
dimming=i;
delay(10);
}
}


i don't know which one is the better, i think that maybe the second one, im not sure, so please if anybody have an idea i will be grateful.
Thanks for read :slight_smile:

Please read the forum guide in the sticky post, then modify your post above and correct it.

There is information about zero crossing detection and AC phase control using opto-isolators and a triac at: Arduino Playground - ACPhaseControl

The circuit as shown is for 110V operation.

I assume, by your mention of 50Hz mains frequency, and from your time zone that you are in Europe.
If so you will need to double the values of R1 and R2 for 220/240V operation (and make sure they are adequately power rated).

Are you suitably qualified/experienced to be dealing with mains voltages?
My guess is not.

"Create a dimmer" probably means "light dimmer".

Triac based light dimmers are not used anymore, because most modern lightbulbs (LEDs) can't be triac dimmed.
Leo..

Hi,

Google esp32 ac dimmer

Tom... :slight_smile:

Triac based light dimmers are not used anymore, because most modern lightbulbs (LEDs) can't be triac dimmed.

There are LOTS of dimmable LED light bulbs! I've got 3 or 4 in-use right now. Google or check the [u]Home Depot selection[/u].

And as far as I know, dimmers sold in home improvement stores as well as home-automation dimmers still use TRIACs, but I haven't seen the schematics. I didn't have to change anything in my home-automation setup-when I screwed-in a couple of LED bulbs.

Wawa:
Triac based light dimmers are not used anymore, because most modern light bulbs (LEDs) can't be triac dimmed.

Except for the ones that can. Presently, more and more LED luminaires are dimmable for a fairly obvious reason. :roll_eyes:

I'd expect most CAN be dimmed.

LED lights (at least the ones that I have ripped open) seem to contain some kind of a buck converter, producing constant current for the LEDs. Those ICs tend to work at 100-500 kHz kind of frequencies, much higher than the 50 Hz of the mains, or the 100 Hz of the phase cutting. As long as those ICs start up fast enough dimming through phase cutting should work quite well. At least they don't have a problem with the 50 Hz AC or the 100 Hz half-sine-wave DC they get.

There are, by the way, lots of dimmer modules commercially available (for dimming lights, that is - they don't have snubbers so most likely not suitable for motors). Zero crossing output signal, TRIAC gate input signal, properly opto-isolated.

wvmarle:
Those ICs tend to work at 100-500 kHz kind of frequencies, much higher than the 50 Hz of the mains, or the 100 Hz of the phase cutting.

An interesting design consideration. High-performance semiconductors for those frequencies which integrate the switching component and the flyback diode (actually, a second switch) are now cheaper while inductors for lower frequencies would be too bulky and simply not cost-effective.

wvmarle:
At least they don't have a problem with the 50 Hz AC or the 100 Hz half-sine-wave DC they get.

No because they have enough energy reserve onboard to ride through those interruptions in power. A classic SMPS rectifies the incoming AC to DC and then has a large capacitor to supply the circuit from there. So switching it on and off at 100Hz or greater will do nothing except heat up that capacitor. The LED will still be illuminated at full brightness until the duty cycle gets so low that the SMPS shuts down. Switching at less than 50Hz will give visible flicker.

Old PC power supplies (still switchmode though) could be easily destroyed by manually switching them off and on 10 or 20 times quickly. Maybe current ones too. I've never done the experiment myself.

wvmarle:
There are, by the way, lots of dimmer modules commercially available (for dimming lights, that is - they don't have snubbers so most likely not suitable for motors). Zero crossing output signal, TRIAC gate input signal, properly opto-isolated.

High-power as well ?

MorganS:
No because they have enough energy reserve onboard to ride through those interruptions in power. A classic SMPS rectifies the incoming AC to DC and then has a large capacitor to supply the circuit from there. So switching it on and off at 100Hz or greater will do nothing except heat up that capacitor. The LED will still be illuminated at full brightness until the duty cycle gets so low that the SMPS shuts down. Switching at less than 50Hz will give visible flicker.

Ah, but there is the mistaken mind-set. :astonished: We are not talking about "classic SMPS" by those rules.

You presume the need for the large capacitor - or indeed, any capacitor. That need is only there if the objective is to provide smooth, regulated DC. That is not the case at all in a LED bulb, and indeed is quite undesirable in regard to achieving a reasonable power factor.

MorganS:
Old PC power supplies (still switchmode though) could be easily destroyed by manually switching them off and on 10 or 20 times quickly. Maybe current ones too. I've never done the experiment myself.

OK, I am myself very careful about that, but we are simply not talking about PC power supplies here, but something completely different - with no reservoir capacitor.

OK, so how does a modern non-dimmable LED bulb work then? I'm sure there's only one or two significant circuits in use.

My quick research on Google says that there are actually several types of dimmers and they will perform differently with "non dimmable" LEDs. Some will work, but not all. So maybe with an Arduino at the helm you could get adequate dimmable control over a bulb labelled "non dimmable".

Deva_Rishi:
High-power as well ?

3-4A I remember is easily available. High enough power for you? If not you would have to start building your own - and if you don't know what you're doing (or even if you kinda do) be prepared for sparks flying.

MorganS:
No because they have enough energy reserve onboard to ride through those interruptions in power. A classic SMPS rectifies the incoming AC to DC and then has a large capacitor to supply the circuit from there.

I don't recall seeing a large capacitor in there. Some small ones probably. I don't really remember, it's been a while since I had one broken and available for destruction :slight_smile:

wvmarle:
3-4A I remember is easily available. High enough power for you? If not you would have to start building your own - and if you don't know what you're doing (or even if you kinda do) be prepared for sparks flying.

That is the sort of power i'm looking for, i build a Velleman, and am of course looking for inexpensive options, do you have any links ?

No links - should be easy enough to find such devices with some googling.

wvmarle:
I don't recall seeing a large capacitor in there. Some small ones probably. I don't really remember, it's been a while since I had one broken and available for destruction :slight_smile:

"Large" is relative. It could be 0.1uF at mains voltage. That will store a lot of energy for an LED bulb.

MorganS:
"Large" is relative. It could be 0.1uF at mains voltage.

0.1 µF actually rated at mains voltage is pretty large for the base of a light bulb. In a downlight luminaire perhaps.

I am going to check out how rapidly my non-dimmable downlights actually turn off and on. I know my "pseudo-fluorescent" light fittings neither turn on or off rapidly.