Battery voltage reading on WiFi LoRa32 v3.1 board
sakiraykurt opened this issue · 0 comments
sakiraykurt commented
#include "Arduino.h"
#include <driver/adc.h>
#define PIN_VBAT ADC1_CHANNEL_1 // Battery Read
#define PIN_ADC_CTRL 37 // Battery Read Connection
uint8_t batterySampleIndex = 0;
const uint8_t batterySampleAmount = 20;
uint16_t batterySamples[batterySampleAmount];
void setup()
{
// Serial
Serial.begin(115200);
// VBAT Connection
pinMode(PIN_ADC_CTRL, OUTPUT);
digitalWrite(PIN_ADC_CTRL, LOW);
delay(10);
for (size_t i = 0; i < batterySampleAmount; i++)
batterySamples[i] = analogRead(PIN_VBAT);
}
void loop()
{
uint16_t voltage = analogRead(PIN_VBAT);
batterySamples[batterySampleIndex++] = voltage;
batterySampleIndex %= batterySampleAmount;
int total = 0;
for (size_t i = 0; i < batterySampleAmount; i++)
total += batterySamples[i];
voltage = round((float)total / (float)batterySampleAmount);
Serial.printf("\nvoltage %d\n", voltage);
delay(1000);
}
Example code.
I can't able to read correct voltage values when the battery is connected only, the battery and the usb are connected and the usb is connected only.
I also tried multipliying with many conversion factors like (3.2, 3.9, 0,00402) to get true voltage.
Should I make my own voltage divider circuit with 2 resistors to read the battery level or is it enough to just write code?