Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. Controlling existing relays

Controlling existing relays

Scheduled Pinned Locked Moved Hardware
54 Posts 9 Posters 30.9k Views 6 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T TimO

    I suppose the relays are impulse relays, which need a impulse to change the state (on/off). I'm using the Relay-Modules you mentioned above to control my impulse relays. My impulse relays are controlled with 230V. The relays are connected in parallel to the existing switches and use a 100ms impulse to change the state.
    Works like a charme.

    T Offline
    T Offline
    twosh
    wrote on last edited by
    #26

    @TimO said:

    I suppose the relays are impulse relays, which need a impulse to change the state (on/off). I'm using the Relay-Modules you mentioned above to control my impulse relays. My impulse relays are controlled with 230V. The relays are connected in parallel to the existing switches and use a 100ms impulse to change the state.
    Works like a charme.

    Could you share your experience in more detail? It feels like you have accomplished what I would like to happen at this point. :)

    1 Reply Last reply
    0
    • T twosh

      Here's my sketch @jeylites !

      I'm using this 8 channel relay board: http://www.ebay.com/itm/181242936438?rmvSB=true

      Had to switch the GPIO HIGH/LOW values around compared to the original sketch, and added two analogue pins controlling channel 7 and 8 on the board.

      // Example sketch showing how to control physical relays. 
      // This example will remember relay state even after power failure.
      
      #include <MySensor.h>
      #include <SPI.h>
      
      #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
      #define RELAY_1A  0  // Arduino Analog I/O pin number for first relay (second on pin+1 etc)
      #define NUMBER_OF_RELAYS 6 // Total number of attached relays
      #define NUMBER_OF_ANALOG_RELAYS 2 // Total number of attached relays
      #define RELAY_ON 0  // GPIO value to write to turn on attached relay
      #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
      
      MySensor gw;
      
      void setup()  
      {   
        // Initialize library and add callback for incoming messages
        gw.begin(incomingMessage, AUTO, true);
        // Send the sketch version information to the gateway and Controller
        gw.sendSketchInfo("Relay", "1.0 AD");
      
        // Fetch relay status
        for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
          // Register all sensors to gw (they will be created as child devices)
          gw.present(sensor, S_LIGHT);
          // Then set relay pins in output mode
          pinMode(pin, OUTPUT);   
          // Set relay to last known state (using eeprom storage) 
          digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        }
      
        // Fetch and present the analogue relays  
        int sensor=NUMBER_OF_RELAYS+1;
        gw.present(sensor++, S_LIGHT);
        pinMode(A0, OUTPUT);
        digitalWrite(A0, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        gw.present(sensor++, S_LIGHT);
        pinMode(A1, OUTPUT);
        digitalWrite(A1, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
        
      }
      
      
      void loop() 
      {
        // Alway process incoming messages whenever possible
        gw.process();
      }
      
      void incomingMessage(const MyMessage &message) {
        // We only expect one type of message from controller. But we better check anyway.
        if (message.type==V_LIGHT) {
           
          // Change relay state
           if (message.sensor <= NUMBER_OF_RELAYS)
             digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
           else if (message.sensor == NUMBER_OF_RELAYS+1) //First analogue sensor
             digitalWrite(A0, message.getBool()?RELAY_ON:RELAY_OFF);
           else //Second analogue sensor
             digitalWrite(A1, message.getBool()?RELAY_ON:RELAY_OFF);
             
           // Store state in eeprom
           gw.saveState(message.sensor, message.getBool());
           
           // Write some debug info
           Serial.print("Incoming change for sensor:");
           Serial.print(message.sensor);
           Serial.print(", New status: ");
           Serial.println(message.getBool());
         } 
      }
      
      SparkmanS Offline
      SparkmanS Offline
      Sparkman
      Hero Member
      wrote on last edited by Sparkman
      #27

      @twosh Based on the spec sheet, I believe your existing latching relays need a 35ms pulse to turn them on and off.

      The standard relay sketch to control your new relays is meant to turn the relays on based on a command from the controller and then another command from the controller to turn them off. It also maintains the relay state by saving them to EEPROM, and sets them to last state on power-up, etc. You don't need most of that. All you need to do is set the relay to on, wait 35 milliseconds and then set the relay to off based on a toggle command from the controller.

      Take a crack at simplifying the sketch, post your results and I, or others, can help with the next steps.

      The one thing to note is that with this system, you won't know if the light is on or on, all you can do is toggle current status. You would have to add current sensors or something like that to be able to know the state. Are there LEDs on the existing system that are on when the light is on and off when the lights are off?

      Cheers
      Al

      PS Have you measured the voltage to the control side of the existing latching relay when operated by a switch?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        twosh
        wrote on last edited by
        #28

        Thank you all for the various ideas and input!

        I've figured out what I had done wrong (not feeding the relays with the common wire correctly). I've also changed the sketch to give a 35 ms impulse, and connected everything. Working great! Here is my sketch if anyone needs it:

        // Example sketch showing how to control physical relays. 
        // This example will NOT remember relay state even after power failure.
        
        #include <MySensor.h>
        #include <SPI.h>
        
        #define RELAY_ON 0  // GPIO value to write to turn on attached relay
        #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
        
        #define noRelays 8
        const int relayPin[] = {A0, A1, A2, A3, A4, A5, 3, 4};
        
        MySensor gw;
        
        void setup()  
        {   
          // Initialize library and add callback for incoming messages
          gw.begin(incomingMessage, AUTO, true);
          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Relay", "1.0 AD");
        
          // Fetch relay status
          for (int sensor=0; sensor < noRelays; sensor++)
          {
            // Register all sensors to gw (they will be created as child devices)
            gw.present(sensor+1, S_LIGHT);
            // Then set relay pins in output mode
            pinMode(relayPin[sensor], OUTPUT);   
            // Set relay to last known state (using eeprom storage) 
            //digitalWrite(relayPin[sensor], gw.loadState(sensor+1)?RELAY_ON:RELAY_OFF);
          }
        }
        
        
        void loop() 
        {
          // Alway process incoming messages whenever possible
          gw.process();
        }
        
        void incomingMessage(const MyMessage &message)
        {
          // We only expect one type of message from controller. But we better check anyway.
          if (message.type==V_LIGHT)
          {
             if (message.sensor <= noRelays)
             {
                 digitalWrite(relayPin[message.sensor-1], RELAY_ON);
                 delay(35); // 35 ms impulse used by my relays
                 digitalWrite(relayPin[message.sensor-1], RELAY_OFF);
             }
        
             // Store state in eeprom
             //gw.saveState(message.sensor, message.getBool());
             
             // Write some debug info
             Serial.print("Incoming change for sensor:");
             Serial.print(message.sensor);
             Serial.print(", New status: ");
             Serial.println(message.getBool());
           } 
        }
        
        

        As @Sparkman says, I won't actually know if the light is on or off, since I can't register a change that would happen via the physical light switches in the house. Any suggestions on how to do this is of course welcome! The output side of the house relay is 230 VAC to the lights, and 24 VAC to the leds in the light switches. What kind of hardware would I need for sensing current and reporting it back to the Arduino? Is there any sketch I could use that lays the groundwork?

        SparkmanS 1 Reply Last reply
        0
        • T Offline
          T Offline
          twosh
          wrote on last edited by
          #29

          Is this what I would need? http://www.ebay.com/itm/310506962976?rmvSB=true

          How would I wire this? Should it sit between the house relay and the light switch (serial), or would I run it parallel somehow? I guess I would need 8 of these to measure my 8 relays / light switches?

          Thanks!

          1 Reply Last reply
          0
          • T twosh

            Thank you all for the various ideas and input!

            I've figured out what I had done wrong (not feeding the relays with the common wire correctly). I've also changed the sketch to give a 35 ms impulse, and connected everything. Working great! Here is my sketch if anyone needs it:

            // Example sketch showing how to control physical relays. 
            // This example will NOT remember relay state even after power failure.
            
            #include <MySensor.h>
            #include <SPI.h>
            
            #define RELAY_ON 0  // GPIO value to write to turn on attached relay
            #define RELAY_OFF 1 // GPIO value to write to turn off attached relay
            
            #define noRelays 8
            const int relayPin[] = {A0, A1, A2, A3, A4, A5, 3, 4};
            
            MySensor gw;
            
            void setup()  
            {   
              // Initialize library and add callback for incoming messages
              gw.begin(incomingMessage, AUTO, true);
              // Send the sketch version information to the gateway and Controller
              gw.sendSketchInfo("Relay", "1.0 AD");
            
              // Fetch relay status
              for (int sensor=0; sensor < noRelays; sensor++)
              {
                // Register all sensors to gw (they will be created as child devices)
                gw.present(sensor+1, S_LIGHT);
                // Then set relay pins in output mode
                pinMode(relayPin[sensor], OUTPUT);   
                // Set relay to last known state (using eeprom storage) 
                //digitalWrite(relayPin[sensor], gw.loadState(sensor+1)?RELAY_ON:RELAY_OFF);
              }
            }
            
            
            void loop() 
            {
              // Alway process incoming messages whenever possible
              gw.process();
            }
            
            void incomingMessage(const MyMessage &message)
            {
              // We only expect one type of message from controller. But we better check anyway.
              if (message.type==V_LIGHT)
              {
                 if (message.sensor <= noRelays)
                 {
                     digitalWrite(relayPin[message.sensor-1], RELAY_ON);
                     delay(35); // 35 ms impulse used by my relays
                     digitalWrite(relayPin[message.sensor-1], RELAY_OFF);
                 }
            
                 // Store state in eeprom
                 //gw.saveState(message.sensor, message.getBool());
                 
                 // Write some debug info
                 Serial.print("Incoming change for sensor:");
                 Serial.print(message.sensor);
                 Serial.print(", New status: ");
                 Serial.println(message.getBool());
               } 
            }
            
            

            As @Sparkman says, I won't actually know if the light is on or off, since I can't register a change that would happen via the physical light switches in the house. Any suggestions on how to do this is of course welcome! The output side of the house relay is 230 VAC to the lights, and 24 VAC to the leds in the light switches. What kind of hardware would I need for sensing current and reporting it back to the Arduino? Is there any sketch I could use that lays the groundwork?

            SparkmanS Offline
            SparkmanS Offline
            Sparkman
            Hero Member
            wrote on last edited by
            #30

            @twosh Glad to hear you have it working. You could wire something like this in: http://www.ebay.com/itm/221649135732 for each circuit that you want to monitor. They simply put out a voltage relative to the current that you can measure on an analog input. This particular type is hard wired into the 230 VAC side. There are also options that you slip over the 230 VAC. See here for a bunch of options: http://www.ebay.com/sch/i.html?_nkw=arduino+current+sensor.

            Cheers
            Al

            1 Reply Last reply
            0
            • T Offline
              T Offline
              twosh
              wrote on last edited by
              #31

              @Sparkman I think that would work out quite nicely! But I think I would rather stay away from the 230VAC and use it on the 24VAC instead - feels safer. If I don't misunderstand how the current sensor works it shouldn't matter if I hard wire it on 230VAC or 24VAC, right?

              SparkmanS 1 Reply Last reply
              0
              • T twosh

                @Sparkman I think that would work out quite nicely! But I think I would rather stay away from the 230VAC and use it on the 24VAC instead - feels safer. If I don't misunderstand how the current sensor works it shouldn't matter if I hard wire it on 230VAC or 24VAC, right?

                SparkmanS Offline
                SparkmanS Offline
                Sparkman
                Hero Member
                wrote on last edited by Sparkman
                #32

                @twosh Because the signals on the 24 VAC side are momentary 35 ms pulses and not a continuous current when the lights are on, it would be more difficult to use as there's a chance you could miss them and then you would get out of sync. It would be most accurate to connect on the 230 VAC side. You would disconnect one side of the 230 VAC from the relay, wire it to one side of the large terminal on the module and then use a new short piece of wire (rated for at least 230 VAC/20A) and connect from the other large terminal on the module to the relay. It basically needs to be inserted in the circuit so that the current would flow through it. If you're uncomfortable with that, then you can use one of the modules that slip over the wire. They tend to cost more and also may need additional components to connect it to an analog in on the Arduino. Here's one that will work without additional components: http://www.ebay.com/itm/171737837875. This one is only rated for 5A which likely is enough depending on how many and what types of light bulbs you have on the circuit.

                Cheers
                Al

                PS The added advantage is that you'll also be able to track power usage for those lights.

                1 Reply Last reply
                0
                • jeylitesJ Offline
                  jeylitesJ Offline
                  jeylites
                  wrote on last edited by
                  #33

                  @twosh the above current sensor could be one option or you could build a voltage divider to measure the 24v side. Something like the battery monitor .... this route is cheap and effective. Thought I don't know how you are going to the programming part but will like to see where it goes :)

                  SparkmanS 1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    twosh
                    wrote on last edited by
                    #34

                    Great, thanks for the clarifications @Sparkman !

                    Being able to measure power would of course be a nice bonus! I'll think about it. I have another question (of course... :) ).

                    The Nano and Pro mini have only 6 analogue inputs, I would be having need for 8... Would I need to use two arduinos or is there any other way?

                    Best,
                    Tim

                    SparkmanS P 2 Replies Last reply
                    0
                    • jeylitesJ jeylites

                      @twosh the above current sensor could be one option or you could build a voltage divider to measure the 24v side. Something like the battery monitor .... this route is cheap and effective. Thought I don't know how you are going to the programming part but will like to see where it goes :)

                      SparkmanS Offline
                      SparkmanS Offline
                      Sparkman
                      Hero Member
                      wrote on last edited by Sparkman
                      #35

                      @jeylites said:

                      the above current sensor could be one option or you could build a voltage divider to measure the 24v side. Something like the battery monitor .... this route is cheap and effective. Thought I don't know how you are going to the programming part but will like to see where it goes :)

                      It's 24 VAC, so would also need to be rectified.

                      Cheers
                      Al

                      1 Reply Last reply
                      0
                      • T twosh

                        Great, thanks for the clarifications @Sparkman !

                        Being able to measure power would of course be a nice bonus! I'll think about it. I have another question (of course... :) ).

                        The Nano and Pro mini have only 6 analogue inputs, I would be having need for 8... Would I need to use two arduinos or is there any other way?

                        Best,
                        Tim

                        SparkmanS Offline
                        SparkmanS Offline
                        Sparkman
                        Hero Member
                        wrote on last edited by
                        #36

                        @twosh said:

                        Great, thanks for the clarifications @Sparkman !

                        Being able to measure power would of course be a nice bonus! I'll think about it. I have another question (of course... :) ).

                        The Nano and Pro mini have only 6 analogue inputs, I would be having need for 8... Would I need to use two arduinos or is there any other way?

                        Best,
                        Tim

                        You're welcome Tim!

                        You can go with multiple Arduinos but I'd consider a Mega instead: http://www.ebay.com/itm/360790082588.

                        Cheers
                        Al

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          twosh
                          wrote on last edited by
                          #37

                          @jeylites - thanks for the alternate suggestion! :)

                          @Sparkman , to clarify, my light switches does glow constantly when turned on, so there is constant current on the output side of the relay going to the light switches. I've measured the voltage and it is 24VAC. So I think that, disregarding the power measurement possibility, I could use that circuit as well.

                          SparkmanS 1 Reply Last reply
                          0
                          • T twosh

                            @jeylites - thanks for the alternate suggestion! :)

                            @Sparkman , to clarify, my light switches does glow constantly when turned on, so there is constant current on the output side of the relay going to the light switches. I've measured the voltage and it is 24VAC. So I think that, disregarding the power measurement possibility, I could use that circuit as well.

                            SparkmanS Offline
                            SparkmanS Offline
                            Sparkman
                            Hero Member
                            wrote on last edited by Sparkman
                            #38

                            @twosh said:

                            @Sparkman , to clarify, my light switches does glow constantly when turned on, so there is constant current on the output side of the relay going to the light switches. I've measured the voltage and it is 24VAC. So I think that, disregarding the power measurement possibility, I could use that circuit as well.

                            If there's 24 VAC on the output side of the relays as well, then yes, you could use that and you have some other options to be able to measure it including options to use digital pins instead of analog.

                            Here's some examples on converting the 24 VAC to 5 VDC: http://rayshobby.net/24vac-to-5vdc-conversion/. Option 4 may be a good choice. In your case, since you would only use it to connect to a digital pin on your Arduino, power draw is very low and some of the issues he talks about don't apply to your case.

                            Cheers
                            Al

                            1 Reply Last reply
                            0
                            • T Offline
                              T Offline
                              twosh
                              wrote on last edited by
                              #39

                              Thanks for the link - will read it tomorrow! :)

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                twosh
                                wrote on last edited by
                                #40

                                I read the site and concluded that I will first of try with the suggested components (thanks again @Sparkman ). Ordered a bunch of them and a Mega. I'll let you know of my progress when they arrive! Thanks everybody!

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  twosh
                                  wrote on last edited by
                                  #41

                                  My Mega arrived today so I could load it with my customized sketch that I've worked on for controlling relays as well as measuring current. I'm still waiting for the current sensors, but I've already got a problem... I keep getting "check wires" via the serial monitor, and the Mega is not detected by the gateway.

                                  Basically I think that the wiring of the radio to the Mega is wrong, but after scavenging the forum and trying a handfull of different wiring combinations I still can't get this to work.

                                  I have tried using the normal (nano, pro mini) pins, using pins 14-16, 50-52, etc. but nothing seems to work. I have NOT modified any config files yet, but based on the various threads I've read it's unclear to me if you should or shouldn't.

                                  @Sparkman , do you use a mega yourself, and have successfully wired the radio?

                                  @hek, would it be possible to add instructions for the Mega (and perhaps the Uno as well) to the general build guide for connecting the radio?

                                  1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    twosh
                                    wrote on last edited by twosh
                                    #42

                                    Finally found a combination that worked with the mega as a sensor. Here is the pin mapping if it will save time for anyone:

                                    9 CE
                                    10 CSN/CS
                                    52 SCK
                                    51 MOSI
                                    50 MISO
                                    2 IRQ

                                    Doesn't require any config changes, but I am currently getting some "0;0;3;0;9;version mismatch" in serial monitor. Have 10 uF cap on the radio.

                                    1 Reply Last reply
                                    0
                                    • T Offline
                                      T Offline
                                      twosh
                                      wrote on last edited by
                                      #43

                                      Tried powering the Mega from computers USB 5V, and two different 12V DC adapters - still getting version mismatch. Anybody knows what's going on?

                                      SparkmanS 1 Reply Last reply
                                      0
                                      • T twosh

                                        Tried powering the Mega from computers USB 5V, and two different 12V DC adapters - still getting version mismatch. Anybody knows what's going on?

                                        SparkmanS Offline
                                        SparkmanS Offline
                                        Sparkman
                                        Hero Member
                                        wrote on last edited by
                                        #44

                                        @twosh From what I've read before, the version mismatch error is often caused by power issues. Are you getting the errors with the relays connected or without? If the relays are connected, try disconnecting them. If the error goes away, it's likely a power issues. Maybe try powering the radio from a separate 3.3v source.

                                        Cheers
                                        Al

                                        1 Reply Last reply
                                        0
                                        • T Offline
                                          T Offline
                                          twosh
                                          wrote on last edited by
                                          #45

                                          Thanks for the suggestion, @Sparkman !
                                          I have just the radio connected for now, but still getting that "version mismatch"-error. I will try getting a separate 3.3v power to the radio tomorrow and see if that would help.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          18

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular