Smart Home Automation Forum
[arduino code examples for T16M]-01 Turn ON/OFF OUTPUT by 9555 chip - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=72)
+--- Forum: T16M (https://www.kincony.com/forum/forumdisplay.php?fid=73)
+--- Thread: [arduino code examples for T16M]-01 Turn ON/OFF OUTPUT by 9555 chip (/showthread.php?tid=9920)



[arduino code examples for T16M]-01 Turn ON/OFF OUTPUT by 9555 chip - admin - 10-29-2026

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program controls a 16-channel relay board via an XL9555
* I/O expander using the PCA95x5 library.
*
* It sequentially turns ON all 16 relays, then sequentially
* turns them OFF, repeating continuously.
*
* Pin Definitions:
* - SDA: GPIO 11
* - SCL: GPIO 12
*
* I2C Address:
* - XL9555: 0x25
*
* Relay Logic:
* - LOW  = ON
* - HIGH = OFF
*
* Switching Interval:
* - 200 milliseconds
*/

#include <Wire.h>
#include <PCA95x5.h>

#define I2C_SDA_PIN  11
#define I2C_SCL_PIN  12
#define I2C_ADDRESS  0x25
#define DELAY_TIME   200
#define RELAY_COUNT  16

// Use the PCA9555 driver for the XL9555 I/O expander.
PCA9555 relayIO;

void setup() {
  // Initialize serial communication.
  Serial.begin(115200);

  // Initialize I2C: SDA = GPIO11, SCL = GPIO12, clock = 100 kHz.
  Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN, 100000);

  Serial.println("XL9555 Relay Control: Starting...");

  // Attach the XL9555 to the I2C bus.
  relayIO.attach(Wire, I2C_ADDRESS);

  // Disable input polarity inversion.
  relayIO.polarity(PCA95x5::Polarity::ORIGINAL_ALL);

  // Preload all output latches HIGH before enabling outputs.
  // HIGH keeps the active-low relays OFF.
  relayIO.write(PCA95x5::Level::H_ALL);

  // Configure all 16 ports as outputs.
  relayIO.direction(PCA95x5::Direction::OUT_ALL);

  delay(50);

  Serial.println("All relays initialized to OFF.");
}

void loop() {
  // Sequentially turn ON relays 1-16.
  for (int i = 0; i < RELAY_COUNT; i++) {
    relayIO.write(
      (PCA95x5::Port::Port)i,
      PCA95x5::Level::L
    );

    Serial.print("Relay ");
    Serial.print(i + 1);
    Serial.println(" ON");

    delay(DELAY_TIME);
  }

  // Sequentially turn OFF relays 1-16.
  for (int i = 0; i < RELAY_COUNT; i++) {
    relayIO.write(
      (PCA95x5::Port::Port)i,
      PCA95x5::Level::H
    );

    Serial.print("Relay ");
    Serial.print(i + 1);
    Serial.println(" OFF");

    delay(DELAY_TIME);
  }
}
arduino ino file download:

.zip   1-output-9555.zip (Size: 1,016 bytes / Downloads: 22)
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: 194.16 KB / Downloads: 18)