I've seen endless tutorials on how to control a relay using Arduino but I want the other way around.
I want to connect a 220v relay to a light switch in my house so when I turn on the light, the relay will send a signal to a pin as an input.
The only way I found is soldering a normal 220v relay to the light switch from one side and to the arduino's 5v pin and a digital pin from the other side.
That way when the light switch is pressed, the relay will close the circuit between the 5v pin and the digital pin and I will receive HIGH at the digital pin.
My question is: is there any better solution? Maybe I could do it without even using a relay?
You can use an optoisolator circuit. They can be obtained off-the-shelf. But usually such questions bring into play the question of whether the person who is asking knows enough to safely avoid electrocuting themselves or someone else.
You can use a relay, or an opto idolator. I rewired my switch to just switch 5V instead of 230V and let the Arduino switch the 230V again. Another way would be to use a USB charger plugged in/connected to the switched 230V so it turns on with the switch and use the 5V from that as a HIGH.
septillion:
Another way would be to use a USB charger plugged in/connected to the switched 230V so it turns on with the switch and use the 5V from that as a HIGH.
Excellent idea!
But shouldn't the charger have an output of 40mA? since it will be connected to the Arduino's digital pin
Why? A digital pin can source/sink 40mA as a output. But as a input you need less then 1mA. And if there is more current available is never a problem. It's only a problem if you have to less current available. The input (or any other damn thing) will only use what it uses. No matter if you connect it to 1A or a 1000A power supply as long as the devices takes less then that.
If the Arduino is off, and you turn the lights on, the Arduino is phantom powered through the input pin.
That could release the magic smoke, and send your Arduino to silicon heaven.
If you want to go that way, add a resistor between charger+ and pin.
Anything between 10k and 100k will do.
Leo..
The question here is - what does the Arduino need to do if the light is not turned on?
If it does not need to do anything, then simply power it from the USB charger and when switched on, it can (after a couple of seconds for the bootloader) - do what it is supposed to.
Yeay, 100k or so would make it complete fool proof. But phantom powering the Arduino does not kill the Arduino in most cases but I agree a simple resistor is a good practice.
Just powering the Arduino from the switch is indeed a simple solution as long as you don't want to do anything will the light is off or turned off. Somethings like a fade out or something isn't possible. The bootloader delay is like nothing as long as you run the Optiboot bootloader (Uno Bootloader) which skips (most of) the bootloader on power on.
The Arduino does nothing when the light is off but since my application sends me an email when the light is switched on, it requires an ethernet shield, and the ethernet shield needs around 3 seconds after power up to start sending data over the internet, so in your case, I won't get an email. I've tried it.
The Arduino is powered by a usb cable always plugged in the electricity so the Arduino will never be phantom powered even if the electricity goes off (yes I live in a third world country and it goes off a lot).
Then you can power the Arduino after the switch. You say you don't receive the email then but that's just programming. It's probably because you try to send the email before there is a connection. But if you program the Arduino to wait until there is a connection and then sent the email you're fine! That's in my opinion the most elegant solution.
Turn light on => turns on the Arduino => Wait untill the Arduino is connected => send the email.
This way you only need 1 power source, no extra components and you don't wast time and energy doing nothing
Your solution is very efficient in case I'm only working with that light switch but I'll be adding more sensors and inputs later, for instance if a door is opened etc..
So I'll need an independent power source for the Arduino.
Thank you anyway, I will try delaying sending the email to see if it works and I might be using that idea for other projects
If you want to add more, yeay, then you have to keep the Arduino alive (or in sleep).
Probably there is a function somewhere (no experience with Arduino + ethernet) to check if the connection is ready so you can use that (instead of a delay timer or something).