Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Battery voltage reading on WiFi LoRa32 v3.1 board #234

Open
sakiraykurt opened this issue Jan 21, 2024 · 0 comments
Open

Battery voltage reading on WiFi LoRa32 v3.1 board #234

sakiraykurt opened this issue Jan 21, 2024 · 0 comments

Comments

@sakiraykurt
Copy link

sakiraykurt commented Jan 21, 2024

#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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant