Smart Home Automation Forum
[arduino code examples for A8v3]-02 Read digital input ports state by XL9555 - 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]-02 Read digital input ports state by XL9555 (/showthread.php?tid=9961)



[arduino code examples for A8v3]-02 Read digital input ports state by XL9555 - admin - 11-20-2026

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* Description:
* This Arduino program reads ports 8-15 of an XL9555 I/O expander
* and prints their states to the Serial Monitor. The state of
* each pin is represented as a bit in a 16-bit value, where each bit corresponds
* to an input pin. The program prints the input state in binary format.
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - XL9555 I2C Address: 0x22
*/

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

// Define I2C pins
#define I2C_SDA 8  // Define SDA pin
#define I2C_SCL 18  // Define SCL pin

// Set I2C address
PCA9555 xl9555_IN1; // XL9555 driver object
#define I2C_ADDRESS 0x22

void setup() {
    Serial.begin(115200);

    // Initialize I2C communication
    Wire.begin(I2C_SDA, I2C_SCL); // Initialize I2C with defined SDA and SCL pins

    // Configure all ports as inputs; only ports 8-15 are read below.
    xl9555_IN1.attach(Wire, I2C_ADDRESS);
    xl9555_IN1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
    xl9555_IN1.direction(PCA95x5::Direction::IN_ALL);

    Serial.println("KinCony B8M 8 channel input state 0:ON  1:OFF");
}

void loop() {
    // Keep ports 8-15 in bits 8-15, as in the original program.
    // Bits 0-7 remain zero; do not shift the input states down.
    uint16_t state = xl9555_IN1.read() & 0xFF00;

    Serial.print("Input state: ");
    Serial.println(state, BIN); // Print the state of inputs in binary

    delay(500); // Delay 500ms
}

.zip   2-digital-input-9555.zip (Size: 929 bytes / Downloads: 3)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   2-digital-input-9555.ino.merged.zip (Size: 194.13 KB / Downloads: 3)