analogRead returns 0 with wifi code added

Using my ESP32 Wroom and a moisture sensor works flawlessly but as soon as I add the wifi module it returns 0's.

#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "ssid";
const char* password = "1234";

const char* serverName = "http://maker.ifttt.com/trigger/plants/with/key/1234";
const int sensorPinMoist = 26;
int sensorPinMoistValue = 0;

# define relayPinSensor 33
# define relayPinPump 25
# define sensorPinMoist 26

int trigger = 300; // set the level

void setup()
{
  Serial.begin(115200);
  //wifi
  WiFi.begin(ssid, password);  // <-----------------------!! this line causes the sensorPinMoist to return 0
  // Serial.println("Connecting");
  // while(WiFi.status() != WL_CONNECTED) {
  //   delay(500);
  //   Serial.print(".");
  // }
  pinMode(relayPinSensor, OUTPUT); // turn on/off Sensor
  pinMode(relayPinPump, OUTPUT);    // turn on/off Pump  
}

void loop()
{
  //turn off Moisture Sensor (prevent corrosion)
  digitalWrite(relayPinSensor, LOW);
  Serial.println("");  
  delay(10000); 

  //turn on Moisture Sensor (prevent corrosion)
  digitalWrite(relayPinSensor, HIGH);
  //check moist value:  
  delay(3000);
  Serial.print("Moisture Sensor Value:");
  sensorPinMoistValue = analogRead(sensorPinMoist);
  Serial.println(sensorPinMoistValue);

  if (sensorPinMoistValue > trigger) 
  {
    // pump for ## seconds
    digitalWrite(relayPinPump, HIGH);
    delay(3000);
    digitalWrite(relayPinPump, LOW);
  }
  else
  {
    digitalWrite(relayPinPump, LOW);
    Serial.println("Pump standing by...");
    delay(3000); 
  }
}

what am i missing?

I don't know, but I'd guess the wifi uses pin 26 for something. Try using a different one for your sensor. Or look up which pins the wifi does actually need.

Nah, WiFi is integrated into the platform. AFAIK it doesn't occupy any of the GPIOs that are wired to external pins.

Don't know, but I'm missing a schematic of your project and details of components.

Which ESP32 do you have?
Bits and pieces of info. Typically, analog pins are on portB which is are pins 32 and up.
There are 2 A:D converts on the ESP32. Typically the 2nd A:D converter is disabled during WiFi. The 2nd AD converter uses portA or pins less then 32.

schematic with ESP32-WROOM-32 dev. board:

ESP32-30PIN-DEVBOARD copy

Sensor:

API Reference - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)

Analog to Digital Converter (ADC) - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)


ADC1:

  • 8 channels: GPIO32 - GPIO39

ADC2:

  • 10 channels: GPIO0, GPIO2, GPIO4, GPIO12 - GPIO15, GOIO25 - GPIO27

  • Since the ADC2 module is also used by the Wi-Fi, only one of them could get the preemption when using together, which means the adc2_get_raw() may get blocked until Wi-Fi stops, and vice versa.

Now that you are using the ADC2 converter. Now the documentation has this to say bout WiFi and ADC2

  • Since the ADC2 module is also used by the Wi-Fi, only one of them could get the preemption when using together, which means the adc2_get_raw() may get blocked until Wi-Fi stops, and vice versa.

Here is a list of ADC2 pins

Notice how pin 26 falls within that range of being on ADC2?

And what happens to ADC2 with WiFi use?

I've tried GPIO4, GPIO39. None of them worked

Post an image of your project. post a schematic.

You did not post that code.

See above. I replaced the GPIO numbers. Nothing spectacular.

Oh well I tried. I cannot see the image well enough to read the pin number, even after zooming in. Good luck.

Here you go

Try this connection:

  • Sensor VCC to 3.3V
  • Sensor GND to GND
  • Sensor A0 connected to GPIO 34, 35, 36 or 39.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.