Skip to content

Conflict Between Wifi & analogRead()? #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kherNI opened this issue Dec 15, 2016 · 94 comments
Closed

Conflict Between Wifi & analogRead()? #102

kherNI opened this issue Dec 15, 2016 · 94 comments
Assignees
Labels
Type: For reference Common questions & problems
Milestone

Comments

@kherNI
Copy link

kherNI commented Dec 15, 2016

Is anyone seeing a conflict with analogRead() values when #include <WiFi.h> is included in a sketch? analogRead works great without WiFi.h included; however, if WiFi.h is included then the analogRead values returned are all maxed out (4096 values). Note I've tested on pins 12, 14, 26 & 27.

@palomarcoder
Copy link

I found a note here that may be relevant: https://www.espressif.com/sites/default/files/documentation/esp32_hardware_design_guidelines_en.pdf

Pin1, Pin43 and Pin46 are the analog power supply pins. Pin3 and Pin4 are the power supply pins for the power amplifiers. It should be noted that the sudden increase in current draw, when ESP32 is in transmission mode, may cause a power rail collapse. Therefore, it is highly recommended to add another 0603 10 uF capacitor to the power trace, which can work in conjunction with the 0402 0.1 uF capacitor.

@JeroenVanOort
Copy link

I've been having trouble with this as well. After a lot of trial and error I have found out that analogRead will work on some pins while using WiFi, 32 and 36 are examples of those pins. I hope this helps when trying to find out why this happens.

@me-no-dev me-no-dev added the Type: For reference Common questions & problems label Feb 23, 2017
@nissimzur
Copy link

Hi
It seems the ESP32 pin 32 is been used as SDO to flash. Are you sure you can use is as analogRead input?
Please call my Skype nissim.test

@igrr
Copy link
Member

igrr commented Apr 19, 2017

This happens because ADC2 pins can not be used when WiFi is used.

On the other hand, ADC1 pins can be used even when WiFi is enabled.

@hkotkanen
Copy link

hkotkanen commented May 13, 2017

@JeroenVanOort thank you kindly, switched to a pin you indicated and got mine working.

@igrr many thanks for the information! Your statement is so casual and matter-of-fact, but this thread seems to be then only place that documents this ...feature. Was I supposed to have found it out from another source?

@CarlosGS
Copy link

A few months ago I got around this issue by periodically soft-restarting the ESP32 to measure from ADC2; then turning on wifi to send the data.

Unfortunately this has been broken in latter updates, right now if you issue esp.restart() after turning on wifi, the ADC2 no longer works. Now it is needed to physically disconnect power.

Any thoughts on why esp.restart() doesn't restore all radio registers to their default state?

@me-no-dev
Copy link
Member

There is a fix coming for ADC2 and WiFi soon :)

@pieman64
Copy link

@me-no-dev any news on the ADC2 fix?

@JulienHai
Copy link

any news? when will adc2 be fixed?

@palomarcoder
Copy link

Any estimated time? I was really hoping to use this in a project over the holiday.

@JasonWolfson
Copy link

Hi,
I am unable to read the analogRead value for GPIO 33 pin on ESP-WROOM-32. I am using Arduino IDE. Any help with a sample code?

Thanks,
Anusha

@beegee-tokyo
Copy link
Contributor

beegee-tokyo commented Jan 4, 2018

GPIO33 should be ok to use, it is connected to ADC1. Check Wiki.

Extract of my code to read an analog value from a LDR:

int ldrPin = 33;
void setup() {
  pinMode(ldrPin,INPUT);
  adcAttachPin(ldrPin);
  analogReadResolution(11);
  analogSetAttenuation(ADC_6db);
}
void loop() {
  Serial.println(analogRead(ldrPin));
  delay(2000);
}

@JasonWolfson
Copy link

Thank you @beegee-tokyo. I will try that. I was not using "adcAttachPin(ldrPin);", probably that is why I was unable to read the correct values.

@JasonWolfson
Copy link

@beegee-tokyo A quick question. Do I have to use any libraries or header files to include analogSetAttenuation(ADC_6db); and adcAttachPin(ldrPin);?

Thanks,
Anusha

@beegee-tokyo
Copy link
Contributor

You might need to include esp32-hal-adc.h

@JasonWolfson
Copy link

@beegee-tokyo I am not getting stable values. They change and they don't make sense. Please check my code,
#include "esp32-hal-adc.h" // to read analog values

int pin =33;

void setup() {
Serial.begin(115200);
pinMode(19, OUTPUT);
pinMode(pin, INPUT);
digitalWrite(19,1);
adcAttachPin(pin);
analogReadResolution(11);
analogSetAttenuation(ADC_6db);
}

