Skip to content

Commit

Permalink
Fixed ADC_CTRL , Checks for valid ADC readings
Browse files Browse the repository at this point in the history
line 230/386 For heltec v3 and heltec tracker a different approach was used with the ADC_CTRL pin, now is more uniform using the same code for the 3 boards.

line 236 Check if the raw reading we are getting is Valid or not, count only the valid readings. This could lead to a division by 0 (improbable) so that's why at line 258 there is a check for that.
  • Loading branch information
Gabrielerusso committed Feb 12, 2024
1 parent 6f50d8b commit b3238e4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
31 changes: 15 additions & 16 deletions src/Power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class AnalogBatteryLevel : public HasBatteryLevel

#ifndef BATTERY_SENSE_SAMPLES
#define BATTERY_SENSE_SAMPLES \
30 // Set the number of samples, it has an effect of increasing sensitivity in complex electromagnetic environment.
15 // Set the number of samples, it has an effect of increasing sensitivity in complex electromagnetic environment.
#endif

#ifdef BATTERY_PIN
Expand All @@ -208,12 +208,10 @@ class AnalogBatteryLevel : public HasBatteryLevel
raw = raw / BATTERY_SENSE_SAMPLES;
scaled = operativeAdcMultiplier * ((1000 * AREF_VOLTAGE) / pow(2, BATTERY_SENSE_RESOLUTION_BITS)) * raw;
#endif
// LOG_DEBUG("battery gpio %d raw val=%u scaled=%u\n", BATTERY_PIN, raw, (uint32_t)(scaled));
last_read_value += (scaled - last_read_value) * 0.5; // Virtual LPF
return scaled;
} else {
return last_read_value;
//LOG_DEBUG("battery gpio %d raw val=%u scaled=%u filtered=%u\n", BATTERY_PIN, raw, (uint32_t)(scaled), (uint32_t) (last_read_value));
}
return last_read_value;
#endif // BATTERY_PIN
return 0;
}
Expand All @@ -226,19 +224,24 @@ class AnalogBatteryLevel : public HasBatteryLevel
{

uint32_t raw = 0;
uint8_t raw_c = 0; //raw reading counter

#ifndef BAT_MEASURE_ADC_UNIT // ADC1
#ifdef ADC_CTRL
#ifdef ADC_CTRL //enable adc voltage divider when we need to read
pinMode(ADC_CTRL, OUTPUT);
digitalWrite(ADC_CTRL, HIGH);
digitalWrite(ADC_CTRL, ADC_CTRL_ENABLED);
delay(10);
#endif
for (int i = 0; i < BATTERY_SENSE_SAMPLES; i++) {
raw += adc1_get_raw(adc_channel);
int val_ = adc1_get_raw(adc_channel);
if(val_ >= 0){ //save only valid readings
raw += val_;
raw_c ++;
}
// delayMicroseconds(100);
}
#ifdef ADC_CTRL
digitalWrite(ADC_CTRL, LOW);
#ifdef ADC_CTRL //disable adc voltage divider when we need to read
digitalWrite(ADC_CTRL, !ADC_CTRL_ENABLED);
#endif
#else // ADC2
int32_t adc_buf = 0;
Expand All @@ -249,10 +252,10 @@ class AnalogBatteryLevel : public HasBatteryLevel
SET_PERI_REG_MASK(SENS_SAR_READ_CTRL2_REG, SENS_SAR2_DATA_INV);
adc2_get_raw(adc_channel, ADC_WIDTH_BIT_12, &adc_buf);
raw += adc_buf;
raw_c ++;
}
#endif // BAT_MEASURE_ADC_UNIT
raw = raw / BATTERY_SENSE_SAMPLES;
return raw;
return (raw / (raw_c < 1 ? 1 : raw_c));
}
#endif

Expand Down Expand Up @@ -383,10 +386,6 @@ bool Power::analogInit()
} else {
LOG_INFO("ADCmod: ADC characterization based on default reference voltage\n");
}
#if defined(HELTEC_V3) || defined(HELTEC_WSL_V3)
pinMode(37, OUTPUT); // needed for P channel mosfet to work
digitalWrite(37, LOW);
#endif
#endif // ARCH_ESP32

#ifdef ARCH_NRF52
Expand Down
3 changes: 2 additions & 1 deletion variants/heltec_v2.1/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
#define LORA_DIO1 35 // https://www.thethingsnetwork.org/forum/t/big-esp32-sx127x-topic-part-3/18436
#define LORA_DIO2 34 // Not really used

#define ADC_MULTIPLIER 3.8
#define ADC_MULTIPLIER 3.2 //220k + 100k (320k/100k=3.2)
//#define ADC_WIDTH ADC_WIDTH_BIT_10

#define BATTERY_PIN 37 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define ADC_CHANNEL ADC1_GPIO37_CHANNEL
Expand Down
2 changes: 2 additions & 0 deletions variants/heltec_v3/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define VEXT_ENABLE Vext // active low, powers the oled display and the lora antenna boost
#define BUTTON_PIN 0

#define ADC_CTRL 37
#define ADC_CTRL_ENABLED LOW
#define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define ADC_CHANNEL ADC1_GPIO1_CHANNEL
#define ADC_ATTENUATION ADC_ATTEN_DB_2_5 // lower dB for high resistance voltage divider
Expand Down
1 change: 1 addition & 0 deletions variants/heltec_wireless_tracker/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#define ADC_ATTENUATION ADC_ATTEN_DB_2_5 // lower dB for high resistance voltage divider
#define ADC_MULTIPLIER 4.9
#define ADC_CTRL 2 // active HIGH, powers the voltage divider. Only on 1.1
#define ADC_CTRL_ENABLED HIGH

#undef GPS_RX_PIN
#undef GPS_TX_PIN
Expand Down
2 changes: 2 additions & 0 deletions variants/heltec_wsl_v3/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#define VEXT_ENABLE Vext // active low, powers the oled display and the lora antenna boost
#define BUTTON_PIN 0

#define ADC_CTRL 37
#define ADC_CTRL_ENABLED LOW
#define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage
#define ADC_CHANNEL ADC1_GPIO1_CHANNEL
#define ADC_ATTENUATION ADC_ATTEN_DB_2_5 // lower dB for high resistance voltage divider
Expand Down

0 comments on commit b3238e4

Please sign in to comment.