Skip to content

ESP32-S2 analogRead function not returning correct value #5691

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
brentru opened this issue Sep 21, 2021 · 25 comments
Closed

ESP32-S2 analogRead function not returning correct value #5691

brentru opened this issue Sep 21, 2021 · 25 comments

Comments

@brentru
Copy link

brentru commented Sep 21, 2021

Hardware:

Board: Adafruit FunHouse ESP32-S2
Core Installation version: 2.0.0
IDE name: Arduino IDE
Flash Frequency: 80Mhz
PSRAM enabled: Yes
Upload Speed: 115200
Computer OS: macOS and Ubuntu

Description:

Calls to Arduino's analogRead (https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/) function are not working on BSP v2.0.0. It seems to always return a value of 0, instead of the correct value from the ADC. This functionality was working prior to 2.0.0.

image

Sketch: (leave the backquotes for code formatting)

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println(analogRead(A3));
  delay(100);
}
@felmue
Copy link

felmue commented Sep 22, 2021

Hello @brentru

do you know which GPIO corresponds to A3? I am only seeing A0 to A2 looking at the FunHouse pinout.

Some analog inputs share the same ADC module with WiFi and fail to work when WiFi is turned on at the same time. Have a look here.

That said, have you tried to turn off WiFi? Have you tried with a different analog input?

Thanks
Felix

@brentru
Copy link
Author

brentru commented Sep 22, 2021

@felmue - looking at the board variant (https://github.com/espressif/arduino-esp32/blob/master/variants/adafruit_funhouse_esp32s2/pins_arduino.h#L53), it'd be GPIO18.

Some analog inputs share the same ADC module with WiFi and fail to work when WiFi is turned on at the same time. Have a look here.

We've had success reading the analog input using the same code, prior to the v2.0.0 BSP update.

Have you tried with a different analog input?

We've also tried reading the value of a pot. with analog pin A0.

That said, have you tried to turn off WiFi?

I'm not sure how to turn off the ESP32's WiFi via the Arduino IDE.. The sketch attached with the issue doesn't perform a network setup.

@felmue
Copy link

felmue commented Sep 22, 2021

Hello @brentru

understood, afaik some WiFi stuff has been changed in v2.0.0 and now it can interfere with reading analog inputs.

Well, A0 and A3 are both using the same ADC module (ADC2) as WiFi so I'd expect it to fail as well. Could you try A1 or A2?

If in the past you've ran a sketch using WiFi there is a chance it is still running (in the background) even though your current sketch isn't using WiFi. You can try to stop WiFi using this: WiFi.mode(WIFI_OFF);

Thanks
Felix

@atanisoft
Copy link
Collaborator

@felmue WiFi and ADC is only a problem on the ESP32 and not ESP32-S2/ESP32-C3 chips.

@felmue
Copy link

felmue commented Sep 23, 2021

@atanisoft

thank you for sharing - I wasn't aware of that.

Any idea what the issue @brentru is facing could be?

@brentru : sorry, but I am out of ideas. Hopefully somebody else can help you.

Good luck!

Thanks
Felix

@atanisoft
Copy link
Collaborator

According to Adafruit FunHouse schematic A3 (GPIO18) is connected to a light sensor output pin with a 10k pull-down resistor.

Based on this, it would make sense that you are receiving a zero value for analogRead().

@felmue
Copy link

felmue commented Sep 23, 2021

Hello @atanisoft

but the light sensor should be able to pull it above 0 when it catches some light, wouldn't it?

And according to @brentru it worked with an earlier version of Arduino, e.g. before v2.0.0.

Thanks
Felix

@atanisoft
Copy link
Collaborator

Depends on the sensor, it also couldn't have worked in v1.0.x as the S2 wasn't supported in that version. Since I don't have the board I can't do any testing.

@ladyada
Copy link
Contributor

ladyada commented Sep 24, 2021

@felmue @atanisoft do you have other ESP32-S2 boards that you can test analog input with?

@atanisoft
Copy link
Collaborator

@ladyada I haven't tested ADC on the S2 in a while but last I checked it was working with the Saola and WROVER modules that I have. There have also been a few issues filed about ADC attenuation defaults not working correctly and the ADC returning 4095 (max value). But in this case it is returning zero which indicates that either the wrong pin is being read or it is remaining at GND.

@ladyada
Copy link
Contributor

ladyada commented Sep 24, 2021

@atanisoft ok - did you try analog input with 2.0.0? if not, is there someone who can try to replicate this bug so it can be assigned?

@chegewara
Copy link
Contributor

chegewara commented Sep 24, 2021

Yes, i can confirm there is at least 1 bug. By default it is setup 12 bit width, but:

For ESP32s2, only Two Point values calibration and only ADC_WIDTH_BIT_13 is supported. The parameter default_vref is unused.

and i am getting this error:

E (58030) ADC: adc2_get_raw(578): WIDTH ERR: see `adc_bits_width_t` for supported bit width
[ 58490][E][esp32-hal-adc.c:174] __analogRead(): GPIO18: ESP_ERR_INVALID_ARG

EDIT
here is this bug described and how to solve it, now only PR is required:
#5658

@felmue
Copy link

felmue commented Sep 24, 2021

Hello guys

I ran some tests with an ESP32-S2-Kaluga-1 board and can confirm that the issue seems to be related to the analog resolution. The only resolution working for GPIOs using ADC2 seems to be 13. Unfortunately analogReadResolution(13) doesn't seem to help. Below code fails with this error:
E (100335) ADC: adc2_get_raw(578): WIDTH ERR: see 'adc_bits_width_t' for supported bit width

void setup()
{
  Serial.begin(115200);
  analogReadResolution(13);
}

void loop()
{
  Serial.println(analogRead(18));
  delay(2000);
}

However I was able to read the analog value from GPIO18 with the following code. Inspired by the ESP-IDF adc2 example.

#include <driver/adc.h>

int read_raw;
esp_err_t r;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  // ADC2_CHANNEL_7 is GPIO18
  r = adc2_get_raw(ADC2_CHANNEL_7, ADC_WIDTH_BIT_13, &read_raw);
  if ( r == ESP_OK ) {
      printf("%d\n", read_raw);
  } else if ( r == ESP_ERR_INVALID_STATE ) {
      printf("%s: ADC2 not initialized yet.\n", esp_err_to_name(r));
  } else if ( r == ESP_ERR_TIMEOUT ) {
      //This can not happen in this example. But if WiFi is in use, such error code could be returned.
      printf("%s: ADC2 is in use by Wi-Fi.\n", esp_err_to_name(r));
  } else {
      printf("%s\n", esp_err_to_name(r));
  }
  delay(2000);
}

