Smart Home Automation Forum
[arduino code examples for TA]-01 Turn ON/OFF relay - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=72)
+--- Forum: TA (https://www.kincony.com/forum/forumdisplay.php?fid=76)
+--- Thread: [arduino code examples for TA]-01 Turn ON/OFF relay (/showthread.php?tid=7898)



[arduino code examples for TA]-01 Turn ON/OFF relay - admin - 04-03-2025

Code:
// Made by KinCony IoT: https://www.kincony.com

#include <Wire.h>        // Include I2C communication library
#include <PCF8575.h>     // Include PCF8575 library

#define SDA_PIN 8        // I2C SDA pin
#define SCL_PIN 18       // I2C SCL pin

#define RELAY1 14        // Relay 1 (PCF8575 CH15)
#define RELAY2 15       // Relay 2 (PCF8575 CH16)

#define I2C_ADDRESS 0x22 // I2C address of PCF8575

PCF8575 pcf8575(I2C_ADDRESS); // Create PCF8575 object

void setup() {
    Wire.begin(SDA_PIN, SCL_PIN); // Initialize I2C bus
    Serial.begin(115200);
    Serial.println("PCF8575 Relay Control: Starting...");

    pcf8575.begin(); // Initialize PCF8575

    // Initialize relay state, default OFF (HIGH)
    pcf8575.write(RELAY1, HIGH);
    pcf8575.write(RELAY2, HIGH);
    Serial.println("All relays are OFF");
}

void loop() {
    Serial.println("Turning Relay 1 ON");
    pcf8575.write(RELAY1, LOW); // Turn ON relay 1
    delay(2000);
    Serial.println("Turning Relay 1 OFF");
    pcf8575.write(RELAY1, HIGH); // Turn OFF relay 1
    delay(2000);

    Serial.println("Turning Relay 2 ON");
    pcf8575.write(RELAY2, LOW); // Turn ON relay 2
    delay(2000);
    Serial.println("Turning Relay 2 OFF");
    pcf8575.write(RELAY2, HIGH); // Turn OFF relay 2
    delay(2000);
}
arduino ino file download:

.zip   1-output.zip (Size: 631 bytes / Downloads: 346)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download:

.zip   1-output.ino.merged.zip (Size: 192.13 KB / Downloads: 349)