IOT light dimmer using Arduino and Nodemcu esp8266 wifi module
Table of Contents
IoT Light Dimmer Project Description:
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:
Arduino Nano USB-C Type (Recommended)
MOC3021 optoisolator Triac Driver:
*Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
#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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#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 = ""; } |
Can I make an AC light dimmer using Nodemcu only, to control the brightness using the blynk app?
yes you can.
Bro,how to connect multiple ac dimmer circuits to arduino or NODEMCU