void loop() {
digitalWrite(19,0);
for(int i = 0;i<10;i++){
delay(2000);
Serial.print("INA0_OUT:"); // print it
Serial.println(analogRead(pin));
}
delay(5000);
digitalWrite(19,1);
for(int i = 0;i<10;i++){
delay(2000);
Serial.print("INA1_OUT:");
Serial.println(analogRead(pin));
}
delay(3000);

}
and The output is:
INA0_OUT:492
INA0_OUT:481
INA0_OUT:488
INA0_OUT:511
INA0_OUT:568
INA0_OUT:784
INA0_OUT:552
INA0_OUT:549
INA0_OUT:562
INA0_OUT:573
INA1_OUT:2047
INA1_OUT:2047
INA1_OUT:2047
INA1_OUT:2047
INA1_OUT:1925
INA1_OUT:2047

Am I doing something wrong?

@beegee-tokyo
Copy link
Contributor

I tried your code and found no problems. Output:

Testing with out = 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0
INA1_OUT1 ADC5: 0

Testing with out = 1
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047
INA0_OUT ADC5: 2047

Did you measure the voltage on GPIO19? Is it changing from 3.3V to 0V?
Did you measure the voltage on ADC5/GPIO33? Is it changing from 3.3V to 0V?
Either your module is somehow busted or the connection between the output 19 and analog input 33 is bad.
Can you try another output pin and another analog input?

@twanek
Copy link

twanek commented Jun 17, 2018

@me-no-dev:

"There is a fix coming for ADC2 and WiFi soon :)"

any progress on this? we would need more than 6 analog inputs in our project. there is any workaround for this?

thanks!

@cgonzaloch
Copy link

Hi all!

Any update on this? is there any fix already for ADC2 and WiFi support?

Thanks in advance!

@tavdog
Copy link

tavdog commented Nov 10, 2018

I've tried turning off WIFI and then doing analogRead(A0), but it still read the wrong value. Why doesn't turning off WIFI work ? Is there something more to do than WiFi.mode(WIFI_OFF); ?

@markingle
Copy link

Any progress on this issue? I am trying to use the pulse counter function along with Bluetooth but the counter hangs when BT starts up. I suspect the use of the same power rail is causing my problems too.

@intellica
Copy link

@me-no-dev any news on the ADC2 and Wifi fix?

@danielcolchete
Copy link

For the record, resetting registers SENS_SAR_START_FORCE_REG and SENS_SAR_READ_CTRL2_REG to the old values they had before WiFi was turned seem to be a work around for this issue. And will allow you to use ADC2 again after you stop and deinitialize WiFI.

An algorithm here would be something like:
old1 <- SENS_SAR_START_FORCE_REG
old2 <- SENS_SAR_READ_CTRL2_REG
InitWifi
StartWifiAndConnect
(Do your wifi thing here)
StopWifiAndDisconnect
DeinitWifi
SENS_SAR_START_FORCE_REG <- old1
SENS_SAR_READ_CTRL2_REG <- old1

@danielcolchete
Copy link

I forgot to include the SENS_SAR_MEAS_START2_REG register. This one is important too.

@boddydan
Copy link

For anyone struggling with the saving and restoring of register states method, I had the code working perfectly on my computer but then reinstalled windows and the ESP32 once again no longer worked with ADC2 readings. Turns out I'd installed an updated version of the esp32 boards within the Arduino IDE
(from https://dl.espressif.com/dl/package_esp32_index.json). - esp32 version 1.0.6 did not work with this method for me. I tried installing 1.0.3 which amazingly worked like a switch. Apologies I wasn't able to test 1.0.4 and 1.0.5 as I was in a rush but hopefully this will prove helpful to someone.

@VojtechBartoska
Copy link
Contributor

We won't fix this. We will implement diagnostic message in 2.0.0.

@dev-rijan
Copy link

dev-rijan commented May 27, 2021

I also tried the saving and restoring of register states method and it works on esp32 version 1.0.3. Is there any bug on 1.0.6?

@tablatronix
Copy link
Contributor

I never got it to do anything , ill test again one day and update what I find

@SmartBoy84
Copy link

SmartBoy84 commented Jun 12, 2021

I can't get this to work on an esp32 cam (pin 14)
I am trying the reset register method but I still get "ADC2 is in use by WiFi"

@edstoica solution relies on "esp32-hal-adc.h" which doesn't seem to be available in the Arduino IDE?

@VojtechBartoska
Copy link
Contributor

@me-no-dev
Copy link
Member

@VojtechBartoska done :)

@fkoteam
Copy link

fkoteam commented Jul 16, 2021

i don't understand. is it done improving the error message? what about the workaround proposed in this thread and the message (written in 2017) "There is a fix coming for ADC2 and WiFi soon :)"?

