Arduino get data from Wall light switch

as a rule, we do not offer help to those wanting to get involved with mains power. the help and advise can results in injury, death or lawsuits.

that being said, if your lighting circuit is connected to a wall receptacle or any sort of plug in. the lights in my bathroom have a place to plug in a shaver or tooth brush. it is turned on and off with the light switch so I do not have to touch or alter any mains wires.

one can plug in a cell phone charger to the receptacle. when the lights are off, there is no power and the phone charger has no output. when the lights are on, the phone charger has power and a 5 volt signal is present. Bob's your Uncle !

In doing this, you only plug in a device that is already made to be plugged in to the receptacle and you never have to touch the mains wiring.

in my old house there was a light switch for the attic light and in the attic, there was a light with receptacle, see other options on the above link.

Also, as a logic exercise, regardless of the switch position, and regardless of power being present, there is no guarantee that with power present, that there is light.

the LDR is a very cheap sensor, is all low voltage, and is isolated from the mains power so there is no risk of compromising the wiring of the house.

One thing that home-owners and hobbyist never consider is that at some future time, an actual licensed electrician may be called to do work, see some problem, created by the homeowner, and try to return it back to the way it 'should be' only to put mains voltage on wires that are connected to some unknown device that catches fire and burns the house down.

as a rule of thumb, if you have to ask about touching mains wiring, we suggest you talk to a local, licensed electrician. for the money they will charge, you will be assured it is done properly and to local codes.

dave-in-nj:
as a rule, we do not offer help to those wanting to get involved with mains power. the help and advise can results in injury, death or lawsuits.

that being said, if your lighting circuit is connected to a wall receptacle or any sort of plug in. the lights in my bathroom have a place to plug in a shaver or tooth brush. it is turned on and off with the light switch so I do not have to touch or alter any mains wires.

Amazon.com

one can plug in a cell phone charger to the receptacle. when the lights are off, there is no power and the phone charger has no output. when the lights are on, the phone charger has power and a 5 volt signal is present. Bob's your Uncle !

In doing this, you only plug in a device that is already made to be plugged in to the receptacle and you never have to touch the mains wiring.

in my old house there was a light switch for the attic light and in the attic, there was a light with receptacle, see other options on the above link.

Also, as a logic exercise, regardless of the switch position, and regardless of power being present, there is no guarantee that with power present, that there is light.

the LDR is a very cheap sensor, is all low voltage, and is isolated from the mains power so there is no risk of compromising the wiring of the house.

One thing that home-owners and hobbyist never consider is that at some future time, an actual licensed electrician may be called to do work, see some problem, created by the homeowner, and try to return it back to the way it 'should be' only to put mains voltage on wires that are connected to some unknown device that catches fire and burns the house down.

as a rule of thumb, if you have to ask about touching mains wiring, we suggest you talk to a local, licensed electrician. for the money they will charge, you will be assured it is done properly and to local codes.

thank you for replying.
I'm pretty sure you haven't understood what I'm trying to do.
Iam not an electrician but I know some things.
What I am trying to do need the cables that are in the wall switch. I CAN NOT finish the project with out them.
If I call an electrician first of all he is not going to do what I need him to do. Also I'm gonna miss the magic of creating something by my own. I like electronics and I'm currently studying it.

Smackflad:
What I'm trying to do :
Turn on and off the lights from Wall switch (like normal rooms)
Turn on and off the lights from arduino relay

Side things :
If I turn on the lights from the wall switch I need arduino to know that I've done it.

Temporary things :
I use arduino uno.
Serial monitor + coding turn on a relay.
I have a temporary wall switch that I use to try and find the solution.

I hope that made things a little bit easier for you.
Thanks for your time.

Draw out your wring.
google ladder logic. power on the left, neutral on the right.
you have a switch connected to the left, and a light connected to the right.
there would be a wire from the switch to the light.
you have a relay COMM connection connected separately to the left.
and the relay N/O or Normally Open connection connected to the light.
the light is connected to the neutral with no change.
all wire connected to the left is black, all wires connected to the right is white all wires in the middle could be red, or black, not white.
the light will have power if either of the devices is closed. the power would not be present if all of the devices are open.
problem : the Arduino cannot turn the light off if the manual switch is provding power.
senerio #2 :
you instlal a new switch, low voltage, connected only to the Arduino
you tell the Arduino to turn on the lights and the Arduino is the only device that can turn it on.
problem is that with a device failure, you have no lights.
senerio #3 :
the light switch is a 3-way switch and a second 3-way switch is replaced by the relay. the first switch will be in either an up or a down postion, depending on the state of the relay.
the light can be turned on or off manaully at any time.
the light can be turned on or off by the Arduino at any time.
problem : electrician is required to re-wire the lighting circuit.
one great thing is that now the manufacturers are making a wall box that has both mains voltage and also low power in the same box. this way, you have everything where your electrician can see it and there are no surprises.

