Using an Arduino to read a simple MODBUS/RTU RS-485 temperature sensor

By: (plus.google.com) +David Herron; Date: June 18, 2017

Tags: MODBUS » Arduino

Arduino's are very popular for DIY or IoT projects involving a small computerized controller board, some GPIO pins to read inputs from the environment or to control devices, with optional communication with remote services. Theoretically an Arduino, or for that matter larger computers like the Raspberry Pi, can replace the PLC devices commonly used in industrial control applications. Why spend several hundred dollars on an expensive PLC device when the same goal can be achieved more flexibly and inexpensively with newfangled embedded computers like the Arduino?

A key to this is for an Arduino (or other embedded control computer) to interface with MODBUS and other protocols used in industrial settings. In this article we'll go over using an RS-485 Shield for Arduino, using it to communicate with the simple MODBUS temperature sensor we're using.

The libraries we'll discuss in this article enable using the Arduino as either MODBUS Master or Slave. The last is interesting considering the huge variety of sensors available for Arduino. Consider a simple energy monitoring project - One can interface a CT (current transformer) to measure the current in a circuit, and directly measure the voltage using an analog input, a Sketch could read that data from the sensor, and make it available via MODBUS. An Arduino could be a very simple and inexpensive device in a MODBUS network.

Elsewhere we've used this same temperature sensor

This device is cheap enough that it's a small price-of-entry to experimenting with MODBUS on the Arduino.

To get started with Arduino and see how to wire up the LCD display being used, see:

Getting the RS-485 shield and other hardware

The Arduino cannot natively connect with an RS485 network, but can do so with this shield. You must already have an Arduino, if not consider Arduino Starter Kit - English Official Kit With 170 Page Book

RS232/RS485 Shield for Arduino
RS232/RS485 Shield for Arduino:

The shield is inexpensive and slots directly onto the Arduino. Once you get the RS485 shield, go ahead and slide it on top of the Arduino.

This project also assumes you have the LCD monitor we discussed in Displaying text on LCD screen from the Arduino UNO

Once you have the parts assembled, it will look something like the picture at the top of the article. That picture shows the Arduino connected via USB (the blue cable) to my laptop, the temperature sensor connected to the RS485 port, and the LCD display connected to the GPIO on the RS485 shield.

Installing the ModbusMaster library

In this project we want the Arduino to be a MODBUS Master device, and the temperature sensor will be the Slave device.

The ModbusMaster library ( (github.com) https://github.com/4-20ma/ModbusMaster) does the job. With it you connect to MODBUS devices and make requests either retrieving or setting values. This library is easily installable via the Arduino IDE Library Manager, simply search for ModbusMaster. At the Github project you'll find additional examples and documentation.

Another library, Modbus Master-Slave ( (github.com) https://github.com/smarmengol/Modbus-Master-Slave-for-Arduino), can make an Arduino act as a MODBUS slave device. For example you could attach a bunch of sensors, write a Sketch to read values from the sensors, making their values available over the MODBUS protocol to MODBUS master devices.

An application to read a MODBUS temperature sensor, displaying on the LCD screen

Close-up of wiring the temperature sensor to the Arduino RS485 shield

It is very simple to wire the temperature sensor to the RS485 shield. The shield conveniently provides the +5 volts and Ground required to power the sensor. Simply match the wires from the sensor to the clearly labeled connections on the shield.

In the Arduino IDE, start a new Sketch. Name it something memorable like LCD_MODBUS_Temp_Sensor_01.

// include the library code:
#include <LiquidCrystal.h>
#include <ModbusMaster.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// instantiate ModbusMaster object
ModbusMaster node;

We'll be taking the program section by section, so keep appending to the same Sketch. In this section we're setting up the LiquidCrystal and ModbusMaster libraries.

// variable
int tp_lcd;
int tp_modbus;
int tp_status;
uint32_t i;

These global variables will hold information we'll display on the LCD. The value in tp_lcd is what's actually to be displayed, while the value in tp_modbus is what was read from the MODBUS device, and tp_status contains the status flag from when that value was read.

void setup() {  
  lcd.begin(20, 4);   // init display

  // init vars
  tp_lcd = 0;
  tp_modbus = 0;
  i = 0;

  DisplayCurrentValues();

  // use Serial (port 0); initialize Modbus communication baud rate
  Serial.begin(9600);

  // communicate with Modbus slave ID 255 over Serial (port 0)
  node.begin(1, Serial);
}

The setup function is run once, of course. We initialize the display parameters and the global variables. The DisplayCurrentValues will be shown later, and it takes care of displaying values on the screen. Then we set up the RS485 port for use with MODBUS so we can read the temperature sensor.

void loop() {
  // read modbus data
  uint8_t j, result;
  uint16_t data[6];

  i++;

  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));

  // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));

  // read holding register - temperature
  result = node.readInputRegisters(1, 1);
  if (result == node.ku8MBSuccess) {
      tp_modbus = node.getResponseBuffer(0);
  }
  tp_status = result;

  // display data on LCD
  CheckForDataChange();

  delay(1000);
}

Remember that loop is repeatedly executed, and the delay(1000) at the bottom means this will be executed once a second. In the middle we readInputRegisters to retrieve the temperature value from the sensor. Remember that in this sensor, the value is stored at input register 1, in centigrade, but multiplied by 100.

