Skip to content

Commit

Permalink
Added M5Stack battery voltage measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-bs committed Apr 10, 2024
1 parent 816447b commit c81c8a0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
29 changes: 16 additions & 13 deletions src/adc/adc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
#include "adc.h"
#include "../logging.h"

#ifdef ADC_EN
#if defined(ARDUINO_M5STACK_Core2) || defined(ARDUINO_M5STACK_CORE2)
#include <M5Unified.h>
#endif

#ifdef ESP32
#if defined(ESP32) && defined(ADC_EN)
// ESP32 ADC with calibration
ESP32AnalogRead adc; //!< ADC object for supply voltage measurement

Expand All @@ -63,13 +65,14 @@ ESP32AnalogRead adc3; //!< ADC object
//
// Get supply / battery voltage
//
#if defined(ADC_EN)
uint16_t
getVoltage(void)
{
float voltage_raw = 0;
for (uint8_t i = 0; i < UBATT_SAMPLES; i++)
{
#ifdef ESP32
#if defined(ESP32)
voltage_raw += float(adc.readMiliVolts());
#else
voltage_raw += float(analogRead(PIN_ADC_IN)) / 4095.0 * 3300;
Expand All @@ -81,20 +84,27 @@ getVoltage(void)

return voltage;
}
#endif

uint16_t getBatteryVoltage(void)
{
#if defined(ARDUINO_ARCH_RP2040)
// Not implemented - no default VBAT input circuit (connect external divider to A0)
return 0;
#else
#elif defined(ARDUINO_M5STACK_Core2) || defined(ARDUINO_M5STACK_CORE2)
uint16_t voltage = M5.Power.getBatteryVoltage();
log_d("Voltage = %dmV", voltage);
return voltage;
#elif defined(ADC_EN)
return getVoltage();
#else
return 0;
#endif
}
//
// Get an additional voltage
//
#if defined(ESP32)
#if defined(ESP32) && defined(ADC_EN)
uint16_t
getVoltage(ESP32AnalogRead &adc, uint8_t samples, float divider)
{
Expand All @@ -109,7 +119,7 @@ getVoltage(ESP32AnalogRead &adc, uint8_t samples, float divider)

return voltage;
}
#else
#elif defined(ARDUINO_ARCH_RP2040)
uint16_t
getVoltage(pin_size_t pin, uint8_t samples, float divider)
{
Expand All @@ -125,10 +135,3 @@ getVoltage(pin_size_t pin, uint8_t samples, float divider)
return voltage;
}
#endif
#else
uint16_t getBatteryVoltage(void)
{
return 0;
}
#endif

11 changes: 6 additions & 5 deletions src/adc/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@

#include <Arduino.h>
#include "../BresserWeatherSensorLWCfg.h"
#ifdef ADC_EN
#ifdef ESP32

#if defined(ESP32) && defined(ADC_EN)
// ESP32 calibrated Analog Input Reading
#include <ESP32AnalogRead.h>
#endif

#if defined(ADC_EN)
/*!
* \brief Get supply / battery voltage
*
Expand All @@ -56,6 +57,7 @@
* \returns Voltage in mV
*/
uint16_t getVoltage(void);
#endif

/*!
* \brief Get battery voltage
Expand All @@ -74,11 +76,10 @@ uint16_t getBatteryVoltage(void);
*
* \returns Voltage in mV
*/
#if defined(ESP32)
#if defined(ESP32) && defined(ADC_EN)
uint16_t getVoltage(ESP32AnalogRead &adc, uint8_t samples, float divider);
#else
#elif defined(ARDUINO_ARCH_RP2040)
uint16_t getVoltage(pin_size_t pin, uint8_t samples, float divider);
#endif
#endif

#endif // _ADC_H

0 comments on commit c81c8a0

Please sign in to comment.