2

I am learning Arduino, a beginner in programming. I want to use Arduino UNO to connect a wind direction sensor RS485. The problem is the output in the serial monitor, the return data from the wind direction sensor is unstable. enter image description here

I need to get the return data from the wind direction sensor to get the wind direction. From the factory manual, they said sent a data to the sensor: 0x01 0x03 0x00 0x00 0x00 0x02 0xC4 0x0B and the sensor will return, like 0x01 0x03 0x04 0x06 0x48 0x00 0xA0 0x7A 0xD5

The wind direction dat at the above 5 and 6 position, the 0x00 0xA0 => 00A0H(HEX)=160 degree.

Tools: Arduino Uno Board

Wind Direction Sensor http://www.jnrsmcu.com/equipment/299.html

RS485 Board (3.3V) https://www.waveshare.com/rs485-board-3.3v.htm

The following is my coding:

#include <SoftwareSerial.h>

SoftwareSerial RS485Serial(10, 11); // RX, TX

void setup()
{
    RS485Serial.begin(4800);   // set the data rate
    Serial.begin(4800);
    Serial.println("Start");

    pinMode(12, OUTPUT);
    digitalWrite(12, HIGH);  // Init Transceiver
}

byte buf[8];

int byteReceived;

void loop()
{
    digitalWrite(12, HIGH);

    byte wind[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B};
    RS485Serial.write(wind, sizeof(wind));
    Serial.flush();
    RS485Serial.flush();
    // Serial.print(wind[0], HEX);
    Serial.println("Send");

    digitalWrite(12, LOW);
    delayMicroseconds(200);

    RS485Serial.readBytes(buf, 8);
    delay(10);
    Serial.println(buf[0], HEX);
    Serial.println(buf[5], HEX);
    Serial.println(buf[6], HEX);
    Serial.flush();
    RS485Serial.flush();
    delay (1000);
}

Connection map: enter image description here

  • Is there any documentation available in English for this sensor? – Edgar Bonet Aug 1 at 19:38
  • It's probably SoffwareSerial not being able to respond fast enough to the incoming data in time. You should hook up a logic analyser and check that the data being sent does in fact differ from the data you are receiving. If so, then you really need to use a development board that has a hardware UART that you can use. I would never ever recommend using SoftwareSerial for anything, ever. Period. Exclamation mark. – Majenko Aug 1 at 21:12
  • You could use SoftwareSerial for sending the outbound packet, and the hardware UART for both receiving the response (on RX) and talking to the PC (on TX). May be not be elegant, but it should be more reliable that trying to read from SoftwareSerial. – Edgar Bonet Aug 2 at 7:47
  • @Majenko recommend to us, your peons, a well-rounded tactical budget logic analyzer (case not necessary) – user2497 Sep 24 at 15:37
  • I use a little usb dongle I got from Alibaba or somewhere for $5... – Majenko Sep 24 at 15:38
0

I would suggest you simplify your setup to the point where you have a 'working baseline configuration', and then start building the system up one step at a time from there.

Start by eliminating the SoftwareSerial interface as a possible contributor to the problem. Get an Arduino Mega with multiple hardware serial ports.

Next, verify that you can send characters to the selected hardware serial port and receive those same characters back when the Txn and Rxn lines (the 'n' here is the selected hardware serial port number) are connected via a jumper wire. This is called a 'loopback' configuration and is a basic serial comms troubleshooting technique (software serial ports don't support simultaneous transmit and receive, so they don't support loopback testing). Don't proceed until you have this step completed successfully.

Next, get a second RS-485 module and extend your previous 'loopback' configuration with the hardware serial port on the Mega to include the two RS-485 modules. Then you should be able to send characters out to one RS-485 module and receive the same characters back from the other, by connecting TXn to DI on one of the two RS-485 modules, and Rxn to DO on the other.

At this point you will have verified all the communications connections, so you can be confident that whatever you send will actually show up at the DO port of the RS-485, and whatever shows up at the DI port of the RS-485 will actually make it back to your serial console. Now you can connect the sensor and figure out any issues do to data formatting, command sequences, and the like.

This basic 'cut the problem in half' building block technique will work on any system, large or small, simple or complex.

Frank

0
  • I don't see any connection between the sensor's GND and your Arduino's. This is likely the problem if they're not connected, since a common GND is necessary even with RS-485.
  • Check the sensor's power supply, make sure it's actually within the advised 10-30 V range. Also check the polarity of the differential lines.
  • Have you tried using the provided configuration software?
  • flush() does nothing for SoftwareSerial ports since they have no TX buffer, so you can just get rid of those lines, including the unnecessary Serial.flush()
  • Finally, your device datasheet says to wait for at least 200 milliseconds not microseconds, before checking for a response. Though it really shouldn't matter since you're using readBytes() after that, which waits for a second anyways by default, before giving up. Instead, use a bigger buffer, say 20 bytes, then try to read just 8 (the expected length) into it with readBytes():

    byte buf[20];
    int rlen = RS485Serial.readBytes(buf, 8);
    

Then try to print rlen which tells you how many bytes were actually read and also print the whole response to verify the packet format is as it should be. You should get enough info from this to at least eliminate a bunch of possible problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.