11-03-2025, 11:27 PM
Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - PCF8575 I2C Address: 0x24 for inputs 1-16, 0x26 for inputs 17-24, 0x27 for inputs 18-25
*/
#include "Arduino.h"
#include "PCF8575.h"
// Define I2C pins
#define I2C_SDA 8 // Define SDA pin
#define I2C_SCL 18 // Define SCL pin
// Set I2C addresses
PCF8575 pcf8575_IN1(0x24); // The I2C address of the first PCF8575 (inputs 1-16)
PCF8575 pcf8575_IN2(0x26); // The I2C address of the second PCF8575 (inputs 17-24)
PCF8575 pcf8575_IN3(0x27); // The I2C address of the second PCF8575 (inputs 25-32)
void setup() {
Serial.begin(115200);
// Initialize I2C communication
Wire.begin(I2C_SDA, I2C_SCL); // Initialize I2C with defined SDA and SCL pins
// Initialize both PCF8575 modules
pcf8575_IN1.begin();
pcf8575_IN2.begin();
pcf8575_IN3.begin();
Serial.println("KinCony F32 32 channel input state 0:ON 1:OFF");
}
void loop() {
uint32_t state = 0; // Use a 32-bit variable to store the state of all 24 pins
// Read the state of each pin from the first PCF8575 (inputs 1-16)
for (int pin = 0; pin < 16; pin++) {
if (pcf8575_IN1.read(pin)) {
state |= (1UL << pin); // Set the bit for the active pin (inputs 1-16)
}
}
// Read the state of each pin from the second PCF8575 (inputs 17-24, corresponding to pins 0-7)
for (int pin = 0; pin < 8; pin++) {
if (pcf8575_IN2.read(pin)) {
state |= (1UL << (pin + 16)); // Set the bit for the active pin (inputs 17-24)
}
}
// Read the state of each pin from the second PCF8575 (inputs 25-32, corresponding to pins 0-7)
for (int pin = 0; pin < 8; pin++) {
if (pcf8575_IN3.read(pin)) {
state |= (1UL << (pin + 24));
}
}
// 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
}
2-digital-input.zip (Size: 885 bytes / Downloads: 68)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
2-digital-input.ino.merged.zip (Size: 197.91 KB / Downloads: 73)
YouTube: https://www.youtube.com/c/KinCony
Online Store: https://shop.kincony.com
Alibaba Store: https://kincony.en.alibaba.com/
Online Store: https://shop.kincony.com
Alibaba Store: https://kincony.en.alibaba.com/

