Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[arduino code examples for A32 Pro]-01 Turn ON/OFF relay
#1
Code:
/*
  Made by KinCony IoT: https://www.kincony.com

  Program functionality:
  This program uses ESP32-S3 to control two PCA9555 I/O expander chips via I2C,
  controlling a total of 32 relays. The first PCA9555 chip has an I2C address of 0x21
  and controls relays 1-16, while the second PCA9555 chip has an I2C address of 0x22
  and controls relays 17-32.

  The relays are turned ON (set to low) and OFF (set to high) in sequence with a small
  delay between each operation. The program reads and prints the current state of the
  relays to the serial monitor after each change.
*/

#include <PCA95x5.h>

// Initialize two PCA9555 objects for controlling 32 relays
PCA9555 ioex1; // 1-16 relays (I2C address 0x21)
PCA9555 ioex2; // 17-32 relays (I2C address 0x22)

void setup() {
    // Start serial communication for debugging
    Serial.begin(115200);
    delay(10);

    // Initialize the I2C bus with GPIO 11 as SDA and GPIO 10 as SCL, 100kHz frequency
    Wire.begin(11, 10, 100000);

    // Configure the first PCA9555 chip (1-16 relays)
    ioex1.attach(Wire, 0x21);  // Set I2C address to 0x21
    ioex1.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // Set polarity to normal (no inversion)
    ioex1.direction(PCA95x5::Direction::OUT_ALL);  // Set all pins as output
    ioex1.write(PCA95x5::Level::H_ALL);  // Initialize all outputs to HIGH (relays OFF)

    // Configure the second PCA9555 chip (17-32 relays)
    ioex2.attach(Wire, 0x22);  // Set I2C address to 0x22
    ioex2.polarity(PCA95x5::Polarity::ORIGINAL_ALL);  // Set polarity to normal (no inversion)
    ioex2.direction(PCA95x5::Direction::OUT_ALL);  // Set all pins as output
    ioex2.write(PCA95x5::Level::H_ALL);  // Initialize all outputs to HIGH (relays OFF)

    delay(50);  // Wait for settings to take effect
}

void loop() {
    // Control the first PCA9555 chip (1-16 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port low (chip 1): ");  // Turning ON the relay
        Serial.println(i);

        ioex1.write((PCA95x5::Port::Port)i, PCA95x5::Level::L);  // Set the relay to LOW (ON)
        Serial.println(ioex1.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay activation
    }

    // Control the second PCA9555 chip (17-32 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port low (chip 2): ");  // Turning ON the relay
        Serial.println(i + 16);  // Display relay numbers 17-32

        ioex2.write((PCA95x5::Port::Port)i, PCA95x5::Level::L);  // Set the relay to LOW (ON)
        Serial.println(ioex2.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay activation
    }

    // Turn off the first PCA9555 chip (1-16 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port high (chip 1): ");  // Turning OFF the relay
        Serial.println(i);

        ioex1.write((PCA95x5::Port::Port)i, PCA95x5::Level::H);  // Set the relay to HIGH (OFF)
        Serial.println(ioex1.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay deactivation
    }

    // Turn off the second PCA9555 chip (17-32 relays)
    for (size_t i = 0; i < 16; ++i) {
        Serial.print("set port high (chip 2): ");  // Turning OFF the relay
        Serial.println(i + 16);  // Display relay numbers 17-32

        ioex2.write((PCA95x5::Port::Port)i, PCA95x5::Level::H);  // Set the relay to HIGH (OFF)
        Serial.println(ioex2.read(), BIN);  // Print the current state of all relays (in binary)
        delay(50);  // Small delay between each relay deactivation
    }
}
arduino ino file download: 
.zip   1-relay.zip (Size: 1.13 KB / Downloads: 73)
BIN file (you can use esp32 download tool download to ESP32-S3 with address 0x0 then directly to use) download: 
.zip   1-relay.ino.merged.zip (Size: 187.64 KB / Downloads: 57)
Reply
#2
ho provato questo codice con kc868 A32 e i led della scheda rimangono accesi e gli ingressi non funzionano
Reply
#3
take some photos, how you did.
Reply
#4
/*
Made by KinCony IoT: https://www.kincony.com

Program functionality:
This program uses ESP32-S3 to control two PCA9555 I/O expander chips via I2C,
controlling a total of 32 relays. The first PCA9555 chip has an I2C address of 0x21
and controls relays 1-16, while the second PCA9555 chip has an I2C address of 0x22
and controls relays 17-32.

The relays are turned ON (set to low) and OFF (set to high) in sequence with a small
delay between each operation. The program reads and prints the current state of the
relays to the serial monitor after each change.
*/

