Altium
Arduino ProjectsESP8266IOT Projects

IOT light dimmer using Arduino and Nodemcu esp8266 wifi module

IoT Light Dimmer Project Description:

iot light dimmer

IoT light dimmer– In this tutorial, you will learn how to control the brightness of a 110/220v Ac light Bulb using Arduino, Nodemcu esp8266 wifi module, MOC3021, BTA16 Triac, Zero crossing detector and Blynk application. With the help of this project, the ac light bulb brightness can be controlled from anywhere around the world. This project is entirely based on my previous tutorial in which I used a potentiometer to control the brightness of the ac light bulb. So I highly recommend you should watch my previous tutorial as I will be using the same connections.

In this tutorial, I will only explain the modifications.


Amazon Links:

12v Adaptor:

Arduino Uno

Arduino Nano

Mega 2560:

MOC3021 optoisolator Triac Driver:

BTA16 Triac:

5×7 cm vero board:

EL817 Optocoupler:

Nodemcu ESP8266 WiFi Module:

LM7805 Voltage Regulator:

470uf capacitor:

330-ohm resistor:

DC Female Power Jack:

Female Headers:

Male Headers:

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

DISCLAIMER:

Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


Nodemcu ESP8266:

I have a very detailed getting started tutorial on the Nodemcu ESP8266 explaining Pinout, Features, and other important things.

IoT light dimmer Circuit Diagram:

In this project, I am using the same circuits and connections as explained in my previous tutorial.

The only modification that I made in this project is the addition of the regulated power supply based on the lm7805 voltage regulator and Nodemcu esp8266 wifi module. The 5 volt regulated power supply will be used to power up the Nodemcu esp8266 wifi module.

For the circuits interfacing and Blynk application designing watch video Tutorial.


IoT light dimmer Programming:

Arduino Programming:

#include <SoftwareSerial.h>
SoftwareSerial nodemcu(7,8);

int firstVal,secondVal;
String myString; // complete message from arduino, which consistors of snesors data
char rdata; // received charactors
String cdata; // complete data
int flag = 0;

int zeroc = 2 ;
int  triac = 3 ; 
int brightness;

void setup()
{
Serial.begin(9600);
nodemcu.begin(9600);
pinMode(triac, OUTPUT);
attachInterrupt(0, angle, RISING);
}

void loop()
{
/*
      if(nodemcu.available() == 0 )
      {
       
        ; 
       
      }
*/
      if ( nodemcu.available() > 0 ) 
{

      rdata = nodemcu.read(); 
    myString = myString+ rdata; 
    //Serial.print(rdata);
    if( rdata == '\n')
    {
Serial.println(myString);
// new code
String l = getValue(myString, ',', 0);
String m = getValue(myString, ',', 1);
firstVal = l.toInt(); // 
secondVal = m.toInt();
brightness = secondVal;
myString = "";



            if ( (firstVal == 10)&& ( flag == 0) ) // Turns off the ac light blub
            {

             digitalWrite(triac, LOW); 
              flag = 1; 
                
            } 

                        if ( (firstVal == 11)&& ( flag == 1) ) // turn ON on the ac light bulb
            {

             digitalWrite(triac, HIGH); 
              flag = 0;                
            } 

            
    }
  
}
  
}


String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void angle(){
 
 // brightness = map(brightness,0,1023,0,3000); 
  Serial.println(brightness);
  if( brightness < 0 )
  {
    ;
  }
  delayMicroseconds(brightness);
  digitalWrite(triac, HIGH);
  delayMicroseconds(10);
  digitalWrite(triac, LOW);
}


Nodemcu esp8266 wifi module programming:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <SimpleTimer.h>

int pinValue1,pinValue2;

int firstVal;
String v2arduino; // values to arduino

char auth[] = "09124ea7a93847168755f4c42960e155";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ZONG MBB-E8231-6E63";
char pass[] = "08659650";

SimpleTimer timer;

String myString; // complete message from arduino, which consistors of snesors data
char rdata; // received charactors

// This function sends Arduino's up time every second to Virtual Pin (1).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000);
  
}



void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);


}

void loop()
{

  
  
   if (Serial.available() == 0 ) 
   {
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  //toarduino();
   }
   
  if (Serial.available() > 0 ) 
  {
    rdata = Serial.read(); 
    myString = myString+ rdata; 
   // Serial.print(rdata);
    if( rdata == '\n')
    {

// new code
String l = getValue(myString, ',', 0);



firstVal = l.toInt();


  myString = "";
// end new code
    }
  }

}


String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;

    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

// in Blynk app writes values to the Virtual Pin V3
BLYNK_WRITE(V10)
{
   pinValue1 = param.asInt(); // assigning incoming value from pin V10 to a variable
v2arduino = v2arduino + pinValue1 + "," + pinValue2 + "," ; 
Serial.println(v2arduino); 
delay(100); 
v2arduino = ""; 
}

BLYNK_WRITE(V2)
{
   pinValue2 = param.asInt(); // assigning incoming value from pin V10 to a variable
v2arduino = v2arduino + pinValue1 + "," + pinValue2 + "," ; 
Serial.println(v2arduino); 
delay(100); 
v2arduino = ""; 
}


void toarduino()
{
v2arduino = v2arduino + pinValue1 + "," + pinValue2 + "," ; 
Serial.println(v2arduino); 
delay(100); 
v2arduino = ""; 
}

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

3 Comments

  1. Can I make an AC light dimmer using Nodemcu only, to control the brightness using the blynk app?

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button