ESP_ERR_TIMEOUT: ADC2 is in use by Wi-Fi only with PlatformIO

Hello,
I have written a small piece of code to (for the moment) connect a DHT11 module to the Arduino IOT Cloud.

[ 11220][E][esp32-hal-adc.c:192] __analogRead(): GPIO0: ESP_ERR_TIMEOUT: ADC2 is in use by Wi-Fi. Please see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/adc.html#adc-limitations for more info

I keep getting this error. I tried an empty code (just WiFi and Arduino Cloud connection (the basic sketch that Arduino provides)) and this error is still here. I tried using the Arduino IDE and with this one, I don’t get this error anymore.

main.cpp:

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
  https://create.arduino.cc/cloud/things/5dc6e150-06bd-463d-802a-24e6ade1cc63

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes
  are made to the Thing

  int random_value;
  bool led_switch;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have
  functions which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this
  sketch.
*/

#include "devices.h"
#include "thing/thingProperties.h"
#include "thing/variables.h"

#include <Arduino.h>
#include <NexgenTimer.h>
#include <WiFi.h>

#define LED 2
bool led_switch;

NexgenTimer dht_timer = NexgenTimer();

void setup() {
   /*
  The following function allows you to obtain more information
  related to the state of network and IoT Cloud connection and errors
  the higher number the more granular information you’ll get.
  The default is 0 (only errors).
  Maximum is 4
 */
   setDebugMessageLevel(2);

   Serial.begin(9600);
   // pinMode(LED, OUTPUT);

   // Initialize serial and wait for port to open:

   // This delay gives the chance to wait for a Serial Monitor without blocking
   // if none is found
   delay(1500);

   // Defined in thingProperties.h
   initProperties();

   // Connect to Arduino IoT Cloud
   ArduinoCloud.begin(ArduinoIoTPreferredConnection);

   while (WiFi.status() != WL_CONNECTED) {
      ArduinoCloud.update();
      delay(1000);
      Serial.println("Connecting to WiFi...");
   }

   ArduinoCloud.printDebugInfo();

   setupDHT11_sensor();

   dht_timer.expiredHandler(readDHT11_sensor);
   dht_timer.setInterval(5000);
   dht_timer.start();
}

void loop() {
   ArduinoCloud.update();
   
   if ((ArduinoCloud.connected() == 0)) {
      Serial.println("Waiting for connection to Arduino Cloud ...");
   } else {
      dht_timer.run();
   }
}

/*
  Since LedSwitch is READ_WRITE variable, onLedSwitchChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLedSwitchChange() {
   // Add your code here to act upon LedSwitch change
   if (led_switch) {
      digitalWrite(LED, HIGH);
   } else {
      digitalWrite(LED, LOW);
   }
}

devices.cpp:

#include "devices.h"
#include "thing/variables.h"

DHT_Unified dht(DHTPIN, DHTTYPE);

CloudTemperatureSensor temperature;
CloudRelativeHumidity humidity;

void setupDHT11_sensor() {
   sensor_t sensor;
   dht.begin();

   dht.temperature().getSensor(&sensor);
   dht.humidity().getSensor(&sensor);
}

void readDHT11_sensor(NexgenTimer &nt) {
   Serial.println("[INFO] readDHT11_sensor() called.");
   sensors_event_t event;
   dht.temperature().getEvent(&event);
   temperature = event.temperature; // Read temperature as Celsius

   dht.humidity().getEvent(&event);
   humidity = event.relative_humidity;       // Read Humidity

   // Check if any reads failed and exit early (to try again).
   if (isnan(humidity) || isnan(temperature)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
   }
}

Or, here is the link to see the code on Arduino IDE : Arduino Cloud

Do you have any idea how I can stop having this error?

Thank you for your help!

If it only happens in PlatformIO, then let’s investigate that. What Arduino-ESP32 core version is used when you compiled in in the cloud, or locally in the Arduino IDE?

I’m using a DOIT ESP32 Devkit V1

Here is my platformio.ini

[env:esp32doit-devkit-v1]
platform = espressif32
board = esp32doit-devkit-v1
framework = arduino
lib_deps = 
	adafruit/DHT sensor library@^1.4.3
	arduino-libraries/ArduinoIoTCloud@^1.6.1
	bitbucket-davidsuch/NexgenTimer@^1.0.1
build_flags = -D MQTT_MAX_PACKET_SIZE=1024 -D MQTT_MAX_TRANSFER_SIZE=128 -D CLOUD4RPI_DEBUG=1
lib_ignore = WiFiNINA

And my Arduino IDE parameters:
image

Hm it doesn’t show the crucial Arduino-ESP32 core version.

Upload the PlatformIO project with platform = espressif32@3.5.0 (instead of platform = espressif32) once and once with platform = espressif32@4.4.0. Do the firmwares behave differently?

The Arduino-ESP32 core version is 4.4.0, so with espressif32@4.4.0 I have the same errors. And with espressif32@3.5.0 I’m missing freertos/FreeRTOS.h and esp_err.h.

Can you please compile the code locally in the Arduino IDE per Installing — Arduino-ESP32 2.0.6 documentation and check if it exhibits the same error or not? Same core version should lead to same results with PlatformIO :confused:

I don’t have the problem with the Arduino software :face_with_diagonal_mouth:

Is this Arduino IDE v2.0 or the Arduino IDE cloud or Arduino IDE 1.x?

I still couldn’t find out what core version of Arduino-ESP32 the working firmware uses. 2.0.2? 2.0.1? 1.0.6?

The screenshot is Arduino IDE cloud.

From pio :

 - framework-arduinoespressif32 @ 3.20003.220619 (2.0.3)
 - tool-esptoolpy @ 1.30300.0 (3.3.0)
 - toolchain-xtensa-esp32 @ 8.4.0+2021r2-patch3

Exactly, and it doesn’t say specifically what core version is used :frowning:. Hence why I want you to install the Arduino IDE 1.x software locally so that we know exactly which core is used “as intended” and can try 2.0.3, 2.0.2, or 2.0.1. (apparently it doesn’t build with 1.0.6 so that kinda rules it out)

I just noticed that the problem did not appear on the Arduino IDEs because they force error messages not to appear (except on the software where you can modify it).

With this setting I now have the errors.
image

Okay so the behavior is equal.

I think to solve the real error you would have to switch to a different ADC pin from ADC1 (so GPIO32 - GPIO39), not ADC2 (not usable when WiFi is on). See Page not Found - ESP32 - — ESP-IDF Programming Guide latest documentation.

I’m not using ADC … (DHT only uses digital if I understood correctly). I still tried to use the ADC1 pins but it didn’t work

I’m even having this error with an empty sketch … (only wifi and arduino cloud connection)

I see. Maybe some library is using an analogRead() to draw initial entropy for “something”, e.g. cryptographic orother randomization. I’ll have to look into the library you’re using.

If you want to supress the error message (of course not the cause) in PlatformIO, do per documentation with build_flags = -DCORE_DEBUG_LEVEL=0.

That’s what I think too, I don’t really have the C++ skills for that :face_with_diagonal_mouth:

Thanks !