IOT

How to measure Soil Nutrients using Arduino & Soil NPK Sensor

In this tutorial, we’ll discover how to connect an Arduino to a soil NPK sensor and create our own DIY soil NPK meter. NPK Soil Sensor & Arduino make it simple to measure the nutrient content of the soil. To calculate how much extra nutrient content needs to be supplied to the soil to boost crop fertility, the N, P, and K contents of the soil must be measured.

Proposed System

NPK sensors are used to find the fertility of the soil. Nitrogen, phosphorous, and potassium make up a significant portion of soil fertilizer. Knowing the concentration of nutrients in the soil can inform us of nutritional abundance or deficiency in soils that support plant growth.

There are several ways to measure the amount of nutrients in the soil, including employing spectrometers or optical sensors. However, the spectrum analysis method is not practical, and a disadvantage is that the results are only 60–70% accurate. Given the lack of available information, it is still unclear whether the spectrum analysis method is as accurate as more established wet chemistry techniques.

Therefore, in this case, a JXCT Soil NPK Sensor will be used to measure the soil’s nitrogen, phosphorous, and potassium levels. With Modbus RS485, the JXCT Soil NPK sensor offers low cost, quick response, high precision, and portability. This sensor’s benefit over more conventional detection techniques is that it provides extremely quick measurement and highly precise data. You only need to bury its probe in the ground to use Arduino to read the reading. So let’s explore the Soil NPK Sensor’s interface with Arduino in more depth.

Components Required

  • Arduino Nano
  • NPK Sensor
  • 0.96″ I2C OLED Display
  • Modbus Module-MAX485 Modbus
  • 9V – 12V DC Supply
  • Connecting Wires
  • Breadboard

What are NPK values? Why it is important to monitor and maintain them for plants?

The nitrogen, phosphorus, and potassium content of your soil or substrate is indicated by the soil’s NPK value. These three basic macronutrients are required by all plants. Knowing the NPK values of the substrates or soil is crucial so you can give your plants the proper amount of external NPK fertilizers based on what they require.

Analyze the benefits that these 3 macronutrients provide for your plant.

  • Nitrogen is indicated by the N. A plant needs nitrogen to develop and produce healthy, vibrant leaves.
  • For phosphorous, use the P symbol. Phosphorous encourages the growth of roots, flowers, and fruits.
  • K is a symbol for potassium. Potassium enhances plant resilience to disease and supports healthy root systems in addition to assisting with water and nutrient transmission and photosynthesis.

At each stage, a plant needs a different quantity of these nutrients. Your plant must receive an adequate supply of NPK and other nutrients, so do so. If you don’t, the plant can start to exhibit signs of a deficiency like the one below.

Giving the excess amount of NPK causes:

  • Nitrogen: Overgrowth with a decrease in the ability to fruit and flowers attracts insects and reduces the strength of the stem.
  • Phosphorus: Reduces ability to absorb Zinc and Iron and few micronutrients even though they are present in the soil.
  • Potassium: Leads to the inability to absorb major nutrients like nitrogen, magnesium, and calcium which leads to their deficiency.

So it is important to monitor those values and provide the required amounts of NPK for healthy plant growth. So taking this into account let’s build a soil NPK monitoring device with its sensor and Arduino.

Soil NPK Sensor

The soil NPK sensor can be used to measure the soil’s nitrogen, phosphorus, and potassium concentration. It facilitates the systematic evaluation of the soil condition by assisting in assessing the soil’s fertility. For a very long time, the sensor can be buried in the ground. To ensure the long-term functionality of the probe component, it has a high-quality probe that is corrosion resistant to salt and alkali, rust, and electrolysis. It is therefore appropriate for all types of soil. Alkaline, acidic, substrate, seedling bed, and coconut bran soil can all be detected using this method.

Soil NPK Sensor

Chemical reagents are not necessary for the sensor. It may be used with any microcontroller due to its high measurement accuracy, quick response time, and strong interchangeability. Because it includes a Modbus Communication interface, the microcontroller cannot be used directly with the sensor. As a result, you need to connect the sensor to the microcontroller using any Modbus Module, such as RS485 or MAX485.

The sensor uses relatively little power and runs on 9 to 24 volts. When referring to the sensor’s accuracy, it is up to within 2%. The measurement resolution for nitrogen, phosphorous, and potassium is up to 1 mg/kg (mg/l).

