Analog RGB Led Strip Fade effects

I tried implementing a smart roof light at my house. I had successfully implemented it with the zeRGBa widget and also made it turn on and off using Alexa. Now I want help in added fade in and fade out effects during turn on and turn off. as delay blocks can’t be used here. Could someone help on how to go forward with it.

I have used a NodeMCU and couple of TIP31c mosfets to control them.


      Download latest Blynk library here:

      Blynk is a platform with iOS and Android apps to control
      Arduino, Raspberry Pi and the likes over the Internet.
      You can easily build graphic interfaces for all your
      projects by simply dragging and dropping widgets.

        Downloads, docs, tutorials:
        Sketch generator: 
        Blynk community:  
        Social networks:  

      Blynk library is licensed under MIT license
      This example code is in public domain.

      This example runs directly on NodeMCU.

      Note: This requires ESP8266 support package:

      Please be sure to select the right NodeMCU module
      in the Tools -> Board menu!

      For advanced settings please follow ESP examples :
       - ESP8266_Standalone_Manual_IP.ino
       - ESP8266_Standalone_SmartConfig.ino
       - ESP8266_Standalone_SSL.ino

      Change WiFi ssid, pass, and Blynk auth token to run :)
      Feel free to apply it to any other example. It's simple!

    /* Comment this out to disable prints and save space */
    #define BLYNK_PRINT Serial

    #include <ESP8266WiFi.h>
    #include <DNSServer.h>
    #include <WiFiManager.h>
    #include <ESP8266WebServer.h>
    #include <BlynkSimpleEsp8266.h>
    #include <WiFiUdp.h>
    #include <ArduinoOTA.h>
    //#include <SimpleTimer.h>
    #include "WemoSwitch.h"
    #include "WemoManager.h"
    #include "CallbackFunction.h"
    #define VPIN V2  //Use a unique virtual pin for each device using the same token / dashboard

    char auth[] = "******"; //Get token from Blynk

    //on/off callbacks
    void lightOn();
    void lightOff();

    WemoManager wemoManager;
    WemoSwitch *light = NULL;

    boolean LampState = 0;
    boolean SwitchReset = true;   //Flag indicating that the hardware button has been released
    int switchState = 0;

    const int RelayPin = D4;      //Relay switching pin. Relay is pin 12 on the SonOff

    const int RedPin1 = D0; 
    const int RedPin2 = D5; 
    const int GreenPin1 = D1; 
    const int GreenPin1 = D6;
    const int BluePin1 = D2;
    const int BluePin2 = D7;

    SimpleTimer timer;

    int Red = 0;
    int Green = 0;
    int Blue = 0;
    // Your WiFi credentials.
    // Set password to "" for open networks.
    char ssid[] = "****";
    char pass[] = "*****";

    BLYNK_WRITE(V4) // zeRGBa assigned to V4
      // get a RED channel value
      Red = param[0].asInt();
      // get a GREEN channel value
      Green = param[1].asInt();
      // get a BLUE channel value
      Blue = param[2].asInt();
      if (LampState)
      { analogWrite(RedPin1, Red);
        analogWrite(GreenPin1, Green);
        analogWrite(BluePin1, Blue);

        analogWrite(RedPin2, Red);
        analogWrite(GreenPin2, Green);
        analogWrite(BluePin2, Blue);
    void setup()
      pinMode(RelayPin, OUTPUT);
      WiFiManager wifi;   //WiFiManager intialization.
      wifi.autoConnect("Roof Light"); //Create AP, if necessary

      // Format: Alexa invocation name, local port no, on callback, off callback
      light = new WemoSwitch("Roof Light", 102, lightOn, lightOff);

      pinMode(RelayPin, OUTPUT);
      pinMode(LED, OUTPUT);
      //pinMode(TacSwitch, INPUT_PULLUP);
      digitalWrite(RelayPin, LOW);
      digitalWrite(LED, LOW);

      ArduinoOTA.setHostname("Roof Light");

      Blynk.virtualWrite(VPIN, HIGH);


      if (switchState !=  param.asInt()) {
        if (LampState == 1) {
        else {
        switchState = param.asInt();

    void lightOn() {

      Serial.println("Switch 1 turn on ...");
      digitalWrite(RelayPin, HIGH);
     // Setting it to a specific colour.
      analogWrite(RedPin1, 1023);// Red
      analogWrite(RedPin2, 1023);//

      analogWrite(GreenPin1, 425);
      analogWrite(GreenPin2, 425);// Green

      analogWrite(BluePin1, 0);
      analogWrite(BluePin2, 0);// Blue

      LampState = 1;
      Blynk.virtualWrite(VPIN, HIGH);     // Sync the Blynk button widget state
    void lightOff() {
      digitalWrite(RelayPin, LOW);

      Serial.println("Switch 1 turn off ...");

      analogWrite(RedPin1, 0);// Red
      analogWrite(RedPin2, 0);//

      analogWrite(GreenPin1, 0);
      analogWrite(GreenPin2, 0);// Green

      analogWrite(BluePin1, 0);
      analogWrite(BluePin2, 0);// Blue
      LampState = 0;
      Blynk.virtualWrite(VPIN, LOW);      // Sync the Blynk button widget state
    void ToggleRelay() {
      LampState = !LampState;

      if (LampState) {
      else lightOff();

    void loop()

You are correct that delay() is a bad choice… however, timers can be used to do very similar effects.

This is a very crude but functional example…

int Fadelvl;

BLYNK_WRITE(V39) {  // Button Widget on V39, set to switch
  int LEDFadeFlag = param.asInt();
  if (LEDFadeFlag == 1) {
    FadeLvl = 0;
    timer.setTimer(15, FadeLEDUP, 255);  // runs designated function every 15ms for 255 times, then stops
  } else {
    FadeLvl = 256;
    timer.setTimer(15, FadeLEDDOWN, 255);  // runs designated function every 15ms for 255 times, then stops

void FadeLEDUP()
  analogWrite(6, FadeLvl);  // LED on pin 6

void FadeLEDDOWN()
  analogWrite(6, FadeLvl);  // LED on pin 6

@Gunner Thank you

Because of the way your eyes preceve light brightness, it usually looks better if you dim/brighten the leds on a logarithmic scale. It’s quite a bit more involved to write the code to do that, so definitely try what gunner has suggested first.

but do I have to include a library for the timer and if yes which one?
Or how it runs?

@Hannes.P Are you asking about BlynkTimer? It is built in… but you do need to set it up as in the example shown in the documents

And I have a some topics about timers here…