Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Christian
Published © GPL3+

Control Up to 65,280 Relays with Your Arduino!

Use the IO Expander and Relay Expander with the Arduino to control up to 65,280 relays!

BeginnerFull instructions provided1 hour82,492

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
IO Expander
IO Expander
×1
Relay Expander
×1
IO Extender
×1
Arduino 16 Relay Board
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Control 64 Relays with a Single Expander

Control 64 Relays with Multiple Expanders

Code

Control 64 Relays with a Single Expander

C/C++
/* IO Expander
 *
 * Control 64 Relays
 *
 */

#include <SoftwareSerial.h>
#include "IOExpander.h"
#include <avr/wdt.h>

//#define SERIAL_DEBUG
#define MAX_RELAYS 64

#ifdef SERIAL_DEBUG
SoftwareSerial swSerial(8,7);
#endif

char cmd[10];

void setup()
{
  Serial.begin(115200);
#ifdef SERIAL_DEBUG
  swSerial.begin(115200);
  swSerialEcho = &swSerial;
#endif
  wdt_enable(WDTO_8S);
  sprintf(cmd, "eb%d", MAX_RELAYS / 16);
  SerialCmdDone(cmd);
}

void loop()
{
  static int i = 1;

  sprintf(cmd, "e%df", i);
  SerialCmdDone(cmd);
  if (i++ >= MAX_RELAYS) i = 1;
  sprintf(cmd, "e%do", i);
  SerialCmdDone(cmd);

  delay(100);
  wdt_reset();
}

Control 64 Relays with Multiple Expanders

C/C++
/* IO Expander
 *
 * Relay Bonnaza with IO Expanders!
 *
 */

#include <HardwareSerial9Bit.h>
#include "IOExpander9Bit.h"
#include <avr/wdt.h>

#define MAX_BOARDS     4

char cmd[10];

void setup()
{
  Serial9Bit.begin(115200, SERIAL_9N1);
  Serial9Bit.write(0);                     // Set IO Expanders to 9-bit
  wdt_enable(WDTO_8S);
  for (int board = 1; board <= 4; board++)
    SerialCmdDone(board, "eb1");
}

void loop()
{
  static int board = 1;
  static int relay = 1;

  sprintf(cmd, "e%df", relay);
  SerialCmdDone(board, cmd);
  if (++relay > 16) {
    relay = 1;
    if (++board > MAX_BOARDS) board = 1;
  }
  sprintf(cmd, "e%do", relay);
  SerialCmdDone(board, cmd);

  delay(100);
  wdt_reset();
}

Credits

Christian
24 projects • 134 followers
Senior Embedded Engineer

Comments