PLEASE HELP! ESP32 not reading analog sensors

Hi!
After finding a ESP WROOM 32 DEV KIT in my sock draw, I thought of a little project for feeding and giving controlled light with UV and IR LEDs ect to my chilli plants.
As I have 3 plants and would like to use several analog inputs and outputs for this a D! mini wouldn’t be sufficient and the ESP32 would be perfect.
I have give it a quick test using several capacitive soil moisture sensors and I can’t get it to read a value at all! but if I try a sketch that doesn’t use Blynk and just reads through the analog pin and shows the values on the serial monitor, they all work fine.

I just don’t understand why I can’t get it to work through Blynk. is it me? as I am a complete novice. Is it the ESP32 giving me some payback for locking it in a sock draw for god knows how long? or is it that they just don’t work with Blynk?

Here is the code for just trying to read from one capacitive soil sensor…


#define BLYNK_PRINT Serial

int Soil1 = 15;
int Soil2 = 2;
int output_value;
int moisturelevel;

const long eventTime_soil1 = 1000;
unsigned long previousTime_1 = 0;

#include <Wire.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***********************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "****************";
char pass[] = "***********";





void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);


}

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

void soil1(){
  
   output_value = analogRead(Soil1);
   moisturelevel = constrain ( map(output_value, 1000, 4095, 100, 0), 0, 100);
    unsigned long currentTime = millis();
   if ( currentTime - previousTime_1 >= eventTime_soil1) {
    Blynk.virtualWrite(V1, moisturelevel );
    previousTime_1 = currentTime;
   }
 // Blynk.virtualWrite(V1, Soil1);


}

void soil2()
{
  
 
}

I think I’d start by adding a pinMode declaration to your void setup to declare pin soil1 as an input.

Also, you shouldn’t really be calling your soil function(s) from your void loop, or be messing around with Millis comparisons. Much better to use a BlynkTimer for that instead…

Pete.

Hi Pete,
I have cleared my void loop and removed Millis.
I still have a problem with reading from that pin. I have changed to pin 39 and it works fine.
but anything that is on CH2 doesn’t work.
I have zero experience with the esp32.
any ideas?
thanks for the help!

Mark

Note: ADC2 pins cannot be used when Wi-Fi is used. So, if you’re using Wi-Fi and you’re having trouble getting the value from an ADC2 GPIO, you may consider using an ADC1 GPIO instead, that should solve your problem.

Pete.

The Guy with the Swiss Accent also did a great tutorial video re ESP32 GPIO pin functions and uses.

cul
billd

2 Likes

Thanks! Ill give it a watch.