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
}