Nano losing program code

  • I have an irrigation system with an arduino nano (clone) driving 4 relays that control the irrigation valves. This has been working the vast majority of the time for 2 years.

    However once every few weeks, the nano loses its program code.

    The nano and relays are mounted outside in a waterproof box. They are mains powered and turned on at the same time as the water butt pump. If I initiate a valve opening within sight of the gateway (indoors) I can tell the relay node is not working as there is no initial handshake visible on the gateway leds.

    When I go to reload the program into the relay node, the red onboard led is flashing every second as if it is running the 'blink' code. There is nothing being output from the serial monitor.

    Controller: Vera 3
    MySensors Lib version: 2.3.0

     * Version 1.0 - Henrik Ekblad Relays remembered previous state at start up
     * Version 1.2 - M Young Relays reset to off at startup
     * Update latest version of software in variale 'VERSION' below
     * Example sketch showing how to control physical relays.
    //Set node id manually out of the range of the soil moisture sensors
    #define MY_NODE_ID 100
    // Enable debug prints to serial monitor
    //#define MY_DEBUG
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    // Enable repeater functionality for this node
    #include <MySensors.h>
    #define RELAY_1  3  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 4 // 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
    #define VERSION "V1.3"
    void before()
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);
        // Set relay to last known state (using eeprom storage)
        //digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
        //Set relay to always be off at startup
        digitalWrite(pin, RELAY_OFF);
      Serial.println(F("Relay Actuator - "VERSION));//F means use program memory, dont copy to RAM  
    void setup()
    void presentation()
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("Relay Actuator", VERSION);
      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)
        present(sensor, S_BINARY);
    void loop()
    void receive(const MyMessage &message)
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_STATUS) {
        // Change relay state
        digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
        // Store state in eeprom
        saveState(message.sensor, message.getBool());
        // Write some debug info
        Serial.print("Incoming change for sensor:");
        Serial.print(", New status: ");

    Inside Relay enclosure

    Why would the nano lose its code?

    Is there any method of diagnosing the cause?

Log in to reply

Suggested Topics

  • 3
  • 15
  • 1
  • 3
  • 10
  • 24