dave-in-nj:
Draw out your wring.
google ladder logic. power on the left, neutral on the right.
you have a switch connected to the left, and a light connected to the right.
there would be a wire from the switch to the light.
you have a relay COMM connection connected separately to the left.
and the relay N/O or Normally Open connection connected to the light.
the light is connected to the neutral with no change.
all wire connected to the left is black, all wires connected to the right is white all wires in the middle could be red, or black, not white.
the light will have power if either of the devices is closed. the power would not be present if all of the devices are open.
problem : the Arduino cannot turn the light off if the manual switch is provding power.
senerio #2 :
you instlal a new switch, low voltage, connected only to the Arduino
you tell the Arduino to turn on the lights and the Arduino is the only device that can turn it on.
problem is that with a device failure, you have no lights.
senerio #3 :
the light switch is a 3-way switch and a second 3-way switch is replaced by the relay. the first switch will be in either an up or a down postion, depending on the state of the relay.
the light can be turned on or off manaully at any time.
the light can be turned on or off by the Arduino at any time.
problem : electrician is required to re-wire the lighting circuit.
one great thing is that now the manufacturers are making a wall box that has both mains voltage and also low power in the same box. this way, you have everything where your electrician can see it and there are no surprises.

I have tried to do the third and failed because when I turn off the switch the relay won't turn the light on. But the opposite works.
I have also tried to do the second and make the switch to send signal to the arduino so it can control the relay but when the switch is off it sends random 1s and 0. When the switch is on is correct and send only 0. If I fix this the project will be solved.

Smackflad:
I have tried to do the third and failed because when I turn off the switch the relay won't turn the light on. But the opposite works.
I have also tried to do the second and make the switch to send signal to the arduino so it can control the relay but when the switch is off it sends random 1s and 0. When the switch is on is correct and send only 0. If I fix this the project will be solved.

Sounds like a floating pin. Enable the internal pull-up (set pinMode to INPUT_PULLUP) or add an external pull-up resistor.

Smackflad:
thank you for replying.
I'm pretty sure you haven't understood what I'm trying to do.
Iam not an electrician but I know some things.
What I am trying to do need the cables that are in the wall switch. I CAN NOT finish the project with out them.
If I call an electrician first of all he is not going to do what I need him to do. Also I'm gonna miss the magic of creating something by my own. I like electronics and I'm currently studying it.

IMHO, you are missing the point.
mains wiring, mains power, is NOT electronics.
if you want to study to be an electrician, great.
if you want to study electronics, then early on, you need to understand the difference.
in your study of electronics, you use sensors. detect gas, detect moinsture, temperature, movement.... those are your tools.
your question as far as electronics goes, is
'how to best detect a thing'
we, as a group will often try to make you look and make sure that you are trying to sense the correct 'thing'
in your case, it is either that there is a light that is working, or there is power.
they are both separate and also dependant on each other.
you can sense electricity by measuring voltage, or current, and even by the flux field generated by electricity in the wire. there are many devices that are able to sense the present of electricity without touching the wires.
THAT is the electronics, that is what we do.
you can use a CT, or current transformer to detect the current flowing in a wire. with the circuit at the switch completed, but the lamp broken, there is no current flow. if the lamp is present and working, there would be current. a CT circuit does not touch the wires.
you can connect a phone charger so that regardless if the lamp is present, if there is power, you know there is power (but not that there is light)
simply put, we deal with electronics and touching mains voltage to electronics will destroy the electronics.
you MUST, you are REQUIRED, it HAS TO BE, that there is some device, sensing the mains power, that is NOT mains power, but outputs a signal so something results in a low voltage signal that the electronics can sense.
if you want to sense that there is mains power, you need some device. the plug in phone charger is the simplest to detect voltage .
if you want to detect light, the LDR is the simplest
your study of electronics will improve when you think of the sensor and what it really sees, and how limited it is to the one thing it senses.

