Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
KC868-A8 OpenPLC Function Blocks for RELAY
#1
Code:
#include <Arduino.h>                // Core Arduino functions and definitions
#include <PCF8574.h>               // Library for PCF8574 I2C I/O expander

#define I2C_RELAYS_ADR 0x24          // I2C address of the PCF8574 module


PCF8574 pcf(I2C_RELAYS_ADR);        // Create PCF8574 object using the defined I2C address

void setup() {
    // Initialize I2C communication
    Wire.begin(4, 5);                 // Parameters: SDA pin = 4, SCL pin = 5
   
    pcf.begin();            // Initialize the PCF8574 device

    // Configure all PCF8574 pins (P0–P7) as outputs
    pcf.pinMode(P0, OUTPUT);
    pcf.pinMode(P1, OUTPUT);
    pcf.pinMode(P2, OUTPUT);
    pcf.pinMode(P3, OUTPUT);
    pcf.pinMode(P4, OUTPUT);
    pcf.pinMode(P5, OUTPUT);
    pcf.pinMode(P6, OUTPUT);
    pcf.pinMode(P7, OUTPUT);

    // Safe startup state
    pcf.digitalWrite(P0, HIGH);
    pcf.digitalWrite(P1, HIGH);
    pcf.digitalWrite(P2, HIGH);
    pcf.digitalWrite(P3, HIGH);
    pcf.digitalWrite(P4, HIGH);
    pcf.digitalWrite(P5, HIGH);
    pcf.digitalWrite(P6, HIGH);
    pcf.digitalWrite(P7, HIGH);
}

void loop() {
    // Continuously update each PCF8574 output pin based on OpenPLC variables
    // OUT_0 to OUT_7 are boolean variables provided by OpenPLC

    // If OUT_x is true → set pin LOW (turn relay ON)
    // If OUT_x is false → set pin HIGH (turn relay OFF)

    pcf.digitalWrite(P0, OUT_0 ? LOW : HIGH);
    pcf.digitalWrite(P1, OUT_1 ? LOW : HIGH);
    pcf.digitalWrite(P2, OUT_2 ? LOW : HIGH);
    pcf.digitalWrite(P3, OUT_3 ? LOW : HIGH);
    pcf.digitalWrite(P4, OUT_4 ? LOW : HIGH);
    pcf.digitalWrite(P5, OUT_5 ? LOW : HIGH);
    pcf.digitalWrite(P6, OUT_6 ? LOW : HIGH);
    pcf.digitalWrite(P7, OUT_7 ? LOW : HIGH);
}
   
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)