Soil moisture sensor calibrating

Hi there,
I try to do a node mcu project, to read humidity and temp data from DHT (this thing works just great) and read my plant soil moisture (problem point)


Moisture sensor is connected to A0 pin and returns me ~200 in water and 400 dry, how to remap it to virtual pin to show data from 0 when dry to 100 when wet?

Here is my code

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "DHT.h"
#include <I2CSoilMoistureSensor.h>
#define DHTPIN 2 // The pin you connect to
#define DHTTYPE DHT11   // DHT 11 Change this if you have a DHT22
DHT dht(DHTPIN, DHTTYPE,16); // Change this to 

int Sensorvalue=0;
int sensorPin=A0;
int sensorValuePercent=0;

SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d4d23273fdcc459e85aff835ae30db28";
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "YK", "ykompan!");
  timer.setInterval(2000, sendDHT);
}

void sendDHT()
{
//Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  int hum = (int) h;
  int tem = (int) t;
//Write values to V04 and V05
  Blynk.virtualWrite(4, hum);
  Blynk.virtualWrite(5, tem);
  
}
void Moisture()
{
  Sensorvalue = analogRead(A0);
  Sensorvalue = map(Sensorvalue, 0, 1023, 1023, 0);
  sensorValuePercent = (Sensorvalue / 1023) * 100; // In Prozent
  Blynk.virtualWrite(V1, Sensorvalue);
}

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

Thank you in advance, im steel a noobie, and learning.

1 Like

Your exact problem was covered in details a few days ago. Search the site for the solution.

That guy did not post answer, and he haв it from 0 to 1230, he just wanted to turn it round, and I need to change a scale somehow, also tries mapping from that post, but receive no value to virtual pin 1 at all

You looked at this thread and all the links contained in it, right?

yes, I included this code, but it doesnt return me any value to V1, I edited the post with latest code

Forget Blynk until you know how the Arduino functions work.

Study map() on Arduino and test by sending output to Serial Monitor.

When you can do that move back to Blynk and V1.

1 Like

Alreafy did that, and cant figure out what is wrong, that is why created this post

Please confirm you have spent some time reading this https://www.arduino.cc/en/Reference/Map

Its 1 page long, sure i did it

Ok forget the sensor.

int x = 200;

void sendDHT(){
  int y = map(x, 400, 200, 0, 100);
  if(x < 400){
    x= x + 50;  
  }
  else{
    x = 200;
  }  
  Serial.println(y);
}

Gives the following Serial Monitor output:

[26961] Ready (ping: 111ms).
Connected OK
100
75
50
25
0
100
75
50
25
0
100

Look ok?

Is this approximately 200 or minus 200?

I did my code, now works perfect
Thank you Costas

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include "DHT.h"
#include <I2CSoilMoistureSensor.h>
#define DHTPIN 2 // The pin you connect to
#define DHTTYPE DHT11   // DHT 11 Change this if you have a DHT22
DHT dht(DHTPIN, DHTTYPE,16); // Change this to 

int x = analogRead(A0);

SimpleTimer timer;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d4d23273fdcc459e85aff835ae30db28";
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "YK", "ykompan!");
  timer.setInterval(2000, sendDHT);
}

void sendDHT()
{
//Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  int hum = (int) h;
  int tem = (int) t;
//Write values to V04 and V05
  Blynk.virtualWrite(4, hum);
  Blynk.virtualWrite(5, tem);
    timer.setInterval(2000, moisture);
}
  void moisture(){

  int x = analogRead(A0);
  int y = map(x, 600, 230, 0, 100);
  if (y > 100)
{
  y = 100;
}
   Blynk.virtualWrite(V1, y);
}
  void loop()
{
  Blynk.run();
  timer.run();
}`Preformatted text`

Glad we got there and thanks for posting your working sketch.

Edit: you shouldn’t need this bit:

  if (y > 100)
{
  y = 100;
}

as the map() function restricts the range of 0 to 100. I was using the x variable (not y) in the if statement as I don’t have a sensor so I was just simulating the values.

In my simulation I now have the following, using 250 as it makes the +50 easier to work with.

int x = 250; // global variable

  int y = map(x, 600, 250, 0, 100);
  if(x < 600){
    x= x + 50;  
  }
  else{
    x = 250;
  }

Serial Monitor:

[26930] Ready (ping: 113ms).
Connected OK
100
85
71
57
42
28
14
0
100
85
71
57
42
28
14
0
100

do you have the library? i cant up load it need some help please.