Skip to content

analogReadMilliVolts() does not handle changes in resolution made by analogReadResolution() #8999

@bkari02

Description

@bkari02

Board

ESP32-S2

Device Description

Tested with Unexpected Maker FeatherS2 & senseBox MCU-S2 ESP32-S2

Hardware Configuration

Any analog sensor (e.g. Truebner SMT50 or SparkFun SEN-13322) is connected.

Version

v2.0.14

IDE Name

ArduinoIDE

Operating System

macOS 14.1.2

Flash frequency

80MHz

PSRAM enabled

yes

Upload speed

921600

Description

Expected behaviour:
When using ESP32-S2 and analogReadMilliVolts(pin) I should always get the measured voltage in milliVolts.

Weird behaviour: When changing the default 13 bit resolution of the ADC1 to e.g. 12bit using analogReadResolution(12); the outputs of analogReadMilliVolts(pin) are incorrect (half in size in case of 12bit).

Tried to track it down and found this to be happening (in esp32-hal-adc.c):
The __analogRead() function uses the 12 bit resolution (as expected because of analogReadResolution(12)). But __analogReadMilliVolts() internally also uses __analogRead() which returns a 12bit ADC value. But during transformation in esp_adc_cal_raw_to_voltage((uint32_t)adc_reading, &chars) the adc_reading is always expected to be the raw 13bit value in case of the ESP32-S2. There it assumes the 12 bit value to be 13 bit and the resulting value in milliVolts half of the correct value.

I assume this also happens with other chips than the S2, but haven't tested it so far.

Sketch

// Define the pin that the analog sensor is connected to (in my case A2 of the UM FeatherS2)
#define analogSensorPin A2

void setup() {
  // initialize serial communication at 115200 bits per second:
  Serial.begin(115200);
  delay(1000); 
  
  // set the resolution to the 13 bits (0-8192)  --> this is default, but just to make sure
  analogReadResolution(13);
  int analogValue = analogRead(analogSensorPin);
  int analogVolts = analogReadMilliVolts(analogSensorPin);
  Serial.printf("13 bit - ADC analog value = %d\n",analogValue);
  Serial.printf("13 bit - ADC millivolts value = %d\n",analogVolts);

  // set the resolution to 12 bits (0-4096)
  analogReadResolution(12);
  analogValue = analogRead(analogSensorPin);
  analogVolts = analogReadMilliVolts(analogSensorPin);
  Serial.printf("12 bit - ADC analog value = %d\n",analogValue);
  Serial.printf("12 bit - ADC millivolts value = %d\n",analogVolts);

    // set the resolution to 10 bits (0-1024)
  analogReadResolution(10);
  analogValue = analogRead(analogSensorPin);
  analogVolts = analogReadMilliVolts(analogSensorPin);
  Serial.printf("10 bit - ADC analog value = %d\n",analogValue);
  Serial.printf("10 bit - ADC millivolts value = %d\n",analogVolts);
}

void loop() {

}

Debug Message

Serial Output:
13 bit - ADC analog value = 2232
13 bit - ADC millivolts value = 711
12 bit - ADC analog value = 1104
12 bit - ADC millivolts value = 349
10 bit - ADC analog value = 278
10 bit - ADC millivolts value = 88

Other Steps to Reproduce

No response

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions