Here is the schematic diagram of my setup:
Following is the code part that i used to send data from Modbus module to the sensor:
#include <SoftwareSerial.h>
#include <Wire.h>
/*
RO = Receiver Output. Connects to a serial RX pin on the microcontroller
RE = Receiver Enable. Active LOW. Connects to a digital output pin on a microcontroller. Drive LOW to enable receiver, HIGH to enable Driver
DE = Driver Enable. Active HIGH. Typically jumpered to RE Pin.
DI = Driver Input. Connects to serial TX pin on the microcontroller
*/
//2 connections between Arduino and 'MAX485 TTL to RS-485 Interface Module'
#define RE 8 //[digital pin 8 -> RE]
#define DE 7 //[digital pin 7 -> DE]
const byte tem[] = {0x01, 0x04, 0x00, 0x00, 0x00, 0x03, 0xb0, 0x0b}; //Request code for temperature
const byte water_con[] = {0x02, 0x04, 0x00, 0x00, 0x00, 0x03, 0xb0, 0x0b}; //Request code for water content
const byte conduct[] = {0x03, 0x04, 0x00, 0x00, 0x00, 0x03, 0xb0, 0x0b}; //Request code for conductivity
byte values[11]; //empty byte array of length 11
Is any one knows how to get the correct addresses that should send from Using sensor user manual. User manual is not understandable for me.
Link to user manual:
https://files.seeedstudio.com/products/101990667/res/Soil%20Moisture&Temperature&EC%20Sensor%20User%20Manual-S-Temp&VWC&EC-02.pdf
And my complete code as follows.
#include <SoftwareSerial.h>
#include <Wire.h>
/*
RO = Receiver Output. Connects to a serial RX pin on the microcontroller
RE = Receiver Enable. Active LOW. Connects to a digital output pin on a microcontroller. Drive LOW to enable receiver, HIGH to enable Driver
DE = Driver Enable. Active HIGH. Typically jumpered to RE Pin.
DI = Driver Input. Connects to serial TX pin on the microcontroller
*/
//2 connections between Arduino and 'MAX485 TTL to RS-485 Interface Module'
#define RE 8 //[digital pin 8 -> RE]
#define DE 7 //[digital pin 7 -> DE]
const byte tem[] = {0x01, 0x04, 0x00, 0x00, 0x00, 0x03, 0xb0, 0x0b}; //Request code for temperature
const byte water_con[] = {0x02, 0x04, 0x00, 0x00, 0x00, 0x03, 0xb0, 0x0b}; //Request code for water content
const byte conduct[] = {0x03, 0x04, 0x00, 0x00, 0x00, 0x03, 0xb0, 0x0b}; //Request code for conductivity
byte values[11]; //empty byte array of length 11
//another 2 connection between Arduino and 'MAX485 TTL to RS-485 Interface Module'
//[digital pin 2 -> RO]
//[digital pin 3 -> DI]
SoftwareSerial mod(2, 3); //defining the module as 'mod' object
void setup() {
Serial.begin(9600); //initialize serial monitor
mod.begin(9600); // initialize sensor module
pinMode(RE, OUTPUT); //set digital pin 8 as output
pinMode(DE, OUTPUT); //set digital pin 7 as output
delay(1000); //wait 1 second
}
void loop() {
byte val1, val2, val3; //defining three byte variables
val1 = temperature(); //calling function temp()
delay(250); // wait 0.25 seconds
val2 = water_content(); //calling function water_content()
delay(250); // wait 0.25 seconds
val3 = conductivity(); //calling function conductivity()
delay(250); // wait 0.25 seconds
Serial.print("Temperature: ");
Serial.print(val1); //print Temperature in serial monitor
Serial.println(" ℃");//unit
Serial.print("Water content: ");
Serial.print(val2); //print Water content in serial monitor
Serial.println(" %");//unit
Serial.print("Conductivity: ");
Serial.print(val3); //print Conductivity in serial monitor
Serial.println(" μs/cm");//unit
delay(2000);
}
byte temperature() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10); //wait 0.01 seconds
if (mod.write(tem, sizeof(tem)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//Serial.print(mod.read(),HEX);
values[i] = mod.read(); //retrieving the temperature in hexadecimal
Serial.print(values[i], HEX); //converting temperature into decimal
}
Serial.println();
}
return values[4]; //send the value
}
byte water_content() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10); //wait 0.01 seconds
if (mod.write(water_con, sizeof(water_con)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//Serial.print(mod.read(),HEX);
values[i] = mod.read(); //retrieving the water content in hexadecimal
Serial.print(values[i], HEX); //converting water content level into decimal
}
Serial.println();
}
return values[4]; //send the value
}
byte conductivity() {
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(10); //wait 0.01 seconds
if (mod.write(conduct, sizeof(conduct)) == 8) {
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
for (byte i = 0; i < 7; i++) {
//Serial.print(mod.read(),HEX);
values[i] = mod.read(); //retrieving the electric conductivity level in hexadecimal
Serial.print(values[i], HEX); //converting electric conductivity level into decimal
}
Serial.println();
}
return values[4]; //send the value
}
Thank you.