3 hours ago
Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program controls a 32-channel relay board via three 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
#define I2C_ADDRESS_R3 0x27 // I2C address of the second XL9555SS module
PCA9555 relayIO1; // Address 0x25, ports 0-15: relays 17-32
PCA9555 relayIO2; // Address 0x26, ports 8-15: relays 9-16
PCA9555 relayIO3; // Address 0x27, ports 8-15: relays 1-8
// Port indices for physical output channels 9-16 on address 0x26.
constexpr uint8_t RELAY_PORTS_9_TO_16[] = {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);
relayIO2.direction(0x00FF); // Ports 0-7: inputs; ports 8-15: outputs.
relayIO3.attach(Wire, I2C_ADDRESS_R3);
relayIO3.write(PCA95x5::Level::H_ALL);
relayIO3.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
relayIO3.direction(0x00FF); // Ports 0-7: inputs; ports 8-15: outputs.
}
void loop() {
// Sequentially turn on each relay (1-32)
// First control the relays on the second XL9555SS (relays 1-8, corresponding to pins 8-15)
for (int i = 8; i < 16; i++) {
relayIO3.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
}
// First control the relays on the second XL9555SS (relays 9-16, corresponding to pins 8-15)
for (uint8_t port : RELAY_PORTS_9_TO_16) {
relayIO2.write(static_cast<PCA95x5::Port::Port>(port), PCA95x5::Level::L); // Turn on the relay at pin i (LOW means ON for the relay)
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
// Then control the relays on the first XL9555SS (relays 17-32)
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-32)
// First control the relays on the second XL9555SS (relays 1-8, corresponding to pins 8-15)
for (int i = 8; i < 16; i++) {
relayIO3.write(static_cast<PCA95x5::Port::Port>(i), PCA95x5::Level::H); // Turn on the relay at pin i (LOW means ON for the relay)
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
// First control the relays on the second XL9555SS (relays 9-16, corresponding to pins 8-15)
for (uint8_t port : RELAY_PORTS_9_TO_16) {
relayIO2.write(static_cast<PCA95x5::Port::Port>(port), PCA95x5::Level::H); // Turn off the relay at pin i (HIGH means OFF for the relay)
delay(DELAY_TIME); // Wait for DELAY_TIME milliseconds
}
// Then control the relays on the first XL9555SS (relays 17-32)
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
}
}
1-relay-9555.zip (Size: 1.28 KB / Downloads: 3)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
1-relay-9555.ino.merged.zip (Size: 194.06 KB / Downloads: 3)
YouTube: https://www.youtube.com/c/KinCony
Online Store: https://shop.kincony.com
Alibaba Store: https://kincony.en.alibaba.com/
Online Store: https://shop.kincony.com
Alibaba Store: https://kincony.en.alibaba.com/

