I've been having this problem for weeks in displaying the right values from my 6 in 1 soil sensor. I've read so many forums and tried all of them but the output is still the same.
This is always the output that I am receiving.
When it comes to the schematic diagram, I followed this schematic from How2Electronics.
I used the same components in the schematic and connected my sensor to 12V and I have them all in common ground.
This is the code that I used in getting the values from the sensor.
#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 };
const byte ph[] = { 0x01, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x0b }; // New
const byte mois[] = { 0x01, 0x03, 0x00, 0x12, 0x00, 0x01, 0x24, 0x0F };
const byte temp[] = { 0x01, 0x03, 0x00, 0x13, 0x00, 0x01, 0x75, 0xcf }; // New
byte values[11];
SoftwareSerial mod(2, 3);
float envhumidity = 0.0, envtemperature = 0.0, soil_ph = 0.0, soil_mois = 0.0, soil_temp = 0.0; // New
byte val1 = 0, val2 = 0, val3 = 0, val4 = 0, val5 = 0, val6 = 0, val7 = 0; // New
void setup() {
Serial.begin(4800);
mod.begin(4800);
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();
// put RS-485 into receive mode
digitalWrite(DE, LOW); // New
digitalWrite(RE, LOW); // New
delay(3000);
}
void loop() {
byte val1, val2, val3, val4, val5, val6;
val1 = nitrogen();
delay(300);
val2 = phosphorous();
delay(300);
val3 = potassium();
delay(300);
val4 = phydrogen() / 10; // New
soil_ph = val4;
delay(300);
val5 = moisture();
soil_mois = val5 / 10.0;
delay(300);
val6 = temperature();
soil_temp = temperature() / 10.0; // New
delay(300);
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");
Serial.print("ph: "); // New
Serial.print(soil_ph);
Serial.println(" ph");
Serial.print("Moisture: ");
Serial.print(soil_mois);
Serial.println(" %"); //
Serial.print("Temperature: ");
Serial.print(soil_temp);
Serial.println(" C");
Serial.println(); // New
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 5);
display.print("N: ");
display.print(val1);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(1);
display.setCursor(0, 15);
display.print("P: ");
display.print(val2);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(1);
display.setCursor(0, 25);
display.print("K: ");
display.print(val3);
display.setTextSize(1);
display.print(" mg/kg");
display.setTextSize(1); // New
display.setCursor(0, 35);
display.print("pH: ");
display.print(soil_ph);
display.setTextSize(1);
display.print(" pH");
display.setTextSize(1);
display.setCursor(0, 45);
display.print("Moisture: ");
display.print(soil_mois);
display.setTextSize(1);
display.print(" %");
display.setTextSize(1);
display.setCursor(0, 55);
display.print("Temperature: ");
display.print(soil_temp);
display.setTextSize(1);
display.print(" C"); // New
display.display();
}
byte nitrogen() {
// clear the receive buffer
mod.flush();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(nitro); i++) mod.write(nitro[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte phosphorous() {
mod.flush();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(phos); i++) mod.write(phos[i]);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte potassium() {
mod.flush();
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
for (uint8_t i = 0; i < sizeof(pota); i++) mod.write(pota[i]);
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(200);
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
// Serial.print(values[i], HEX);
// Serial.print(' ');
}
return values[4];
}
byte phydrogen() {
// clear the receive buffer
mod.flush();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(ph); i++) mod.write(ph[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(1000);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[4];
}
byte moisture() {
// clear the receive buffer
mod.flush();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(mois); i++) mod.write(mois[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(1000);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[4];
}
byte temperature() {
// clear the receive buffer
mod.flush();
// switch RS-485 to transmit mode
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(1);
// write out the message
for (uint8_t i = 0; i < sizeof(temp); i++) mod.write(temp[i]);
// wait for the transmission to complete
mod.flush();
// switching RS485 to receive mode
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
// delay to allow response bytes to be received!
delay(1000);
// read in the received bytes
for (byte i = 0; i < 7; i++) {
values[i] = mod.read();
Serial.print(values[i], HEX);
Serial.print(' ');
}
return values[3] << 8 | values[4];
}
Hope that someone can help me because it's been giving me headaches for weeks and the outputs are still the same and I can't figure out what to do in order to troubleshoot this anymore.