Temperature sensor (DS18B20)
This small module has a high precision temperature sensor that produces the measured values to two decimal places using the Modbus RTU protocol. It also has an option to shift the temperature by given value. This guide shows how to connect it to the computer, how to get the measured temperature data and how to customize the temperature offset and the module's slave ID.
Connect the modbus temperature sensor to PC
The module can operate at 5 or 12 volts. The Figure 1 shows how to connect it to the 5 volt power source. In this case, if you have an USB to RS485 plus 5V converter, you can wire them each other without using an external power supply. But if not, you need one to drive the module, and a simple USB to RS485 converter for the communication. Please note that the module has a separate pin for 12 volt operation. After you have wired the module to the converter, please plug the converter's USB to the PC, and it will detect it as a serial COM port.
Specifications of the temperature sensor
Specification | Value |
Power supply | 5 or 12V DC |
Operating temperature | -50 to +125 Celsius degree |
Temperature accuracy | +/-0.5 Celsius |
Characteristics of the sensor
For communication with the module, the baud rate must be set to 9600 bit/s and the data bits must be 8. The possible values for the parity and the stop bit are shown in the Table 2 below. In addition to the settings, the device will receive the modbus command message successfully and will respond to it if need to. The slave ID is 1 by default, but it can be modified using a modbus command message.
Characteristics | Value |
Baud rate (bit/s) | 9600 |
Parity | None / Odd / Even |
Stop bit | 1 / 2 |
Device address (Slave ID) | 1 - 247 (default 1) |
Data bits | 8 |
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 temperature sensors
To connect more temperature sensors to the master, you need to wire them like the Figure 2 shows. You can connect up to 247 slaves in parallel to the bus. Be sure that each device has a unique slave ID.
Temperature sensor modbus command frame
The module supports modbus function code 4 and 6. By using function code 4, you can get data from the device e.g. measured temperature. While using function code 6, you can set data to the device, for example slave ID or temperature offset. In both case, the Modbus RTU command frame build up from 8 bytes totally. It is constructed from 1 byte address field that contains the slave ID of the module. Then, there is 1 byte function code field. This determines the operation. It is followed by 4 bytes data field. For function code 4, this contains what data (register values) you would like to read. For function code 6, it contains the data about the register address and the value to write. Finally, there is 2 bytes checksum field calculated from the previous 6 bytes using CRC-16.
Address | Function | Data | Checksum |
Read temperature from temperature sensor
The module stores the temperature data in its input register #30001. To read the value of the register, you need to use function code 4. This identifies the reading of 30000's registers. In this case, the data field needs to contain 2 bytes offset and 2 bytes length fields. The offset specify the address of the register. Offset 1 (0x0001) determines the register #30001. The length field specifies how many registers you want to read from the specified register. The module stores the value in one register, so it needs to be 1 (0x0001) now. The Table 4 summarizes these.
Slave ID | Function | Register offset | Register length | CRC-16 | |||
01 - F7 | 04 | 00 | 01 | 00 | 01 | 2 bytes CRC |
After you have sent the message, the module will send back a response message which data field will contain the temperature value. The example below illustrate this.
Set temperature offset to the sensor
To set this value, you need to use the function code 6. By using this function code, you can set values to 4X registers. It is ideal for setting the temperature offset due to it is stored in register #40004. In this case, the data field constructed from 2 bytes offset field and 2 bytes value field. To specify register #40004, the offset must be 4 (0x0004), and the value filed needs to contain the value to be set (0x0000 - 0xFFFF). The Table 5 summarizes these.
Slave ID | Function | Register offset | Value to set | CRC-16 | |||
01 - F7 | 06 | 00 | 04 | 00 - FF | 00 - FF | 2 bytes CRC |
If the operation has been success, the module will send a response message which register has been modified and what value has wrote to it, like the example shows below. If the register value of the response message does not equal to the command messages's one, the value did not modify.
Set slave ID
You can set the slave ID of the temperature module using function code 6. The register #40000 contains this information. So, the register offset must be 0 in the data field. The value field's values can be set between 1 (0x0000) and 247 (0x00F7) like the following Table 6 shows.
Slave ID | Function | Register offset | Value to set | CRC-16 | |||
01 - F7 | 06 | 00 | 00 | 00 | 00 - F7 | 2 bytes CRC |
In this case, you will get a response message that contains the modification information. Once the you have been received the response message, the device can be accessed through the new set slave ID. There is an example below that shows how to change the slave ID from 1 to 2.
Examples
Read temperature from temperature sensor
The following query will request the temperature from the sensor with slave address 1.
Octet(s) | Description | Format | In this example |
01 | slave ID | hex-octet | the slave ID of the sensor is 1 |
04 | function code | hex-octet | function code 4 reads the contents of the 3X registers |
00 01 | register offset | hex-octet | determining register #30001 |
00 01 | register length | hex-octet | the temperature value stored in one register |
60 A0 | checksum | hex-octet | checksum calculated from the previous 6 bytes using CRC-16 |
The response below returns the temperature value as 26.43 Celsius degree.
Octet(s) | Description | Format | In this example |
01 | slave ID | hex-octet | the slave ID of the temperature sensor is 1 |
04 | response code | hex-octet | response code 4 equals to the function code sent in command message |
02 | data length | hex-octet | the data bytes will be 2 bytes long |
0A 53 | data | hex-octet | it is 2643 in decimal format, the temperature equals to 26.43 Celsius degree |
FF AD | checksum | hex-octet | calculated from the previous 5 bytes using CRC-16 |
Set temperature offset
The following query will set the temperature offset to +1 Celsius degree.
Octet(s) | Description | Format | In this example |
01 | slave ID | hex-octet | the slave ID of the sensor is 1 |
06 | function code | hex-octet | function code 6 writes the contents of the 4X registers |
00 04 | register offset | hex-octet | determines the register #40004 contains the temperature offset |
00 64 | value to set | hex-octet | set temperature offset to +1 Celsius degree (0x0064) |
C9 D3 | checksum | hex-octet | checksum calculated from the previous 6 bytes using CRC-16 |
The response below returns that the operation was successful.
Octet(s) | Description | Format | In this example |
01 | slave ID | hex-octet | the slave ID of the temperature sensor is 1 |
06 | response code | hex-octet | 6 - equal to the function code sent in command message |
00 04 | register address | hex-octet | address of the modified register |
00 64 | set value | hex-octet | the wrote value needs to be equal to the command message's one |
C9 D3 | checksum | hex-octet | calculated from the previous 6 bytes using CRC-16 |
Set slave ID
The following query will set the slave ID of the temperature sensor from 1 to 2.
Octet(s) | Description | Format | In this example |
01 | slave ID | hex-octet | the slave ID of the sensor is 1 |
06 | function code | hex-octet | function code 6 writes the contents of the 4X registers |
00 00 | register offset | hex-octet | determines the register #40000 contains the slave ID |
00 02 | value to set | hex-octet | set the slave ID to 2 |
08 0B | checksum | hex-octet | checksum calculated from the previous 6 bytes using CRC-16 |
The response below returns that the operation was successful.
Octet(s) | Description | Format | In this example |
01 | slave ID | hex-octet | the slave ID of the temperature sensor is 1 |
06 | response code | hex-octet | response code equals to the function code sent in command message |
00 00 | register address | hex-octet | address of the modified register |
00 02 | set value | hex-octet | the wrote value needs to be equal to the command message's one |
08 0B | checksum | hex-octet | calculated from the previous 6 bytes using CRC-16 |
More information
- Temperature and humidity sensor
- Relay board
- Energy meter
- Temperature sensor (DS18B20)
- Digital counter