Arduino Pro MICRO Pinout for 8 channel Relay



  • Hello,

    does the Library Support using on Pro Micro Analog Inputs A0 A1 A2 A3
    as Digital Output for More Relay steering up to 8 Relays ?

    Greetings from Germany

    Stefan


  • Admin

    It has really nothing to do with the MySensors library how you choose to control the GPIOs in the sketch.

    But yes, you can control relays with the analog outputs.

    Remember to power the relays separately though. They are power hungry beasts.



  • Hi,

    ok i defined Relay 6,7,8 at A0,A1,A2, when i initial send a digitalwrite(high) or low within the sketch and uload that, it gone OK, but when want to change the states at runtime per radio, nothing changed but Relays 1 to 5 are ok.

    example:

    void setup()
    {
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(18, OUTPUT);
    pinMode(19, OUTPUT);
    pinMode(20, OUTPUT);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(18, HIGH);
    digitalWrite(19, HIGH);
    digitalWrite(20, HIGH);

    any idea ?

    Bye Stefan


  • Admin

    You will have to post your sketch code as well if we should be able to help you. Enclose it in ```` to get code formatting.



  • Ok, thanks

    ""// 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 SPI_CE 9
    //#define SPI_SS 10 // PB0, pin 8, Digital17
    //#define SPI_MISO 14 // PB3, pin 11, Digital14
    //#define SPI_MOSI 16 // PB2, pin 10, Digital16
    //#define SPI_SCK 15 // PB1, pin 9, Digital15
    //#define MySensor(uint8_t cepin = 9, uint8_t cspin = 10);
    #define RELAY_1 3 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_2 4
    #define RELAY_3 5
    #define RELAY_4 6
    #define RELAY_5 7
    #define RELAY_6 18
    #define RELAY_7 19
    #define RELAY_8 20
    #define NUMBER_OF_RELAYS 8 // Total number of attached relays
    #define RELAY_ON 1 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay

    //MySensor(uint8_t cepin = 9, uint8_t cspin = 10) gw;
    MySensor gw;
    void setup()
    {
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(18, OUTPUT);
    pinMode(19, OUTPUT);
    pinMode(20, OUTPUT);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(18, HIGH);
    digitalWrite(19, HIGH);
    digitalWrite(20, HIGH);

    //MySensor(uint8_t cepin=9, uint8_t cspin=10)
    // 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
    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);
    }
    }

    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
    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(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
    }
    }
    ""


  • Admin

    @Porky6666

    That will ot work as you don't actually use the GPIO-defines you created at the top of your sketch.

    I would probably have created an array with the pins you intend to use and loop over it when initializing and handling the commands from gateway.

    E.g.

    char[] pins = [3,4,5,6,7,18,19,20]; 
    for (int i=0;i<pins.length;i++) {
         pinMode(pin[i], OUTPUT); 
         digitalWrite(pin[i], HIGH);
    }
    

    And likewise when receiving controller commands.



  • Hi,

    this ist the orginal example from this site, and so it works vor 5 relays.
    "// 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_2 4
    #define RELAY_3 5
    #define RELAY_4 6
    #define RELAY_5 7
    //#define RELAY_6 18
    //#define RELAY_7 19
    //#define RELAY_8 20
    #define NUMBER_OF_RELAYS 5 // Total number of attached relays
    #define RELAY_ON 1 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // 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");

    // 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);
    }
    }

    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
    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(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
    }
    }
    "
    but not more.
    On an Arduino Pro Mini
    this sketch works for 6 Relays inkl. D8, i have changed to pro micro
    an i used 2 different new ones same result, 5 Relays working -- externel power supply to the relays, arduino only needs to pull up or pull down.

    bye
    Stefan
    thanks for your help



  • That sketch only works when the desired output pins are in numerical sequence. Once you need to use a variety of pin numbers in an arbitrary order, you will need to mange them differently.

    Using an array as Hek suggested is no doubt your best option.



  • Hi,

    Thats a fact i'd. Never known,
    Thanks for your help.

    Stefan


  • Hero Member

    @Porky6666

    You can insert 4 spaces at the beginning of each line to show it as code here:

    // 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_2 4
    #define RELAY_3 5
    #define RELAY_4 6
    #define RELAY_5 7
    //#define RELAY_6 18
    //#define RELAY_7 19
    //#define RELAY_8 20
    
    #define NUMBER_OF_RELAYS 5 // Total number of attached relays
    #define RELAY_ON 1 // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // 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");
        
        // 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);
        }
    }
    
    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
            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(message.sensor);
            Serial.print(", New status: ");
            Serial.println(message.getBool());
        }
    }


  • Hi,

    Here is the example of the code using an 8 delay board.
    I used the Analog devices as much as possible because I intend to include 1-wire temp sensors as well.
    On my (clone) Mini there are 8 analog pins but useable on the bread board because of the location. I will use them in the final perfboard.

      // Example sketch showing how to control physical relays. 
      // This example will remember relay state even after power failure.
      // Using an Array to define the pin's 
    
    #include <MySensor.h>
    #include <SPI.h>
    
    const int RELAY[] = {A0, A1, A2, A3, A4, 6, 7, 8}; // I/O pins for the relays
    #define NUMBER_OF_RELAYS 8 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // 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");
    
      // Fetch relay status
      for (int sensor=1, pin=0; 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(RELAY[pin], OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(RELAY[pin], 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
         digitalWrite(RELAY[message.sensor-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(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    

    "


  • Hero Member

    @sjoerd14 said:

    1-wire temp sensors as well.

    I learned the other day that you can use the "Analog" I/O for one-wire anyway? just treat them as they were digital!



  • @sjoerd14

    Great thx a lot -- is working fine your example.

    bye
    Stefan



  • Hi, I'm trying to create an array of binary switches based on the 8Ch relay script, but I'm having issues setting it up. I don't have much knowledge in array and I was wondering if anyone here could guide me.

    // Simple binary switch example 
    // Connect button or door/window reed switch between 
    // digitial I/O pin 3 (BUTTON_PIN below) and GND.
    
    #include <MySensor.h>
    #include <SPI.h>
    #include <Bounce2.h>
    
    const int BUTTON[] = {3, 4, 5};
    #define NUMBER_OF_BUTTONS 3
    //#define CHILD_ID 3
    //#define BUTTON_PIN  3  // Arduino Digital I/O pin for button/reed switch
    
    MySensor gw;
    Bounce debouncer = Bounce(); 
    int oldValue=-1;
    
    // Change to V_LIGHT if you use S_LIGHT in presentation below
    MyMessage msg(V_TRIPPED);
    
    void setup()  
    {  
      gw.begin();
    // Fetch relay status
      for (int sensor=1, pin=0; sensor<=NUMBER_OF_BUTTONS;sensor++, pin++) {
        
     // Setup the button
      pinMode(BUTTON[pin],INPUT);
      // Activate internal pull-up
      digitalWrite(BUTTON[pin],HIGH);
      
      // After setting up the button, setup debouncer
      debouncer.attach(BUTTON[pin]);
      debouncer.interval(5);
      
      // Register binary input sensor to gw (they will be created as child devices)
      // You can use S_DOOR, S_MOTION or S_LIGHT here depending on your usage. 
      // If S_LIGHT is used, remember to update variable type you send in. See "msg" above.
      gw.present(sensor, S_DOOR);  
      
    
    }
    
    //  Check if digital input has changed and send in new value
    void loop() 
    {
      debouncer.update();
      // Get the update value
      int value = debouncer.read();
     
      if (value != oldValue) {
         // Send in the new value
         gw.send(msg.set(value==HIGH ? 1 : 0));
         oldValue = value;
      }
    } 
    
    


  • In addition to my message above, I'm getting a bunch of errors when I try to compile the above binary sketch. Hope to hear from some one.


 

323
Online

8.4k
Users

9.3k
Topics

97.9k
Posts