ESP32 analogRead() doesn't work

Hi, the analogRead() function works whenever I comment out the setup() part. I've tried commenting out chunks of setup() but in vain. Could somebody help please?

// Import required libraries
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
#include <Wire.h>

float voltage = 0;
int analogInput = 15;

const char* ssid = "***";
const char* password = "***";

// Create AsyncWebServer object on port 80
AsyncWebServer server(80);

void setup() {
  /*
  Some problem in this setup() function causing ESP32 to not read Analog at pin 4, but I don't know why yet
  */
  // Serial port for debugging purposes
  Serial.begin(115200);

  // Initialize SPIFFS
  if(!SPIFFS.begin()){
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while(WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  // Print ESP32 Local IP Address
  Serial.println(WiFi.localIP());

  // Route for root / web page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send(SPIFFS, "/index.html");
  });
  server.on("/voltage", HTTP_GET, [](AsyncWebServerRequest *request){    
    request->send_P(200, "text/plain", String(voltage).c_str());
  });

  // Start server
  server.begin();
}

void loop() {  
  // read the input on analog pin 15:
  int sensorValue = analogRead(analogInput);
  // Convert the analog reading (which goes from 0 - 4095) to a voltage (0 - 3.3V):
  voltage = sensorValue * (3.3 / 4095.0);
  Serial.println(voltage);
  delay(2);
}

Hi @bibb , when you say "pin 15" do you mean:

  • Nano ESP32's pin 15 - not accessible on the board, it is the "green LED".
  • ESP32-S3's (MCU) GPIO15 aka XTAL_32K_P which is reserved for an external 32 kHz clock signal (read more)

Have you tried another pin (e.g. A0, A1)?

Hi, I am actually using ESP32, not the NanoESP32, sorry for the confusing category, I couldn't find one that matched.
Yes, I have tried different (ADC) pins. The problem remains.

Start simple

Write a small sketch that simply reads the pin that you want to test and prints the value

@bibb

it really depends on which board you are using.
consider that you'll need to look at the manufacturer's schematic to find out which physical pin on the board is connected to the ESP32's GPIO15 pin, also minding that the specific pin is allowed to function as an ADC pin

Welcome to the forum.

ADC2 is not available when the Wifi is turned on.
The analog input of the pin GPIO15 is using ADC2.

See: ESP32 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials
Scroll down for the section " Analog to Digital Converter (ADC)".

4 Likes

From Net:


Quote form Espressif Documents:
• ADC1 is recommended over ADC2 as the latter cannot be used when Wi-Fi function is enabled.

3 Likes

Thank you so much! This solved everything I asked for.

1 Like

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