ESP32 with RS485 16 Channel Relay Module Connection and Code
Tags: esp32, iot, rs485, modbus
Published: 2023-08-18 15:41:17

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfiez893n9x3q0n09s3j.jpg)


### Code

```c
#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()
{
 
}

```

#### Thanks you !!!