Hello dear Arduino community, apologies if this has been asked before and I know the title seems very simple but I have read a lot of the forums relating to ESP32 coding with RS485 sensors and they always seem to use the MAX485 converter while I'm using the XY-485. I have been going crazy trying to figure out why I keep on encountering the 0xE2 error code.
I am currently using the ESP32-WROOM-32, with an RD-DO-F-04 from Honde, an XY485 TTL module as well as a separate 12V power source to power the sensor with.
I apologise if anything is lacking and please ask me for whatever you may need to figure out the issues.
Here is my coding:
#define RX 16 // Pin for UART2
#define TX 17 // Pin for UART2
ModbusMaster node; // instantiate ModbusMaster object
void preTransmission(){
delay(10);
}
void postTransmission(){
delay(10);
}
void setup() {
Serial.begin (115200); // Initializes Modbus communication baud rate, also needs to match the Serial Monitor's baud rate
Serial2.begin (9600, SERIAL_8N1, RX, TX);
Serial2.setTimeout(1000);
Serial.println ("Serial Txd is on pin: " +String(TX));
Serial.println ("Serial Rxd is on pin: " +String(RX));
node.begin(1, Serial2); // Modbus slave ID 1 for the DO Sensor
delay(1000);
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
}
void loop() {
uint8_t j, result;
uint16_t data [4];
// node.setTransmitBuffer (0, lowWord(i));
// node.setTransmitBuffer (1, highWord(i));
// result = node.writeMultipleRegisters (0,2);
result = node.readHoldingRegisters (0x0000,4); // Reads 4 registers starting from address 0x0000.
Serial.print("Read result: 0x");
Serial.println (result, HEX);
if (result ==node.ku8MBSuccess)
{
Serial.print ("DO:");
Serial.println ((node.getResponseBuffer(0) + node.getResponseBuffer(1)) /100);
Serial.print ("Temperature:");
Serial.println ((node.getResponseBuffer(2) + node.getResponseBuffer (3)) /100);
}
else
{
Serial.println ("Sensor is not responding.");
}
delay (2000);
}
Code tags are three back ticks (```) on their own line before the code and after the code. Please edit your post and fix it.
Where / when do you get the error code. If it's during upload, please enable verbose output during upload and disable verbose output during compilation under file → preferences, perform the upload and past the output here (again using code tags).
I would start by disconnecting everything and check if you can upload your code.
The MAX485 is a 5V device so might not work with the 3.3V ESP32; What are the specs of the XY-485?
E2 error means timeout, you don't get any response from sensor.
Could be wrong wiring, wrong serial parameters or wrong slave address.
Post your wiring here. Do you have some protocol sheet for the sensor?
Can you post some images of your project?
So we can see your component layout.
Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.
Good afternoon, thank you for the response, here is the layout of my project as well as the schematic diagram created on Canvas. Apologies if it looks haphazard, but the schematic diagram should be accurate.
I already replied to @TomGeorge about the wiring, but here are the address registers for the DO and Temperature values as well as the full manual, I hope that helps.
Hello Tom, I plug in the ESP32 into my laptop using a data cable, the cable works because it's able to upload the code. The 485 module is powered up with the Vin pin on the ESP32, which is 5V last time I measured with a multimeter.
Your parameters look correct.
But your image presents that xy-485 TX is connected to esp RX (and rx to tx) and that is incorrect. Rs485 converter is just translator, so tx-tx, rx-rx.
Also, many devices don't respect standard A/B polarity, so if after correcting your serial wiring you still get only E2, try to swap A and B.