2 way lighting circuit control with blynk and feedback updates

Those AC detector boards are a bit overkill… the ACS712 can also report current to the Blynk app…handy for calculating watt-hours.

Also those detector boards require 24V… the ASC712 requires only 5V… easyer to hook up

Here is the diagram for the above code:

But do these not require an analog pin for every input as well ???

I read that those boards designed really for Plc require a 5v supply and give a logic 1 or 0 output??

Yes if you look at the diagram, an analog input is required to read the voltage different on the ACS721.
If you are using more than 1 sensor, then yes you will need an ESP32 or UNO/Yun etc… or an ADC breakout for the ESP8266.

Upon detection of positive current, it will light up the LED widget (the state of power)…

If it detects 0 current, it will turn off the LED widget.

You then also have seperate control over the relay using a PUSH button.

The relay is connected so that as long as the physical button and relay are inverted from each other then current will flow.

Hi gunner just so I’m aware of this why does it say have way down the add for the paolo board no higher than 30v??? I’m intrigued is it just a warning to stop people playing with mains ?

Cheers kev

Thanks Jamin but as my early post states I want to use a latching button on blynk app with no led and a physical latching 2nd switch as per normal 2 way circuit

Cheers Kev

You mean SWITCH type button widget? Just change the LED widget to a switch type… it will reflect the state of current… but not the state of the RELAY… so if the relay is ACTIVE… the current might not flow because it depends on the state of the phyiscal switch.

This is why your idea becomes very complicated. Better to read state of current, and light up an LED, then use a PUSH button to change the state of RELAY.

See diagram. There is a physical lightswitch in there.

EDIT: actually… just thought of another way to hook up the button to a SWITCH type widget. brb writing the code

UPDATE: here you go… now the BUTTON (SWITCH type) will light up when Current is detected. If you switch the BUTTON widget off, then current will stop, and the relay will invert its current option.

//#define BLYNK_DEBUG
//#define BLYNK_PRINT Serial

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

SimpleTimer timer;

char auth[] = "xxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";


int RELAY_PIN = 14; // GPIO-14 or D5 on ESP8266
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
int ACSoffset = 2500;
double Voltage, Amps;

void readACS712() {
  Voltage = (analogRead(A0) / 1024.0) * 5000;
  if (Voltage <= 2500) {
    // detected nothing
    Blynk.virtualWrite(V0, 0); // update BUTTON (SWITCH) widget state when detecting current
    Voltage = 2500;
    Amps = 0;
  } else {
    // detected current
    Blynk.virtualWrite(V0, 1); // update BUTTON (SWITCH) widget state when detecting current
    Amps = ((Voltage - ACSoffset) / mVperAmp);
    Blynk.virtualWrite(V2, Amps); // report Amps, because why not monitor power usage too?

// button control for relay...
// needs to be SWITCH type button, detect CHANGE OF STATE only, and invert relay state
    digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN));

void setup() {
  digitalWrite(RELAY_PIN, HIGH); // most relays are active LOW, so on boot, set HIGH (OFF)
    Blynk.begin(auth, ssid, pass, IPAddress(192, 168, 1, 2));
  } else {
    Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false) {}
  timer.setInterval(1000L, readACS712);

