PLC Modbus Tutorial: How to get temperature with a Dallas sensor

PLC Modbus protocol & Industrial Arduino based PLC
July 30, 2021 by
PLC Modbus Tutorial: How to get temperature with a Dallas sensor
Boot & Work Corp. S.L., Fernandez Queralt Martinez

Introduction

In the previous post, we learned how to connect our Dallas DS18B20 sensor to an Ardbox, an Arduino based PLC automation.

In this post, we are going to learn how to get temperature with the Arduino IDE using the Modbus protocol.

Related Links

How to

Read Dallas temperature sensor and data log in µSD Card with Arduino automation

Read 

How to

Connect temperature sensor to industrial Raspberry PLC

Read 

How to

Parse the temperature with Node-RED and Raspberry Pi industrial

Read 

How to

Control temperature with Dallas DS18B20 sensor and Arduino PLC industrial

Read 

How to

Get the temperature with an industrial Raspberry PLC

Read 

How to

Program the 10 I/Os ESP32 industrial controller PLC via WiFi

Read 

Explanation

Modbus

According to Wikipedia, Modbus is a communications protocol located at levels 1, 2 and 7 of the OSI Model, based on the master/slave (RTU) or client/server (TCP/IP) architecture, designed in 1979 by Modicon for its range of programmable logic controllers (PLCs).

Converted into a de facto standard communications protocol in the industry, it is the one with the highest availability for the connection of industrial electronic devices. 

Let's see some relevant information about Modbus:

  • Each device on the Modbus network has a unique address.
  • Any device can send Modbus commands, although it is usually allowed only by a master device.
  • Each Modbus command contains the address of the device receiving the order.
  • All devices receive the frame, but only the recipient executes it (except for a special mode called "Broadcast").
  • Each of the messages includes redundant information that ensures its integrity at reception.
  • The basic Modbus commands allow you to control an RTU device to modify the value of its registers or to request the content of these registers.
      Modbus - Exmplanation - Modbus Tutorial: How to get temperature with a Dallas sensor

Arduino Automation Libraries

Now we know a little more about Modbus, we are going to program our Arduino based PLC industrial!

As we know, the Dallas DS18B20 temperature sensor works with the 1-Wire protocol, so we will need two libraries to be able to get the temperature:

  1. Arduino-Temperature-Control-Library 

  2. OneWire >>> 

For the Modbus protocol, we will need the library from Industrial Shields:

Arduino Code

Once the libraries are already installed in our PC, let's do the following:
   · Include the libraries we need.
   · Define the pin with the One Wire bus. In our case, Pin 2.
   · Define the RS485 baud rate. In our case 9600UL.
   · Define the number of analog inputs. In our case, 1.
   · Create a Modbus slave.

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <RS485.h>
#include <ModbusRTUSlave.h>
//Sensor Pin: 2
#define ONE_WIRE_BUS 2
#define RS485_RATE 9600UL
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensor(&oneWire);
#define numAnalogInputs 1
uint16_t analogInputs[numAnalogInputs];
//Slave Address: 1
ModbusRTUSlave modbus(RS485, 1);

In the setup function, we are going to do the following:

   · Initialize the serial port, the RS485 and the sensor.

   · Set input registers to the slave instance.

void setup(void)
{
  // Start serial port
  Serial.begin(9600);
  // Start up the library
  sensor.begin();
  RS485.begin(RS485_RATE, HALFDUPLEX, SERIAL_8N1);
  modbus.begin(RS485_RATE);
  modbus.setInputRegisters(analogInputs, numAnalogInputs);
}

Finally, in the loop function we are going to write the following in order to request the temperature every, 10000 milliseconds while the PLC Modbus instance is constantly updating to set the input registers.

void loop(void) {
  static uint32_t lastStatusTime = millis();
  
  if (millis() - lastStatusTime > 10000) {
    sensor.requestTemperatures(); // Send the command to get temperatures
    float temp = sensor.getTempCByIndex(0);
    analogInputs[0] = temp * 100;
    Serial.println(analogInputs[0]);
    lastStatusTime = millis();
  }
  modbus.update();
}

Now your code is done, download the Industrial Shields boards as shown HERE >>> . Then, just select your board, model and port from the Tools menu.

In our case:

  • Board: Industrial Shields boards > Ardbox family.
  • Model: Ardbox Analog HF+ w/ HW RS-485.


Once everything is ready, upload your sketch, open the serial monitor, and check out the temperature from your Dallas sensor!


​Search in our Blog

PLC Modbus Tutorial: How to get temperature with a Dallas sensor
Boot & Work Corp. S.L., Fernandez Queralt Martinez July 30, 2021
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>