BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP32 Capacitive Soil Sensor Read Problem

Hello guys,

I’m searching a solve about this problem for a long time. I read all topics and tried many different variations but still cant fix it maybe i can learn my faults with your helps.

My capacitive soil sensor value is constant at 4095. If i scale it 0-100 it’s constant at 100. How can i see it correctly with all details?

I’m working with a mh-et live esp32 mini kit with 9V battery and regulating to 5V for esp32 and all sensors(tried with 3,3V esp pin for soil vcc and result still constant value). By the way DHT11 is working well.

Here’s my failed code for capacitive soil sensor;

#include <BlynkSimpleEsp32.h>
#include <WiFiClient.h>
#include <DHT.h>
#define BLYNK_PRINT Serial

#define DHTPIN 27
#define DHTTYPE DHT11
#define Nem 26
int Nemdegeri=0;

char auth[]="";
char ssid[]="";
char pass[]="";

BlynkTimer timer;
DHT dht(DHTPIN,DHTTYPE);

void Sicaklikveri(){
float h=dht.readHumidity();
float t=dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("DHT hatası!");
return;
}
Blynk.virtualWrite(V1,h);
Blynk.virtualWrite(V2,t);
}

void Nemveri(){
Nemdegeri=analogRead(Nem);
if (isnan(Nemdegeri)) {
Serial.println("Toprak sensör hatası!");
return;
}
Blynk.virtualWrite(V0,Nemdegeri);
}

void setup(){
Serial.begin(9600);
Blynk.begin(auth,ssid,pass);
dht.begin();
timer.setInterval(1000L,Sicaklikveri);
timer.setInterval(1000L,Nemveri);
pinMode(26,INPUT);
pinMode(27,INPUT);
}

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

Thanks :v:.

Not quite!

GPIO26 is an ADC2 pin, and shouldn’t be used as an analogue input when using WiFi.

Pete.

Also I tried GPI34 and 35 pins but still reading constant 4095. If I wont connect sensor the value is 0.
When sensor data pin connected to V0, value turns 4095.
My failed sketch with adc1;

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiClient.h>
#include <DHT.h>
#define BLYNK_PRINT Serial

#define DHTPIN 27
#define DHTTYPE DHT11
#define Nem 34
int Nemdegeri=0;

char auth[]="";
char ssid[]="";
char pass[]="";

BlynkTimer timer;
DHT dht(DHTPIN,DHTTYPE);

void Sicaklikveri(){
float h=dht.readHumidity();
float t=dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("DHT hatası!");
return;
}
Blynk.virtualWrite(V1,h);
Blynk.virtualWrite(V2,t);
}

void Nemveri(){
Nemdegeri=analogRead(Nem);
if (isnan(Nemdegeri)) {
Serial.println("Toprak sensör hatası!");
return;
}
Blynk.virtualWrite(V0,Nemdegeri);
}

void setup(){
Serial.begin(9600);
Blynk.begin(auth,ssid,pass);
dht.begin();
timer.setInterval(1000L,Sicaklikveri);
timer.setInterval(1000L,Nemveri);
pinMode(27,INPUT);
}

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

Can you post a picture of your sensor, and how it is connected?

Pete.

Here my ugly design :sweat_smile:


White regulator output gnd
Blue regulator output +
Green GPI34
and also i tried esp 3,3v pin to sensor vcc and esp gnd pin to sensor gnd.

It’s the soil sensor that I need to see in close-up, with enough detail to see the details of what how each pin is labelled, which may require a photo of both sides of the board.

Pete.

The dark side of the moon :metal:

Don’t read the sensors that often, especially not the DHT. It’s slow as hell and will give you other problems later on! :smiley:

If you try a bare minimum sketch, do you get correct readings from the sensor?

Google gave me some examples, change analogRead() to match your setup:

void setup() {
  Serial.begin(9600); // open serial port, set the baud rate as 9600 bps
}
void loop() {
  int val;
  val = analogRead(A0); //connect sensor to Analog 0
  Serial.println(val); //print the value to serial port
  delay(2000);
}

[/quote]

If you try a bare minimum sketch, do you get correct readings from the sensor?

Google gave me some examples, change analogRead() to match your setup:

void setup() {
  Serial.begin(9600); // open serial port, set the baud rate as 9600 bps
}
void loop() {
  int val;
  val = analogRead(A0); //connect sensor to Analog 0
  Serial.println(val); //print the value to serial port
  delay(2000);
}

[/quote]
Hello distans,
I observed that from serial monitor,
It shows data for a lil time(maybe not but looks like capacitor discharge chars)


And soil sensor close-up
I have 2 piece and same values for both of them.
Now know that is not blynk related and im not sure is it as it’s supposed to be :)) I’m on search.

