Soil Nutrient Measurement using Soil NPK Sensor and Arduino

Introduction

In this tutorial, you will learn how to connect a soil NPK sensor to an Arduino board, measure the levels of the main soil nutrients Nitrogen-N, Phosphorus-P, and Potassium-K, and display the results on an OLED display with I2C support and a serial monitor.

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

The NPK value of soil indicates the amount of nitrogen, phosphorus, and potassium in your soil or substrate. These three essential macronutrients are required by all plants. It is critical to understand the NPK values of the soil or substrate so that you can provide the appropriate amount of external NPK fertilizer to our plants.

Let’s Analyze how these 3 macro nutrients helps your plant.

  • The N stands for nitrogen. Nitrogen is necessary for the growth of a plant with good leaf growth and color.
  • The P stands for phosphorus. Phosphorous promotes root formation and flower and fruit development.
  • The K stands for potassium. Potassium helps in transfer of water and nutrients in your plants and helps photosynthesis, it also increases the resistance against diseases and support healthy root systems.

At each stage of development, a plant requires a different amount of these nutrients. It is critical to provide your plant with an adequate amount of NPK and other nutrients. If you don’t, the plant may begin to exhibit deficiency symptoms, as shown in the image below.

deficient nutrients such as nitrogen, phosphorus, and potassium in plant

The above image shows how a corn plant turns when nutrients such as nitrogen, phosphorus, and potassium are deficient. These are essential nutrients for the plant, and providing too much or too little of them can harm it.

Giving excess amount of NPK causes:

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

It is critical to monitor those values and supply the necessary amounts of NPK for healthy plant growth. Taking this into consideration, let us make a soil NPK monitoring device using its sensor and Arduino.

Bill Of Materials

SNCOMPONENTS NAMEDESCRIPTIONQUANTITY
1Arduino Nano Boardmicrocontroller1https://amzn.to/3EBaQFT
2NPK SensorSoil Sensor1https://amzn.to/3UcrYYE
3Connecting wiresjumper wiresomehttps://amzn.to/3tX7PLl
4BreadboardNormal1https://amzn.to/3Vlefzw
50.96" OLED Display1https://amzn.to/3GGU3Uy
6MAX485 Modbus Module 1https://amzn.to/3EVqTjp
79-12V DC Power Supply1https://amzn.to/3Ovwv7a

PCB Manufacturer

PCBWAY is quite professional in the field of PCB manufacturing; you can try their services at extremely low prices, Only 5 dollars for 10 PCBs , besides this the new members also get a 5 Dollars bonus.

You can now upload the Gerber File to the Website and place an order. The PCB quality is superb & high standard. That is why most of people trust PCBWAY for PCB & PCBA Services.

You Will get FREE prototype pcb from PCBWAY. So do not be late to register and place your first order from PCBWAY

If you want to order pcb from PCBWAY . CLICK IN THE LINK BELOW:

Some Sample PCB ‘ S From PCBWAY

Soil NPK Sensor

The soil npk sensor is suitable for detecting the content of nitrogen, phosphorus, and potassium in the soil, and judging the fertility of the soil. thereby facilitating the systematic evaluation of the soil condition. Can be buried in the soil for a long time, resistant to long-term electrolysis, corrosion resistance, vacuum potting, and completely waterproof. Soil npk sensors are widely used in soil nitrogen, phosphorus and potassium detection, precision agriculture, forestry, soil research, geological prospecting, plant cultivation and other fields.

Soil NPK Sensor

Soil NPK sensor features

1. Simple to use, few operation steps, fast measurement, no reagents, unlimited detection times.
2. High measurement accuracy, fast response speed, and good interchangeability.
3. The electrode is made of specially treated alloy material, which can withstand strong external impact and is not easy to damage.
4. Completely sealed, resistant to acid and alkali corrosion, and can be buried in soil for long-term dynamic testing.
5. The probe plug-in design ensures accurate measurement and reliable performance.

How do soil NPK sensors work?

The soil NPK sensor is suitable for detecting the content of nitrogen, phosphorus and potassium in the soil, and judges the fertility of the soil by detecting the conductivity transformation caused by different nitrogen, phosphorus and potassium concentrations in the soil.

How do soil NPK sensors work?

Soil npk sensor parameters

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

MAX485 TTL to RS-485 Interface Module

MAX485 TTL to RS-485 Interface Module

