Save current changes before reorganizing drivers and cleaning workspace
This commit is contained in:
+5
-5
@@ -3,7 +3,7 @@ from machine import ADC, Pin
|
||||
class BatteryMonitor:
|
||||
"""Utility class for monitoring battery voltage and capacity on ESP32-S3-RLCD-4.2."""
|
||||
|
||||
def __init__(self, pin_num=4):
|
||||
def __init__(self, pin_num=9):
|
||||
import sys
|
||||
self.adc = ADC(Pin(pin_num))
|
||||
if sys.platform == 'esp32':
|
||||
@@ -18,16 +18,16 @@ class BatteryMonitor:
|
||||
try:
|
||||
# Try calibrated reading in microvolts first
|
||||
uv = self.adc.read_uv()
|
||||
# 3x voltage divider onboard scales 3.0V-4.2V battery to 1.0V-1.4V
|
||||
voltage = (uv / 1_000_000.0) * 3.0
|
||||
# 2x voltage divider onboard scales 3.0V-4.2V battery to 1.5V-2.1V
|
||||
voltage = (uv / 1_000_000.0) * 2.0
|
||||
return round(voltage, 3)
|
||||
except AttributeError:
|
||||
# Fallback if read_uv() is not supported on older MicroPython builds
|
||||
try:
|
||||
# Read 12-bit value (0-4095)
|
||||
raw = self.adc.read()
|
||||
# 3.3V reference at 11dB attenuation, 3x divider
|
||||
voltage = (raw / 4095.0) * 3.3 * 3.0
|
||||
# 3.3V reference at 11dB attenuation, 2x divider
|
||||
voltage = (raw / 4095.0) * 3.3 * 2.0
|
||||
return round(voltage, 3)
|
||||
except Exception as e:
|
||||
print(f"Error reading battery raw ADC: {e}")
|
||||
|
||||
Reference in New Issue
Block a user