batteryReading for Heltec WT

This commit is contained in:
richonguzman 2024-05-11 12:38:05 -04:00
parent 54496a9f86
commit 53461f289c
1 changed files with 18 additions and 7 deletions

View File

@ -25,25 +25,36 @@ namespace BATTERY_Utils {
int sample; int sample;
int sampleSum = 0; int sampleSum = 0;
#ifdef ADC_CTRL #ifdef ADC_CTRL
digitalWrite(ADC_CTRL, LOW); #ifdef HELTEC_WIRELESS_TRACKER
digitalWrite(ADC_CTRL, HIGH);
#endif
#ifdef HELTEC_V3
digitalWrite(ADC_CTRL, LOW);
#endif
#endif #endif
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
#ifdef BATTERY_PIN #ifdef BATTERY_PIN
sample = analogRead(BATTERY_PIN); sample = analogRead(BATTERY_PIN);
#endif #endif
#if defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa) #if defined(ESP32_DIY_LoRa) || defined(ESP32_DIY_1W_LoRa)
sample = 0; sample = 0;
#endif #endif
sampleSum += sample; sampleSum += sample;
delayMicroseconds(50); delayMicroseconds(50);
} }
#ifdef ADC_CTRL #ifdef ADC_CTRL
digitalWrite(ADC_CTRL, HIGH); #ifdef HELTEC_WIRELESS_TRACKER
double inputDivider = (1.0 / (390.0 + 100.0)) * 100.0; // The voltage divider is a 390k + 100k resistor in series, 100k on the low side. digitalWrite(ADC_CTRL, LOW);
return (((sampleSum/100) * adcReadingTransformation) / inputDivider) + 0.285; // Yes, this offset is excessive, but the ADC on the ESP32s3 is quite inaccurate and noisy. Adjust to own measurements. #endif
#ifdef HELTEC_V3_GPS
digitalWrite(ADC_CTRL, HIGH);
#endif
double inputDivider = (1.0 / (390.0 + 100.0)) * 100.0; // The voltage divider is a 390k + 100k resistor in series, 100k on the low side.
return (((sampleSum/100) * adcReadingTransformation) / inputDivider) + 0.285; // Yes, this offset is excessive, but the ADC on the ESP32s3 is quite inaccurate and noisy. Adjust to own measurements.
#else #else
return (2 * (sampleSum/100) * adcReadingTransformation) + voltageDividerCorrection; // raw voltage without mapping return (2 * (sampleSum/100) * adcReadingTransformation) + voltageDividerCorrection; // raw voltage without mapping
#endif #endif
// return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage // return mapVoltage(voltage, 3.34, 4.71, 3.0, 4.2); // mapped voltage