#include <PCA95x5.h>

// Initialize two PCA9555 objects for controlling 32 relays
PCA9555 ioex1; // 1-16 relays (I2C address 0x21)
PCA9555 ioex2; // 17-32 relays (I2C address 0x22)

void setup() {
// Start serial communication for debugging
Serial.begin(115200);
delay(10);

// Initialize the I2C bus with GPIO 11 as SDA and GPIO 10 as SCL, 100kHz frequency
Wire.begin(11, 10, 100000);

// Configure the first PCA9555 chip (1-16 relays)
ioex1.attach(Wire, 0x21); // Set I2C address to 0x21
ioex1.polarity(PCA95x5:Tongueolarity::ORIGINAL_ALL); // Set polarity to normal (no inversion)
ioex1.direction(PCA95x5:Big Grinirection::OUT_ALL); // Set all pins as output
ioex1.write(PCA95x5::Level::H_ALL); // Initialize all outputs to HIGH (relays OFF)

// Configure the second PCA9555 chip (17-32 relays)
ioex2.attach(Wire, 0x22); // Set I2C address to 0x22
ioex2.polarity(PCA95x5:Tongueolarity::ORIGINAL_ALL); // Set polarity to normal (no inversion)
ioex2.direction(PCA95x5:Big Grinirection::OUT_ALL); // Set all pins as output
ioex2.write(PCA95x5::Level::H_ALL); // Initialize all outputs to HIGH (relays OFF)

delay(50); // Wait for settings to take effect
}

void loop() {
// Control the first PCA9555 chip (1-16 relays)
for (size_t i = 0; i < 16; ++i) {
Serial.print("set port low (chip 1): "); // Turning ON the relay
Serial.println(i);

ioex1.write((PCA95x5:Tongueort:Tongueort)i, PCA95x5::Level::L); // Set the relay to LOW (ON)
Serial.println(ioex1.read(), BIN); // Print the current state of all relays (in binary)
delay(50); // Small delay between each relay activation
}

// Control the second PCA9555 chip (17-32 relays)
for (size_t i = 0; i < 16; ++i) {
Serial.print("set port low (chip 2): "); // Turning ON the relay
Serial.println(i + 16); // Display relay numbers 17-32

ioex2.write((PCA95x5:Tongueort:Tongueort)i, PCA95x5::Level::L); // Set the relay to LOW (ON)
Serial.println(ioex2.read(), BIN); // Print the current state of all relays (in binary)
delay(50); // Small delay between each relay activation
}

// Turn off the first PCA9555 chip (1-16 relays)
for (size_t i = 0; i < 16; ++i) {
Serial.print("set port high (chip 1): "); // Turning OFF the relay
Serial.println(i);

ioex1.write((PCA95x5:Tongueort:Tongueort)i, PCA95x5::Level::H); // Set the relay to HIGH (OFF)
Serial.println(ioex1.read(), BIN); // Print the current state of all relays (in binary)
delay(50); // Small delay between each relay deactivation
}

// Turn off the second PCA9555 chip (17-32 relays)
for (size_t i = 0; i < 16; ++i) {
Serial.print("set port high (chip 2): "); // Turning OFF the relay
Serial.println(i + 16); // Display relay numbers 17-32

ioex2.write((PCA95x5:Tongueort:Tongueort)i, PCA95x5::Level::H); // Set the relay to HIGH (OFF)
Serial.println(ioex2.read(), BIN); // Print the current state of all relays (in binary)
delay(50); // Small delay between each relay deactivation
}
}

ho provato a inserire questo codice e non funziona lo stesso
Reply
#5
Hi
I used the same code but got this output and the device keeps doing this again and again

rst:0x8 (TG1WDT_SYS_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
modeBig GrinIO, clock div:1
load:0x3fff0030,len:4604
ho 0 tail 12 room 4
load:0x40078000,len:15488
load:0x40080400,len:4
load:0x40080404,len:3180
entry 0x400805b8
ets Jul 29 2019 12:21:46
Reply
#6
you can try to download the BIN firmware file to ESP32 directly by ESP32 DOWNLOAD TOOL.
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)