Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 24 input states from two PCF8575 I/O expanders and
* controls a corresponding 24-channel relay module.
* When an input pin is LOW, the corresponding relay is turned ON (LOW means ON for the relay).
* When the input pin is HIGH, the corresponding relay is turned OFF.
*
* Pin Definitions:
* - SDA: GPIO 8
* - SCL: GPIO 18
* - Input I2C Address 1 (inputs 1-16): 0x24
* - Input/Relay I2C Address 2 (inputs 17-24 and relays 1-8): 0x26
* - Relay I2C Address 3 (relays 9-24): 0x25
*/
#include <Wire.h> // I2C communication
#include <PCF8575.h> // Library to control the PCF8575 I/O expander
// Define I2C pins
#define SDA 8 // SDA pin
#define SCL 18 // SCL pin
// I2C addresses for the PCF8575 modules
#define INPUT_I2C_ADDRESS_1 0x22 // I2C address for the first input PCF8575 module (inputs 1-16)
#define INPUT_RELAY_I2C_ADDRESS_2 0x25 // I2C address for inputs 17-24 and relays 1-8
#define RELAY_I2C_ADDRESS_3 0x24 // I2C address for relays 9-24
PCF8575 pcf8575_IN1(INPUT_I2C_ADDRESS_1); // Create an object for the first input PCF8575 (inputs 1-16)
PCF8575 pcf8575_IN2(INPUT_RELAY_I2C_ADDRESS_2); // Create an object for the second input PCF8575 (inputs 17-24 and relays 1-8)
PCF8575 pcf8575_RL(RELAY_I2C_ADDRESS_3); // Create an object for the relay PCF8575 (relays 9-24)
void setup() {
// Initialize I2C communication
Wire.begin(SDA, SCL);
// Initialize serial communication
Serial.begin(115200);
// Initialize input and relay modules
pcf8575_IN1.begin(); // For inputs 1-16
pcf8575_IN2.begin(); // For inputs 17-24 and relays 1-8
pcf8575_RL.begin(); // For relays 9-24
// Turn off all relays at the start (LOW means OFF)
for (int i = 8; i < 16; i++) {
pcf8575_IN2.write(i, HIGH); // Turn off relays 1-8
}
for (int i = 0; i < 16; i++) {
pcf8575_RL.write(i, HIGH); // Turn off relays 9-24
}
Serial.println("System started: Input state controlling 24 relays");
}
void loop() {
if (pcf8575_IN1.read(8)==0) pcf8575_IN2.write(8, LOW); else pcf8575_IN2.write(8, HIGH);
if (pcf8575_IN1.read(9)==0) pcf8575_IN2.write(9, LOW); else pcf8575_IN2.write(9, HIGH);
if (pcf8575_IN1.read(10)==0) pcf8575_IN2.write(10, LOW); else pcf8575_IN2.write(10, HIGH);
if (pcf8575_IN1.read(11)==0) pcf8575_IN2.write(11, LOW); else pcf8575_IN2.write(11, HIGH);
if (pcf8575_IN1.read(12)==0) pcf8575_IN2.write(12, LOW); else pcf8575_IN2.write(12, HIGH);
if (pcf8575_IN1.read(13)==0) pcf8575_IN2.write(13, LOW); else pcf8575_IN2.write(13, HIGH);
if (pcf8575_IN1.read(14)==0) pcf8575_IN2.write(14, LOW); else pcf8575_IN2.write(14, HIGH);
if (pcf8575_IN1.read(15)==0) pcf8575_IN2.write(15, LOW); else pcf8575_IN2.write(15, HIGH);
if (pcf8575_IN1.read(0)==0) pcf8575_RL.write(0, LOW); else pcf8575_RL.write(0, HIGH);
if (pcf8575_IN1.read(1)==0) pcf8575_RL.write(1, LOW); else pcf8575_RL.write(1, HIGH);
if (pcf8575_IN1.read(2)==0) pcf8575_RL.write(2, LOW); else pcf8575_RL.write(2, HIGH);
if (pcf8575_IN1.read(3)==0) pcf8575_RL.write(3, LOW); else pcf8575_RL.write(3, HIGH);
if (pcf8575_IN1.read(4)==0) pcf8575_RL.write(4, LOW); else pcf8575_RL.write(4, HIGH);
if (pcf8575_IN1.read(5)==0) pcf8575_RL.write(5, LOW); else pcf8575_RL.write(5, HIGH);
if (pcf8575_IN1.read(6)==0) pcf8575_RL.write(6, LOW); else pcf8575_RL.write(6, HIGH);
if (pcf8575_IN1.read(7)==0) pcf8575_RL.write(7, LOW); else pcf8575_RL.write(7, HIGH);
if (pcf8575_IN2.read(0)==0) pcf8575_RL.write(8, LOW); else pcf8575_RL.write(8, HIGH);
if (pcf8575_IN2.read(1)==0) pcf8575_RL.write(9, LOW); else pcf8575_RL.write(9, HIGH);
if (pcf8575_IN2.read(2)==0) pcf8575_RL.write(10, LOW); else pcf8575_RL.write(10, HIGH);
if (pcf8575_IN2.read(3)==0) pcf8575_RL.write(11, LOW); else pcf8575_RL.write(11, HIGH);
if (pcf8575_IN2.read(4)==0) pcf8575_RL.write(12, LOW); else pcf8575_RL.write(12, HIGH);
if (pcf8575_IN2.read(5)==0) pcf8575_RL.write(13, LOW); else pcf8575_RL.write(13, HIGH);
if (pcf8575_IN2.read(6)==0) pcf8575_RL.write(14, LOW); else pcf8575_RL.write(14, HIGH);
if (pcf8575_IN2.read(7)==0) pcf8575_RL.write(15, LOW); else pcf8575_RL.write(15, HIGH);
// Delay for 500 milliseconds
delay(200);
}
11-input-trigger-output.zip (Size: 1.18 KB / Downloads: 239)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:
11-input-trigger-output.ino.merged.zip (Size: 192.44 KB / Downloads: 237)
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/

