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

By: ( +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 ( ( 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 ( (, 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;


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

  // 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];


  // 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


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) {

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.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.

« Make Live: PiKon Raspberry Pi Telescope Matt Lucas (Nardole) answers questions about Doctor Who Season 10 »
2016 Election Acer C720 Ad block AkashaCMS Amazon Amazon Kindle Amazon Web Services America Amiga Android Anti-Fascism AntiVirus Software Apple Apple Hardware History Apple iPhone Apple iPhone Hardware April 1st Arduino ARM Compilation Artificial Intelligence Astronomy Astrophotography Asynchronous Programming Authoritarianism Automated Social Posting AWS DynamoDB AWS Lambda Ayo.JS Bells Law Big Brother Big Finish Bitcoin Mining Black Holes Blade Runner Blockchain Blogger Blogging Books Botnet Botnets Cassette Tapes Cellphones China China Manufacturing Christopher Eccleston Chrome Chrome Apps Chromebook Chromebox ChromeOS CIA CitiCards Citizen Journalism Civil Liberties Clinton Cluster Computing Command Line Tools Comment Systems Computer Accessories Computer Hardware Computer Repair Computers Cross Compilation Crouton Cryptocurrency Curiosity Rover Currencies Cyber Security Cybermen Daleks Darth Vader Data backup Data Storage Database Database Backup Databases David Tenant DDoS Botnet Detect Adblocker Developers Editors Digital Photography Diskless Booting Disqus DIY DIY Repair DNP3 Do it yourself Docker Docker MAMP Docker Swarm Doctor Who Doctor Who Paradox Doctor Who Review Drobo Drupal Drupal Themes DVD E-Books E-Readers Early Computers Election Hacks Electric Bicycles Electric Vehicles Electron Emdebian Encabulators Energy Efficiency Enterprise Node EPUB ESP8266 Ethical Curation Eurovision Event Driven Asynchronous Express Face Recognition Facebook Fake News Fedora VirtualBox File transfer without iTunes FireFly Flickr Fraud Freedom of Speech Gallifrey git Github GitKraken Gitlab GMAIL Google Google Chrome Google Gnome Google+ Government Spying Great Britain Heat Loss Hibernate Hoax Science Home Automation HTTP Security HTTPS Human ID I2C Protocol Image Analysis Image Conversion Image Processing ImageMagick In-memory Computing InfluxDB Infrared Thermometers Insulation Internet Internet Advertising Internet Law Internet of Things Internet Policy Internet Privacy iOS Devices iPad iPhone iPhone hacking Iron Man iTunes Java JavaScript JavaScript Injection JDBC John Simms Journalism Joyent Kaspersky Labs Kindle Kindle Marketplace Lets Encrypt LibreOffice Linux Linux Hints Linux Single Board Computers Logging Mac Mini Mac OS Mac OS X Machine Learning Machine Readable ID macOS MacOS X setup Make Money Online March For Our Lives MariaDB Mars Mass Violence Matt Lucas MEADS Anti-Missile Mercurial MERN Stack Michele Gomez Micro Apartments Microsoft Military AI Military Hardware Minification Minimized CSS Minimized HTML Minimized JavaScript Missy Mobile Applications Mobile Computers MODBUS Mondas Monetary System MongoDB Mongoose 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 npm NVIDIA NY Times Online advertising Online Community Online Fraud Online Journalism Online Photography Online Video Open Media Vault Open Source Open Source Governance Open Source Licenses Open Source Software OpenAPI OpenVPN Palmtop PDA Paywalls Personal Flight Peter Capaldi Photography PHP Plex Plex Media Server Political Protest Postal Service Power Control Privacy Production use Public Violence Raspberry Pi Raspberry Pi 3 Raspberry Pi Zero ReactJS Recaptcha Recycling Refurbished Computers Remote Desktop Removable Storage Republicans Retro Computing Retro-Technology Reviews RFID Right to Repair River Song Robotics Rocket Ships RSS News Readers rsync Russia Russia Troll Factory Russian Hacking Rust SCADA Scheme Science Fiction SD Cards Search Engine Ranking Season 1 Season 10 Season 11 Security Security Cameras Server-side JavaScript Serverless Framework Servers Shell Scripts Silence Simsimi Skype SmugMug Social Media Social Media Warfare Social Networks Software Development Space Flight Space Ship Reuse Space Ships SpaceX Spear Phishing Spring Spring Boot Spy Satellites SQLite3 SSD Drives SSD upgrade SSH SSH Key SSL Stand For Truth Strange Parts Swagger Synchronizing Files Telescopes Terrorism The Cybermen The Daleks The Master Time-Series Database Torchwood Total Information Awareness Trump Trump Administration Trump Campaign Ubuntu Udemy UDOO US Department of Defense Virtual Private Networks VirtualBox VLC VNC VOIP Vue.js Web Applications Web Developer Resources Web Development Web Development Tools Web Marketing Website Advertising Weeping Angels WhatsApp William Hartnell Window Insulation Windows Windows Alternatives Wordpress World Wide Web Yahoo YouTube YouTube Monetization