Measuring Voltage with ESP32 / S2 mini?

I recently bought an S2 Mini and a Arudino Voltmeter 10 stücke/1pc intelligente elektronik dc 0 25v standard spannung sensor modul test elektronische steine smart roboter für arduino diy kit| | - AliExpress but can't get it to work.

Goal
I have an 3,7V Lipo Battery that I want to measure the voltage from. The actual voltage currently is 3,9V.

Wiring
I connected + and - to the Voltmeter PCB input and the outputs like this: - to S2 Mini ground, + unconnected and S to pin 2.
Since I am getting a reading, I guess the wiring doesnt seem to be the problem.

Interpreting the results
As far as I understand, the S2 mini does not really have a Pin to read analog voltages, but an ADC (analog to digital converter). Yet I read that you dont have to convert the numbers but use the code analogReadMilliVolts(2). So now, only the Voltmeter needs to be converted since it reduces the input of the battery.
On the product page it gives the code:
Float temp;
Val11 = analog read (1);
Temp = val11/4.092;
Val11 =(int)temp;//
Val2 =((val11 % 100)/10);
Serial.println(val2);

Programming

int val11;
int val2;
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
analogReadResolution(12);
}

void loop() {
float temp;
val11 = analogReadMilliVolts (2);
temp = val11/4.092;
val11 =(int)temp;//
val2 =((val11 % 100)/10);
Serial.println(val11);
Serial.println(val2);
delay(100); // delay in between reads for clear read from serial
}

Results
val11 gives me a value of 112 when battery is not connected and 201 when the battery is connected
val2 gives me 1 when batters is not connected and 0 when its connected

also I tried
int sensorValue = analogRead(2);
int analogVolts = analogReadMilliVolts(2);
but all give me weird numbers both when the battery is or is not connected

Question
How can I fix my code to get the voltage of my battery?

i'm not familiar with the voltage sensor nor the S2 mini. i am familar with the esp32 and have used the ADC to measure pot settings.

many microcontrollers have ADCs. they are typically limited to measuring voltages up to the operating voltage of the processor, if not a little less. The Arduino atmel processor has an ADC ref pin which i believe provides the maximum voltage the ADC will read

so i'm puzzled how the esp32 operating at 3.3V reads a lipo at 3.7V without a calibrated resistor divider.

it not clear what analogReadMilliVolts() returns. if the reported value of val11 is 201 after being divided by 4.092, then the value returned by analogReadMilliVolts() was presumably 822. neither value makes sense to me

not clear if the 4.092 is related to the 12 bit resolution of the ADC, 4096 or some other scaling value converting the value to volts. not at all clear why any scaling is needed if analogReadMilliVolts()`
returns mV


a resistor divider can drop a high input voltage within the range of the ADC. a simple divider that cuts the value in half would drop 3.7V to 1.85V

if the ADC ha s12 bits of resolution, its max value is 4095 and corresponds to operating voltage of the processor, 3.3V (or whatever the ADCref value is for the esp32).

with these values, the ADC should return 2296 = 4095 * 1.85 / 3.3 when 3.7V is across the resistor divider and 1.85V is at the ADC input

that value can be converted to 3.7 = 3.3V * 2 * 2296 / 4095

that voltage sensor must have a divider since it says it operates from 0-25V. but it's not clear what the ratio is

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.