Relay not clicking

Hello, all was explained on photo:

here is code:

#include <EEPROM.h>


#define INIT_FLAG_ADDR 100  // Address to store the init flag
#define INIT_FLAG_VALUE 0xAA // Value to check if EEPROM is initialized


// Define relay pins
const int relayPins[8] = {1, 2, 3, 4, 5, 6, 7, 8}; // Extend to 8 channels


void setup() {
  Serial.begin(9600);
  
  // Set relay pins as OUTPUT
  for (int i = 0; i < 8; i++) {
    pinMode(relayPins[i], OUTPUT);
    digitalWrite(relayPins[i], HIGH); // Set all relays to OFF (HIGH)
  }


  // Check initialization flag
  if (EEPROM.read(INIT_FLAG_ADDR) != INIT_FLAG_VALUE) {
    // Clear EEPROM and initialize states
    for (int i = 0; i < 8; i++) {
      EEPROM.write(i, HIGH);  // Write HIGH (OFF) to every address
    }
    
    // Set the initialization flag
    EEPROM.write(INIT_FLAG_ADDR, INIT_FLAG_VALUE);
  } else {
    // Load relay states from EEPROM if already initialized
    for (int i = 0; i < 8; i++) {
      int state = EEPROM.read(i); // Read state from EEPROM
      digitalWrite(relayPins[i], state); // Set the relays to their stored states
    }
  }
}


void loop() {
  if (Serial.available() > 0) {
    String command = Serial.readStringUntil('\n'); // Read until newline
    if (command.length() > 1) {
      char state = command.charAt(0); // '1' or '0'
      int relayNumber = command.charAt(1) - '0'; // Convert char to int
      
      if (relayNumber >= 0 && relayNumber < 8) { // Check valid relay number
        if (state == '1') {
          digitalWrite(relayPins[relayNumber], LOW); // Turn on relay (LOW means ON)
          EEPROM.write(relayNumber, LOW); // Save the updated state to EEPROM
        } else if (state == '0') {
          digitalWrite(relayPins[relayNumber], HIGH); // Turn off relay (HIGH means OFF)
          EEPROM.write(relayNumber, HIGH); // Save the updated state to EEPROM
        }
      }
    }
  }
}


Welcome to the forum

Don't use pins 0 and 1 for the relays. These pins are used by the Serial interface

2 Likes

Uno cannot power 8 relays. Each relay will need approximately 80mA, which is a total of 640mA if all 8 relays are energised at the same time. Uno can power 3, maybe 4 relays at the same time. You need an external power supply. 5V, 1A PSU can power all 8 relays and the Uno at the same time.

1 Like

What was the load that you tried to turn on/off with relay 3?
These relays are not of good quality. Large and inductive loads may weld the contacts together... after that the relay will not click anymore...

Hint: Not all relays make a clicking sound every time. Verify whether the contacts are actually opening and closing as you control them.

That block could result in a short relay activation (all eight) during bootup. It's better for active LOW relay boards with opto couplers to first activate pull up for that reason.
Here pinMode is preferred over digitalWrite.

for (int i = 0; i < 8; i++) pinMode(relayPins[i], INPUT_PULLUP); // first all off (HIGH)
for (int i = 0; i < 8; i++) pinMode(relayPins[i], OUTPUT); // then all output

Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.