Inverse values of soil moisture sensor

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// Replace with your Blynk authentication token
char auth[] = "your_blynk_auth_token";

// Replace with your WiFi credentials
char ssid[] = "your_wifi_ssid";
char pass[] = "your_wifi_password";

// DHT sensor setup
#define DHTPIN D4  // Pin where the DHT11 is connected
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Soil moisture sensor setup
#define SOIL_MOISTURE_PIN A0

// Relay module setup
#define RELAY_PIN D1
int relayState = LOW;

// Virtual Pins for Blynk app
#define MANUAL_MODE_PIN V0
#define SOIL_MOISTURE_PIN_V1 V1
#define TEMPERATURE_PIN_V2 V2
#define HUMIDITY_PIN_V3 V3

BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);

  dht.begin();

  // Set relay pin as output
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, relayState);

  // Setup a function to be called every 10 seconds
  timer.setInterval(10000L, sendSensorData);
}

void loop() {
  Blynk.run();
  timer.run();
}

void sendSensorData() {
  // Read soil moisture value
  int soilMoisture = analogRead(SOIL_MOISTURE_PIN);

  // Read temperature and humidity from DHT sensor
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();

  // Print sensor data to Serial Monitor
  Serial.print("Soil Moisture: ");
  Serial.print(soilMoisture);
  Serial.print(", Temperature: ");
  Serial.print(temperature);
  Serial.print(", Humidity: ");
  Serial.println(humidity);

  // Send data to Blynk app
  Blynk.virtualWrite(SOIL_MOISTURE_PIN_V1, soilMoisture);
  Blynk.virtualWrite(TEMPERATURE_PIN_V2, temperature);
  Blynk.virtualWrite(HUMIDITY_PIN_V3, humidity);

  // Check soil moisture and control relay
  if (soilMoisture < 500) {  // Adjust the threshold based on your sensor
    // Soil is dry, turn on the relay (watering)
    controlRelay(LOW);
  } else {
    // Soil is wet, turn off the relay
    controlRelay(HIGH);
  }
}

void controlRelay(int state) {
  if (state != relayState) {
    digitalWrite(RELAY_PIN, state);
    relayState = state;
    Blynk.virtualWrite(MANUAL_MODE_PIN, relayState);
  }
}

BLYNK_WRITE(MANUAL_MODE_PIN) {
  int manualMode = param.asInt();
  controlRelay(manualMode);
}

Can anyone please help me out and tell me why my soil moisture sensor is showing values of 700 in air while it shows 589 or somewhat like that in water.
I am using NODEMCU, DHT SENSOR, RESISTIVE SOIL MOISTURE SENSOR, AND 5V RELAY MODULE, programmed in aruduino ide

@P-p12 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

It would probably help if you provided a link to the actual sensor you are using, along with details of how it’s powered and connected to your NodeMCU.

Pete.

Sure, here are the pin connections for the NodeMCU, DHT11 sensor, soil moisture sensor, and relay module:

  1. NodeMCU Pin Connections:

    • D1 (GPIO 5): Relay module control pin
    • D4 (GPIO 2): DHT11 sensor data pin
    • A0: Soil moisture sensor analog output pin
  2. DHT11 Sensor Pin Connections:

    • Connect the VCC pin to 3.3V on NodeMCU
    • Connect the GND pin to GND on NodeMCU
    • Connect the data pin to D4 (GPIO 2) on NodeMCU
  3. Soil Moisture Sensor Pin Connections:

    • Connect the VCC pin to 3.3V on NodeMCU
    • Connect the GND pin to GND on NodeMCU
    • Connect the analog output pin to A0 on NodeMCU
  4. Relay Module Pin Connections:

    • Connect the VCC pin to 3.3V on NodeMCU
    • Connect the GND pin to GND on NodeMCU
    • Connect the control pin to D1 (GPIO 5) on NodeMCU

The nodemcu is powered by a powerbank.


Moreover i didnt understand what link to the sensor are you talking about…

I was hoping you’d provide a link to the manufacturer’s data sheet for the soil moisture sensor that you’re having issues with.

Pete.

Well i havent got that… it didnt come…it was a chinese company. I received it from amazon…

So maybe that’s how it’s meant to work and you just need to use the map function to invert the values?

Pete.

Well i did so but no change was observed.

Maybe you did it incorrectly then.

Pete.

So can you please give me the correct map function and where to put it in the code?

Well now I am using a capcitive soil moisture sensor but nodemcu still shows inverse values…

Okay, I’m not sure what you’re expecting from this Blynk community forum, but clearly you’re not going to get assistance with this issue unless you share far more information than you currently are.

Personally, If I have to go through a “20 questions” type of process each time you post something then I tend to disengage from the conversation because it’s too tedious.

Pete.

But sir I have sent you all of the info needed the codings and the images.

Digikeyhttps://media.digikey.com › s…PDFCapacitive Soil Moisture Sensor SKU:SEN0193
https://www.google.com/url?q=https://media.digikey.com/pdf/data%2520sheets/dfrobot%2520pdfs/sen0193_web.pdf&sa=U&sqi=2&ved=2ahUKEwjBpITs3pGEAxXkzTgGHUkhDU8QFnoECAoQAQ&usg=AOvVaw20vUdnGKub2vsI5ulfaKzE

Do you think that…

without any information about exactly WHAT you did, and…

without any information about that sensor and the results you are seeing qualifies as “all the info needed”?

The datasheet that you’ve since linked to explains that the the results you are seeing are indeed the expected results for this type of sensor…

  • Dry: (520 430]
  • Wet: (430 350]
  • Water: (350 260]

Pete.

Well, I saw the same sensor, the capacitive one, being used to collect data like 700 in water and 300 in air in other’s project. but mine didn’t do that, and moreover I am confused where to put in the map function in the first code I provided to inverse the values. Please help me just with that.

I’d put it immediatly after this line…

Pete.

Well thank you very much it worked flamboyantly…