Go Down

Topic: Two way lighting circuit - anything I need to be aware of? (Read 534 times) previous topic - next topic

877

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





Code: [Select]
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);
        }
        
}








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 .

3Dgeo

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:

Code: [Select]

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.

3Dgeo

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.

877

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 :)

hammy

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

877

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

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

bluejets

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.

877

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 :)

wolframore

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.
Bad boys rate our young girls but Violet goes willingly :)
- this is a mnemonic from BEC

hammy

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 .

3Dgeo

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?

GoForSmoke

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
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

3Dgeo

...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...

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.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

Go Up