Skip to content

Fix battery measurement by using calibrated ADC if available #171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/include/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ double System::readBattery()
#endif
*/
delay(1);
int adc = analogRead(35);
uint32_t batt_mv = analogReadMilliVolts(35);
if (state)
{
pinModeInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, INPUT);
Expand All @@ -140,10 +140,8 @@ double System::readBattery()
digitalWriteInternal(MCP23017_INT_ADDR, mcpRegsInt, 9, LOW);
#endif
*/
// Calculate the voltage using the following formula
// 1.1V is internal ADC reference of ESP32, 3.548133892 is 11dB in linear
// scale (Analog signal is attenuated by 11dB before ESP32 ADC input)
return (double(adc) / 4095 * 1.1 * 3.548133892 * 2);
// divide by 1000 for mv to V, * 2 for voltage divider circuit
return (double(batt_mv) / 1000 * 2);
}

/**
Expand Down