Arduino Soil NPK Sensor: Maximizing Plant Nutrition

by AKP in Circuits > Arduino

708 Views, 3 Favorites, 0 Comments

Arduino Soil NPK Sensor: Maximizing Plant Nutrition

soil-npk-sensor-arduino-tutorial.png

Similar to humans, your plants require nourishment to flourish. To enhance their well-being and yield, plants depend on three vital nutrients: nitrogen, phosphorus, and potassium, often known as NPKs.

Inadequate levels of these nutrients in your garden soil can impede plant growth. It is essential to evaluate the present levels of N, P, and K in the soil to determine the required nutrient supplementation for improved crop fertility.

Utilizing an NPK Soil Sensor in conjunction with an Arduino enables you to accurately measure the soil’s nutrient levels.

Supplies

Parts Required

What Is NPK? and Why It’s So Important?

NPK stands for the three primary nutrients essential for the growth and flourishing of plants: nitrogen, phosphorus, and potassium.

  • Nitrogen is responsible for the growth and greenness of plant leaves.
  • Phosphorus helps the plant grow strong roots, fruit, and flowers.
  • Potassium improves the overall health and hardiness of a plant.

JXCT Soil NPK Sensor

Soil-NPK-Sensor.jpg
Soil-NPK-Sensor-Probes-Epoxy-Sealing.jpg

The JXCT Soil NPK sensor is a cost-effective, rapidly responsive, reasonably precise, and portable sensor. It facilitates real-time monitoring of NPK nutrient levels in soil for smart agriculture.

This soil NPK sensor can identify nitrogen, phosphorus, and potassium levels in soil (not water), aiding in soil fertility assessment for a more systematic understanding of soil conditions.

Operating on 5-30V with minimal power consumption, the sensor boasts a resolution of up to 1 mg/kg (mg/l) for nitrogen, phosphorus, and potassium measurement, as per the datasheet.

Featuring a rust-proof, electrolytic-resistant, and salt-alkali-resistant stainless steel probe, the sensor is adaptable to various soil types, including alkaline, acidic, substrate, seedling bed, and coconut bran soils.

The probe is securely sealed with high-density epoxy resin to prevent moisture ingress.

Moreover, the sensor holds an IP68 rating, ensuring protection against dust and moisture, ensuring prolonged operational longevity.

For extended-range usage, the sensor incorporates an RS485 communication interface supporting the Modbus-RTU communication protocol.

However, note that direct use with Arduino isn’t possible. To interface with Arduino, an RS-485 transceiver module is required to convert UART serial stream to RS-485.

Wiring a Soil NPK Sensor to an Arduino

RS485-Transceiver-Module.jpg
Arduino-Wiring-Soil-NPK-Sensor.jpg

As previously mentioned, direct integration of the NPK sensor with an Arduino is not feasible. To enable communication with Arduino, an RS-485 transceiver module is necessary, converting UART serial stream to RS-485, similar to the one shown below.

Let’s now proceed with the wiring.

The soil NPK Sensor consists of four wires. The brown wire, indicating power, should be connected to the 5V-30V power supply. The black wire, representing ground, should be linked to a common ground.

Connect the yellow wire of the NPK sensor to the RS485 module’s A pin, and the blue wire to the RS485 module’s B pin.

Connect the RS485 module’s R0 and DI pins to Arduino’s digital pins 2 and 3, respectively. These digital pins serve as virtual RX and TX serial lines.

The RS485 module’s VCC pin should be connected to Arduino’s 5V output, and the DE and RE pins should be linked to digital pins 7 and 8, respectively.

Ensure that both your circuit and Arduino share a common ground.

The wiring configuration is illustrated in the image below.

Usage Instructions

Soil-NPK-Sensor-Horizontal-Insertion.jpg
Soil-NPK-Sensor-Vertical-Insertion.jpg
  • Select an appropriate measuring location, avoiding stones, and ensure that the steel probe does not encounter any hard objects. Vertically insert the sensor into the soil.
  • Optionally, the sensor can be inserted horizontally into a pit. In this case, excavate the pit vertically with a diameter exceeding 20 cm before tightly backfilling it.

Arduino Example Code

The provided test sketch retrieves the nitrogen, phosphorus, and potassium values from the soil NPK sensor and displays them on the serial monitor.

#include <SoftwareSerial.h>
#include <Wire.h>

// RE and DE Pins set the RS485 module
// to Receiver or Transmitter mode
#define RE 8
#define DE 7

// Modbus RTU requests for reading NPK values
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};

// A variable used to store NPK values
byte values[11];

// Sets up a new SoftwareSerial object
// Digital pins 10 and 11 should be used with a Mega or Mega 2560
SoftwareSerial mod(2, 3);
//SoftwareSerial mod(10, 11);

void setup() {
// Set the baud rate for the Serial port
Serial.begin(9600);

// Set the baud rate for the SerialSoftware object
mod.begin(9600);

// Define pin modes for RE and DE
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);

delay(500);
}

void loop() {
// Read values
byte val1,val2,val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);

// Print values to the serial monitor
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");

delay(2000);
}

byte nitrogen(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(nitro,sizeof(nitro))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}

byte phosphorous(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(phos,sizeof(phos))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}

byte potassium(){
digitalWrite(DE,HIGH);
digitalWrite(RE,HIGH);
delay(10);
if(mod.write(pota,sizeof(pota))==8){
digitalWrite(DE,LOW);
digitalWrite(RE,LOW);
for(byte i=0;i<7;i++){
//Serial.print(mod.read(),HEX);
values[i] = mod.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
return values[4];
}


See Full Article Here