0
\$\begingroup\$

Background

I am building a simple web application using raspberry as a server. I am using this raspberry also to control an arduino. A user can send commands through the browser to the raspberry, and it can control some arduino functions!

What I want

enter image description here

I want to use an arduino to turn a lamp on and off, parallel to a normal manual switch that I can use to turn on the lamp on and off, like this:

1: If the lamp is off and the switch is also on OFF position (open switch), I can turn the lamp on by using the switch, putting it on the ON position (closing the switch). Then, the monitoring system is gonna tell arduino (and the web server) that the switch is on/closed and the light is on.

2: I can use arduino to turn off the light, without necessarily puting the switch on the OFF position. If the light is OFF (because I turned it off using an arduino) and the switch is on the ON position (closed), I should need to switch it to OFF position and then to ON position again, in a manner that I can sense these changes and send it to arduino (and consequently, to the web server).

3 (Possible improvement?): Whenever I change the state of the switch, the light would change its state to whichever is the opposite of the current one: if the light is on, it goes off and vice-versa, regardless of the switch state.

The problem

How can I actually know if the switch is on and off? There is anyway to use a sensor, some electronic circuit or component that can if tell the switch is on or off? I was thinking about using a relay to control the light, depending of the state of the light that is stored on arduino/web-server. Is this a reliable way (I mean, using a relay) to do that?

I appreciate any possible help on this, I have been interested on IOT and I want to do some simple applications for home automation; this is my first one! Thanks in advance for any help, again.

\$\endgroup\$
0
\$\begingroup\$

To get the functionality you describe, you need to have "3-way" (SPDT) switches — a manual one and a relay that the Arduino controls. This is just like having two switches that control the lights from either end of a hallway, except that one of them is controlled by your MCU.

Sensing the state of the light is a separate matter. There are many circuits on this site and others for safely detecting the presence of line voltage at the light socket.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer! Your approach is valid and I am gonna implement it, though I am still wondering how I would install such system in a simple SPST switch, even though they are not commonly used as lamp switches (I guess?) \$\endgroup\$ – Roger Peixoto Jul 15 '19 at 12:09
  • \$\begingroup\$ They are (where?) regularly used in stair wells where you have a switch at the top and bottom. Toggling either of them turns the light on or off. Many relays are "3-way" and you simply wire them in series with a second "3-way" switch and the light. Wall mounted light switches often are 3-way too so they can be wired to turn the light on in either configuration or actually be used as 3-way switch. \$\endgroup\$ – Goswin von Brederlow Jul 15 '19 at 14:21
0
\$\begingroup\$

As you would need to control mains anyway, you can connect only relay and lights in series. Existing switch you wire to GPIO of Arduino. This way Arduino know state of switch and can control lights without extra connections and sensors. There is a problem that your Arduino have to function correctly to control lights even if you only want to use physical button. So this solution is only appropriate if you can live with inability to switch state of lights until you repair Arduino.

Regarding reliability. I have exactly same solution as in this answer and I have experience some minor problems with relay not disconnecting (contacts welded), so I would advise to buy quality relay and not some unknown part from ebay even if its "parameters" looks OK.

\$\endgroup\$
0
\$\begingroup\$

If you're not comfortable working with 120VAC (could be disastrous if you don't know what you're doing) then a mechanical solution is much safer. You're going to hate this, but for proof of concept you could hot glue a limit switch onto the faceplate underneath your light switch like the picture below (painting it white might make it more tolerable). I'd use a pulldown resistor to give you an active HIGH signal which is usually more intuitive. Check this switch tutorial for guidance.

enter image description here

For a more advanced solution, I'd reduce and rectify the voltage across the switch to see if there's the same thing on both sides of the switch (CLOSED) or different things (OPEN). You'd ideally have a small isolation step-down transformer between any mains voltage and your sensitive microcontroller. For the sake of a cheap solution though, you can use "X rated" capacitors for isolation. Either solution will need to be rectified to DC to be readable by your microcontroller operating between 0 and 5 volts.

schematic

simulate this circuit – Schematic created using CircuitLab

Beautiful output I'd say, and it only sips about 25mW according to the simulator (blue is the isolated, rectified voltage and orange is the zener regulated voltage). Read that output with one of your microcontroller inputs and you've got an indicator! (FYI 0V means no difference across switch = CLOSED)

Capacitor Isolated Voltage Supply

\$\endgroup\$
  • \$\begingroup\$ While certainly a good solution for a seasoned engineer, suggesting mains voltage circuits to people who just have become firm with µC stuff is a receipe for deadly disaster. \$\endgroup\$ – Janka Jul 15 '19 at 3:29
  • \$\begingroup\$ Good point, I added a mechanical solution. Although OP intends to drive a 120V relay with a microcontroller, so that'll be a challenge with a high voltage interface too. \$\endgroup\$ – Kent Altobelli Jul 15 '19 at 3:53
  • \$\begingroup\$ Thank you for your answer! Well, I am not sure if I got what you meant on the first paragraph, but I have no intention on using arduino to change the position of the switch; I may want to make arduino knows if the switch is on or off, but I dont want to use arduino to change its state! \$\endgroup\$ – Roger Peixoto Jul 15 '19 at 12:07
  • \$\begingroup\$ Added a picture to help visualize. This is bulky and unsightly but it should work well once you adjust the positioning to get it to trigger. If you're going to add a project box above or below the light switch to house your IoT equipment, you could also drill a small hole side-to-side through the light switch and then use a wire linkage running down into your project box to hide all the electronics from view. The up/down light switch motion would be coupled to the same type of limit switch in your project box to read its linear position (up vs down). \$\endgroup\$ – Kent Altobelli Jul 15 '19 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.