Issue while trying a connexion between 2 MKR 485 SHIELDS

Hi,
I want to connect two MKR 485 SHIELDS (one as Master and the other as Slave) but I have a "timed out error" from the lasError function...
My wiring is :
MASTER | SLAVE
Y ======>A
Z ======>B
ISO GND ======>GND pin of MKR 485 Shield

The switch state is :
MASTER | SLAVE
Y-Z/ON ===Y-Z/OFF
HALF/ON
A-B/Off===A-B/ON

to check this out I have written this code :

MASTER CODE:

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

void setup() {

  Serial.begin(9600);
  if (!ModbusRTUClient.begin(9600)) {
    Serial.println("Failed to start Modbus RTU Client!");
    while (1);
  }
}

void loop() {
  if (!ModbusRTUClient.requestFrom(0x01, HOLDING_REGISTERS, 0x00F3, 1)) {           // make the call to the register
    Serial.print("failed to read value! ");
    Serial.println(ModbusRTUClient.lastError());                                  // error handler
  } else {
    uint16_t value = ModbusRTUClient.read();                                       // read data from the buffer
  }
  return value;
}

SLAVE CODE:

#include <ArduinoRS485.h>
#include <ArduinoModbus.h>

void setup() {
  Serial.begin(9600);
  while (!Serial);

  ModbusRTUServer.begin(0x01,9600);
}

void loop() {
    ModbusRTUServer.holdingRegisterWrite(0x00F3, 15 );
    ModbusRTUServer.holdingRegisterWrite(0x00F9, 10 );
    ModbusRTUServer.holdingRegisterWrite(0x00FD, 8 );
    delay(1000);
    
}

Thanks in advance for your help!

From the switch setting I think you are intending to operate your RS485 in half of full duplex mode? Half duplex is what most of the simpler boards use (with a MAX485 chip). The datasheet for the MAX3157 chip says that for half duplex you connect Y to Y, and Z to Z, leaving A and B disconnected.

Ho thank you for your reply, I'll check this and give you update!

Thank you!

Hi! I've test what you said but I didn't manage to make it work...
Here is what I've done on the picture right below:
(I've tested A to A and B to B, I've just re placed Y to A and Z to B finally ISOGND to GND)

I think the DIP switch settings are correct in your photo. For half duplex it should be Y-Y and Z-Z. I think ISOGND goes to either the other device ISOGND or the other device GND.

I did find this tutorial: Sending data between two MKR 485 shields that shows a setup that looks very much like yours. It has example sketches for Tx and Rx.

I'm somewhat confused by the suggested wiring in that tutorial as it has Y-A and Z-B. However, if i'm reading the MAX3157 datasheet correctly, then when the chip is is half duplex mode, it is not listening on A & B(1). It internally routes the Y & Z signals into its receiver.

I would suggest you follow the above tutorial and see if that works for you. If it doesn't then i'd try wiring Y-Y and Z-Z and see what happens then.

EDIT: (1) See the block diagram in the datasheet (page 11 on mine) where the H/F (Half/Full) signal is used to switch the Rx inputs from A+B to Y+Z in half duplex mode.

HI! I tested what you've suggested to me and I didn't managed to make it work.... I don't understand.
Reading the datasheet and don't have any clues :disappointed_relieved:...

Hmmm, ok. Let's try something really simple that doesn't use Modbus or any software serial code. Hopefully this really simple code will work and give you confidence that the electrical wiring between the 2 boards is right.

There's 2 pieces of code, one is a transmitter and the other is the receiver:

  • The transmitter code simply drives the RS485 bus high then low toggling every 2 seconds.
  • The receiver simply reads the state of the RS485 bus and uses the built-in LED to show high or low.

I think you can see the built-in LED on the lower board when there is a shield on top!

TX Code:

// TRANSMITTER
// Simple code to toggle the RS485 lines high/low

// MKR485 shield pins
#define MAX3157_DE      A6
#define MAX3157_RE_NEG  A5
#define MAX3157_DI      14
#define MAX3157_RO      13

uint8_t ledState = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(MAX3157_RE_NEG, OUTPUT);
  pinMode(MAX3157_DE, OUTPUT);
  pinMode(MAX3157_DI, OUTPUT);

  // put MAX3157 into Tx mode
  digitalWrite(MAX3157_RE_NEG, 1);
  digitalWrite(MAX3157_DE, 1);
}

void loop() {
  if (ledState == 0) {
    ledState = 1;
    digitalWrite(MAX3157_DI, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
  } else {
    ledState = 0;
    digitalWrite(MAX3157_DI, LOW);
    digitalWrite(LED_BUILTIN, LOW);
  }
  delay(2000);
}

Rx Code:

// RECEIVER
// Simple code to toggle the built-in LED based on RS485 lines

// MKR485 shield pins
#define MAX3157_DE      A6
#define MAX3157_RE_NEG  A5
#define MAX3157_DI      14
#define MAX3157_RO      13

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(MAX3157_RE_NEG, OUTPUT);
  pinMode(MAX3157_DE, OUTPUT);
  pinMode(MAX3157_RO, INPUT);

  // put MAX3157 into Rx mode
  digitalWrite(MAX3157_RE_NEG, 0);
  digitalWrite(MAX3157_DE, 0);
}

void loop() {
  if (digitalRead( MAX3157_RO ) == LOW ) {
    digitalWrite(LED_BUILTIN, LOW);
  } else {
    digitalWrite(LED_BUILTIN, HIGH);
  }
  delay(250);
}

You should see the built-in LEDs on both boards toggle on and off together. There might be a slight delay on the receiver LED but it should follow the the transmitter LED.

Now, I don't have any MKR boards here to test this on, but it does work on an UNO so it should work with your MKR485 boards.

If it does work, then we know that electrically, the 2 boards are connected correctly.

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