Temperature and humidity sensor

This is a high precision temperature and humidity sensor module. It has a high precision probe that guarantees the products with excellent measurement performance. Thanks to this, it is ideal for real-time monitoring of multiple site environment. On this page you can learn how to connect this sensor to your PC and get its measured data using the Modbus RTU protocol.

temperature sensor
Figure 1 - Modbus temperature sensor

Connect the temperature sensor to PC

To connect the device to your computer you will need an USB to RS485 converter. The following figure shows how you can attach the sensor and to the computer with it. After you have connected them together you need to plug the USB of the converter to your PC and it will detect it as a serial COM port. Note that the device will perform the initialization for 2 seconds after it have turned on. Then it will be able to receive the Modbus messages and will execute them according to the master (your PC) commands.

modbus temperature sensor wiring diagram
Figure 2 - Modbus temperature sensor wiring diagram

Specifications of the temperature sensor

You can find a lot of similar temperature and humidity sensor on the market that look like this at first glance, but their specifications may be different from this one. So, be careful when you setting the voltage level to the device! The specification below applies to this used device, for information purposes.

Specification Value
Power supply DC 5 - 28V
Power consumption <0.2W
Temperature measurement range -20 - +70 Celsius
Humidity measurement range 0 - 80% RH
Temperature precision +/-0.5 Celsius
Humidity precision +/-0.1% RH
Table 1 - Specifications of the temperature sensor

Characteristics of the sensor

The model supports various settings as shown on the table below. The default values are usually: 9600 bit/s baud rate, no parity, 1 stop bit, 8 data bits and device address is 1. You can change and get these settings by using Modbus command messages that you can read about later. Note that, these values may vary by model!

Characteristics Value
Baud rate (bit/s) 1200 / 2400 / 4800 / 9600
Parity None / Odd / Even
Stop bit 1 / 2
Device address (Slave ID) 1 - 247
Data bits 8
Table 2 - Characteristics of the temperature sensor

About Modbus

Modbus is a serial communication protocol that uses command and response frames to implement the communication between one master and many slaves. Only one master is connected to the bus, and one or several (247 maximum) slaves are also connected to the same serial bus. The Modbus communication is always initiated by the master. There are several versions of Modbus protocol that you can find more information about under Connections. The master can write the register values of the slave device and get them by sending a command message. The slave device sends back a response one that contains the requested information.

Connect more sensors to master

If you would like to connect two or more slaves to the master, you can do that like the Figure 2 shows. Be sure that each device has a unique slave ID and their serial port parameters match. Otherwise the communication will not be able to work between the master and the slaves.

connect more temperature sensors to master
Figure 3 - Connect more temperature sensors to master

Temperature sensor modbus command frame

The Modbus RTU command frame build up from 8 bytes totally. It is constructed from 1 byte address field which value can be set between 1 and 247 (broadcast is 0). This is the ID of the slave (or device). After that there is 1 byte function field. This module supports 4 type of function codes: 2 ones for reading temperature and/or humidity data and 2 ones for setting/reseting device settings. Then it followed by 4 byte data field. In this case, this field tells what you would like to read (for example temperature data and/or humidity data) or which setting value you would like to set to the slave (such as slave ID). So, the interpretation of this data field depends on the function code. Finally, 2 bytes for checksum field that calculated from the previous 6 bytes using CRC16 checksum.

Address Function Data Checksum
Table 3 - Modbus ADU

You can see some information about what function codes are supported. The function code 0x03 and 0x04 means the data reading. Function code 0x06 allows you to customize the device settings (for example slave ID, baud-rate, stop bit etc.). Finally, using 0x6E you can reset the sensor ID to default. The following Table 4 summarizes these.

Function code Format Description
03 hex-octet Read temperature and/or humidity data
04 hex-octet Read temperature and/or humidity data
06 hex-octet Set slave ID and serial port parameters
6E hex-octet Reset slave ID to default
Table 4 - Function codes of the temperature and humidity sensor

Examples

In this step, you will find some examples what frame you can send to the slave and what will you get from it. You will also get an answer what the sent command frames means, and how to interpret the response frame.

Read temperature

To receive the temperature data measured by the sensor, the following byte array must be sent to the slave in hexadecimal format: 11 04 00 01 00 01 62 9A. The first byte (0x11) means the slave ID to whom the message to be sent. The second byte is the function code. In this case, the 4 (0x04) means the data reading. So, the device will be know, you would like to read some data. The next 4 bytes will tell what kind of data you would like to read. To get temperature, the data is 0x00010001. The last two byte (0x629A) are the checksum calculated from the previous 6 bytes. The following Table 6 explains how to interpret these bytes as a command message.

Octet(s) Description Format In this example
11 slave ID hex-octet the slave ID of the temperature sensor is 17
04 function code hex-octet function code 4 means the data reading
00 01 00 01 data hex-octet request for temperature data
62 9A checksum hex-octet calculated from the previous 6 bytes using CRC16
Table 5 - Reading temperature command message

If you have sent the command message above to the sensor, you will get a response message like the below one. The first byte (0x11) is the slave ID from which the response message came from. The second byte is the response code that is the same as the function code in the command message. The third byte (0x02) is the data length. Now it is 2, so the length of data will be 2 bytes long. In this case, the data bytes are 0x00 and 0xEE. In decimal numeral system this means 238 that is equal to 23,8 Celsius degree. The last two bytes are the checksum calculated from the previous 5 bytes. The following table explains how to interpret these bytes as a response message.

Octet(s) Description Format In this example
11 slave ID hex-octet the slave ID of the temperature sensor is 17
04 response code hex-octet 4 - equal to the function code sent in command message
02 describes how long the data byte is hex-octet the data bytes will be 2 bytes long
00 EE data hex-octet 238 - the temperature equal to 23,8 Celsius degree
F8 BF checksum hex-octet calculated from the previous 5 bytes using CRC16
Table 6 - Reading temperature response message

Read humidity

To request the humidity data value from the sensor, you need to send the following bytes as a command message if the slave ID of the sensor is 17: 11 04 00 00 00 01 33 5A. The Table 7 below shows how to interpret it.

Octet(s) Description Format In this example
11 slave ID hex-octet the slave ID of the temperature sensor is 17
04 function code hex-octet 4 - means the data reading
00 00 00 01 data hex-octet request for humidity data
33 5A checksum hex-octet calculated from the previous 6 bytes using CRC16
Table 7 - Reading humidity command message

After you have sent the command message above, you will get a response message as 11 04 02 01 C3 39 32 bytes.

Octet(s) Description Format In this example
11 slave ID hex-octet the slave ID of the temperature sensor is 17
04 response code hex-octet 4 - equal to the function code sent in command message
02 describes how long the data byte is hex-octet the data bytes will be 2 bytes long
01 C3 data hex-octet 451 - the humidity value equal to 45,1%
39 32 checksum hex-octet calculated from the previous 5 bytes using CRC16
Table 8 - Reading humidity response message

For more information and examples, please download the temperature and humidity data sensor documentation.

Buy the modbus temperature sensor

More information