Switch State ideas please help

Hi guys,

basically, I was wondering if there was any safe ways for my arduino to read the state of a mains switch.

Extra karma for cheapness, reversibility and simplificity.

Thank you for your time

You need extra HW, for an example "zero crossing detector" like this:
http://www.dextrel.net/diyzerocrosser.htm.
Pulses on input mean switch is on, without pulses - switch is off.
I'm using this circuit without any problem. It takes current as glow tube, under 1mA. In my case about 400uA.

You need extra HW, for an example “zero crossing detector” like this:
http://www.dextrel.net/diyzerocrosser.htm.
Pulses on input mean switch is on, without pulses - switch is off.
I’m using this circuit without any problem. It takes current as glow tube, under 1mA. In my case about 400uA.

Thank you for your reply, however I am a little confused as to what you mean.

basically, the goal is to control my house lighting with a bunch odf dual altching relays that are pulsed into either state. These will be connected in a two way switch within each wall cavity.

The downside to this is of course is that I have no way of telling what state the switch is in and therefore have no idea whether or not I need to set or reset the dual altching relay.

Can I use a current sensor module. The readings will be over a distance, so wont be exact, but surely, if I put it in series with the hot after the second switch in the circuit, it will always be able to tell if there is current going through the circuit?

Or will a cheap chinese hall effect sensor module, just completely malfunction at this distance.

5-15 metres of cat6 or phone cable.

Thank you for your time.

Your first post described the problem little bit poorly. Did you read the link I posted? There is described way how to sense the mains voltage. Pulses on output mean presence of mains. You need just to read pulses with the Arduino, which is very simple.
More simple way to detect switch position would be to use a switch with additional contact.
The hall sensor can sense a current but the current have to flow via circuit. It is different. I hope you mean something like this: http://embedded-lab.com/blog/a-brief-overview-of-allegro-acs712-current-sensor-part-1/ This sensor produces DC voltage on output which is weak to resist from interference, 5m shouldn’t be a problem but 15m hard to say. More yes than no. Better would be to put it at the place somehow. The parts are small enough nowadays.
The sensor from my first post have to have better resistible output because it produces pulses. I dare say the length of cable could be tens of meter. Clear disadvantage seems to me that there is no fabricated device on the market, as I know. You probably need several of them.

Simple and cheap, USB power supplies.....

But uhm, I don't see the problem... Isn't it YOUR software that latches/resets the relay? So then it KNOWS in which state it is...

Myself I would not bother using laching / bi-stable relays. Way to expensive....

Use polarized latching relays? Then you know what state they are in.

Or have a single current monitor on the entire lighting circuit, and if your controller resets or ends up in an
unknown state, have it pulse each relay looking for change in current to see which state it is in.
This will also identify any defective lights which pull no current.

With battery-backup your controller would never normally forget anything - if the battery runs low
save the state to EEPROM.

Your first post described the problem little bit poorly. Did you read the link I posted? There is described way how to sense the mains voltage. Pulses on output mean presence of mains. You need just to read pulses with the Arduino, which is very simple.
More simple way to detect switch position would be to use a switch with additional contact.
The hall sensor can sense a current but the current have to flow via circuit. It is different. I hope you mean something like this: A brief overview of Allegro ACS712 current sensor (Part 1) | Embedded Lab This sensor produces DC voltage on output which is weak to resist from interference, 5m shouldn't be a problem but 15m hard to say. More yes than no. Better would be to put it at the place somehow. The parts are small enough nowadays.
The sensor from my first post have to have better resistible output because it produces pulses. I dare say the length of cable could be tens of meter. Clear disadvantage seems to me that there is no fabricated device on the market, as I know. You probably need several of them.

Yes that is the exact current sensor I have alot of. I can either read the signals and send the reset and set pulses for dual latch relays over long cat 6 cable or, I can use that cat 6 and enc28j60 and arduino nanos, to create nano hubs in every wall cavity which are connected to the home network and can be controlled via http gets and posts.

Simple and cheap, USB power supplies.....

But uhm, I don't see the problem... Isn't it YOUR software that latches/resets the relay? So then it KNOWS in which state it is...

Myself I would not bother using laching / bi-stable relays. Way to expensive....

I found some 250VAC 16A dual latch relays. They require 5v short pulse to switch. I have successfully been able to latch the relay in the NC or NO position and it keeps its state when there is not power supplied. I got them from mouser for 6 dollars each omron dual latch (9 pins). Delivered in 4 days (free postage if over 60 dollar)

Use polarized latching relays? Then you know what state they are in.

Or have a single current monitor on the entire lighting circuit, and if your controller resets or ends up in an
unknown state, have it pulse each relay looking for change in current to see which state it is in.
This will also identify any defective lights which pull no current.

With battery-backup your controller would never normally forget anything - if the battery runs low
save the state to EEPROM.

Never heard of polarized relays but will look into it. Not sure if the one I am using is polarized. It is an Omron G5RL-k1-e-Dc5, and not to sure on how to save eto EEPROM. But are you suggesting it might be a good idea to have a battery backup for my system in case there is a power outage? My thoughts are that if the power is off, the lights wont work anyways.

Thank you for your time everyone

Mikie_lomas:
I found some 250VAC 16A dual latch relays. They require 5v short pulse to switch. I have successfully been able to latch the relay in the NC or NO position and it keeps its state when there is not power supplied. I got them from mouser for 6 dollars each omron dual latch (9 pins). Delivered in 4 days (free postage if over 60 dollar)

6 dollars, not exactly cheap... But why do you want latching relays? What's the point?

And you mist the most important bit. After latching or resetting the relay, which YOUR program does, how come you (/your program) can't remember doing so?

Mikie_lomas:
Never heard of polarized relays but will look into it. Not sure if the one I am using is polarized.

Did you even open the datasheet? They are clearly polarized....

Mikie_lomas:
But are you suggesting it might be a good idea to have a battery backup for my system in case there is a power outage?

If your problem is you don't know the state of the relays after a power outage then why not simply set/reset them to a known state?

Did you even open the datasheet? They are clearly polarized....

I opened the datasheet, but I am new to this and didnt even know it was a thing.

If your problem is you don't know the state of the relays after a power outage then why not simply set/reset them to a known state?

6 dollars, not exactly cheap... But why do you want latching relays? What's the point?

Because , I will have a normal light switch and the dual altch relay. So basically I can turn the switch from the wall or from the arduino. As it is in a two way switch setup, the state of the relay and manual switch will change. Reset or set, up or down, may not always mean the same thing. If for example the light is turned on from arudino and off from the wall switch. It would appear as if the wall switch is set to on.

And you mist the most important bit. After latching or resetting the relay, which YOUR program does, how come you (/your program) can't remember doing so?

My program could remember doing so, I could create an object of some sort and keep the values in an sql light database...But i still have the trouble of losing the state of the switch, if the light is toggled from the manual light switch rather than the arduino relay.

I hope i expplained myself clearly. Sorry about the confusion.

Simplificity, I kind of like that word. If I were approaching a project like that, I would think about x10 modules, and control those. Something to think about.

That still doesn't tell me why use latching relays.... That can be done with normal relays as well.

Also, why make it so complicated? Instead of dual switches, just connect that switch to the Arduino.

And NO, this way you suggest now is NOT fail safe! Yes, if you screw up and freeze the Arduino you can still control the lights. But you are as likely to screw up in a way that the relay is turn on/off all the time or always counteracts your switch action.

The only advantage is you can control the light if you unpower the Arduino. But if it's worth all the fuzz... Simpler to add a Arduino bypass jumper in your hardware that just links the switch to the relay again in case of a screw up....

Simpler to add a Arduino bypass jumper in your hardware that just links the switch to the relay again in case of a screw up....

Thank you for your reply. Can you please elaborate on how this would be set up. Currently it baffles me.

Simplificity, I kind of like that word. If I were approaching a project like that, I would think about x10 modules, and control those. Something to think about.

I looked these up and they are a tiny bit beyond my price range.

That still doesn't tell me why use latching relays.... That can be done with normal relays as well.

The way I imagine it is:

Manual switch has two states, one goes to NC and one goes to NO. The middle line of the relay then leads to the light bulb.

The arduino will have a built in fuction to set or reset the dual altch relay. When this occurs the light will either be turned off or on, depending on the state of the manual switch. The relay is pulsed into either state. This has got to be more ennergy efficient than constantly setting a pin to high for as long as you want to keep the relay out of the natural state? Then on the slight chance that arduino fails, the relay is kept in the last state, the manual light switch works as normal. I mean obviously....on thoughts, even if the arduino failed, it would revert the normal relay to NC and then the light switch would work anyways.