Smackflad:
I have tried to do the third and failed because when I turn off the switch the relay won't turn the light on. But the opposite works.
I have also tried to do the second and make the switch to send signal to the arduino so it can control the relay but when the switch is off it sends random 1s and 0. When the switch is on is correct and send only 0. If I fix this the project will be solved.

Now try it OUR way.
create a sketch. show the wiring. black is hot, RED is switched and white is neutral.
no amount of words will help as fast as one sketch.
in your case, you need to show us how you did it both ways.
we all do a sketch, paper and pencil or on the computer, when we run into something that we need to figure out.
is sounds like a simple wiring issue.
google LADDER LOGIC
it is how electricians look at the work.

the sketch shows a receptacle in parallel to the light. this way, you can plug in your phone charger to sense the 5v when power is on.

Smackflad it is time to stop what you are doing before you get hurt.
You do not seem to understand enough about the Arduinos, Electronics or about High Voltage.
I understand that you have some knowledge but i believe this is enough to get you hurt.

There are better ways to approach this!

First i think you should look at some of the tutorials as a lot of what you want to do is covered in them.

I have worked in the electronic industry for over 30 years and have been around 240 volts for most of that so please listen.

You can set up the same circuit that you have in your house, switch, wires and globe using low voltage (under 50 volts) and then set up the Arduino to play around and get what you want but i think that trying to learn about Arduinos, Electronics and High Voltage is just asking to much.

Start off small and work your way up.
Learn about transformers, voltage dividers, AC and DC Power and a whole lot more.

I think you are having trouble making your self understood and understanding and this will lead to mistakes you cant afford to make.

You could be hurt, killed or burn the house down.

Make a low voltage model first and experiment.

That is all I can offer you.
Good luck to you.

Daz.

Daz1712:
Smackflad it is time to stop what you are doing before you get hurt.
You do not seem to understand enough about the Arduinos, Electronics or about High Voltage.
I understand that you have some knowledge but i believe this is enough to get you hurt.

There are better ways to approach this!

First i think you should look at some of the tutorials as a lot of what you want to do is covered in them.

I have worked in the electronic industry for over 30 years and have been around 240 volts for most of that so please listen.

You can set up the same circuit that you have in your house, switch, wires and globe using low voltage (under 50 volts) and then set up the Arduino to play around and get what you want but i think that trying to learn about Arduinos, Electronics and High Voltage is just asking to much.

Start off small and work your way up.
Learn about transformers, voltage dividers, AC and DC Power and a whole lot more.

I think you are having trouble making your self understood and understanding and this will lead to mistakes you cant afford to make.

You could be hurt, killed or burn the house down.

Make a low voltage model first and experiment.

That is all I can offer you.
Good luck to you.

Daz.

Daz thank you for your advice and concern.
I know that I might die because of this but I'm not trying to do it by myself. I have 2 stages. Make it with an small led and then transfer it to the lamps. The second stage I'm doing it with help NOT by my self. The other ways aren't working for me. It is not the first time that I open a high voltage circuit.

When discussing circuits we always ask the person to send or post a sketch of what they're trying to do this work is so simple your sketch should be as simple as one I posted earlier send us a sketch let us figure it out there's no reason why it shouldn't be working with an LED

this is what i have done right now.
i haven't connected it to any voltage yet.
i need to control the relay from the switch (with code).
only problem is that with this setup the switch when is pressed sends random 0 and 1. when it is not pressed sends only 0.

When it comes to mains supply, these should be nipped in the bud before being allowed to go this far.
Clearly the op has no idea.

Sounds like a dog chasing its tail, trying to align switch reading while futzing around a 3 way.

Seems like sensing current going to the light would be pretty easy to hide.

Also I'm guessing you're using one of those jank hobby relays that constantly draw power the whole time the light is on. What you want for a safer, better setup, is a latching power relay.

And instead of having both the switch and the relay share light powering duties, let the relay do everything. Turn the switch into a switch on the Arduino and nothing more. You will always know what state the light is on without having to sense a darn thing. That's how I did it and it works fantabulously.

INTP:
And instead of having both the switch and the relay share light powering duties, let the relay do everything. Turn the switch into a switch on the Arduino and nothing more.

This is exactly what I'm trying to do but it don't work correctly...
I am trying to control the relay from the switch but the output of the switch gives random 1 and 0 when it's off and 0s when it's on... Is there a solution for this?....

Smackflad:
I am trying to control the relay from the switch but the output of the switch gives random 1 and 0 when it's off and 0s when it's on... Is there a solution for this?....

See #24.