And what about the 3 connections of the relay: NC (normaly closed), COM and NO (normaly open~)
How do i connect it to a simple light bulb (two wires) ?
I have this light cord.
Disassemble the switch on the cord. One of the wires is connected on the two terminals of the switch. Disconnect the wire from the switch and put one end of the wire to COM and the other end to NO.
It seems that the relay is energized (switched on) when IN0 in the picture sinks current, i.e., when the output pin is low.
Everything worked as expected
Now i changed the code a little bit and made the light turn on/off by an IR remote controll
GOAL: Now i want to mod my wall switch to have both wall switch and arduino to controll the room light I want it that if someone turns the light with switch the light will work normally, but also arduino can controll it (and know the state of the light even if it was changed by the wall switch) How can i achieve this ?
You could use the existing wall switch and wire it to an input on your Arduino. Like a really big pushbutton or such. Adjust your code accordingly.
If you want a sort of failsafe switch then you would need to replace the existing switch with a three way switch. If you do a search on how to wire three way switches you'll get plenty of diagrams demonstrating such a configuration.
As to determining if the light is on or off you would need a seperate sensor -- either a current sensor or a light sensor.
Can you give me some examples of a 3 way wall switch ? I can only find some weird ones
Also, about the light sensor i dont think i can use it because this is for a room and in the morning and afternoon there will be sun light all over the room... how can the sensor help in this way?
About the current sensor, can you show me some examples too.
See pic for an example of three-way switch wiring. I'm assuming you're in the US; I'm not familiar with the wiring in other countries.
On the three-way switch you buy in the store you'll have three screw terminals (well, four with the ground) and one of those terminals will be black or a different color -- that's the common. Connect the hot wire in your wall to that terminal. The other two terminals on the switch connect to NC and NO on your relay, and then the hot/black wire to your light connects to the COM position. The white wire from your light connects back to the white wire in the wall.
You need to think it through about how the current path changes as the switch and relay are actuated. It'll make sense after a while.
With respect to the sensor, you're looking for something like this. The hot wire to your light would be stuck through the hole in the center. You can find examples of their use if you do a web search for "non-invasive current sensor".
[Edit: removed by author]
[Edit2: corrected wiring instructions]
You can also monitor the voltage instead of the current. There are several ways you can do it. You can put a voltage divider+rectifier between the wires and use it to drive an opto isolator, and pass the output of the opto to the Arduino. But the easiest way to monitor the voltage is to use an AC-relay (a relay which coil uses 110V or 220V AC instead of +5V DC like the relays you already have use). Put the relay's coil across the lamp and put the Arduino's +5V to COM and an input pin to NO.