Connections:
RS485 interface is connected to the Arduino board as follows:
RE pin to Arduino pin 6
DE pin to Arduino pin 7
RO pin to Arduino pin 8
DI pin to Arduino pin 9
The RS485 A and B pins are connected to the corresponding pins of the sensor
The snubber circuit is connected to the sensor as follows:
Snubber -ve pin to Arduino or sensor ground pin
Snubber +ve pin to Arduino 5V and sensor +v pin
Diagram-
Code-
//NOTE : Make changes in the code as per your soil specifications, you are developing a new product.
#include <AltSoftSerial.h>
#include <Ethernet.h>
#include <AltSoftSerial.h>
// RO to pin 8 & DI to pin 9 when using AltSoftSerial
#define RE 6
#define DE 7
const byte temp[] = {0x01,0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf};//
const byte mois[] = {0x01,0x03,0x00,0x12,0x00,0x01,0x24,0x0F};
const byte econ[] = {0x01,0x03, 0x00, 0x15, 0x00, 0x01, 0x95, 0xce};
const byte ph[] = {0x01,0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b};//0x0B64
const byte nitro[] = { 0x01, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C };
const byte phos[] = { 0x01, 0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc };
const byte pota[] = { 0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0 };
byte values[11];
AltSoftSerial mod;
float envhumidity = 0.0, envtemperature = 0.0, soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0;
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0,val5 = 0, val6 = 0, val7 = 0;
void setup() {
Serial.begin(9600);
mod.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
// put RS-485 into receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
delay(3000);
}
void loop() {
val1 = moisture();
soil_mois = val1/1.8;
delay(1000);
soil_temp = temperature()/10.0;
delay(1000);
val3 = econduc();
delay(1000);
val4 = phydrogen()/25;
soil_ph = val4;
delay(1000);
val5 = nitrogen();
delay(1000);
val6 = phosphorous();
delay(1000);
val7 = potassium();
delay(1000);
Serial.print("Moisture: ");Serial.print(soil_mois);Serial.println(" %");
delay(1000);
Serial.print("Temperature: ");Serial.print(soil_temp);Serial.println(" C");
delay(1000);
Serial.print("EC: ");Serial.print(val3);Serial.println(" us/cm");
delay(1000);
Serial.print("ph: ");Serial.print(soil_ph);Serial.println(" ph");
delay(1000);
Serial.print("Nitrogen: "); Serial.print(val5);Serial.println(" mg/kg");
delay(1000);
Serial.print("Phosphorous: ");Serial.print(val6);Serial.println(" mg/kg");
delay(1000);
Serial.print("Potassium: ");Serial.print(val7);Serial.println(" mg/kg");
Serial.println();
delay(3000);
}
byte moisture() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte temperature() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[3]<<8|values[4];
}
byte econduc() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(econ); i++) mod.write(econ[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte phydrogen() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte nitrogen() {
// clear the receive buffer
mod.flushInput();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte phosphorous() {
mod.flushInput();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte potassium() {
mod.flushInput();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
Setup Pic:
Video link-
https://drive.google.com/file/d/1wTBBZKngc2TXyntnJmJhXCjvJyVEvdRg/view?usp=drivesdk
Note : Make sure you are using power supply is noise less , if there is noise in power supply you will loss data, and receive random values (255/225)