2 way lighting circuit control with blynk and feedback updates

@Costas
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?

Cheers

kev

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.

cheers

kev

EDIT - forgot to tag you @Jamin

#include<BlynkSimpleEthernet.h>
#include<SimpleTimer.h>
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()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  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
  }
  else
  {
    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()
{
  Blynk.run();
  timer.run();
}
2 Likes

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.

CHeers

Kev

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

3 Likes

Hi again blynkers, finally the parts for the ac detection have turned up from China so I have now hardwired a single channel of this 2 way switching circuit up on my bench actually using 240v mains and a lamp and physical 2 way switch…

DISCLAIMER - PLEASE BE AWARE IF ANYONE ATTEMPTS THIS PROJECT YOU MUST HAVE A THOROUGH UNDERSTANDING OF ELECTRICAL ENGINEERING AND THE VOLTAGES YOU ARE USING ARE POTENTIALLY LETHAL - YOU HAVE BEEN WARNED.

Ok here are a few pics of this setup on the bench if the on state and off state. You can see in the on state that the LED on the AC to 5 logic board is on indicating data is being fed back to pin3 on the Arduino.

Here is a pic in the off state

Here is a pic of the ‘manual’ two physical switch where you can still turn the light on and off, and because of the AC feedback provided by the 240v to 5v logic board the widget also gets updated on the app so all the buttons will be kept in sync.

Clearly I have used a 4 ch relay board and 8ch 240v to 5 logic board, using a single channel and button widget for the demo here. The test rig is wired exactly as per the diagram above this post.
Next stage is I am building the whole thing into a box with 16 channels(using a Mega2560 for this). I will upload pics when that is complete. @Jamin and @speed57

I will post the code once I tidy it up.

here are the ebay links to some bits

240v to 5v logic board optoisolator

8ch relay board

4 Likes

@newdos

Great project, exactly what I’m working on and trying to get to work .

Could you expound on the use of the Optocoupler isolation voltage Test Board and what role it plays. I understand it’s the trigger device to tell Blynk the circuit is on or active. I see it has terminals for N & L, is that litoral, or can we just pass the Load or Neutral through it within the flow of the circuit and voltage. Similar to the ACS712 where it’s L in and L out. Pass-thru …?? I guess I just need to confirm how it’s wired on the mains side.

Thanks so much
laf0138

PS - Will most of these 1bit Optocoupler Isolations Voltage Test Boards also work with 110v ?

@Lane0138

These current sensing boards are generally using HALL effect (magnetic) sensing, so yes, you just “pass through” the AC wiring. How they are used in the code can be dependent on application… depending on the sensor they can either determine the level of current flowing through and/or be used simply as a logic gate (any current flow = ON no current flow = OFF).

If the device is labeled H (Hot) & N (Neutral) then they might have a reason for wanting the Hot side on a particular terminal (perhaps for additional isolation) or just because of someone’s choice of silk screen labeling :wink: Then in that case, such as with L in L out (load & load) it doesn’t matter.

And yes they will work on 110v just as easily (but best to always check the specs to be sure).

Thanks @Gunner ! You’re bailing me out on multiple topics this week.

THANKS !!

Hi @Lane0138

@Gunner This isnt a hall effect board so it is NOT pass through and must not be wired that way. It has terminals marked L N which in UK is live and Neutral. The board works on the principle of coverting the AC to a low level and then triggers an opto isolator which gives yo either a logic 0 or logic 1 at 5v. This is fed into digital pin3 of the arduino(in my example) and constantly read. If the arduino sees a logic one it writes direct to the associated widget to change it ‘ON’ state or ‘OFF’ state if a logic 0. This means that which ever source switches the light on ie the arduino controlled relay via blynk, or the manual wall switch, the Blynk app will always represent the the actual state of the power to the lamp. if you didn’t use this board when switching with the manual switch blynk would not be updated and all your buttons would get out of sync - very messy and confusing if you have 16 of them!!!

this is another homemade version of the isolation board which will help understand how it works. I have used this on a blynk controlled ac dimmer

I will post the code later today or tomorrow

Does that help and make sense ?

Cheers

Kev

EDIT - NOTE the code earlier in this thread will work but I have improved it and will post later, but at least it will help you understand how it works until I updated it.

Yes, I see how your board is setup, and correct, not quite what I was referring too. I was referring in general about the varying types, particularly current sensing as they must be pass through.

So this board needs a splice wire for each circuit you are monitoring… makes sense, no real current flow, so you don’t need a heavy gauge thus run many smaller gauge wires to all the needed locations to monitor that circuit loop It is still opto-isolated from the MCU but yes, best to wire accordingly to the Live/Hot / Neutral/Ground :wink:

Spot on @Gunner :relaxed:

And here is the code as promised

Cheers

kev

#include<BlynkSimpleEthernet.h>
#include<SimpleTimer.h>
#include <TimeLib.h>            // Used by WidgetRTC.h
SimpleTimer timer;

// Lighting control pins and AC feedback
int RELAY1_PIN = 2;       // Relay 1 pin
int AC_Sens1_pin = 3;     // Relay 1 ac sens pin
int feedback1;            // AC feedback variable
int feedback2;            // AC feedback variable

char auth[] = "xxxxxxxxxxxxxxxxxxxx";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(RELAY1_PIN, OUTPUT);
  pinMode(AC_Sens1_pin, INPUT);

  // Default ALL relays to OFF before reinstating stored states
  digitalWrite(RELAY1_PIN, 0);

  //Lets sync up all the store states for the V pins and load the appropriate variables listed below
  Blynk.syncVirtual(V3);             // Light 1 Relay state

  timer.setInterval(300, AC_detect);      // Check AC_detect for AC presence every 50 milliseconds
}

void loop()
{
  Blynk.run();
  timer.run();
}

void AC_detect()
{
  feedback1 = digitalRead(AC_Sens1_pin); // Read the digital pin for an AC input

  // update SWITCH widget state only if AC input has changed since last check
  // this prevents widget switch bounce on the app device screen
  if (feedback1 != feedback2) Blynk.virtualWrite(V2, !feedback1);
  feedback2 = feedback1;
}

// Sync up all of the virtual pin data
BLYNK_WRITE(V2)  // Runs every-time switch widget is toggled - For Light 1
{
  digitalWrite(RELAY1_PIN, !digitalRead(RELAY1_PIN)); // Toggle relay1
  Blynk.virtualWrite(V3, digitalRead(RELAY1_PIN));    // Store Light 1 relay state in V3
}

BLYNK_WRITE(V3)
{
  int pinData = param.asInt();
  digitalWrite(RELAY1_PIN, pinData); // and reinstate relay state
}
3 Likes

Thanks @newdos - This will be a huge help !!

@Lane0138

Pleasure @Lane0138 just shout if you need any more help

Cheers

Kev

@newdos - I do have one more question if you don’t mind.

In the end, there’s been a great deal of talk on this chain about ACS712 and Optocoupler Voltage Boards.

In my project, like yours, I just need to know if there is current flowing, on/off that’s it. Which would you recommend? Much like your like photo example of 3/2/17.

Thanks Again !!

deffo mine as thats what it is designed for - the acs712 is really for measuring the current flow and quantifying it

Cheers

Kev

hello!

sorry for just dropping in, but i still do not understand exactly what do you want to accomplish with this setup…

do you want to turn on / off a lamp from:

  • blynk app
  • physical switch
  • motion sensor

this is correct?

@wanek - Jump right in !

It’s to monitor a physical switch, so I know at a glance if it’s on.

Cheers

Lane0138

ok, but if that’s all, wouldn’t be easier to use a simple push button instead the physical switch? like you have on the door bell?

than every time the button is pressed, it toggles the lamp. and in the mcu you have to monitor only the state of the digital pin, which controls the relay, to know if the lamp is on or off.

why to complicate with current measurement and other stuff?