Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

MagTag battery level
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

MagTag battery level

by craig02445 on Thu Dec 02, 2021 10:22 pm

How do I read the battery voltage on a MagTag in Arduino? I am doing
Code: Select all | TOGGLE FULL SIZE
analogRead(A13)
but I know there is more to do. Thanks!

craig02445
 
Posts: 7
Joined: Sun Jan 24, 2021 1:00 pm

Re: MagTag battery level

by adafruit_support_carter on Mon Dec 06, 2021 12:22 pm

Try:
Code: Select all | TOGGLE FULL SIZE
analogRead(BATT_MONITOR);

to get the raw ADC counts.

adafruit_support_carter
 
Posts: 22219
Joined: Tue Nov 29, 2016 2:45 pm

Re: MagTag battery level

by craig02445 on Wed Dec 08, 2021 10:52 am

Before getting your response I did find some information at espressif. The raw data from the a/d is almost the same value as what your call returns. The important part, and I am posting here for others, is what to do with it.
Code: Select all | TOGGLE FULL SIZE
#include <driver/adc.h>

float readVoltage()
{
//https://github.com/espressif/esp-idf/blob/v4.2-beta1/components/soc/include/hal/adc_types.h#L67
//ADC_WIDTH_BIT_12 = 3, /*!< ADC capture width is 12Bit. Only ESP32 is supported. */
adc1_config_width(adc_bits_width_t (3)); //sets the analog to digital converter to 12 bits.
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11); //set the analog converter on GPIO_NUM_4 to use the full range reference voltage of 3.3V.

// https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/adc.html
float battV = adc1_get_raw(ADC1_CHANNEL_3);
  battV = battV * 2.5 / 8191;

return(battV * 2);

When plugged in with USB, or on a fully charged battery, I am getting 3.95 volts. I am now trying to figure out if this is correct or if I need to do calibration of the A2D. Can you shed light on that? Thanks.

craig02445
 
Posts: 7
Joined: Sun Jan 24, 2021 1:00 pm

Please be positive and constructive with your questions and comments.