Thanks
Felix

@chegewara
Copy link
Contributor

The only resolution working for GPIOs using ADC2 seems to be 13. Unfortunately analogReadResolution(13) doesn't seem to help.

Resolution is 13 bits, but value to pas is 4:
#define ADC_WIDTH_BIT_13 4
https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/adc.html#_CPPv416adc_bits_width_t

The ESP32-S2 integrates two 13-bit SAR (Successive Approximation Register) ADCs

https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/adc.html#overview

@felmue
Copy link

felmue commented Sep 24, 2021

Hello @chegewara

thank you for the information. According to the description analogReadResolution() takes a value in # of bits.

That said I actually tried to pass both values (13 and 4) to analogReadResolution(). The error is the same in both cases.

Thanks
Felix

@chegewara
Copy link
Contributor

chegewara commented Sep 25, 2021

I tested by myself, before ive been told about this issue already opened:
#5658 (comment)

All i had to do is to change this value to 4:
https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-adc.c#L49
I am working with idf-master branch if that helps.

EDIT and maybe set pin mode pinMode(pin, ANALOG), but im not sure, it was rush test

@felmue
Copy link

felmue commented Sep 25, 2021

Hello @chegewara

yes, I concur, modifying the __analogWidth value in esp32-hal-adc.c for ESP32s2 from 3 to 4 resolves the issue.

Thanks
Felix

@caternuson
Copy link
Contributor

caternuson commented Sep 25, 2021

Hello. I also have an Adafruit FunHouse ESP32-S2 and can recreate the issue in the original post.

I also just tested the one line change suggested above (change 3 to 4) and it does appear to fix the issue. Using same sketch in original post, now get varying output from the light sensor attached to A3:
Screenshot from 2021-09-25 08-34-59

@ladyada
Copy link
Contributor

ladyada commented Sep 25, 2021

hihi @me-no-dev do you need a PR to fix?

@me-no-dev
Copy link
Member

@ladyada it will be greatly appreciated and will save some time :)

@ladyada
Copy link
Contributor

ladyada commented Sep 27, 2021

@chegewara @felmue can either of you submit a PR for the one-line change you made so @me-no-dev can merge the fix in

@chegewara
Copy link
Contributor

@ladyada There is few espressif employes working on arduino-esp32 and i believe they can do such simple PR.
Ive been asked by @atanisoft to check if there is bug and i found it, thats it.

@atanisoft
Copy link
Collaborator

Since this seems to be related to #5658 it likely can be closed as duplicate and then use that issue instead for tracking. It has a code diff in it but it will break ESP32-C3, so that will need to be covered in any PR for this.

@ladyada
Copy link
Contributor

ladyada commented Sep 29, 2021

@me-no-dev hiya there are now multiple PRs!

@VojtechBartoska
Copy link
Contributor

as there is a PR which didn't sync. I'm closing this manually, if needed you can reopen this. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants