Hi there.
I’m still having difficulties to read battery voltage. I’ve configured GPIOs and ADC attenuation like following:
#define GPIO_HABILITA_LEITURA_BATERIA 21
#define GPIO_ADC_BATERIA 13
#define BITS_RESOLUCAO_ADC 12 //max: 4095
#define MAX_VALOR_ADC_BAT 4095
void configura_leitura_tensao_bateria(void)
{
/* Configura GPIO do ADC inicialmente como open drain para evitar que
o habilitar da leitura de tensão de bateria envie diretamente 4.7V (max)
ao ADC */
pinMode(GPIO_ADC_BATERIA, OPEN_DRAIN);
/* Configura ADC */
analogSetPinAttenuation(GPIO_ADC_BATERIA, ADC_6db);
analogReadResolution(BITS_RESOLUCAO_ADC);
/* Habilita leitura da tensão de bateria */
pinMode(GPIO_HABILITA_LEITURA_BATERIA, OUTPUT);
digitalWrite(GPIO_HABILITA_LEITURA_BATERIA, LOW);
}
And I’m trying to read as follows:
bat_analog_read = analogRead(GPIO_ADC_BATERIA);
Serial.print("Analog read (battery): ");
Serial.println(bat_analog_read);
However, I’ve got very inconsistent readings:
ead (battery): 256
Analog read (battery): 502
Analog read (battery): 721
Analog read (battery): 448
Analog read (battery): 64
Analog read (battery): 6
Analog read (battery): 86
Analog read (battery): 256
Analog read (battery): 491
Analog read (battery): 709
Analog read (battery): 416
Analog read (battery): 49
Analog read (battery): 6
If I switch to GPIO 37 (as used in board V2.1), I only get 0 as readings.
Please, would you tell me what I’m doing wrong? I have no idea what’s wrong with my approach.