RS485 to TTL communication issue with Arduino nano

Hello everyone,

I am working on a project that intends to read and save values from a wind vane.

Here are the components/references:

I am currently attempting to use the Modbus Master library: GitHub - 4-20ma/ModbusMaster: Enlighten your Arduino to be a Modbus master

The primary issue I am having is interpreting the slave response frame, as no matter which of the addresses I use from the data sheet I am not getting a different output when I rotate the wind vane. I am also not sure if I am reading from the correct register or printing out the desired frame (I am interested in the 360 degree wind direction).

I also understand that there is some example code that comes with the wind vane wiki, however it is not out of the box configured for the nano and it is not obvious to me how I could alter the source code to make it work for that board, hence the use of the other library.

Here is my code:

#include <ModbusMaster.h>

/* 
    Using the RS485 transceiver module, Rx/Tx is hooked up to the hardware
    serial port at 'Serial" to DI (driver input, connected to Tx) and RO 
    (receiver ouput, connected to Rx). 
    The data enable and receiver enable pins are connected as follows:
*/
#define MAX485_DE 4
#define MAX485_RE_NEG 5

// instantiate the 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_RE_NEG, OUTPUT);
  // initialize in receive mode
  digitalWrite(MAX485_RE_NEG, 0);
  digitalWrite(MAX485_DE, 0);

  // Modbus communication runs at 9600 baud
  Serial.begin(9600);

  // Modbus slave ID of wind vane is 2 
  node.begin(2, Serial);

  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}


void loop() 
{
  uint8_t result;
  uint16_t data[7]; // slave response frame for buffer is 7 bytes according to data sheet

  // read wind direction value at register 0x0000
  result = node.readHoldingRegisters(0x0000, 1);
  if (result == node.ku8MBSuccess) 
  {
    //Serial.println("Wind direction of 360 degrees (hex): "); 
    Serial.println(node.getResponseBuffer(0x0));  
  }

  Serial.println();
  delay(1000);


}

And here is the wiring diagram:

Any help would be greatly appreciated, I am very much a beginner to all of this. Thank you in advance!

How much current does the vane draw from the Arduino? The 5V pin is not a good source of power for other devices. It's not likely your vane needs much, but it's a common problem with projects on this forum.

With one low enable and one high enable, those two pins (DE, RE) are most commonly used tied together on one output from the Arduino. You're either listening, or talking, there's no 'third option'.

Can you show the output that you get?

It looks like you are trying to use Serial to both communicate with the weather vane AND send the results back to the Serial Monitor on your PC.

Since your are using a Nano, you only have 1 serial port so use that with your PC and use SoftwareSerial to communicate with the weather vane. Or get a different board that has more than 1 serial port.

Thank you for all of the help. I adjusted my code and wiring according to your suggestions, i.e. shorting DI and RE together to pin 4 on the nano, and adding another serial communication line to allow both communication between the nano and the wind vane and communication between the nano and my computer via the serial monitor. Here is my updated code:

#include <ModbusMaster.h>
#include <SoftwareSerial.h>

// shorting DE and RE together 
#define MAX485_DE_RE 4

// instantiate the ModbusMaster object
ModbusMaster node;

// configure serial communication pins for wind vane
SoftwareSerial mySerial(10,11); // RX, TX

void preTransmission()
{
  digitalWrite(MAX485_DE_RE, 1);
}

void postTransmission() 
{
  digitalWrite(MAX485_DE_RE, 0);
}

void setup()
{
  pinMode(MAX485_DE_RE, OUTPUT);
  // initialize in receive mode
  digitalWrite(MAX485_DE_RE, 0);

  // Serial communication for printing to serial monitor
  Serial.begin(9600);

  // Serial communication for transmitting and receiving data from wind vane
  mySerial.begin(9600);

  // Modbus slave ID of wind vane is 2 
  node.begin(2, mySerial);

  // Callbacks allow us to configure the RS485 transceiver correctly
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
}


void loop() 
{
  uint8_t result;

  // read wind direction value at register 0x0000
  result = node.readHoldingRegisters(0x0000, 1);
  if (result == node.ku8MBSuccess) 
  {
    Serial.print("Wind direction (0 is due north): "); 
    Serial.print(node.getResponseBuffer(0x0)/10.0f); 
    Serial.println(" degrees");
  }

  Serial.println();
  delay(1000);



}

And here is my updated schematic.

And now I am getting the expected output, for example when the vane is aligned with the white dot I measure 0 degrees, rotated 90 degrees to the clockwise I measure 90 degrees, and so on and so forth. Out of curiosity, is the V_in pin a better choice for powering a project, particularly with more devices in general, whether the power is coming in via usb or through an external battery? If so, why would that be the case?

It really depends on your power source. V_in goes into the on board voltage regulator to supply +5V to everything. V_in normally is anything from 6-9V. It must be above 5V so the regulator can function. It is a good choice if you also need to power external things like motors that can take the 9V directly.

If everything connected to the board runs at 5V, then using an external, regulated 5V supply would be the better way to go. In this case, you would feed it directly to the +5V pin on the Nano, not V_in. This is the method required if you need to supply more than a couple hundred milliamps since that is all the onboard regulator can deliver.

The USB connector is a regulated, 5V supply so the power pin connects directly to the +5V within the board, bypassing the regulator. Depending on how much current your USB host (typically your PC) can deliver, this also works.

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