Or are you saying that the relay has a chance to be stuck somewhere in between?

Thank you for your time

Mikie_lomas:
This has got to be more ennergy efficient than constantly setting a pin to high for as long as you want to keep the relay out of the natural state?

That's true. But now you need energy to detect the state of the light again...

Mikie_lomas:
Then on the slight chance that arduino fails, the relay is kept in the last state,

And THAT is where you make a thinking error :wink: A hardware failure (assumed you didn't connect it in a way that's out of spec) is very very rare. A programming error on the other hand... If I had to bet between hardware failure and a software bug, I know where I would put my money! Even if I wrote the code myself. Granted, the power supply of the Arduino is the weakest link but if you get a decent one for it it will last a long time.

And the problem with a software bus is, you don't know in which way it will fail. If might fail in a way it's unresponsive. When that's the case then yeah, with this setup you can still control the light. But it's just as likely you screw up in a different way. It might just "randomly" switch an output. Or it might even start counteracting your switching. Aka, it switches the light off the moment you try to switch the light on. Murphy tells us "If the Arduino CAN mess with the light, the Arduino WILL mess with the light." :smiley: And then this whole setup is useless. Then you need to turn off the Arduino anyway.

I would not spend all that money and time to wire it like that. I would say that's not worth it.

And if you want to be able to control the light in case of major screw up I would use a Pro Mini (as I do most of the time) and place it into a header. And in case of massive failure (aka, failure where a reset doesn't work) I would make a Pro Mini replacement PCB that just links the switches to the relays. Then it's just as simple as switching out the Pro Mini for that PCB and it's just a dumb light again.

And if you just switch lights I would grab a G3MB-202P (not the module, those s*ck). Just takes 1mA @ 5V to turn on, no sound, no mechanics and cheap.

In my house I have multiple Arduino's controlling lights. All switches there are just wired to an Arduino which in turn has full control over the lights (via triacs or relays). The one in the living room (turns off light and AV when no people present for 15 minutes) is working for the last 6 year, no major problems. Only problem I had in the beginning turned out a bug I wrote :smiley:

post #8
Because , I will have a normal light switch and the dual [latch] relay. So basically I can turn the switch from the wall or from the arduino. As it is in a two way switch setup, the state of the relay and manual switch will change. Reset or set, up or down, may not always mean the same thing. If for example the light is turned on from arudino and off from the wall switch. It would appear as if the wall switch is set to on.

You want to retain the existing (manually operated) light switches to control the lights, and additionally have the lights controlled by relays ? If I’ve understood that correctly, that means converting each single way light circuit to a 2 way light circuit. If any of the existing light circuits are already 2 way (that is 2 or more switches control the same light), then you may have to rewire the existing light circuit to accommodate intermediate (or crossover) switches. intermediate light switch wiring | Light wiring. It is of course possible, and in the worst case, a relay (2 pole changeover) can be configured as an intermediate switch.

That’s true. But now you need energy to detect the state of the light again…

I guess my main fear is sort of like an OCD need. As in if light is turned on manually and then turned off with arduino, the relay is in constant pin High, this could be days unless I made a conscious effort to set them back to their original state. This seems less smart house-ish. I am still not completely passed the hall current sensor module idea. I heard that they use minimal current when operating. I wonder if in the long run this would use less current than relays that must be energised to keep their state?

In my house I have multiple Arduino’s controlling lights. All switches there are just wired to an Arduino which in turn has full control over the lights (via triacs or relays). The one in the living room (turns off light and AV when no people present for 15 minutes) is working for the last 6 year, no major problems. Only problem I had in the beginning turned out a bug I wrote :smiley:

this is my dream eventually, but constantly coming up with obstacles. Such as electricians code and such haha. I still have a large respect for AC.

I have tested my circuit and my programming and it all works, I just need to wait for a bunch of opto-isolators I purchased from china to arrive, so that I can check to see if the circuit still works with the two sections isolated from one another.

Then if all sweet, i will look into designing it onto a PCB with eagle and then getting a chinese company to make like 100, for $1.80 , these will hopefully be measured correctly and my relays will sit flush. Current only 6 of the 9 pins fit onto normal spacings. The arduino inputs are 3.75mm wide and i can only fit it on grids diagonally. Then i will have enough mounts with all the places for all the diodes and resistors and optoisolators, to easily be soldered into place ( my idea before this was to get a whole bunch of prototype boards and solder myself, but the chinese made PCB idea sounds like a cleaner option. I can then do my mums house too :).

You want to retain the existing (manually operated) light switches to control the lights, and additionally have the lights controlled by relays ? If I’ve understood that correctly, that means converting each single way light circuit to a 2 way light circuit. If any of the existing light circuits are already 2 way (that is 2 or more switches control the same light), then you may have to rewire the existing light circuit to accommodate intermediate (or crossover) switches. intermediate light switch wiring | Light wiring. It is of course possible, and in the worst case, a relay (2 pole changeover) can be configured as an intermediate switch.

By 2 pole changeover do you mean like my dual latching relays? but yes, this is exactly what I am trying to do.

Thank you all for your time and patience with me.

But the first is no problem if you give the Arduino full control. Then all the switches are low voltage (aka, no problem with electrical code) and the relay is only on when the light is on. And if you use the SSR I linked it hardly uses current and is nice and small. No need for driving transistors, no sound, no mechanical etc :slight_smile:

Like I said, wiring all all switches in 2 way style (or even 3, 4 5, 6) is in my opinion not worth it. It just gives you a wrong feeling of fail save... And it will be very expensive...

And with AC I think you mean mains? Because respect for mains is a good thing :slight_smile: But the code is sometimes a bit over the top / weird. (Yes I'm looking at you nation with the stars and strips.) Here it's kind of a grey area. But I find it a bit bull and just did it in my own house. As an electrical engineer I'm confident my creation is perfectly save :slight_smile:

And as far as optos, they draw current as well. And if you drive them to lightly you have the change they will also react on phantom power on the lines (inductive / captive coupling).

And as far as PCB's, don't order 100 straight away.... You can get smaller batches from China for cheap as well. +-10 (most of the time I got 11) PCB's of 50x50mm at DirtyPCB is just $12,-. The alignment is not as sharp as more expensive services but the PCB quality itself is just fine. And 50x50mm is plenty for a node type PCB :slight_smile:

And it sound like you make nodes? How do they talk to each other? I like MySensors for that, easily coupled to something like Domoticz on a Raspberry Pi :slight_smile:

But the first is no problem if you give the Arduino full control. Then all the switches are low voltage (aka, no problem with electrical code) and the relay is only on.

A few people have mentioned this to me. How would I go about swapping a wall switche to arduino jumpers. I have done alot with arudino, but I have never actually attempted to use the arduino to check if a switch is closed. Can you provide information on how I would do this?

nd as far as optos, they draw current as well. And if you drive them to lightly you have the change they will also react on phantom power on the lines (inductive / captive coupling).

Are you saying that lights could turn on or off randomly when they are not supposed to?

And as far as PCB’s, don’t order 100 straight away… You can get smaller batches from China for cheap as well. ±10 (most of the time I got 11) PCB’s of 50x50mm at DirtyPCB is just $12,-. The alignment is not as sharp as more expensive services but the PCB quality itself is just fine. And 50x50mm is plenty for a node type PC

I am very interested in this. It would allow me to test that my proposed circuit works before ordering the bulk lot for the two houses. I do struggle with the requirements of gerber files and compiling them. i have never had a pcb made for me, only purchased completed modules online.

And it sound like you make nodes? How do they talk to each other? I like MySensors for that, easily coupled to something like Domoticz on a Raspberry P

Not sure about the nodes, but I have successfully managed to create a voice activated Windows Universal raspberry pi app which I can use to control an arduino mega via firmata. I have attempted to do the same with an enc28j60 and a bunch of nanos, except it has proven quite difficult to get the firmata and the windows universal plug in to work with enc28j60 and arduino. I have heard its possible however.

My current option is to either use an enc28j60 or an ESP8266. Then query sensor information or trigger arduino functions via POST and GET web calls to the nanos.

This has been quite difficult so far with the enc28j60’s, but I have a feeling this is simply because the technology has evolved and everyone has just been working with the ESP8266’s. Either way, I should receive my one in the mail soon and then I can set up a listening server on each arduino.

From then itll be simpler a matter of tying web calls fromthe raspberry pi to voice commands (which should be much easier than dealing with the multiple versions of firmata.

What can you tell me abouit My Sensors and domoticz?

Thank you so much for your time

Mikie_lomas:
A few people have mentioned this to me. How would I go about swapping a wall switche to arduino jumpers. I have done alot with arudino, but I have never actually attempted to use the arduino to check if a switch is closed. Can you provide information on how I would do this?

Just like any other switch... A wall switch is no different then any other normal switch. Just disconnect it from mains and connect it to the arduino between GND and a pin. Depending on wire length (you should try to keep that to a minimum anyway) I would add a stronger external pull up resistor then the build in one to make it more robust.

Mikie_lomas:
Are you saying that lights could turn on or off randomly when they are not supposed to?

Not really how to read that in relation with that quote but:

A opto coupler that doesn't need much current to turn on. And by inductive and/or capacitive coupling the wire might induce enough current to turn on the opto. A load (like a light) might suppress it enough.

And in general, of course lights might turn on randomly. You writing a bug like that is as likely as a bug where nothing turns on. Like I said, it's Murphy's law at work. Translated to this situation you have ""If the Arduino CAN mess with the light, the Arduino WILL mess with the light."

That's why this 2-way wiring can give you a false sens of fail safe. Yes, if the Arduino stops working you can still control the light. But it's not very likely the hardware just stops working. 99,9% chance it's due to a bug you wrote. And that bug might as well turn on / off the light at moments you don't want it to. And nothing you can do about it with that 2-way switch construction...

So I would say, not worth it. Just connect the switches to a Arduino and the Arduino to a relay and debug your program properly before installing it in a whole house. :slight_smile:

Mikie_lomas:
I am very interested in this. It would allow me to test that my proposed circuit works before ordering the bulk lot for the two houses. I do struggle with the requirements of gerber files and compiling them. i have never had a pcb made for me, only purchased completed modules online.

What do you sue to make them? A PCB with mains is not the best start... Keep things like clearance and creepage in mind.

Mikie_lomas:
I have attempted to do the same with an enc28j60 and a bunch of nanos
[...]
My current option is to either use an enc28j60 or an ESP8266. Then query sensor information or trigger arduino functions via POST and GET web calls to the nanos.

I would say, drop that idea. The power you might save by not energizing the relay coil all the time you lose in multiple with Wifi or ethernet in general. Wifi and ethernet both are not energy efficient at all. Then I would switch to RS485-ish or a NRF24L01 radio.

Mikie_lomas:
What can you tell me abouit My Sensors and domoticz?

MySensors uses NRF24l01 radios which have decent energy efficiency. Even supports message validation.

And instead of building a whole framwork to control everything you can use Domotics (or OpenHab or or or...). They are flexable systems which you can run on a Pi and link different systems to it. Might be MySensors but also Z-Wave and Philips Hue for example are supported. Gives you a nice interface to control and set up everything :slight_smile:

OpenHab is more abstract and more flexible (really a software project) but is more code based. Domotics has a nice GUI for everything so is a bit more "normal user" friendly. :slight_smile:

What do you sue to make them? A PCB with mains is not the best start... Keep things like clearance and creepage in mind.

Is a PCB with mains not possible? Even with proper design?

I would say, drop that idea. The power you might save by not energizing the relay coil all the time you lose in multiple with Wifi or ethernet in general. Wifi and ethernet both are not energy efficient at all. Then I would switch to RS485-ish or a NRF24L01 radio.

MySensors uses NRF24l01 radios which have decent energy efficiency. Even supports message validation.

And instead of building a whole framwork to control everything you can use Domotics (or OpenHab or or or...). They are flexable systems which you can run on a Pi and link different systems to it. Might be MySensors but also Z-Wave and Philips Hue for example are supported. Gives you a nice interface to control and set up everything :slight_smile:

OpenHab is more abstract and more flexible (really a software project) but is more code based. Domotics has a nice GUI for everything so is a bit more "normal user" friendly. :slight_smile:

Thank you for these tips, I will look into these radio implementations for sure.

As always thank you for your time :slight_smile:

Is a PCB with mains not possible? Even with proper design?

Of course, it is possible but with proper design. On the other side, I have to agree with septillion, it is not the best start if you have no experience.