# ESP32 analogRead() doesn't work

**URL:** <https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345>\
**Category:** 3rd Party Boards\
**Created:** [September 20, 2023, 11:28am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345 "2023-09-20T11:28:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bibb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bibb/32/932226_2.png) [@bibb](https://forum.arduino.cc/u/bibb)\
**Post date:** [September 20, 2023, 11:28am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/1 "2023-09-20T11:28:22Z")

</div>

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?

```arduino
// 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);
}

```

---

<div class="post-metadata">

**Author:** ![ksoderby](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ksoderby/32/468872_2.png) [@ksoderby](https://forum.arduino.cc/u/ksoderby)\
**Post date:** [September 20, 2023, 1:06pm UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/2 "2023-09-20T13:06:01Z")

</div>

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](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/clk_tree.html))

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

---

<div class="post-metadata">

**Author:** ![bibb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bibb/32/932226_2.png) [@bibb](https://forum.arduino.cc/u/bibb)\
**Post date:** [September 21, 2023, 6:09am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/3 "2023-09-21T06:09:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [September 21, 2023, 6:19am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/4 "2023-09-21T06:19:24Z")

</div>

Start simple

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

---

<div class="post-metadata">

**Author:** ![ubidefeo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ubidefeo/32/217164_2.png) [@ubidefeo](https://forum.arduino.cc/u/ubidefeo)\
**Post date:** [September 21, 2023, 6:22am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/5 "2023-09-21T06:22:25Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [September 21, 2023, 6:24am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/6 "2023-09-21T06:24:53Z")

</div>

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](https://randomnerdtutorials.com/esp32-pinout-reference-gpios/)  
Scroll down for the section " Analog to Digital Converter (ADC)".

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [September 21, 2023, 8:16am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/7 "2023-09-21T08:16:39Z")

</div>

> [@Koepel](#):
>
> ADC2 is not available when the Wifi is turned on.  
> The analog input of the pin GPIO15 is using ADC2.

From Net:

 ![esp32ADC2](https://europe1.discourse-cdn.com/arduino/original/4X/5/b/6/5b668bef21f1d309056037be1ec8d95a8454787c.png)  
 ![esp32ADC2-2](https://europe1.discourse-cdn.com/arduino/original/4X/2/a/4/2a4da4e5f330877fdf8e170ba75a7e1bb7da4d35.png)

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

> **[esp32\_hardware\_design\_guidelines\_en.pdf](https://www.espressif.com/sites/default/files/documentation/esp32_hardware_design_guidelines_en.pdf)**

---

<div class="post-metadata">

**Author:** ![bibb](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/bibb/32/932226_2.png) [@bibb](https://forum.arduino.cc/u/bibb)\
**Post date:** [September 21, 2023, 9:15am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/8 "2023-09-21T09:15:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 19, 2024, 9:15am UTC](https://forum.arduino.cc/t/esp32-analogread-doesnt-work/1170345/9 "2024-03-19T09:15:35Z")

</div>

Closed on March 19, 2024, 9:15am UTC

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