Can't get relay to work

  • I have used the relay sketch unmodified but cant get it to work. I get a lightswitch in domoticz but nothing happens when I toggle it.
    I tested using the simple blink- sketch and used the relays "in" instead of a led and the relay turns on and of without problem. But it seems like the relay sketch never sets the pin to high.

     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     * Created by Henrik Ekblad <>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list:
     * Documentation:
     * Support Forum:
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     * Version 1.0 - Henrik Ekblad
     * Example sketch showing how to control physical relays. 
     * This example will remember relay state after power failure.
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON HIGH  // GPIO value to write to turn on attached relay
    #define RELAY_OFF LOW // GPIO value to write to turn off attached relay
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    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");
      // Fetch relay status
        // 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);
    void loop() 
      // Alway process incoming messages whenever possible
    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
         digitalWrite(message.sensor-1+RELAY_1, 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(", New status: ");

  • Hardware Contributor

    It doesnt set to High unless you get a repons from Domoticz i think. This happens to my device also sometimes (one i have in the far end of the house). Sometimes when its not getting trought the radiomsg it doesnt switch the relay. Do you have a cap on the radio?

    It could also be a power issue - do you have the relay with seperate power or do you power it from the arduino?

  • @sundberg84 said:

    lay. Do y

    I have caps on all radios and I tested with them onli like 1,5m apart. I am powering from arduino and have tried both 5v an VIN. But as I said it works when using a simple blink-sketch. I have also tried tapping the "in" on the relay with a wire from 3.3v and 5v from the arduino and it clicks every time. So there is nothing wrong electricly, I am just not getting any signal from domoticz when turning on/off.

  • Hardware Contributor

    If you are powering it from Arduino that might be the problem. The relay needs good power source or the radio will fail. Try to take the power directly from the 5v

  • Hardware Contributor

    This is from my blog (swedish) image where you see tha power wiring,
    It can also be good to take the 5V from source but run the ground through the arduino.

  • I don´t really understand but If I power the relay using 5v from a psu, then I cant get eny response when connecting any pin/power from the arduino to the "in" on the relay. But If I take another 5v from the psu and connect it to the "in " on the relay then it switches.

    If I use 5v and gnd from a nano to power it and a pin for the "in" on the relay then it also works. But I cant turn it on/of from domoticz. If the radio and relay is using to much power would the sktch still show up in domoticz like normal?

  • Ahhhh!!, finally got it! I missed that I had to connect both GND from the arduino and "-" from the psu to the same connector.

  • spoke too soon. Still cant toggle from domoticz. But now I can switch the relay by tapping 5v from the arduino to the "in" terminal. But still domoticz does not actually turn the pin to high when clicking on the lightbulb icon.

    This is the relay:

    link text

  • @Cliff-Karlsson - I may be way off base here and I am new to this myself - but, does that sketch you quoted actually compile? - I ask because it looks like you have used the relay sample on the mysensors site - but cut out some of the loop.

    The bit that sets the output pin that the relay is connected to pinMode (pin, OUTPUT) .....

    From what you have posted, you haven't got pin defined anywhere - in the original example it gets declared in the line

    for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)

    In your sketch i can't see pin or sensor defined - so I can't see how it's compiling....... but I may have missed something

    If I have general advice would be check what pin the relay is connected to, and make sure thats that one you set as output and the one that you do the digitalWrite to.

    Hope this helps - but if not - sorry if I am way off base.