Two way lighting circuit - anything I need to be aware of?

Hi all,

I have an existing 2 way lighting circuit in my garage (to be clear, it's two switches of which either one can turn the light on or off). It occurred to me that the lighting circuit could be controlled via one of my Wemos D1 mini's (please see attached proof of concept GIF).

I'm interested in what is best practice in these type of projects.

I've draw a pull up resistor, would pull-down be better? No difference?

Basic code is below, does it look adequate? (I will learn how to add Blynk virtual button later).

Many thanks

int RELAY = D5;   //relay shield connected to D5 via selectable solder tab (Lolin)
int SWITCH = D6;  //light switch circuit connected to D6
    
void setup() 
{
  pinMode(RELAY, OUTPUT);
  pinMode(SWITCH, INPUT);
}

void loop()
{
       
        if(digitalRead(SWITCH) == HIGH)
        {
        digitalWrite(RELAY, HIGH);
        }else
        {
        digitalWrite(RELAY, LOW);
        }
        
}

I don’t see why you need anything other than a relay to do this , or even why not just use the switches to turn the lights on directly .

This should work, but I would go with pull down resistor. Also add extra 10K resistor from 5V output to first switch, it will limit the current when circuit is closed (if you listen my advice and change your wiring to pull down setup).

Also you don't need "if" statement on your code, just add pull down resistor and code should look like this:

void loop()
{
        digitalWrite(RELAY, !digitalRead(SWITCH));
        
}

P.S. I forget about debouncing, it can mess things up, tho relay consists of a inductor that should suppress debouncing, if not and you notice relay acting strange while switching ON or OFF you'll have to add more code to detect state change and add a very small delay for debouncing. Debouncing basically is a micro vibrations of a metal contact in switch that appears for a very short time after pressing switch, at that short time your D1 can read few very fast on/off states.

P.S.S I'm not familiar with D1 Mini interrupts, but it would be better to use interrupts instead of manually checking pin state.

hammy:
I don’t see why you need anything other than a relay to do this , or even why not just use the switches to turn the lights on directly .

This wiring makes perfect sense, especially on stairs, where you turn light on downstairs, climb up and turn light off. Usage of relay is odd, but it adds level of protection, for example you can't get shock if switches wired like this gets damaged, in worst case scenario it will turn light on. To be honest your comment does not help in any case or add any useful information to this topic, save your time and don't clutter thread for no reason.

Thanks 3Dgeo for the helpful comments!

To clarify, i will be adding WiFi control via the Blynk app. I want to be able to switch the lights on/off via a virtual button on the app, and receive indication status of the lights.

I am partway through writing (actually attempting to write) the code for this functionality, I didn’t want to ask for help yet as I have not had a proper go at it :slight_smile:

But you didn’t state that ; that is a different project .

“ (I will learn how to add Blynk virtual button later).”

I thought it was clear it was to be improved upon, but no worries

877:
Hi all,

I'm interested in what is best practice in these type of projects.

It depends on which country you live in.

In some, you will find yourself behind bars for "fiddling" with mains installations without an appropriate licence or product certification.

bluejets:
It depends on which country you live in.

In some, you will find yourself behind bars for "fiddling" with mains installations without an appropriate licence or product certification.

Hi bluejets,

I was actually referring just to the Arduino side of things. I’m in the UK where I have been a qualified sparky for over 20yrs. Probably should be behind bars though lol :slight_smile:

I’ve always hated the way light switches that both point up when off. It would be nice to make them one latching button with a status light.

You could use the relay as an intermediate switch in your circuit , and use another input to determine if the light is actually on . That way , if your Arduino stuff fails the light can still be used independently of it .

Light status indicator will be D1 pin that tuns light ON/OFF, no-brainer here.

If you use your circuit and you want to turn same light via WiFi you have to sort out some scenarios, for example:
If you turn your light via Wifi and then "turn it on" again via switch – are you OK with it doing nothing or you want it to turn your light OFF? If second scenario – you have to code not for input pin "HIGH" detection but rather pin "change" detection (it going from low to high or from high to low). It may make no sense now, but when you add Wifi button you'll realize what I mean. And in this scenario debouncing will become an issue, so you have to add a small delay.

Sorry, I don't know what do you mean by "Blynk virtual button", can you give more information on this? You goal is to control it via android app/PC?

You don't need no steenking pullup!

Arduino pinMode( pin, INPUT_PULLUP ) gives an input pin you can read a pullup through internal resistor 20K-50K. If you ground the pin through a switch, it will read LOW else HIGH.

You could replace the light switches with "buttons" (press to change whatever state the lights are in) such as capacitive touch buttons (no moving parts, can roll your own super cheap) or piezo disk buttons (can tell force, know touch from tap, can take a smacking, can sense knocking THROUGH a door) or a variety of ways. Suppose you had a "clapper" that recognized a clap pattern to do some special thing? What you can imagine you -may- be able to make work and show off.

Arduino Playground capacitive sensing articles:
https://playground.arduino.cc/Main/InterfacingWithHardware#capsens

The article that helped me roll my own from foil and paper and a couple cheap bits. It senses through some things, can be placed under a non-conductive surface with labels printed on top.
https://playground.arduino.cc/Main/CapacitiveSensor?from=Main.CapSense

GoForSmoke:
...You could replace the light switches with "buttons" (press to change whatever state the lights are in) ...

Considering that Wifi will be added to the circuit – yes, GoForSmoke is right, it makes sense to get rid of those switches (and their wiring) and use simple push buttons instead, wiring will be simpler and, how GoForSmoke pointed out, there are tone of options to use as a switch. Tho I do not recommend using D1 internal pullups at this situation, D1 mini is a sissy and wires will be long, stuff waiting to happen...

I would leave 1 physical switch wired in parallel with the relay (wired OR switch) so if the duino quits or goes nuts (not with my code!) the light can be turned on by hand.

Thanks both for all the replies and ideas, I hadn't got that far thinking things through but here are my thoughts below:

3Dgeo:
If you turn your light via Wifi and then "turn it on" again via switch – are you OK with it doing nothing or you want it to turn your light OFF? If second scenario – you have to code not for input pin "HIGH" detection but rather pin "change" detection (it going from low to high or from high to low). It may make no sense now, but when you add Wifi button you'll realize what I mean. And in this scenario debouncing will become an issue, so you have to add a small delay.

Sorry, I don't know what do you mean by "Blynk virtual button", can you give more information on this? You goal is to control it via android app/PC?

Yes I would want it to turn the light off, so would use pin change detection. I will need to look into that! Also I understand in principle about debounce but I need to understand the code (my weak point is the code).

Blynk is an iOS app, you can create a layout and receive data from the Wemos coded with the appropriate auth key. You may use an alternative program/app?

GoForSmoke:
You could replace the light switches with "buttons" (press to change whatever state the lights are in) such as capacitive touch buttons (no moving parts, can roll your own super cheap) or piezo disk buttons (can tell force, know touch from tap, can take a smacking, can sense knocking THROUGH a door) or a variety of ways. Suppose you had a "clapper" that recognized a clap pattern to do some special thing? What you can imagine you -may- be able to make work and show off.

Very interesting, I'm trying to keep it simple for me to understand but I will take a good look at that. I already have all my switch wiring in place (currently running on mains 230V), so I thought it would be easy to convert. But as you said below if the arduino dies I lose the ability to switch the lights on. I don't mind altering the wiring if it's worth it.

3Dgeo:
Considering that Wifi will be added to the circuit – yes, GoForSmoke is right, it makes sense to get rid of those switches (and their wiring) and use simple push buttons instead, wiring will be simpler and, how GoForSmoke pointed out, there are tone of options to use as a switch. Tho I do not recommend using D1 internal pullups at this situation, D1 mini is a sissy and wires will be long, stuff waiting to happen...

OK thank you, I will look at this more. Have I got this correct:
The push switches would be in parallel. And effectively the Blynk "virtual switch" would be in parallel too. So that a signal from either of the two switches, or Blynk app, would signal a pin on the arduino/wemos, and by using pin detection would flip the state of the relay? And when the relay is in the 'on or off' state it could signal back to the Blynk app.

GoForSmoke:
I would leave 1 physical switch wired in parallel with the relay (wired OR switch) so if the duino quits or goes nuts (not with my code!) the light can be turned on by hand.

Great idea! I will definitely do that :slight_smile:

Thank you both for the help, I will try and put a schematic together and a draft sketch and report back :slight_smile:

I'm not going to comment on 220V part, thats out of my comfort zone, bus here's how I do it (do not add pull down before limiting resistor cos it act as a voltage divider, and separate buttons if you want to control separate relays):
P.S. I don't remember if D1 5V compatable, if not – replace 5V on buttons with 3.3V

I used ULN283A just for example, it's a good IC for driving relays, but you can use your D1 mini shield, it will work just fine.

877:
Blynk is an iOS app, you can create a layout and receive data from the Wemos coded with the appropriate auth key. You may use an alternative program/app?
https://www.blynk.cc/

Sorry, no, I have no experience with iOS.

877:
already have all my switch wiring in place (currently running on mains 230V), so I thought it would be easy to convert. But as you said below if the arduino dies I lose the ability to switch the lights on. I don't mind altering the wiring if it's worth it.

It will work with current wiring just fine if you don't want to change anything, even the code will be the same (I talk about code below).

877:
Have I got this correct:
The push switches would be in parallel. And effectively the Blynk "virtual switch" would be in parallel too. So that a signal from either of the two switches, or Blynk app, would signal a pin on the arduino/wemos, and by using pin detection would flip the state of the relay? And when the relay is in the 'on or off' state it could signal back to the Blynk app.

OK, lets talk about code a bit.

It will be better to divide code in parts:

  1. D1 pin input processing
  2. Virtual input processing
  3. Setting output state

Lets start with easy part – "change output state" function, when it's called it will flip relay output:

void flipRelayOutput()
{
        digitalWrite(RELAY, !digitalRead(RELAY));
        
}

Now input pin processing – here you will need to look for pin input state change, it's best to use pin change interrupts, and when detected just call flipRelayOutput() – easy peasy so far.

Aaand the fun part, Wifi button – after connection to device first thing you send to device is "RELAY" pin state so it knows if relay is ON or OFF. I don't know iOS UI so I assume it will be one toggle button with set background color according to "RELAY" pin state. So, when you push that button device will send command to D1 to call same good old flipRelayOutput() and same command about "RELAY" pin state to confirm that command was received and executed.

I think thats it :slight_smile:

3Dgeo:
I'm not going to comment on 220V part, thats out of my comfort zone, bus here's how I do it (do not add pull down before limiting resistor cos it act as a voltage divider, and separate buttons if you want to control separate relays):
P.S. I don't remember if D1 5V compatable, if not – replace 5V on buttons with 3.3V

I used ULN283A just for example, it's a good IC for driving relays, but you can use your D1 mini shield, it will work just fine.

Thanks 3Dgeo I appreciate the schematic and your help (what software did you use please?)

I'm actually ok with the 230V side, my job is now working on high voltage power systems up to 11kV :slight_smile:

It's 3.3V on the Wemos I/O pins. With your circuit, it would be 0V on D7 when pulled low, or 2.5V when switch is closed (1/2 of Vcc if resistors are identical). So it should be ok on 5V? (sorry for the basic question). I was just thinking about voltage drop in the wiring also being a factor, so higher voltage may be preferred (prob about 8m to furthest switch).

Or would it work better for me with just the pull down resistor, and use 3V?

Thanks :slight_smile:

I updated my previous post.

877:
Thanks 3Dgeo I appreciate the schematic and your help (what software did you use please?)

I'm actually ok with the 230V side, my job is now working on high voltage power systems up to 11kV :slight_smile:

It's 3.3V on the Wemos I/O pins. With your circuit, it would be 0V on D7 when pulled low, or 2.5V when switch is closed (1/2 of Vcc if resistors are identical). So it should be ok on 5V? (sorry for the basic question). I was just thinking about voltage drop in the wiring also being a factor, so higher voltage may be preferred (prob about 8m to furthest switch).

Or would it work better for me with just the pull down resistor, and use 3V?

Thanks :slight_smile:

No, in current circuit D1 will get 5V, DON'T DO IT :slight_smile:

If you move horizontal resistor to right of a vertical resistor than yes, it would read 2,5V which is still digital high, tho just stick with 3,3V on a buttons, I think you'll be fine even with 6 m wires.

P.S. I'm fine with playing with 220V myself, but not OK on recommendations to others, If someone dies/gets hurt/destroys property while trying to implement stuff you recommend or implement incorrectly you may end up in serious trouble...

Looks like Proteus :smiley: