IO Expansion Shield For Arduino (V5) (SKU: DFR0088). RS485 interface.

In the previous article, I reviewed several Arduino RS485 shields. Some of them are quite expensive, more than 20 USD. In this article, I tested one of Arduino RS485 shields, which costs about 3 USD.

I ordered at Aliexpress IO expansion shield with RS485 interface and met with some problem while trying to run it. There is no description of the board at Aliexpress, unfortunately. I’ve found a description at the web site of one of the supplier. Since the test example published at the web site doesn’t work, I thought the problem with the board and had tried to determine the reason, checking by ohmmeter and drew connections line (in Microsoft PowerPoint 🙂 ) by red.

Connections line to MAX485 on the IO Espansion Shield DFR0088
The prinicipal scheme MAX485 on IO Espansion Shield DFR0088

As a RS485 Modbus device, I used termal and humidity sensor from my previouse article. Programming code for Arduino:

#include "ModbusMaster.h"

/*!
  IO Expansion Shield For Arduino (V5) (SKU: DFR0088) RS485 interface.

  We're using a MAX485-compatible RS485 Transceiver.
  Rx/Tx is hooked up to the hardware serial port at 'Serial'.
  The Data Enable (DE) and Receiver Enable (RE) pins are hooked up as follows:
*/
#define MAX485_RE_NEG  2 //RS485 has a enable/disable pin to transmit or receive data. Arduino Digital Pin 2 = Rx/Tx 'Enable'; High to Transmit, Low to Receive
#define Slave_ID       1 

// instantiate ModbusMaster object
ModbusMaster node;

void preTransmission()
{
  digitalWrite(MAX485_RE_NEG, HIGH); //Switch to transmit data
}

void postTransmission()
{
  digitalWrite(MAX485_RE_NEG, LOW); //Switch to receive data
}

void setup()
{
  pinMode(MAX485_RE_NEG, OUTPUT);

  // Init in receive mode
  digitalWrite(MAX485_RE_NEG, LOW);

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

  // Modbus slave ID 1
  node.begin(Slave_ID, Serial);

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

void loop()
{
  // Read 2 registers starting at 0x01
  uint8_t result = node.readInputRegisters(0x01, 2);
  if (getResultMsg(result))
  {
    Serial.println();
    
    double res_dbl = node.getResponseBuffer(0)/10;
    String res = "Temperature: " + String(res_dbl) + " C\r\n";
    res_dbl = node.getResponseBuffer(1)/10;
    res += "Humidity: " + String(res_dbl) + " %";
    Serial.println(res);
 }
 delay(10000);
}

bool getResultMsg(uint8_t result)
{
  String tmpstr2;

  switch (result) {
  case node.ku8MBSuccess:
    return true;
    break;
  case node.ku8MBIllegalFunction:
    tmpstr2 = "Illegal Function";
    break;
  case node.ku8MBIllegalDataAddress:
    tmpstr2 = "Illegal Data Address";
    break;
  case node.ku8MBIllegalDataValue:
    tmpstr2 = "Illegal Data Value";
    break;
  case node.ku8MBSlaveDeviceFailure:
    tmpstr2 = "Slave Device Failure";
    break;
  case node.ku8MBInvalidSlaveID:
    tmpstr2 = "Invalid Slave ID";
    break;
  case node.ku8MBInvalidFunction:
    tmpstr2 = "Invalid Function";
    break;
  case node.ku8MBResponseTimedOut:
    tmpstr2 = "Response Timed Out";
    break;
  case node.ku8MBInvalidCRC:
    tmpstr2 = "Invalid CRC";
    break;
  default:
    tmpstr2 = "Unknown error: " + String(result);
    break;
  }
  Serial.println(tmpstr2);
  return false;
}

When uploading the sketch to the board sometimes error occured:

avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x00

It can be solved either removing IO expansion shield before “Upload” the sketch or removing right RS485 jumper, to remove connection to RX with MAX485 chip.

Spread the love
This entry was posted in IT Recipies and tagged , , , . Bookmark the permalink.

Leave a Reply