Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[arduino code examples for B8]-11 digital INPUT trigger OUTPUT directly
#1
Code:
/*
* Made by KinCony IoT: https://www.kincony.com
*
* This program reads 8 input states from a PCF8575 I/O expander and
* controls a corresponding 8-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.
*/

#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

#define RELAY_I2C_ADDRESS 0x22   // I2C address for the relay PCF8575 module


PCF8575 pcf8575_RL(RELAY_I2C_ADDRESS);    // Create an object for the relay PCF8575

void setup() {
  // Initialize I2C communication
  Wire.begin(SDA, SCL);
 
  // Initialize serial communication
  Serial.begin(115200);
 
  // Initialize input and relay modules

  pcf8575_RL.begin();
 
  // Turn off all relays at the start
  for (int i = 8; i < 16; i++) {
    pcf8575_RL.write(i, LOW);  // Assuming relays are LOW when OFF, setting all relays to OFF initially
  }
 
  Serial.println("System started: Input state controlling 8 relays");
}

void loop() {
  uint16_t inputState = 0;

  // Read the state of 16 inputs
  for (int i = 0; i < 8; i++) {
    if (pcf8575_RL.read(i)) {
      inputState |= (1 << i);  // If input is HIGH, set the corresponding bit
    } else {
      inputState &= ~(1 << i); // Otherwise, clear the corresponding bit
    }
  }
 
  // Control the relays based on the input state
  for (int i = 0; i < 8; i++) {
    if (inputState & (1 << i)) {
      pcf8575_RL.write(i+8, HIGH);  // If input is HIGH, turn the relay OFF
    } else {
      pcf8575_RL.write(i+8, LOW);   // If input is LOW, turn the relay ON
    }
  }

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

.zip   11-input-trigger-output.zip (Size: 968 bytes / Downloads: 274)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   11-input-trigger-output.ino.merged.zip (Size: 192.21 KB / Downloads: 272)
Reply


Messages In This Thread
[arduino code examples for B8]-11 digital INPUT trigger OUTPUT directly - by admin - 06-23-2025, 05:02 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)