void loop() {

Yes… as they are generally sold to hobbiests, they include a “Cover Their Asp” clause :wink:

But even in the clause, it states…

“Working with higher voltages can be extremely dangerous and should only be attempted by qualified individuals with appropriate equipment and protective gear.”

In other words, certified electricians. Which is why I also included an example for a non-invasive type sensor (usually give its data in the form of resistance value).

Not all, most support 4.5-5v … but as PCL is an industrial rating, thus typically 24v logic, many of these support 24v compliant output levels.

I am done on this topic. Carry on :slight_smile:

1 Like

Thanks for the concise explanation Gunner and all you help bud :slight_smile:



Thanks Jamin, Wil have a play around with this code as soon as I have time. Juast out of interest (and because I’m at work and dont have time at the moment!) what is the arduinoOTA library, not used this before ???



@newdos ArduinoOTA is very powerful. Over the air updates for ESP’s but not available for Arduino’s.
That is why ESP’s are now so popular and generally much more useful than Arduino’s. You can reflash your ESP over the LAN or WAN.

ah ok Costas thanks man thats very cool will have to get into these ESPs - which board should I start with as there seems to be loads of them ??? guess wifi would be good to use ???



@newdos it comes down to personal choice and my soldering skills are very limited so take that into account when you read the details below. ESP’s cost around $4 +/- $2 and I have quite a few types.

I have:

1MB 01’s
1MB 07’s with antenna socket
4MB 12’s
4MB WeMos D1 Mini’s
4MB WeMos D1 Mini Pro’s (technically 16MB and has antenna socket)
4MB Wio Link’s


The 01’s were originally designed with 512Kb of memory and it is quite difficult to use OTA as the ESP’s needs twice your program size to do the flash. Probably still old stock around but unless they are free don’t bother with them.

The ESP’s with 1MB to 4MB can only use the first 1MB for program memory so unless you want to store your webserver in the area above 1MB it generally goes unused. The Pro has 16MB of memory that gives 15MB of storage space but until the ESP core code is updated it is currently only working as a 4MB module.

Board layout

The 01, 07 and 12’s are bare chips and you have to add components to make them work.
The WeMos, Wio Link and nodeMCU (that I don’t have) are complete, ready to go devices with USB ports. As the ready to go units are only a couple of dollars more then they are my preferred choice. If you have a project that needs hundreds of ESP’s and 512kb is enough memory then you might want to consider bare chips. It’s certainly not something I would consider.


During your “bench tests” antennas are not that important but when you fit the ESP’s in a case and move them to your garden house, loft, basement etc they are very important.

My current favourite is the WeMos D1 Mini Pro for $5 plus delivery costs.


Cheers Costas all very useful info - will be ordering up aWeMos D1 Mini Pro!!



@Costas Costas,

Sorry to be a pain, but I forgot I had already ordered one of these last week when looking into this project. can you tell me what the difference is with this and the WeMos d1 mini pro other than the ext antenna socket ??? ( and no soldering!!)



@newdos this is the D1 Mini Pro https://www.wemos.cc/product/d1-mini-pro.html and the official WeMos store is at https://wemoscc.aliexpress.com/store/1331105

This is the latest official D1 https://www.wemos.cc/product/d1.html but I can’t see it in the store at the moment.

Yours looks to be the 2015 model

WeMos removed all details from their website regarding earlier versions of their boards so you will have to go hunting for the pinout details. I believe there was one or two pins changed over time. You could use the silk screen pin references in your sketches but I recommend against this because then your code is not portable to other ESP’s.

There are many “Wemos” products that are nothing to do with WeMos so we recommend purchasing from the official store. That said some knock-off’s do function as per specification.

We use the Mini’s because the D1’s are too big for our requirements and we need antennas for some of our projects. Sometimes you can get away without any soldering on the Mini’s. Just push the supplied headers into a breadboard. Not really recommended because invariably one or more of the pins will not make contact with the WeMos and you will be left wondering why your sketch fails.

Cool Costas and thanks again - so for initial playing with, the board I have ordered will work fine standalone just the pinouts to be wary of - correct?



Yes and the full size D1 should be good for testing purposes.

OK guys, I have cracked this in a test bed scenario. I have rigged up a 5v version just with an LED and two way switching as per the original diagram at the start of this thread, with one of the switches replaced by a relay controlled by Blynk. I have managed to get the current detection feedback logic working great so that when the manual switch is toggled the app widget gets updated as well. This means that which ever switch is turned on or off (via app or physical switch) the app widget will reflect this correctly.

I have also added a little logic to ensure the correct state of any relay is re-instated in the event of a power failure or a reset of the arduino. This is essential when I plan to have at least 8chs or more, as you would not want everything to either go all off, or all on, in the event of power failure. This is achieved by storing the relay state in V31 shown in the code below and re-loading it from the server via the sync command, to re-instate it’s status prior the the power failure/reset etc.

EDIT I should of also said the feedback for the AC is taken from L2 of either the relay or the physical switch. In the real word when this is complete that will feed the board I listed earlier in the thread converting the mains AC to a logic level which is read on pin 3 in my sketch ie. AC_Sens1_pin.

All comments greatly appreciated.



EDIT - forgot to tag you @Jamin

SimpleTimer timer;
int RELAY1_PIN = 2;   // Digital pin 2 for Relay1
int AC_Sens1_pin = 3; // Digital pin 3 for AC sensing1 signal
int feedback1;        // Variable to store status of AC feedback1 presence

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxx";

void setup()
  Blynk.syncVirtual(V31); //Get stored status of V31 from Blynk server at startup
  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(AC_Sens1_pin, INPUT);
  timer.setInterval(200, AC1_detect); // Check AC1_detect for AC presence every 200 milliseconds

void AC1_detect()
  feedback1 = digitalRead(AC_Sens1_pin);
  if (feedback1 == 0)
    Blynk.virtualWrite(V2, 0); // update SWITCH widget state from AC input
    Blynk.virtualWrite(V2, 1); // update SWITCH widget state from AC input

BLYNK_WRITE(V2)  // Runs every-time switch widget V2 is toggled.
  digitalWrite(RELAY1_PIN, !digitalRead(RELAY1_PIN)); // Toggle relay1
  Blynk.virtualWrite(V31, digitalRead(RELAY1_PIN));   // Store relay1 state on V31 for re-sync if AC failure

BLYNK_WRITE(V31) // This runs on V31 re-sync at startup, and re-instates state
                 // of relay before power failure/rest etc
  int V31_val = param.asInt();       // Get V31 value that is stored on Blynk server
  digitalWrite(RELAY1_PIN, V31_val); // Set relay1 to state before power failure/reset etc

void loop()

Great work! Just to help anyone else who happens to stop in to this thread (because I know it will be popular), could you please post a photo, and a link to the hardware you used.

Really glad you worked it out as I will be borrowing your code once my shed is built :slight_smile:

Hi @Jamin,

Yes I will list the hardware I am/going to use as I am still waiting for the mains input opto isolator to 5v logic board to arrive. Probs will add some pics when i bench rig the mains version.

For the time being to prove the software and concept the hardware is as follows -

1 x UNO R2
1 x Ethernet W5100 shield piggy backed onto the UNO
1 x 8ch 5v logic relay board
1 x red led and resisotr to suit(?270Ohm if memory serves me corretc for 5V)
1 x manual domestic 2 way switch

all wired as per diagram at start of thread but obviously because it is 5v logic no opto/main coupler board is required and the feedback goes direct from terminal L2 on the relay to digital pin 3 on UNO.



EDIT - forgot to say I have one minor issue to resolve that is cosmetic more than anything else. I get a bit of switch bounce on the app when toggling from one state to another, but this doesn’t affect the actual relay being switched so all is fine. I suspect the cause of this is the void AC1_detect() routine which re-writes the app widget every 200 millis even when button is not pressed. I need to re-jig the code so it doesn’t constantly update the widget button UNLESS the state of it changes.

Updated diagram with AC sensing added