NPKCODE and Lcd

I tried to find the NPK code for the three-wire model as shown in this picture and found this code. I fixed it and then tried to upload the code to the board. What happened was that it displayed the values ​​on the LCD screen normally, but the values ​​kept going up and down and not staying still. Is this normal? Or what should I do to display the results on the screen?

LCD

  • = 5V
  • = GND
    SDA = P21
    SCL = P22

NPK

  • = 3.3V
  • = GND
    Signal = P34

This code

//Code NPK

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//------------ LCD ---------------------------
LiquidCrystal_I2C lcd(0x27, 20, 4); // PCF8574 = 0x27

const int analogPinNPK = 34; // Define the analog pin for soil fertility measurement

unsigned long Fertility;
unsigned long Nitrogen, Phosphorus, Potassium;

// Define constants for range calculations
const unsigned long FERTILITY_MAX = 100;
const unsigned long MAX_NITROGEN = 300;
const unsigned long MAX_PHOSPHORUS = 20;
const unsigned long MAX_POTASSIUM = 300;

void setup(void) {
    lcd.init();
    lcd.backlight(); // Ensure the backlight is on (if applicable)
    lcd.setCursor(0, 0); lcd.print("Fertility  =    %  ");
    lcd.setCursor(0, 1); lcd.print("Nitrogen   =   ppm");
    lcd.setCursor(0, 2); lcd.print("Phosphorus =   ppm");
    lcd.setCursor(0, 3); lcd.print("Potassium  =   ppm");
}

void loop(void) {
    Fertility = Read_Fertility();

    // Clear previous values
    lcd.setCursor(13, 0); lcd.print("    ");
    lcd.setCursor(13, 1); lcd.print("    ");
    lcd.setCursor(13, 2); lcd.print("    ");
    lcd.setCursor(13, 3); lcd.print("    ");

    // Display Fertility
    lcd.setCursor(13, 0); lcd.print(Fertility);

    // Calculate nutrient levels based on fertility
    if (Fertility <= 20) {
        Nitrogen = (Fertility * 10) / 4; // 0-50
        Phosphorus = Fertility / 5;     // 0-4
        Potassium = (Fertility * 10) / 4;
    } else if (Fertility <= 70) {
        Nitrogen = ((Fertility - 20) * 3) + 50; // 50-200
        Phosphorus = ((Fertility - 20) / 5) + 4; // 5-14
        Potassium = ((Fertility - 20) * 3) + 50;
    } else if (Fertility <= 100) {
        Nitrogen = ((Fertility - 70) * 10) + 200; // 200-300
        Phosphorus = ((Fertility - 70) / 5) + 14;  // 14-20
        Potassium = ((Fertility - 70) * 10) + 200;
    }

    // Display Nitrogen, Phosphorus, and Potassium
    lcd.setCursor(13, 1); lcd.print(Nitrogen);
    lcd.setCursor(13, 2); lcd.print(Phosphorus);
    lcd.setCursor(13, 3); lcd.print(Potassium);

    delay(10000); // Update every second
}

unsigned long Read_Fertility() {
    unsigned long i, total = 0;
    for (i = 0; i < 20; i++) {
        total += analogRead(analogPinNPK);
        delay(1);
    }
    unsigned long average = total / 20;

    // Map the raw sensor values to fertility percentage
    if (average >= 620) {
        return ((average - 620) / 9) + 93;
    } else if (average >= 490) {
        return ((average - 490) / 8) + 77;
    } else if (average >= 360) {
        return ((average - 360) / 8) + 59;
    } else if (average >= 250) {
        return ((average - 250) / 7) + 47;
    } else if (average >= 170) {
        return ((average - 170) / 5) + 31;
    } else if (average >= 90) {
        return ((average - 90) / 5) + 16;
    } else if (average >= 10) {
        return ((average - 10) / 5);
    }
    return 0; // Return 0 if value is less than 10
}

Please post full schematics.

Where did you take this sketch? How did you connect to sensor?
These 'things' should be using an 'ac waveform' to measure between the two electrodes, so if you are reading the voltage on one electrode it is normal that you see a not steady value.

P.S.
add only that the measurement from these sensor can't be considered reliable

Is the "screen" the "meter" on the analyzer with "alkaline/acidic"?

I'm sorry, I'm not good at English and confused. What I want to communicate is (I can connect many modules to the code and display the results). But what I'm stuck on is the npk sensor that I show in the picture above. I can display the measured value on the LCD. But the measured value is very messy. For example, if I leave it alone, the measured value will still swing and not come. It's all messed up!!! But what I noticed is that on the screen of the NPK sensor, the needle that points to the values ​​​​is still there and does not change the value. While the LCD screen, the value of NPK is all messed up!!! "Simply put, how can I display the correct value?"

Additional... I tried using a multimeter to measure the DC voltage coming out of the sensor. What I found was that there was normal power.

Is the sensor broken or is it my fault?

I am very new to this content, can you explain how I can connect the NPK sensor to the ESP32 so that I can get the correct values ​​displayed on the LCD?

The NPK sensor probably never "worked" and will never work. Not your fault. You did nothing to make it fail.

If I change the sensor to the model in this picture, will it be easier to use? Because when I search for information, I mostly find this model.

The chemists on this forum seem to be saying: You can not measure Nitrogen, Phosphorus and Potassium content in soil with an electrical probe. The numbers are generated and fake. None are good. I trust two Arduino content makers: RandomNerdTutorials and DroneBotWorkshop. Neither cover the NPK.

If you must use the probe... fine ONE of the tines (pointy things) that can sense moisture (higher conductivity) and just use that data and make up what the data mean.

Thank you very much for your advice. You gave me good new knowledge, but I just want to study this temporarily for knowledge. I am not serious.

***In my understanding, I can connect in this order, right?
(NPK sensor -> RS485 Module -> Esp32 -> code) In this order, do I understand correctly?

The RS485 is a communications module. If the sensor has a RS485, and you follow a good tutorial, you should be able to receive the sensor data. Try this one:

thank you very much.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.