Using this Soil NPK Sensor, you can make your own Arduino Soil NPK Meter or any Cloud IoT Based Soil Nutrient Content Monitoring System.

Specifications

  • Power: 9V-24V
  • Measuring Range: 0-1999 mg/kg (mg/l)
  • Operating Temperature: 5-45 °C
  • Resolution: 1mg/kg
  • Precision: ±2% F.S.
  • Output Signal: RS485
  • Baud Rate: 2400/4800/9600
  • Protection Class: IP68

MAX485 TTL to RS-485 Interface Module

The MAX485 TTL to RS-485 Interface Module, which is frequently used in industrial settings, enables us to employ RS-485 differential signalling for reliable long-distance serial communications up to 1200 metres or in electrically noisy environments. It can support data speeds of up to 2.5MBit/sec, however as distance increases, the maximum data rate that can be sustained decreases.

The RS-485 module handles translating the electrical signals between TTL and the differential signaling utilized by RS-485, however, the data initially starts off as a conventional TTL level serial as far as the microcontroller is concerned. The fact that RS-485 permits several devices (up to 32) on a single cable, sometimes known as “multi-drop,” is a significant advantage.

Specifications

  • Use MAX485 Interface chip
  • Uses differential signaling for noise immunity
  • Distances up to 1200 meters
  • Speeds up to 2.5Mbit/Sec
  • Multi-drop supports up to 32 devices on the same bus
  • Red power LED
  • 5V operation

Pinout & Module Connection

The module has two 4-pin headers on the assembly.

1 x 4 Header (Data side)

  • 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

1 x 4 Header (Output side)

  • VCC = 5V
  • B = Data ‘B’ Inverted Line. Common with the B
  • A = Data ‘A’ Non-Inverted Line. Connects to A on far-end module
  • GND = Ground

1 x 2 Screw Terminal Block (Output side)

  • B = Data ‘B’ Inverted Line. Connects to B on far-end module
  • A = Data ‘A’ Non-Inverted Line. Connects to A on far-end module

Interfacing Soil NPK Sensor with Arduino

Let’s now use the MAX485 Modbus Module to connect the Soil NPK Sensor to the Arduino Nano Board. Take a look at the connection chart below.

Soil NPK Sensor Arduino

Utilizing software serial, connect the D2 and D3 Arduino pins to the R0 and DI pins of the Modbus. In a similar manner, we must allow DE & RE high. Connect the Arduino’s DE and RE pins to the D7 and D8 pins to do this. There are 4 wires on the NPK Sensor. The brown one is VCC and requires a power supply ranging from 9V to 24V. a black pin known as the GND pin. Connect it to Arduino’s GND by doing so. The MAX485’s B pin and A pin are connected by the yellow wire, which is the A pin, and the MAX485’s B pin, which is the blue wire.

An I2C module, the 0.96′′ SSD1306 OLED Display. Connect the Arduino’s 3.3V and GND pins to the OLED display’s VCC and GND pins. Similar to this, join its SDA and SCL pins to Arduino’s A4 and A5. Either create a custom PCB or follow the circuit design to assemble the circuit on a breadboard.

NPK Sensor Arduino

Project PCB Gerber File & PCB Ordering Online

Here is the PCB you need if you don’t want to build the circuit on a breadboard and want it for your project. With the aid of the EasyEDA online Circuit Schematics & PCB designing tool, the PCB Board for the NPK Meter was created. Below is a description of the PCB’s front and back sides.

Download Gerber File: NPK Meter PCB

Now you can visit the NextPCB official website by clicking here: https://www.nextpcb.com/. So you will be directed to the NextPCB website.

Now that the Gerber File has been uploaded, you can order from the website. The PCB quality is flawless and clean. For PCB & PCBA Services, the majority of consumers rely on NextPCB for this reason.

Modbus Command for NPK Sensor

A Modbus Device can be instructed by Modbus commands to do one of the following things: 

  • Modify the value in one of its registers, which is written to Coil and Holding registers.
  • Read a port for I/O: Obtain information from the Discrete and Coil ports,
  • Instruct the device to transmit back any number of values from its Coil and Holding register.

The Modbus address of the device for which a command is intended is contained in the command (1 to 247). The inquiry frame is another name for the Modbus address. Even while additional devices might get the command, only the specified device will respond and take action.