We check the status because I've seen flaky results with this temperature sensor. In CheckForDataChange ... well, let's just go ahead and look at that function.

void CheckForDataChange() {
  boolean data_has_changed = true;

  if (tp_lcd != tp_modbus) {
    data_has_changed = true;
    tp_lcd = tp_modbus;
  }

  if (data_has_changed == true) {
    DisplayCurrentValues();
  }
}

Remember that tp_lcd is the value to display on the LCD, and tp_modbus is what was read from the sensor. If they're not equal then the data has changed, and at the bottom of the function we call DisplayCurrentValues if it is different.

In other words, the function does what it bills itself to do -- it checks if any data has changed, and if so it calls DisplayCurrentValues.

void DisplayCurrentValues() {
  String tmpstr;
  String tmpstr2;
  double tp_dbl = tp_lcd / 100;

  lcd.clear();        // clear display

  if (tp_status == node.ku8MBIllegalFunction) {
    tmpstr2 = "IllFunc";
  } else if (tp_status == node.ku8MBIllegalDataAddress) {
    tmpstr2 = "IllAddr";
  } else if (tp_status == node.ku8MBIllegalDataValue) {
    tmpstr2 = "IllValue";
  } else if (tp_status == node.ku8MBSlaveDeviceFailure) {
    tmpstr2 = "SlaveFAIL";
  } else if (tp_status == node.ku8MBInvalidSlaveID) {
    tmpstr2 = "InvalSlave";
  } else if (tp_status == node.ku8MBInvalidFunction) {
    tmpstr2 = "InvalFunc";
  } else if (tp_status == node.ku8MBResponseTimedOut) {
    tmpstr2 = "TIME";
  } else if (tp_status == node.ku8MBInvalidCRC) {
    tmpstr2 = "CRC";
  } else {
    tmpstr2 = String(tp_dbl);
  }
  tmpstr = "Temperature: "+ tmpstr2 +"C";
  lcd.setCursor(0,0);
  lcd.print(tmpstr);

  lcd.setCursor(0,1);
  lcd.print("Time: "+ String(millis() / 1000));
}

Finally we get to see DisplayCurrentValues. In this function we display the temperature and the timestamp (seconds) since the Arduino was booted. If an error occurred, an error code is printed instead.

Loading the program into the Arduino

The RS485 shield gives us an extra step to perform whenever uploading a program. To do the upload, we must first set the UART switch on the shield to Soft Serial. With it in Soft Serial, first press the Verify button then the Upload button in the IDE. If all went well, the program will compile and then successfully upload.

As soon as it does the Arduino display will begin showing the display. Except it will say "TIME" in the Temperature field, because the switch is set to Soft Serial. It must be reset to UART in order to communicate with the temperature.

As soon as this switch is set to UART the display will begin showing the temperature.

What's next

As I said - the Arduino can act as a MODBUS device itself. There's a whole slew of sensor gadgets available for Arduino, as well as relay boards, and motor controllers, and on and on. It's possible to develop custom MODBUS devices of many kinds based on Arduino's.

« Colossus & Other Early Computers Matt Lucas (Nardole) answers questions about Doctor Who Season 10 »
2016 Election Acer C720 Ad block AkashaCMS Amiga Android Anti-Fascism Apple Apple Hardware History Apple iPhone Hardware April 1st Arduino ARM Compilation Astronomy Asynchronous Programming Authoritarianism Automated Social Posting Bells Law Big Brother Black Holes Blade Runner Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones Christopher Eccleston Chrome Chrome Apps Chromebook Chromebooks Chromebox ChromeOS CIA CitiCards Civil Liberties Clinton Cluster Computing Command Line Tools Computer Hardware Computer Repair Computers Cross Compilation Crouton Curiosity Rover Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Digital Photography DIY DIY Repair Docker Doctor Who Doctor Who Paradox Drobo Drupal Drupal Themes DVD Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Enterprise Node ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Facebook Fake News File transfer without iTunes FireFly Fraud Freedom of Speech Gallifrey git Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Home Automation HTTPS I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick InfluxDB Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man Iternet of Things iTunes Java JavaScript JavaScript Injection JDBC John Simms Joyent Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac OS Mac OS X Make Money Online MariaDB Mars Matt Lucas MEADS Anti-Missile Mercurial Michele Gomez Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications MODBUS Mondas Monty Python MQTT Music Player Music Streaming MySQL NanoPi Nardole NASA Net Neutrality Node Web Development Node.js Node.js Database Node.js Testing Node.JS Web Development Node.x North Korea Online advertising Online Fraud Open Media Vault Open Source Governance Open Source Software OpenAPI OpenVPN Personal Flight Peter Capaldi Photography PHP Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero Recycling Remote Desktop Republicans Retro-Technology Reviews Right to Repair River Song Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Scheme Science Fiction Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Shell Scripts Silence Simsimi Skype Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spring Spring Boot SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Ubuntu UDOO Virtual Private Networks VirtualBox VLC VNC VOIP Web Applications Web Developer Resources Web Development Web Development Tools Weeping Angels WhatsApp Wordpress YouTube