@me-no-dev
Copy link
Member

@fkoteam unfortunately a stable solution was not found. That is why we resorted to better error message.

@CarlosGS
Copy link

CarlosGS commented Jul 21, 2021

Would you be so kind to provide more details? (i.e. is our only alternative to keep using 1.0.3/1.0.4?)

Context: I would like to re-use many boards (update the software) instead of dropping them in the bin.

@txf-
Copy link

txf- commented Jul 22, 2021

Would the workaround still work with the latest SDK? I have a project that uses the workaround, which works well for my purposes, but would it break?

@me-no-dev
Copy link
Member

why would many devices stop working? Did you design your boards based on that message from 4 years ago?

@palomarcoder
Copy link

I certainly did. Why is that unreasonable? The esp32 was the chip of dreams - 18 ADC, built in wifi, and fast. I designed 3 projects around it. I assumed the features would work together, and I assumed the bugs would be fixed. I appreciate the community support, but what company releases a major SOC and assumes the community will find workarounds to all the problems? The boards still sit languishing on the bench. I'll end up throwing it all away and waiting for esp64, or maybe some day have time to redesign the projects for multiple esp32s.

@Tobbera
Copy link

Tobbera commented Jan 8, 2022

I can confirm that this is working as a workaround.

// Initiate Wifi with ADC2 on ESP32 workaround bellow:
#include "soc/sens_reg.h"    // needed for manipulating ADC2 control register
uint32_t adc_register;
uint32_t wifi_register;
// End initiation of Wifi with ADC2 on ESP32 workaround.

setup:
Serial.begin(115200);
  adc_register = READ_PERI_REG(SENS_SAR_READ_CTRL2_REG); // Wifi with ADC2 on ESP32 workaround.
*** Connect WiFi / Bluetooth stuff here ***
  wifi_register = READ_PERI_REG(SENS_SAR_READ_CTRL2_REG); // Wifi with ADC2 on ESP32 workaround.

loop:
  WRITE_PERI_REG(SENS_SAR_READ_CTRL2_REG, adc_register); // Wifi with ADC2 on ESP32 workaround.
  SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);// Wifi with ADC2 on ESP32 workaround.
*** Analog read stuff here***
  WRITE_PERI_REG(SENS_SAR_READ_CTRL2_REG, wifi_register); // Wifi with ADC2 on ESP32 workaround.

BUT, its broken in the newer version of ESP32 in arduino. This works on 1.0.3 at the moment. I cant understand why they took it away.

@Tobbera
Copy link

Tobbera commented Jan 28, 2022

Another follow up on this:

When using the workaround above the ADC reading reports approx 21mV higher value at 2500 mV compared to not running WiFi at all.

EDIT: And this offset seem to affect BOTH ADC channels, not only the ADC channel that is affected by the this issue. The ESP32 MCU is puzzling me more and more....

@cyberman54
Copy link
Contributor

@Tobbera i can confirm the ~21mV Diff, for BOTH adc channels.

@janioanselmo
Copy link

Tobbera

Hello, I confirm your information! I've been migrating my project from version 1.0.4 to version 2.0.2 for a few weeks and unfortunately, without success so far. With information from Mr. @Tobbera we have had success up to v 1.0.4. For higher versions, I still can't get Wifi + analogRead() to work;

@janioanselmo
Copy link

@Tobbera, another follow up on this:
I just did tests on the new version 2.0.3 and apparently the same thing, still doesn't work (Wifi + AnalogRead).

@pdsmart
Copy link

pdsmart commented May 11, 2022

I can add another issue I've been tracking the last few days, hi-speed digital signals on ADC2 inputs (configured as digital inputs) prevents WiFi Station from establishing a connection. WiFi Access Point has no such issues.

The only solution I have come up with at the moment is to temporarily set ADC2 ports to output (the design uses limiting resistors so this is possible), make a WiFi Station connect with router then reset ADC2 ports to input. Once the Station has a session with a router there are no further issues. (albeit I find enabling the WiFi causes false interrupts to trigger on a neighbouring ESP32 on IO33!!). Not strictly capturing analogue as mentioned in the OP post but all on the same theme, an issue regarding WiFi and ADC2.

I'm using IDF v4.4 with Arduino compatibility installed. SoC is an ESP32-S AI Thinker WROOM.

@svdrummer
Copy link

Are these all Arduino wrapper issues or silicon issues?

@NuclearManD
Copy link

I'm pretty sure they're silicon issues @svdrummer. Hopefully someone can correct me if I'm wrong.

brentru pushed a commit to adafruit/arduino-esp32 that referenced this issue Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: For reference Common questions & problems
Projects
None yet
Development

No branches or pull requests