Smart Home Automation Forum
[arduino code examples for A8v3]-01 Turn ON/OFF relay by XL9555 chip - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=20)
+--- Forum: KC868-A8v3 (https://www.kincony.com/forum/forumdisplay.php?fid=78)
+--- Thread: [arduino code examples for A8v3]-01 Turn ON/OFF relay by XL9555 chip (/showthread.php?tid=9960)



[arduino code examples for A8v3]-01 Turn ON/OFF relay by XL9555 chip - admin - 11-20-2026

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program controls a 8-channel relay board via a XL9555 I/O expander.
* 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>     // XL9555 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 address of the XL9555 module
#define I2C_ADDRESS 0x22 // I2C address of the XL9555 module

PCA9555 xl9555_R1; // Driver object for the XL9555

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("XL9555 Relay Control: Starting...");

  // Attach the XL9555 to the existing I2C bus.
  xl9555_R1.attach(Wire, I2C_ADDRESS);
  xl9555_R1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);

  // Preload HIGH so active-low relays start OFF.
  xl9555_R1.write(PCA95x5::Level::H_ALL);

  // Ports 0-7 are relay outputs; ports 8-15 remain inputs.
  // Direction bits: 0 = output, 1 = input.
  xl9555_R1.direction(0xFF00);
}

void loop() {
  // Sequentially turn on each relay
  for (int i = 0; i < 8; i++) {
    xl9555_R1.write((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
  for (int i = 0; i < 8; i++) {
    xl9555_R1.write((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:

.zip   1-output-9555.zip (Size: 1.01 KB / Downloads: 3)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   1-output-9555.ino.merged.zip (Size: 193.91 KB / Downloads: 3)