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

how to get battery voltage with esp32-sim800L #222

Open
xBLACKICEx opened this issue Nov 16, 2022 · 8 comments
Open

how to get battery voltage with esp32-sim800L #222

xBLACKICEx opened this issue Nov 16, 2022 · 8 comments
Labels
good first issue Good for newcomers

Comments

@xBLACKICEx
Copy link

xBLACKICEx commented Nov 16, 2022

esp32-sim800L can directly insert the battery, which is good, but how to get the remaining battery information? Or battery voltage?

thanks

@streem-ua
Copy link

Have you found any solution? I need to monitor battery voltage and also detect if USB connection is active or not.

@lewisxhe
Copy link
Contributor

lewisxhe commented Jan 6, 2023

If the PMU version is used, the PMU function can be used to directly obtain the voltage.

https://github.com/lewisxhe/AXP202X_Library/blob/master/examples/axp_adc/axp_adc.ino

If the power management of IP5306 is used, only the adc voltage of IO35 needs to be read. You can refer to here

https://github.com/Xinyuan-LilyGO/TTGO-T-Display/blob/7ad1b2807a37b5a6ba33a4fb0efbe8466e96faa3/TFT_eSPI/examples/FactoryTest/FactoryTest.ino#L80

@streem-ua
Copy link

@lewisxhe Thanks for reply! I think, that this is PMU version, because it doesn't power off, when I change power source from USB to LiPo. But I tried each one library separately.

So, what I achieved using this axp_adc.ino

const uint8_t i2c_sda = 21;
const uint8_t i2c_scl = 22;
...
AXP192/AXP202 ADC Test
Ooops, AXP202/AXP192 power chip detected ... Check your wiring!

And this FactoryTest.ino#L80

eFuse Vref:1107 mVVoltage :0.00V
...
Voltage :0.00V

Photo of my board:
one
This is an image
two
This is an image

Am I missing something?

@lewisxhe
Copy link
Contributor

lewisxhe commented Jan 6, 2023

You use IP5306, do you read IO35?
Here is a simple example

#include <Arduino.h>
#include "esp_adc_cal.h"

int vref =1100;

void setup()
{
    Serial.begin(115200);
    esp_adc_cal_characteristics_t adc_chars;
    esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &adc_chars);    //Check type of calibration value used to characterize ADC
    if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
        Serial.printf("eFuse Vref:%u mV", adc_chars.vref);
        vref = adc_chars.vref;
    } 
}

void loop()
{
    uint16_t v = analogRead(35);
    float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
    String voltage = "Voltage :" + String(battery_voltage) + "V";
    Serial.println(voltage);
    delay(1000);
}
@streem-ua
Copy link

@lewisxhe
Yep! This shows up a right Voltage of battery, thanks!

Voltage :4.35V

Maybe you can help with one more thing, I need to monitor USB Type-C input, if it is available or not. (don't need to know USB voltage). I will be very grateful to you!

@streem-ua
Copy link

For everyone struggling in the same situation, I figured out how to check power source, USB or Battery.
So, I'll attach some pieces of code, hope you'll figure out how to use them.

#include <Wire.h>
#define IP5306_REG_READ_0 0x70
#define IP5306_GetPowerSource() ip5306_get_bits(IP5306_REG_READ_0, 3, 1)

uint8_t ip5306_get_bits(uint8_t reg, uint8_t index, uint8_t bits){
  int value = ip5306_get_reg(reg);
  if(value < 0){
    SerialMon.printf("ip5306_get_bits fail: 0x%02x\n", reg);
    return 0;
  }
  return (value >> index) & ((1 << bits)-1);
}

int ip5306_get_reg(uint8_t reg){
  Wire.beginTransmission(0x75);
  Wire.write(reg);
  if(Wire.endTransmission(false) == 0 && Wire.requestFrom(0x75, 1)){
    return Wire.read();
  }
  return -1;
}

void setup() {
  SerialMon.begin(115200);
  SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX);
...
  Wire.begin(21,22); // I2C_SDA, I2C_SCL
}

void loop() {
...
bool usb = IP5306_GetPowerSource();
String powerSource = usb?"USB":"BATTERY";
delay(3000);
...
}

Complete version of IP5306 registers, corresponding to power you can find here

@xBLACKICEx
Copy link
Author

@lewisxhe
@streem-ua
Thank you guys. it look work for me

@xBLACKICEx
Copy link
Author

图片

it norml thant it norml thank VinCurrent is 2250mh?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
3 participants