DEV Community

Cover image for ESP32 with RS485 16 Channel Relay Module Connection and Code
кαтнєєѕкυмαɾ
кαтнєєѕкυмαɾ

Posted on

9

ESP32 with RS485 16 Channel Relay Module Connection and Code

I searched a lot of sites and more tutorials to find the accurate code to control this type of relay. but I cannot find the correct code solution. After researching for three days, I found the correct code and solution for it. I will use it in my future projects. happy to share with you.

RS485 Wiring

Image description

Code



#include <ModbusMaster.h>
int pool_size1;
#define RX2 16 //RX

define TX2 17 //TX

ModbusMaster node1; //ModbusRTU Relay Slave ID1

int8_t pool_size;

void setup()
{
Serial.begin(9600);
Serial2.begin(19200, SERIAL_8N1, RX2, TX2); //RX2=16,RO ,TX2=17, DI
node1.begin(1, Serial2);

Serial.println("successfully node bigining");
delay(1000);

pool_size = node1.writeSingleRegister(0x01, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x02, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x03, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x04, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x05, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x06, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x07, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x08, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x09, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x0A, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x0B, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x0C, 0x0100);
delay(1000);

pool_size = node1.writeSingleRegister(0x0D, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x0E, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x0F, 0x0100);
delay(1000);
pool_size = node1.writeSingleRegister(0x10, 0x0100);

delay(5000);
Serial.println("Attempting to turn off relay");

pool_size = node1.writeSingleRegister(0x01, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x02, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x03, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x04, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x05, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x06, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x07, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x08, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x09, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x0A, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x0B, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x0C, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x0D, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x0E, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x0F, 0x0200);
delay(1000);
pool_size = node1.writeSingleRegister(0x10, 0x0200);
delay(5000);

Serial.println("Attempting to turn on all relays");
pool_size = node1.writeSingleRegister(0x00, 0x0700);
delay(5000);

Serial.println("Attempting to turn off all relays");
pool_size = node1.writeSingleRegister(0x00, 0x0800);
delay(5000);
}

void loop()
{

}

Enter fullscreen mode Exit fullscreen mode




Thanks you !!!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
office_mails_75e56299092d profile image
Office Mails

With the SLA-05VDC-SL-C 5V 30A 1 Channel Relay Module, you may gain strong control. This dependable relay module is perfect for DIY electronics, industrial applications, and home automation because it can easily manage heavy loads. It guarantees smooth operation for your electrical gadgets with a straightforward interface and strong performance. Increase the efficiency of your project right now by investigating the potential of this adaptable relay module.

Check our website:

epro.pk/product/relay-module-sla-0...

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay