image_pdfDownload as PDFimage_printPrint

Control the brightness of bulb using Triac module and NodeMCU

Control the brightness of bulb using Triac module and NodeMCU
=============================================

Keep in mind:

=============================================
  1. Signal pin of triac needs to modified before using it with NodeMCU but zero-crossing pin can work straight forward without any modifications.
  2. Signal pin of triac module works on 5v as it has 330Ω resistor, led and opto-coupler in series. You can either use level shifter between NodeMCU and triac module so that it will shift the voltage from 3.3v to 5v or you could simply replace the 330Ω with 100Ω resistor.
  3. You can use any pin for detecting zero crossing, expect for GPIO 16 as it does not come with interrupt. I have used D5(GPIO 14) for detecting zero-cross and D7(GPIO 13) for triggering the triac.
  4. For timer interrupts, I have used Ticker library in replacement for TimerOne library. Ticker library count in mili seconds and is not very precise but you can always make your own code to count it micro seconds and get better result.
    https://github.com/esp8266/Arduino/tree/master/libraries/Ticker
  5. Add ICACHE_RAM_ATTR in code if you are using new core(2.5.2) as it forces to run ISR in IRAM.
    you can get more detail in following link;
    https://forum.arduino.cc/index.php?topic=616264.0

    =============================================

    CODE:

    =============================================
    #include <Ticker.h>
    
    
    Ticker trigger;
    
    
    void ICACHE_RAM_ATTR zeroCrossingInterrupt(); //add ISR in IRAM, neceassry for 2.5.2 core
    
    
    int zc_pin = D5; // connected to GPIO14
    
    
    int sig_pin = D7; // connected to GPIO13
    
    
    int brightness=0; // variable for counting delay for triggering pulse
    
    
    int state=0;
    
    
    void pulse_gen(){ // timer interrupt to triger gate
    
    
    // Serial.println("pulse genrated");
    
    
    if(state==0){
    
    
    state=1;
    
    
    digitalWrite(sig_pin,HIGH); //turns on the triac
    
    
    trigger.attach_ms(1, pulse_gen); // repeat timer interupt after 1ms
    
    
    }// turn on gate
    
    
    else {
    
    
    state=0;
    
    
    digitalWrite(sig_pin,LOW);
    
    
    trigger.detach(); //disable timer interupt
    
    
    } // turn off gate
    
    
    }
    
    
    void zeroCrossingInterrupt(){ //zero cross detect interrupt
    
    
    trigger.attach_ms(brightness, pulse_gen); //run timer interupt after selected time by user
    
    
    //Serial.println("interupt generated");
    
    
    }
    
    
    void setup()
    
    
    {
    
    
    pinMode(zc_pin, INPUT_PULLUP); //zero cross detect
    
    
    pinMode(sig_pin, OUTPUT); //TRIAC gate control
    
    
    attachInterrupt(zc_pin,zeroCrossingInterrupt, RISING); //interupt for detecting zero cross
    
    
    //Serial.begin(115200); //serial monitor for debuging
    
    
    //Serial.println("Board is ready");
    
    
    }
    
    
    void loop(){
    
    
    brightness = map(analogRead(A0), 0, 1023, 2,9); //read user input and convert it into time in mili-seconds
    
    
    if(brightness>8) {
    
    
    detachInterrupt(zc_pin); // disconnect interrrupt if triggering is closed to next cycle
    
    
    }
    
    
    else{
    
    
    attachInterrupt(zc_pin,zeroCrossingInterrupt, RISING); // connect interrupt
    
    
    }
    
    
    //Serial.print("brightness : ");
    
    
    //Serial.println(brightness);
    
    
    delay(100);
    
    
    }
    =============================================