bug code

hello guys hope you are doing well.
well am trying to read data from pH soil sensor using arduino uno and MAX485(MODBUS COMMUNICATION) hopefully it work and i can see the measure of the PH soil in serial monitor but it show me some weird characters and i want to eliminate it but i don't now how, please can anyone could help me.

//the code
#include <ModbusMaster.h>

#define MAX485_DE 3
#define MAX485_RE_NEG 2

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);

void postTransmission()
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);

void setup()
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);

// Modbus communication runs at 9600 baud

// Modbus slave ID
node.begin(6, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly

bool state = true;

void loop()
uint8_t result;
uint16_t data[8];

// Toggle the coil at address (Manual Load Control)
result = node.writeSingleCoil(0x03, state);
state = !state;

// Read 16 registers starting at 0x3100)
result = node.readInputRegisters(0x40000, 2);
if (result == node.ku8MBSuccess)
Serial.print("La valuer du PH: ");



You’re using Serial for debug and Modbus?


Expecting 10 places of decimals is … optimistic!

Please remember to use code tags when posting code.

presumably your pre/post transmission functions are enabling the 485 transmitter.

when disabling the transmitter, the code needs to wait for transmission to complete, not just that buffering the outgoing bytes is done.

the Serial interface has a flush function that returns when transmission is complete. i would try calling flush() before disabling the transmitter

You had better use SoftwareSerial Instead of Serial for Modbus because Serial is used to communicate with Serial Monitor

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.