From 8f0a9a88f62bcc3c9b3fbd5bd91761e8902c4eae Mon Sep 17 00:00:00 2001 From: unlogisch04 <98281608+unlogisch04@users.noreply.github.com> Date: Wed, 30 Mar 2022 23:19:14 +0200 Subject: [PATCH] battery add esp32 ADC specs Co-authored-by: TheDevMinerTV --- src/batterymonitor.cpp | 19 +++++++++++-------- src/batterymonitor.h | 27 ++++++++++++++++++++++++++- src/defines.h | 12 +++++++++++- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/batterymonitor.cpp b/src/batterymonitor.cpp index 7ddba69..b9309ee 100644 --- a/src/batterymonitor.cpp +++ b/src/batterymonitor.cpp @@ -51,30 +51,33 @@ void BatteryMonitor::Loop() auto now_ms = millis(); if (now_ms - last_battery_sample >= batterySampleRate) { + last_battery_sample = now_ms; voltage = -1; #if ESP8266 && (BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_INTERNAL_MCP3021) - last_battery_sample = now_ms; - auto level = ESP.getVcc(); - if (level > voltage_3_3) + // Find out what your max measurement is (voltage_3_3). + // Take the max measurement and check if it was less than 50mV + // if yes output 5.0V + // if no output 3.3V - dropvoltage + 0.1V + auto ESPmV = ESP.getVcc(); + if (ESPmV > voltage_3_3) { - voltage_3_3 = level; + voltage_3_3 = ESPmV; } else { //Calculate drop in mV - level = voltage_3_3 - level; - if (level < 50) + ESPmV = voltage_3_3 - ESPmV; + if (ESPmV < 50) { voltage = 5.0F; } else { - voltage = 3.3F - ((float)level / 1000.0F) + 0.1F; //we assume 100mV drop on the linear converter + voltage = 3.3F - ((float)ESPmV / 1000.0F) + 0.1F; //we assume 100mV drop on the linear converter } } #endif #if BATTERY_MONITOR == BAT_EXTERNAL - last_battery_sample = now_ms; voltage = ((float)analogRead(PIN_BATTERY_LEVEL)) * batteryADCMultiplier; #endif #if BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 diff --git a/src/batterymonitor.h b/src/batterymonitor.h index 7372f7b..88c52aa 100644 --- a/src/batterymonitor.h +++ b/src/batterymonitor.h @@ -30,14 +30,39 @@ #include #include "logging/Logger.h" +#if ESP8266 + #define ADCResulution 1023.0 // ESP8266 has 12bit ADC + #define ADCVoltageMax 1.0 // ESP8266 input is 1.0 V = 1023.0 +#endif +#if ESP32 + #define ADCResulution 4095.0 // ESP32 has 12bit ADC + #define ADCVoltageMax 3.3 // ESP32 input is 3.3 V = 4095.0 +#endif +#ifndef ADCResulution + #define ADCResulution 1023.0 +#endif +#ifndef ADCVoltageMax + #define ADCVoltageMax 1.0 +#endif + +#ifndef BATTERY_SHIELD_R1 + #define BATTERY_SHIELD_R1 220.0 +#endif +#ifndef BATTERY_SHIELD_R2 + #define BATTERY_SHIELD_R2 100.0 +#endif + #if BATTERY_MONITOR == BAT_EXTERNAL #ifndef PIN_BATTERY_LEVEL #error Internal ADC enabled without pin! Please select a pin. #endif // Wemos D1 Mini has an internal Voltage Divider with R1=220K and R2=100K > this means, 3.3V analogRead input voltage results in 1023.0 // Wemos D1 Mini with Wemos Battery Shield v1.2.0 or higher: Battery Shield with J2 closed, has an additional 130K resistor. So the resulting Voltage Divider is R1=220K+100K=320K and R2=100K > this means, 4.5V analogRead input voltage results in 1023.0 + // ESP32 Boards may have not the internal Voltage Divider. Also ESP32 has a 12bit ADC (0..4095). So R1 and R2 can be changed. + // Diagramm: + // (Battery)--- [BATTERY_SHIELD_RESISTANCE] ---(INPUT_BOARD)--- [BATTERY_SHIELD_R2] ---(ESP_INPUT)--- [BATTERY_SHIELD_R1] --- (GND) // SlimeVR Board can handle max 5V > so analogRead of 5.0V input will result in 1023.0 - #define batteryADCMultiplier 1.0 / 1023.0 * (320 + BATTERY_SHIELD_RESISTANCE) / 100 + #define batteryADCMultiplier ADCVoltageMax / ADCResulution * (BATTERY_SHIELD_R1 + BATTERY_SHIELD_R2 + BATTERY_SHIELD_RESISTANCE) / BATTERY_SHIELD_R1 #elif BATTERY_MONITOR == BAT_MCP3021 || BATTERY_MONITOR == BAT_INTERNAL_MCP3021 // Default recommended resistors are 9.1k and 5.1k #define batteryADCMultiplier 3.3 / 1023.0 * 14.2 / 9.1 diff --git a/src/defines.h b/src/defines.h index 952a867..9e17561 100644 --- a/src/defines.h +++ b/src/defines.h @@ -33,9 +33,19 @@ #define SECOND_IMU_ROTATION DEG_270 // Battery monitoring options (comment to disable): -// BAT_EXTERNAL for ADC pin, BAT_INTERNAL for internal - can detect only low battery, BAT_MCP3021 for external ADC +// BAT_EXTERNAL for ADC pin, +// BAT_INTERNAL for internal - can detect only low battery, +// BAT_MCP3021 for external ADC connected over I2C #define BATTERY_MONITOR BAT_EXTERNAL + +// BAT_EXTERNAL definition +// D1 Mini boards with ESP8266 have internal resistors. For these boards you only have to adjust BATTERY_SHIELD_RESISTANCE. +// For other boards you can now adjust the other resistor values. +// The diagram looks like this: +// (Battery)--- [BATTERY_SHIELD_RESISTANCE] ---(INPUT_BOARD)--- [BATTERY_SHIELD_R2] ---(ESP32_INPUT)--- [BATTERY_SHIELD_R1] --- (GND) #define BATTERY_SHIELD_RESISTANCE 180 //130k BatteryShield, 180k SlimeVR or fill in external resistor value in kOhm +// #define BATTERY_SHIELD_R1 100 // Board voltage divider resistor Ain to GND in kOhm +// #define BATTERY_SHIELD_R2 220 // Board voltage divider resistor Ain to INPUT_BOARD in kOhm // Board-specific configurations #if BOARD == BOARD_SLIMEVR