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