Esp32 5V Power Level Measurement

I would like to power ESP32 over USB connector, from one USB power bank. Altough is in the power bank probably some 2000 mAh 18650 3.7V battery, it gives 5 V on the usb output.

Is it somehow possible to measure battery level directly on the ESP32, without additional electronic components?

Practically no. If battery voltage is above 3.3V, ADC will still measure only up to 3.3V. Above 3.6V it's also abusing your MCU max voltage and possibly/probably damaging your Esp32.

What's the point of this, curiosity?
With two resistors you could make a voltage divider and measure battery voltage.

1 Like

Consider using a voltage sensing module to monitor battery levels with an Arduino. Here’s are some thoughts and potential problems.

Things to Consider

Voltage Sensing Module:
Voltage sensing modules like the one described here are great for reading battery voltages. They typically consist of a voltage divider circuit to step down the input voltage to a level that the Arduino can safely read (0-5V for most Arduinos).

These modules do draw a small current continuously, even when the Arduino is off. This is due to the resistive nature of the voltage divider.

Power Consumption Considerations:
If the current drawn by the voltage sensing module is a concern, you can use a MOSFET or relay to disconnect the sensor from the battery when not in use.

A small N-channel MOSFET like the 2N7000 can be used to switch the low side (ground) of the sensor module.

Alternatively, a P-channel MOSFET can be used on the high side (positive voltage) with proper gate control to minimize the current draw when the Arduino is off.

A relay could also work, but it's bulkier and consumes more power when activated.

Switching Options:

High-Side Switching with a MOSFET:

Using a P-channel MOSFET to control the high side of the sensor module is a common approach. Ensure the MOSFET is fully turned off when the gate is pulled to the battery voltage, and turned on when the gate is pulled low by the Arduino.

Low-Side Switching with a MOSFET:
An N-channel MOSFET on the ground side of the module can be simpler to implement, but it may cause issues with the ground reference of the sensor and the Arduino, especially if they share the same ground.

Relay Consideration:
Relays are easier to implement for beginners but are less efficient and slower compared to MOSFETs. Use a relay module if simplicity is the primary concern and power consumption is less critical.

Code Implementation:
In your Arduino code, you can control the MOSFET or relay using a digital output pin to turn on/off the sensor module only when a voltage reading is needed.

Example Code Snippet for Using a MOSFET:

int sensorPin = A0; // Analog input pin connected to the voltage sensor
int mosfetPin = 7; // Digital pin to control the MOSFET

void setup() {
  pinMode(mosfetPin, OUTPUT);
  digitalWrite(mosfetPin, LOW); // Turn off the sensor initially
  Serial.begin(9600);
}

void loop() {
  digitalWrite(mosfetPin, HIGH); // Turn on the sensor
  delay(100); // Wait for stabilization
  int sensorValue = analogRead(sensorPin); // Read the voltage
  float voltage = sensorValue * (25.0 / 1023.0); // Convert to actual voltage
  Serial.println(voltage);
  digitalWrite(mosfetPin, LOW); // Turn off the sensor to save power
  delay(5000); // Delay between readings
}

Additional Information/Help:

Voltage Sensing with Arduino – How to Electronics
[Controlling MOSFETs with Arduino](https://www.elect

A powerbank tries to maintain it's output at 5volt, so that voltage won't give you any information about the state of the battery. To measure true battery level, you must have access to the battery, which is usually not possible with a powerbank.

A calculated guess is that an average ESP32 could run more than 12 hours from a good 2Ah powerbank.
Leo..

I expected there is access to the battery.
Otherwise my post is not valid.

OK, it goes not this way, so I will use other approach. Actually, I need 5V to power two motors over L298N driver. For this, I can use 2 18650 batteries (3.7V each). But I would also like to know level of the batteries, so I can put it to charge... What is good practice for it?

2 cells make 7.4V nominal in series, up to 8.4V when fully charged. L298N has 1-2V voltage drop. Is your motor ok with these levels?
You can measure battery voltage like posted before, but not without "extra components". You need to connect GND between battery circuit (L298N) and arduino.

Your description is quite confusing, it would be easier if you post your circuit idea and specs of your components

I found solution how to get 3V to power ESP32, and 5V to power motors. And there is also direct access to the 18650 battery, with the 3.7 V voltage. But now is the question how to measure state of charge of this battery?

What's your solution?
You can measure battery voltage to estimate state of charge.
By setting correct Esp32 reference voltage and using voltage divider..

It is Battery Expansion Shield V3

Should be OK to use 10k and 100k resistors to adjust the voltage?

You can do it with three 10k resistors for example.
Be aware of that voltage drop I mentioned on post#7. And current limits of your battery shield.