Hello all, i am using the a RS485 ultrasonic sensor module "A0221A4". I connected the sensor to a TTL to RS-485 module and connected the module to the arduino uno over pin 10 and 11 to act as RX-TX pins.
I am trying to send a command to the RS485 module to get the distance via softwareserial and failing. is there a specific library or an arduino uno example to use for that sensor. You can find the rs485 command data sheet for that sensor in this link https://www.aliexpress.com/item/1005003691760080.html
Thanks for the reply. As you mentioned i tried connecting the ground together and it was the same thing. Also after sending the 8byte hex code of get_address i am not getting anything as a reply.
N.B. I tried the sensor by directly connecting it to pc vial usb-rs485 adapter and did get a reply from the sensor using a serial monitor software. The modbus hex sent was ff 03 02 00 00 01 90 6c and the received data was 01 03 02 00 01 79 84 which is correct.
The bus transceiver controls RE and DO are not connected. In that case, the transceiver can't change the bus data direction. As well as connecting them, they have to be controlled in software.
yes.
and the transmitter needs to be enabled during transmission. flush() can be called to wait for transmission to complete and the transmitted disabled allowing reception of a response
other questions
presumably GET_ADDRESS [] is the command requesting a response from your sensor. are you sure the sensor expects ASCII chars, if so does each ASCII value need a space between it and the next
why not always process any available data from the sensor (i.e. mySerial)? why make it contingent on requested?
why not print everything received from mySerial, why limit it to 7 characters? if there is a specific end character in the response, consider using Serial.readBytesUntil()
i don't believe that's necessary for the balance pair rs-485 signal. there's no common ground between a USB rs-485 adapter and rs-485 bus
From what the datasheeet of the sensor mentioned, the sensor expects an 8 hex code request and a response of 7 hex codes.
e.g. Host (TX): FF 03 02 00 00 01 90 6C
The sensor replies:
(RX): 01 03 02 00 01 79 84
The space is just for debugging. The actual code that is sent to the sensor is from the SoftwareSerial via "mySerial" variable.
I've connected the RE and DE to pin 5 on the arduino. The code now is the following:
After reading through the AliExpress page and what you've told us so far, one of your problems could to be in this bit of code.
Modbus expects a stream of bytes as it monitors the time between the bytes to spot a new message. Try removing (or commenting out) the 2 Serial.print() commands.
Also note that you can write out the command without the use of the loop by doing:
Yes that was the problem. I should have used write instead of print. The problem was with the print and the RE and DE not being controlled. Thanks alot and here is the working code: