Hello everyone, this is my first post in Arduino Forum, and I am quite a beginner in using RS485 sensors, as I am having some trouble understanding Modbus RTU. Firstly I would like to thank everyone for taking your time to help me in this matter. I really appreciate the guidance and advices given here
I would like to use a RS485 based sensor with my NodeMCU ESP32 by using MAX485 converter.
Below is the wiring diagram for my setup:
And below is the code I used.
#include <ModbusMaster.h>
#define MAX485_DE_RE 4
ModbusMaster node; //object node for class ModbusMaster
//Initialize the ModbusMaster object as node
//ModbusMaster node;
void preTransmission()
{
digitalWrite(MAX485_DE_RE, HIGH);
}
void postTransmission()
{
digitalWrite(MAX485_DE_RE, LOW);
}
void setup()
{
pinMode(MAX485_DE_RE, OUTPUT);
digitalWrite(MAX485_DE_RE, LOW);
Serial.begin(115200);
// Transmission mode: MODBUS-RTU, Baud rate: 9600bps, Data bits: 8, Stop bit: 1, Check bit: no
Serial1.begin(9600);
// Slave address: the factory default is 01H (set according to the need, 00H to FCH)
node.begin(1, Serial1)
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
//Setup ends here
void loop()
{
// The 03H Function Code Example: Read The Atmospheric Temperature, Humidity & Pressure
// Host Scan Order (Slave addr:0x01): 01 03 00 00 00 03 05CB
// Slave Response: 01 03 06 01 21 0164 2728 C76E
// Address of the first register Lo bytes is 00
// Number of registers of Lo bytes is 03
uint8_t result = node.readHoldingRegisters( 0, 3 );
Serial.print("result = ");
Serial.println( result );
if (result == node.ku8MBSuccess)
{
Serial.print("Temperature : ");
Serial.println(node.getResponseBuffer(0));
Serial.print("Humidity : ");
Serial.println(node.getResponseBuffer(1));
Serial.print("Pressure : ");
Serial.println(node.getResponseBuffer(2));
}
Serial.print("\n");
delay(1000);
}
This is what I got from serial monitor:
I am not sure what else to do, I made sure the grounds to be common, I think I used the correct function code and slave address, but I still get result = 226. Im quite stuck right now, maybe I am missing something here, any help is appreciated right now, thank you very much.
Below is the datasheet of the RK330-01B for your reference: