Controlling a light with a switch and an arduino

Thread Starter

Henri Jools

Joined Oct 13, 2022
2
I have a project where I need to make a person counter for a room. I have two PIR sensors on either side of the door, depending on which one gets set off first, it means that a person is entering or leaving. The code for this part works well enough that the counter is incremented when someone enters and decreases when someone leaves.
The idea is that when someone enters the room they manually turn the light on with the switch. After this person leaves the counter goes to 0, and the light should switch off automatically if they forget to. But there is also the possibility that they do turn the lights off when they leave, and this should not break the system either.
Making a two way light switch is easy enough. I followed a video and was able to turn off an LED with both switches.
The problem comes from wanting to control it with the signal coming from the physical switch and the Arduino. I can not find a way to connect both the output of the arduino and the output of the switch to the one light.
I've tried looking at solution all day, there were some kind of similar posts here that talked about using diodes. I've never used them, don't have any on hand and the circuits are different.

I made a circuit with a spdt switch, only connected with the first 2 pins, an LED. This way when I turn on the switch the LED would turn on. When everyone leaves the room the counter hits 0, I could then have a normally closed relay turn off, and then the LED would switch off. But then what if someone enters the room and switches the light on, nothing would happen. I do not know of a way to couple the physical switch to the arduino. The XOR logic works well only if people forget to switch off the lights, otherwise without a way of knowing if the lights are on or off, it would break. I made a LED switch cricuit with a pulldown resistor to know when it was on or off, but I don't know how to make the XOR logic gate or how it would work with 2 different inputs.

I could maybe just connect the physical switch to the arduino, that way the software controls it and knows when it is on or off. Control the actual light with only the arduino and a relay.
The project would of been a lot easier if it did not need to also have the physical switch.

If someone could help me find a component or a way to couple an arduino output with a switch to an LED, or tell me it's too hard or too expensive.
 

Ya’akov

Joined Jan 27, 2019
9,138
Welcome to AAC.

You've hit on the correct solution, all the switching should be done by the MCU.

Consider the switch as another sensor, that should simplify the thinking. Also, since it is just another sensor, all it needs to sense is that the person wants to change the light's state. If the switch was switching the light, it would need to retain the state, but it isn't doing that. The state information is stored by the MCU. (this is a hint about the type of switch to use)

If there is no requirement that switch operate independently of the MCU, it is an unnecessary complication to make it do that. Just use the MCU to do all of the work and let the sensors tell you about the world.

Practical applications of MCUs are all about sensors and actuators. To sense the world and to make some changes to it. Make that your baseline thinking and things might seem simpler.
 

Thread Starter

Henri Jools

Joined Oct 13, 2022
2
The original plan was to have the switch operate independently of the MCU but I think we will have to scrap that, as the other solution is much easier. This way should be easy to implement with an LED, and I can just make a schematic for mains where the light switch goes to the Arduino and the Arduino is connected to a relay for the lights.

Thanks for the reply!
 

Ya’akov

Joined Jan 27, 2019
9,138
The original plan was to have the switch operate independently of the MCU but I think we will have to scrap that, as the other solution is much easier. This way should be easy to implement with an LED, and I can just make a schematic for mains where the light switch goes to the Arduino and the Arduino is connected to a relay for the lights.

Thanks for the reply!
If you want the switch to be able to operate the lights independently you can use a latching really that either the switch or the MCU can operate.
 
Top