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

mariuselz
Posts: 9
Joined: Wed Dec 21, 2022 7:41 pm

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

Postby mariuselz » Wed Dec 21, 2022 7:56 pm

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

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » 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.)

mariuselz
Posts: 9
Joined: Wed Dec 21, 2022 7:41 pm

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

Postby mariuselz » Sat Dec 24, 2022 12:14 pm

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!

sbolliri
Posts: 1
Joined: Sun Jun 23, 2024 6:57 pm

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

Postby sbolliri » Sun Jun 23, 2024 7:00 pm

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?

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

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

Postby lbernstone » Fri Jun 28, 2024 3:58 pm

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); 
}

Who is online

Users browsing this forum: No registered users and 106 guests