Page 1 of 1

Approach for use ADC2 and Wifi on ESP32

Posted: Tue Nov 18, 2025 7:34 pm
by undeporaki
Hello, this is my first post here. I am firmware programmer for years and I want to ask if there is a well-know way to use ADC2 and Wifi on a standar ESP32. I know there is not possible simultaneusly because the design of the wifi core but I would like to have an procedure trying to stop wifi, use adc2, and the reinit the wifi. In my mind seems ok, but in my implementation it is not working. In fact, in my current project I do not need to have wifi permantly on, just turn on when needed to send some data and then stop but I really need to have adc2 as much time as possible...

I tried this in 5.4, 5.5 and the newer 6.0 versions of idf:

Code: Select all

void InitWiFi() {
  const wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
  

  ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));

  esp_netif_config_t netif_config = ESP_NETIF_DEFAULT_WIFI_STA();
  esp_netif_t *netif = esp_netif_new(&netif_config);
  assert(netif);

  ESP_ERROR_CHECK(esp_netif_attach_wifi_station(netif));
  ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ip_event_t::IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
  ESP_ERROR_CHECK(esp_wifi_set_default_wifi_sta_handlers());
  ESP_ERROR_CHECK(esp_wifi_set_storage(wifi_storage_t::WIFI_STORAGE_RAM));

  wifi_config_t wifi_config;
  memset(&wifi_config, 0, sizeof(wifi_config));
  strncpy(reinterpret_cast<char*>(wifi_config.sta.ssid), WIFI_SSID, strlen(WIFI_SSID) + 1);
  strncpy(reinterpret_cast<char*>(wifi_config.sta.password), WIFI_PASSWORD, strlen(WIFI_PASSWORD) + 1);

  ESP_LOGI("MAIN", "Connecting to %s...", wifi_config.sta.ssid);
  ESP_ERROR_CHECK(esp_wifi_set_mode(wifi_mode_t::WIFI_MODE_STA));
  ESP_ERROR_CHECK(esp_wifi_set_config(wifi_interface_t::WIFI_IF_STA, &wifi_config));
  ESP_ERROR_CHECK(esp_wifi_start());
  ESP_ERROR_CHECK(esp_wifi_connect());
}

void StopWifi() {
    ESP_ERROR_CHECK(esp_wifi_disconnect());
    ESP_ERROR_CHECK(esp_wifi_stop());
    ESP_ERROR_CHECK(esp_wifi_deinit());
}

void read_adc2(void)
{
    adc_init();
    vbat=Vbat_meas();    
    vcc=Vcc_meas();
    ESP_ERROR_CHECK(adc_oneshot_del_unit(adc2_handle));
}
if I do something like this, it fails on the second InitWifi, rebooting:

Code: Select all

read_adc2();  // first ac2 readings ok
 InitWiFi();
.... wait IP...
.... IP is ok...
... do some stuff
StopWifi();
read_adc2(); // this second adc2 reading is succesful
InitWifi();  // this second InitWifi causes rebooting the board.
Any ideas?

Best regards!

Re: Approach for use ADC2 and Wifi on ESP32

Posted: Wed Nov 19, 2025 4:58 am
by nopnop2002
To use ADC2 and WiFi simultaneously, you need to initialize WiFi and ADC2 as shown below, then stop WiFi with esp_wifi_stop() before reading ADC2.

WiFi connection is restored with esp_wifi_start().

I based on this.
https://github.com/espressif/esp-idf/tr ... ed/station
https://github.com/espressif/esp-idf/tr ... eshot_read

Code: Select all

void app_main(void)
{
    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    wifi_init_sta();

    //-------------ADC2 Init---------------//
    adc_oneshot_unit_handle_t adc2_handle;
    adc_oneshot_unit_init_cfg_t init_config2 = {
        .unit_id = ADC_UNIT_2,
        .ulp_mode = ADC_ULP_MODE_DISABLE,
    };
    ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config2, &adc2_handle));

    //-------------ADC2 Calibration Init---------------//
    adc_cali_handle_t adc2_cali_handle = NULL;
    bool do_calibration2 = example_adc_calibration_init(ADC_UNIT_2, ADC_CHANNEL_0, ADC_ATTEN_DB_12, &adc2_cali_handle);

    //-------------ADC2 Config---------------//
    adc_oneshot_chan_cfg_t config = {
        .atten = ADC_ATTEN_DB_12,
        .bitwidth = ADC_BITWIDTH_DEFAULT,
    };
    ESP_ERROR_CHECK(adc_oneshot_config_channel(adc2_handle, ADC_CHANNEL_0, &config));

    ESP_ERROR_CHECK(esp_wifi_stop());

    ESP_ERROR_CHECK(adc_oneshot_read(adc2_handle, ADC_CHANNEL_0, &adc_raw[1][0]));
    ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT_2 + 1, ADC_CHANNEL_0, adc_raw[1][0]);
    if (do_calibration2) {
        ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[1][0], &voltage[1][0]));
        ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT_2 + 1, ADC_CHANNEL_0, voltage[1][0]);
    }

    ESP_ERROR_CHECK(esp_wifi_start());
}


Re: Approach for use ADC2 and Wifi on ESP32

Posted: Wed Nov 19, 2025 8:36 am
by undeporaki
You are right nopnop2002!. It is working as you said, I prepared a fw based on your explanation and it is working without any issue.

Maybe I was trying to "uninstall" the wifi and doing a complete init of the wifi, and just needed to do start/stop. Or even a wrong wifi initialization (I copied from an example of thingsboard and maybe it is not totally ok).

Again, thank you!