Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program controls a 24-channel relay board via two XL9555SS I/O expanders.
* It sequentially turns on each relay and then turns them off in a loop.
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
*
* Delay Time:
* - 200 milliseconds between switching relays
*/
#include <Wire.h> // Include the Wire library for I2C communication
#include <PCA95x5.h> // XL9555SS I/O expander driver
#define SDA 8 // Define the SDA pin
#define SCL 18 // Define the SCL pin
#define DELAY_TIME 200 // Define the delay time in milliseconds
// Set I2C addresses of the XL9555SS modules
#define I2C_ADDRESS_R1 0x25 // I2C address of the first XL9555SS module
#define I2C_ADDRESS_R2 0x26 // I2C address of the second XL9555SS module
PCA9555 relayIO1; // Relays 9-24
PCA9555 relayIO2; // Relays 1-8
// XL9555SS port indices in physical relay order (1-8).
constexpr uint8_t RELAY_PORTS_1_TO_8[] = {12, 13, 14, 15, 8, 9, 10, 11};
void setup() {
// Initialize I2C communication
Wire.begin(SDA, SCL); // SDA on GPIO 8, SCL on GPIO 18 (according to your board's configuration)
// Initialize serial communication for debugging (optional)
Serial.begin(115200);
Serial.println("XL9555SS Relay Control: Starting...");
// Preload HIGH before enabling the active-low relay outputs.
relayIO1.attach(Wire, I2C_ADDRESS_R1);
relayIO1.write(PCA95x5::Level::H_ALL);
relayIO1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
relayIO1.direction(PCA95x5::Direction::OUT_ALL);
relayIO2.attach(Wire, I2C_ADDRESS_R2);
relayIO2.write(PCA95x5::Level::H_ALL);
relayIO2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
// Ports 0-7 remain inputs, and ports 8-15 drive relays 1-8.
relayIO2.direction(0x00FF);
}
void loop() {
// Sequentially turn on each relay (1-24)
// Control relays 1-8 in physical channel order.
for (uint8_t port : RELAY_PORTS_1_TO_8) {
relayIO2.write(static_cast<PCA95x5::Port::Port>(port), PCA95x5::Level::L);
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
// Then control the relays on the first XL9555SS (relays 9-24)
for (int i = 0; i < 16; i++) {
relayIO1.write(static_cast<PCA95x5::Port::Port>(i), PCA95x5::Level::L); // Turn on the relay at pin i (LOW means ON for the relay)
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
// Sequentially turn off each relay (1-24)
// Control relays 1-8 in physical channel order.
for (uint8_t port : RELAY_PORTS_1_TO_8) {
relayIO2.write(static_cast<PCA95x5::Port::Port>(port), PCA95x5::Level::H);
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
// Then control the relays on the first XL9555SS (relays 9-24)
for (int i = 0; i < 16; i++) {
relayIO1.write(static_cast<PCA95x5::Port::Port>(i), PCA95x5::Level::H); // Turn off the relay at pin i (HIGH means OFF for the relay)
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
}
arduino ino file download: