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



[arduino code examples for T16M]-02 Read digital input ports state by 9555 chip - admin - 10-29-2026

Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* Description:
* This Arduino program reads 16 digital inputs from an XL9555
* I/O expander using the PCA95x5 library.
*
* Input states are printed as a fixed-width 16-bit binary value:
* - Leftmost bit:  CH16 (port 15)
* - Rightmost bit: CH1  (port 0)
* - 0 = ON
* - 1 = OFF
*
* Pin Definitions:
* - SDA: GPIO 11
* - SCL: GPIO 12
* - XL9555 I2C Address: 0x24
*
* Read Interval:
* - 500 milliseconds
*/

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

// Define I2C pins and device address.
#define I2C_SDA      11
#define I2C_SCL      12
#define I2C_ADDRESS  0x24

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

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

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

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

    // Read the original input levels without polarity inversion.
    inputIO.polarity(PCA95x5::Polarity::ORIGINAL_ALL);

    // Configure all 16 ports as inputs.
    inputIO.direction(PCA95x5::Direction::IN_ALL);

    delay(50);

    Serial.println("KinCony XL9555 16-channel input test");
    Serial.println("0 = ON, 1 = OFF");
    Serial.println("Bit order: CH16 ... CH1");
}

void loop() {
    // Read all 16 input ports.
    uint16_t state = inputIO.read();

    // Print all 16 bits, including leading zeros.
    Serial.print("Input state: ");

    for (int bit = 15; bit >= 0; bit--) {
        Serial.print((state >> bit) & 0x01);
    }

    Serial.println();

    delay(500);
}
arduino ino file download:

.zip   2-digital-input-9555.zip (Size: 992 bytes / Downloads: 29)
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.18 KB / Downloads: 27)