Actually, I think the “sensor” (combined with a resistor) is acting like a simple voltage divider and are changing the reactance on the square wave generated by the 555 timer, which is the signal you’re trying to measure. I think!

Is the small 8-pin chip labeled NE555, TLC555 or another “555” combination?

I think you’re right! :stuck_out_tongue:

Ehh… Isn’t pinMode() only used for digital I/O? @PeteKnight?

Try deleting that line!

Yes, it’s a NE555 chip on this soil sensor.

I deleted pinmode line and dht still working well :v:

But soil sensor value constant 0 for now.

Crap! :thinking:

Try without the DHT?! Can you post your latest sketch?

Solo capacitive soil sensor result still same constant 0. :thinking:

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiClient.h>
#define BLYNK_PRINT Serial

int Nem=34;
int Nemdegeri=0;

char auth[]="";
char ssid[]="";
char pass[]="";

BlynkTimer timer;

void Nemveri(){
Nemdegeri=analogRead(Nem);
if (isnan(Nemdegeri)) {
Serial.println("Toprak sensör hatası!");
return;
}
Blynk.virtualWrite(V0,Nemdegeri);
}

void setup(){
Serial.begin(9600);
Blynk.begin(auth,ssid,pass);  
timer.setInterval(10000L,Nemveri);
}

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

I don’t have an ESP32 so I’m only guessing now… According to this pinout GPIO 34 isn’t an analog pin:

The NE555 neads at least 4.5V (max 16).

https://www.ti.com/lit/ds/symlink/ne555.pdf

I don’t have an ESP32 so I’m only guessing now… According to this pinout GPIO 34 isn’t an analog pin:

@PeteKnight reminded me about working on adc channels with wifi when i tried with adc2.
Thats mine;

That’s esp32 priority list shared by Andreas Spiess.
Source youtube link: Which ESP32 pins are safe to use?

I’m trying to supply soil sensor with 3,3V direct connection to esp pins like this;

And I’m following many links about capacitive soil sensor fails, fix, calibration… really confused for now.
Maybe my soil sensors need some modifications. :thinking:

You need more power!

Oh sorry, got it! :v:

Soil sensor input voltage 5V(MP1584EN output) and sensor value is constantly going to 0 to 4095. Its like ekg diagram when sensor stable in water or air. Maybe need to calibration now? :thinking:

There are other more advanced functions to use with the ADC pins that can be useful in other projects.

  • analogReadResolution(resolution): set the sample bits and resolution. It can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution.
  • analogSetWidth(width): set the sample bits and resolution. It can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution.
  • analogSetCycles(cycles): set the number of cycles per sample. Default is 8. Range: 1 to 255.
  • analogSetSamples(samples): set the number of samples in the range. Default is 1 sample. It has an effect of increasing sensitivity.
  • analogSetClockDiv(attenuation): set the divider for the ADC clock. Default is 1. Range: 1 to 255.
  • analogSetAttenuation(attenuation): sets the input attenuation for all ADC pins. Default is ADC_11db. Accepted values:
    • ADC_0db: sets no attenuation. ADC can measure up to approximately 800 mV (1V input = ADC reading of 1088).
    • ADC_2_5db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 1100 mV. (1V input = ADC reading of 3722).
    • ADC_6db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 1350 mV. (1V input = ADC reading of 3033).
    • ADC_11db: The input voltage of ADC will be attenuated, extending the range of measurement to up to approx. 2600 mV. (1V input = ADC reading of 1575).
  • analogSetPinAttenuation(pin, attenuation): sets the input attenuation for the specified pin. The default is ADC_11db. Attenuation values are the same from previous function.
  • adcAttachPin(pin): Attach a pin to ADC (also clears any other analog mode that could be on). Returns TRUE or FALSE result.
  • adcStart(pin), adcBusy(pin) and resultadcEnd(pin): starts an ADC convertion on attached pin’s bus. Check if conversion on the pin’s ADC bus is currently running (returns TRUE or FALSE). Get the result of the conversion: returns 16-bit integer.

Maybe you should look at increasing the analogSetCycles() setting to smooth out the readings?

Pete.

Hello guys,
Finally I fixed my adc (capacitive soil sensor) read problem.

Maybe this post helps those looking for solutions;

  1. First of all of course you need to use ADC1 pins if WIFI inuse
  2. ESP32 ADC channels need a voltage divider for reading values bigger than 3V. You can check this video for voltage divider;Tech Note 143 - ESP32 – Voltage Reading (pre-calculated voltage divider) Examples
  3. After all, i prefered to use [Arrays] for averaging adc values.

Now your ESP can read pretty well(as David says; “every ESP needs calibration if want to use ADC well” but divider worked for me".
Now I’m watching my capacitive soil sensors brilliant results, incredible! :metal:

1 Like