Connecting RS485 Soil Moisture & Temperature Sensor to Arduino
In this guide, we will explore how to interface a Soil Moisture & Temperature Sensor with Arduino using the RS485 protocol. This setup will allow us to monitor the moisture content and temperature of the soil, which are crucial parameters for agriculture and gardening.
Soil moisture sensors measure the volumetric water content in soil. By understanding the moisture levels, farmers and gardeners can make informed decisions about irrigation, ensuring optimal plant growth. The temperature of the soil also plays a significant role in plant health, affecting root growth and nutrient uptake.
For this project, we will utilize the Renkeer Soil Moisture & Temperature Sensor, which is specifically designed for in-ground measurements. This sensor can measure soil moisture levels ranging from 0% to 100% and soil temperature from -40°C to 60°C with impressive accuracy. Its low-power consumption makes it ideal for remote locations, powered by solar panels or batteries. Earlier, we used Capacitive Soil Moisture Sensor and also Resistive Soil Moisture Sensor, but they are not industrial graded.
Therefore we are using Industrial Grade Soil Sensor for this project. To interface the sensor with the Arduino, we will use the RS485 communication protocol. The data from the Soil Moisture & Temperature Sensor will be presented on an OLED screen connected to the Arduino.
Bill of MaterialsFor this project, we need the following components. You can purchase all these components from the given links.
S.N.ComponentsQuantityPurchase Link 1Arduino Nano Board1Amazon | AliExpress | SunFounder 2Soil Temperature Humidity Sensor1Renkeer 30.96" I2C OLED Display1Amazon | AliExpress | SunFounder 4MAX485 Module1Amazon | AliExpress | SunFounder 55V Power Supply1Amazon | AliExpress | SunFounder 6Jumper Wires10Amazon | AliExpress | SunFounder 7Breadboard1Amazon | AliExpress | SunFounder Soil Moisture & Temperature SensorThe Soil Temperature and Humidity Sensor is a device designed to check soil warmth and wetness.
It’s compared with high-quality German sensors to make sure it’s accurate. This sensor is quick to give results and works reliably. It’s good for all soil types, even those with salts. One thing to note is when measuring frozen ice layers, the water value might be low and not accurate, so users need to adjust for that. One of its best features is that it can be left in the ground for a long time. It won’t corrode and is completely waterproof.
This sensor is useful in many areas. It’s great for science projects, helping farmers save water, and making sure plants in greenhouses grow well. It’s also used in places like sewage plants and grain storage.
Technical Parameters- Supply power : 4.5~30V DC
- Power consumption: 0.4W
- Moisture measurement range : 0~100% RH
- Temperature measurement range : -40℃~60℃(personal)
- Moisture accuracy : ±3%RH(default)
- Temperature accuracy : ±0.5℃(default)
- Work environment : -20℃~60℃,0~80%RH
- Output signal : RS485
- Parameter configuration : software
- Probe length: 70 mm
- Probe diameter: 3 mm
- Probe material: 304 stainless steel
- Sealing material: Epoxy resin (black flame retardant)
- Cable length: standard two meters (RVV 4 * 0.3)
- Protection class: IP68
1. Speed Measurement Method: Choose the right spot for measurement, avoiding areas with stones. Ensure the needle doesn’t hit any hard objects. Clear the topsoil up to the desired measurement depth, but keep the soil below compact.
Insert the sensor vertically into the soil without shaking it side to side. For accurate results, it’s recommended to take an average from several measurements at a small point.
2. Buried measurement method: For accurate measurements using the probe, dig a vertical pit with a diameter exceeding 20cm. Once dug, the probe should be inserted horizontally into the pit wall at the desired depth.
After inserting the probe, ensure the pit is filled tightly and allowed to stabilize. This stabilization can take several days, months, or even longer. Once stable, you can begin extended measurement and recording sessions.
Sensor PinoutThe Soil Moisture & Temperature Sensors works on RS485 Protocol and hence can communicate with easily with Arduino, ESP32 or any other microcontrollers using RS485 Module.
Here are the 4 wires for the sensor.
Data Frame Format Definition & Communication Protocol Data frame format definitionUsing the Modbus-RTU communication protocol, the format is as follows:
- Initial structure ≥ 4 bytes of time
- Address code = 1 byte
- Function code = 1 byte
- Data area = N bytes
- Incorrect revision = 16 byte CRC code
- Ending structure ≥4 byte time
Host query frame structure:
Address Code Function Code Register Origin Address Register Length Check code Low Bit Check Code High Bit 1 byte 1 byte 2 bytes 2 bytes 1 byte 1 byteSlave response frame structure:
Address Code Function Code Effective Bytes Data Area 1 Data Area 2 Data Area N Revision Code 1 byte 1 byte 1 byte 2 bytes 2 bytes 2 bytes 2 bytes Read the Soil Moisture & Temperature ValueInquiry frame:
Address Code Function Code Initial Address Data Length Check code Low Bit Check Code High Bit 0x01 0x03 0x00 0x00 0x00 0x02 0xC4 0x0BResponse frame:
Address Code Function Code Returns Byte Number Humidity Number Temperature Number Check code low bit Check code high bit 0x01 0x03 0x04 0x02 0x92 0xFF 0x9B 0x5A 0x3DTemperature calculation : When temperature is under 0 ℃, the temperature date will be updated in complement code. Temperature: FF9B H(hexadecimal)= -101 => temperature = -10.1℃ Humidity calculation: Humidity: 292 H (hexadecimal)= 658 => humidity = 65.8%RH
Interfacing Soil Moisture & Temperature Sensor with ArduinoNow lets interface the Soil Humidity & Temperature Sensor with Arduino & start measuring the Soil Moisture and Temperature.
Here is the schematic for the project.
The Soil Sensor communicates with a Microcontroller using RS485 protocol. Hence we used MAX485 Module for this project. The MAX485 is a popular RS-485 transceiver chip commonly used for serial communication. When interfacing the MAX485 with an Arduino, you’ll typically be dealing with half-duplex communication, where data transmission and reception occur at different times.
Here is a connection mapping between MAX485 & Arduino.
Here is a connection mapping between MAX485 & Soil Humidity Temperature Sensor.
You may use a breadboard for assembly and a jumper wires for connecting the sensor and RS485 Module with Arduino.
Source Code/Program for Reading Soil Moisture & TemperatureAfter assembling the sensor as per the circuit diagram above, lets move to the coding part of this project. According to the above table the inquiry frame for getting the Sensor reading hexadecimal is as follows:
0x01 , 0x03 , 0x00 , 0x00 , 0x00 , 0x02 , 0xC4 , 0x0BThe sensor communicates at a baudrate of 4800. We have set the same parameter in the code to get the required reading.
Copy the following code and paste it on your Arduino IDE.
#include // Include the Adafruit SSD1306 library to interact with the display #define SCREEN_WIDTH 128 // Set the width and height of the display in pixels #define SCREEN_HEIGHT 64 #define OLED_RESET -1 // Set the OLED reset pin to -1 if it shares the Arduino reset pin #define SCREEN_ADDRESS 0x3C // Set the address of the screen as specified in its datasheet // Create an Adafruit_SSD1306 object called display Adafruit_SSD1306 display ( SCREEN_WIDTH , SCREEN_HEIGHT , & Wire , OLED_RESET ) ; // Define the pins for RS485 communication #define RE 8 #define DE 7 // Request frame for the soil sensor const byte soilSensorRequest [ ] = < 0x01 , 0x03 , 0x00 , 0x00 , 0x00 , 0x02 , 0xC4 , 0x0B >; byte soilSensorResponse [ 9 ] ; SoftwareSerial mod ( 2 , 3 ) ; // Software serial for RS485 communication void setup ( ) Serial . begin ( 9600 ) ; // Initialize serial communication for debugging mod . begin ( 4800 ) ; // Initialize software serial communication at 4800 baud rate pinMode ( RE , OUTPUT ) ; // Set RE pin as output pinMode ( DE , OUTPUT ) ; // Set DE pin as output // Start the display, looping forever if it fails if ( ! display . begin ( SSD1306_SWITCHCAPVCC , SCREEN_ADDRESS ) ) < Serial . println ( F ( "SSD1306 allocation failed" ) ) ; display . clearDisplay ( ) ; // Clear the display and display the cleared screen display . display ( ) ; delay ( 500 ) ; // Start the transmission mode for RS485 digitalWrite ( DE , HIGH ) ; digitalWrite ( RE , HIGH ) ; // Send the request frame to the soil sensor mod . write ( soilSensorRequest , sizeof ( soilSensorRequest ) ) ; // End the transmission mode and set to receive mode for RS485 digitalWrite ( DE , LOW ) ; digitalWrite ( RE , LOW ) ; // Wait for the response from the sensor or timeout after 1 second unsigned long startTime = millis ( ) ; while ( mod . available ( ) < 9 && millis ( ) - startTime < 1000 ) if ( mod . available ( ) >= 9 ) // If valid response received // Read the response from the sensor byte index = 0 ; while ( mod . available ( ) && index < 9 ) soilSensorResponse [ index ] = mod . read ( ) ; Serial . print ( soilSensorResponse [ index ] , HEX ) ; // Print the received byte in HEX format Serial . print ( " " ) ; Serial . println ( ) ; // Parse and calculate the Moisture value int Moisture_Int = int ( soilSensorResponse [ 3 ]