Page 1 of 1

Read battery voltage on GPIO 41 (ESP32-S3 and Arduino IDE)

Posted: Wed Dec 21, 2022 7:56 pm
by mariuselz
Hello,

I have built a code for reading a battery voltage - initially on pin GPIO_NUM_35 on normal ESP32
There was no setup for this pin, I only did analogRead(GPIO_NUM_35) and I was getting some values (0-4095) that afterwards I convert in voltage.

My problem is, I have the same code but on ESP32-S3 and GPIO_NUM_41
The code was not modified, the hardware itself was not modified - only the ESP32 was upgraded to ESP32-S3.
analogRead(GPIO_NUM_41) is returning 0 all the time.

It seems the default of this pin is MTDI (and not GPIO) and the GPIO default function is F0 (type I1 - input only). I see type I/O/T is for function F1, but I guess input only is fine for what I need. So not sure if I need any configuration or something.

Does anyone have any idea why analogRead is retuning 0 all the time on this pin?
Is it because there is no Analog Function on ESP32-S3 GPIO 41?

Any help is very much appreciated!

Thank you,
Marius

Re: Read battery voltage on GPIO 41 (ESP32-S3 and Arduino IDE)

Posted: Thu Dec 22, 2022 2:36 am
by ESP_Sprite
mariuselz wrote:
Wed Dec 21, 2022 7:56 pm
Is it because there is no Analog Function on ESP32-S3 GPIO 41?
Exactly that. If you want a pin with an ADC function, check the ESP32-S3 datasheet and pick one of the pins that also have ADC1_CHx as a function. (ADC2 is not really usable on the S3 iirc.)

Re: Read battery voltage on GPIO 41 (ESP32-S3 and Arduino IDE)

Posted: Sat Dec 24, 2022 12:14 pm
by mariuselz
ESP_Sprite wrote:
Thu Dec 22, 2022 2:36 am
mariuselz wrote:
Wed Dec 21, 2022 7:56 pm
Is it because there is no Analog Function on ESP32-S3 GPIO 41?
Exactly that. If you want a pin with an ADC function, check the ESP32-S3 datasheet and pick one of the pins that also have ADC1_CHx as a function. (ADC2 is not really usable on the S3 iirc.)
That was it. I changed the pin while waiting for the post to be approved and is all good.
Thank you for reconfirming!

Re: Read battery voltage on GPIO 41 (ESP32-S3 and Arduino IDE)

Posted: Sun Jun 23, 2024 7:00 pm
by sbolliri
Hello,

I have the same problem. I'm tryng to make a voltage meter for a battery using a Tension Partitor.

i'm connecting the partitor to PIN 2 but the voltage when i switch on the ESP32 at the output of the partitor change.

Can i ask you how your have solve?

Re: Read battery voltage on GPIO 41 (ESP32-S3 and Arduino IDE)

Posted: Fri Jun 28, 2024 3:58 pm
by lbernstone
Your translation is incorrect. The term you are looking for is voltage divider.
Use resistors to get the output voltage to be about 0.7V at your full range. This will give you the greatest accuracy. I use 10000 & 2200 ohm for a lithium battery. Then, set the attenuation to 0db and read the voltage.

Code: Select all

#define PIN_BATTERY 7
void setup() {
  Serial.begin(115200);
  analogSetAttenuation(ADC_0db);
}

void loop() {
  Serial.printf("reading: %lumV\n", analogReadMilliVolts(PIN_BATTERY));
  Serial.printf("battery voltage: %lumV\n", analogReadMilliVolts(PIN_BATTERY) * (10000 + 2200) / 2200);
  delay(1000); 
}