Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[arduino code examples for F24]-02 Read digital input ports state by XL9555 chip
#1
Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* Description:
* This Arduino program reads the state of two 16-channel XL9555SS I/O expanders
* and prints the state of all input pins (1-24) to the Serial Monitor. The state of
* each pin is represented as a bit in a 24-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
* - XL9555SS I2C Address: 0x24 for inputs 1-16, 0x26 for inputs 17-24
*/

#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

constexpr uint8_t INPUT_ADDR_1 = 0x24;
constexpr uint8_t INPUT_ADDR_2 = 0x26;
PCA9555 inputIO1;
PCA9555 inputIO2;

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

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

    // Configure both expanders for the input-only test.
    inputIO1.attach(Wire, INPUT_ADDR_1);
    inputIO1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
    inputIO1.direction(PCA95x5::Direction::IN_ALL);

    // Read only ports 0-7 on 0x26. Leave the relay ports undriven.
    inputIO2.attach(Wire, INPUT_ADDR_2);
    inputIO2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);
    inputIO2.direction(PCA95x5::Direction::IN_ALL);

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

void loop() {
    // DI1-16 occupy bits 0-15; DI17-24 occupy bits 16-23.
    const uint32_t first = inputIO1.read();
    const uint32_t second = inputIO2.read() & 0x00FFU;
    const uint32_t state = first | (second << 16);

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

    delay(500); // Delay 500ms
}
arduino ino file download:

.zip   2-digital-input-9555.zip (Size: 1 KB / Downloads: 4)
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.2 KB / Downloads: 6)
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)