For reading the values of Nitrogen (N), Phosphorous (P), and Potassium (K), the NPK Sensor includes three separate inquiry frames (K). The instruction handbook and inquiry frame are included. The following individual inquiry frameworks for NPK data:


1. Nitrogen: {0x01,0x03, 0x00, 0x1e, 0x00, 0x01, 0xe4, 0x0c}

The inquiry frame for getting Soil Nitrogen Value is:

You will get the following as a response:

You can calculate the Soil Nitrogen from the Response you receive. For example, if you get 0030 as a response then the Soil Nitrogen Value will be:

0020 H(hexadecimal) = 32 (Decimal) => Nitrogen = 32mg/kg


2. Phosphorous:{0x01,0x03, 0x00, 0x1f, 0x00, 0x01, 0xb5, 0xcc}

The inquiry frame for getting Soil Phosphorous Value is:

You will get the following as a response:

From the response you get, you can determine the Soil Phosphorous. For instance, the Soil Nitrogen Value will be as follows if you receive the response 0030:

0025 H(hexadecimal) = 37 (Decimal) => Phosphorous = 37/kg


3. Potassium:{0x01,0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xc0}

The inquiry frame for getting Soil Potassium Value is:

You will get the following as a response:

With the help of the response you get, you can determine the soil potassium. For example, the Soil Potassium Value will be as follows if you receive the response 0030:

0030 H(hexadecimal) =48 (Decimal) => Potassium = 48mg/kg

Source Code/Program

The source code for connecting an Arduino to a soil NPK sensor and getting the sensor’s soil nutrient value via a Modbus command is provided below. Send the command, and you can get the HEX Code value back. To obtain the measured soil nutrient content data, the HEX code must be translated into decimal.

You will need OLED Library because we will be using an OLED display to show the soil nutrient values (nitrogen, phosphorous, and potassium) in mg/kg. Add the following OLED Library to the Arduino IDE by downloading it.

1. Adafruit SSD1306 Library: Download

2. Adafruit GFX Library: Download

Here is the complete source code. Compile the code & upload it to the Arduino Nano Board.

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

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define RE 8
#define DE 7

//const byte code[]= {0x01, 0x03, 0x00, 0x1e, 0x00, 0x03, 0x65, 0xCD};
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];
SoftwareSerial mod(2,3);

void setup() {
Serial.begin(9600);
mod.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);

display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //initialize with the I2C addr 0x3C (128×64)
delay(500);
display.clearDisplay();
display.setCursor(25, 15);
display.setTextSize(1);
display.setTextColor(WHITE);
display.println(” NPK Sensor”);
display.setCursor(25, 35);
display.setTextSize(1);
display.print(“Initializing”);
display.display();
delay(3000);
}

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


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);

display.clearDisplay();


display.setTextSize(2);
display.setCursor(0, 5);
display.print(“N: “);
display.print(val1);
display.setTextSize(1);
display.print(” mg/kg”);

display.setTextSize(2);
display.setCursor(0, 25);
display.print(“P: “);
display.print(val2);
display.setTextSize(1);
display.print(” mg/kg”);

display.setTextSize(2);
display.setCursor(0, 45);
display.print(“K: “);
display.print(val3);
display.setTextSize(1);
display.print(” mg/kg”);

display.display();
}

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];
}

Recall that only the code is able to measure soil NPK concentrations up to 255 mg/kg. We are only reading an 8-bit value, which is why. The Sensor’s datasheet states that you can measure values up to 1999 mg/kg. We must read 16-bit data in order to read such a value. In order to read such high results, consult the Soil EC Sensor Code.

Monitoring Soil NPK Data on OLED Display

The OLED and the sensor will initialize once the code has been uploaded to the Arduino Nano Board. The sensor will take some time to stabilise, and the initial reading can be off.

You can dip the sensor in the soil to obtain an NPK reading once it has stabilized. The amount of Nitrogen, Phosphorous, and Potassium, which make up the soil’s ammonium content, will be shown as mg/Kg.

 Soil NPK Meter Arduino

So this is how you connect an Arduino to a soil nutrient sensor and obtain NPK readings. In a similar fashion, insert the sensor into various soil samples. Depending on the kind of soil, you will observe a change in the volume of NPK.

Soil NPK Sensor Arduino

Conclusion

I hope all of you understand how to Measure Soil Nutrient using Arduino & Soil NPK Sensor. We MATHA ELECTRONICS will be back soon with more informative blogs.

Back to list

Leave a Reply

Your email address will not be published.