Example Code for Arduino-Temperature Measurement (Single Sensor)

Last revision 2026/01/15

This code tests the temperature measurement function of the URM08-RS485 Waterproof Sonar Range Finder. Users can learn how to read temperature data from the sensor.

Hardware Preparation

Software Preparation

Wiring Diagram

URM08-RS485 Connection Diagram

Other Preparation Work

  • Mount the RS485 Shield on the Arduino Leonardo.
  • Connect the URM08-RS485 sensor to the RS485 Shield following the wiring order: Red-VCC, Black-GND, Green-RS485-A, Yellow-RS485-B.
  • Ensure the external power supply (6-12V) is properly connected.

Sample Code

/**************************************************************************************************************
*This code tests the temperature measurement function of the URM08-RS485 Waterproof Sonar Range Finder
*@ author : [email protected]
*@ data   : 11.09.2017
*@ version: 1.0
*RX(TTL-RS485 converter) -> TX1/D1 (Arduino Leonardo)  TX(TTL-RS485 converter)-> RX1/D0 (Arduino Leonardo)
**************************************************************************************************************/

#define header_H     0x55 //Frame header
#define header_L     0xAA //Frame header
#define device_Addr  0x11 //Module address
#define data_Length  0x00 //Data length
#define get_Temp_CMD 0x03 //A temperature measurement command
#define checksum     (header_H+header_L+device_Addr+data_Length+get_Temp_CMD) //Checksum

unsigned char i=0;
int  temperature=0;
unsigned char Rx_DATA[8];
unsigned char CMD[6]={header_H,header_L,device_Addr,data_Length,get_Temp_CMD,checksum}; //temperature  measurement command packet
void setup() {
  Serial1.begin(19200);  //Communicate with module via Serial1, set baud rate to 19200
  Serial.begin(19200);   //Set Serial to serial port of data output
}

void loop() {
 for(i=0;i<6;i++){
    Serial1.write(CMD[i]);
    }
 delay(50);  //Wait data to return
 i=0;
 while (Serial1.available()){  //Read return data package (NOTE: Demo is just for your reference, the data package haven't be calibrated yet)
    Rx_DATA[i++]=(Serial1.read());
    }
 temperature=((Rx_DATA[5]<<8)|Rx_DATA[6]);  //Get temperature(10 times temperature value here)
 Serial.print(temperature/10);             //Print temperature
 Serial.print('.');
 Serial.print(temperature%10);
 Serial.println("C");
}

Result

The Serial Monitor (set to 19200 bps) will display the measured temperature in Celsius, e.g., "27.5C".

Because Leonardo UART communication needs converted to RS485, here we used RS485 expansion board.

Was this article helpful?

TOP