This is the MAX485 TTL to RS-485 interface module which is used to connect the Soil NPK Sensor with the Arduino as this interface module can be easily powered up using the Arduino’s 5 Volts. The max485 interface module is ideal for serial communications over long distances of up to 1200 meters or in electrically noisy environments, this is the reason it is commonly used in industrial environments. It supports up to 2.5MBit/Sec data rates, but as the distance increases, the maximum data rate that can be supported comes down. The RS-485 has the ability to communicate with multiple devices (up to 32) on the same Bus/cable when used in master and slave configuration. I have already written a detailed article on how to use the MAX485 interface module with Arduino and communicate with multiple controllers. So, I highly recommend reading this article.

KEY FEATURES OF MAX485 TTL TO RS-485 INTERFACE MODULE:

  • 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 OF MAX485 TTL TO RS-485 INTERFACE MODULE

PINOUT OF MAX485 TTL TO RS-485 INTERFACE MODULE

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

Now, let us interface the Soil NPK Sensor with the Arduino Nano Board using the MAX485 Modbus Module. Check the connection diagram below.

 interface the Soil NPK Sensor with the Arduino

Connect the R0 & DI pin of from the Modbus to D2 & D3 Arduino using Software Serial. Similarly, we have to enable DE & RE high. To do this connect the DE & RE Pins to the D7 & D8 pin of Arduino. The NPK Sensor has 4 wires. The brown one is VCC which needs a 9V-24V Power Supply. The GND pin which is black in color. So connect it to the GND of Arduino. The Blue wire which is the B pin is connected to the B pin of MAX485 & the Yellow Wire which is the A pin is connected to the A pin of MAX485.

Wiring Soil NPK Sensor

The 0.96″ SSD1306 OLED Display is an I2C Module. Connect the OLED Display VCC & GND pins to 3.3V & GND of Arduino. Similarly, connect its SDA & SCL pins to the A4 & A5 of Arduino. You can follow the circuit diagram & assemble the circuit on a breadboard or make a custom PCB.

Modbus Command for NPK Sensor

Modbus commands can instruct a Modbus Device to:
1. change the value in one of its registers, which is written to Coil and Holding registers
2. read an I/O port: Read data from Discrete and Coil ports,
3. command the device to send back one or more values contained in its Coil and Holding register

A Modbus command contains the Modbus address of the device it is intended for (1 to 247). The Modbus address is also called an inquiry frame. Only the addressed device will respond and act on the command, even though other devices might receive it.

The NPK Sensor has 3 different inquiry frame for reading the value of Nitrogen (N), Phosphorous (P) & Potassium (K). The inquiry frame is provided along with the instruction manual. For the NPK data the following individual inquiry frameworks:

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 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:

You can calculate the Soil Phosphorous from the Response you receive. For example, if you get 0030 as a response then Soil Nitrogen Value will be:
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:

You can calculate the Soil Potassium from the Response you receive. For example, if you get 0030 as a response then Soil Potassium Value will be:
0030 H(hexadecimal) =48 (Decimal) => Potassium = 48mg/kg

Source Code

The source code for interfacing Soil NPK Sensor with Arduino & retrieving Soil Nutrient value from the Sensor via Modbus command is given below. You can send the command and retrieve the value in HEX Code. The HEX code needs to be converted into Decimal to get the Measured Soil Nutrient content data.

Since we are using OLED Display to display the Soil Nutrient values (Nitrogen, Phosphorous & Potassium) in mg/kg, you will need OLED Library. Download the following OLED Library and add it to the Arduino IDE.

1. Adafruit SSD1306 LibraryDownload
2. Adafruit GFX LibraryDownload

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 (128x64)
  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];
}

Note: The code is the only capable of measuring the Soil NPK values up to 255mg/kg. This is because we are only reading 8bit value. According to the datasheet of the Sensor, you can measure the value up to 1999mg/kg. To read such value we have to read 16bit data. Refer to the Soil EC Sensor Code to read such high values.

Monitoring Soil NPK Data on OLED Display

Once you upload the code to Arduino Nano Board, the OLED will initialize along with the sensor. The sensor will take some time for stability and the reading may be incorrect initially.

Once the sensor gets becomes stable, you can dip the sensor in the soil to get the NPK Reading. The volume of Nitrogen, Phosphorous & Potassium which are the Ammonium content in the soil will be displayed as mg/Kg.

So this is how you interface Soil Nutrient Sensor Arduino & get the NPK Readings. Similarly, put the sensor in different samples of soil. You will see a variation in the volume of NPK depending upon the type of soil.

Manufacturing Files

If you don’t want to assemble the circuit on breadboard and you want PCB for the project, then here is the PCB for you.

Download Gerber File

The front side and back side of the PCB is given below.

Youtube Video

Recommended For You

About the Author: admin

Leave a Reply

Your email address will not be published.