1st) i power up board by usb, gpio6 wiring to +battery, ground to ground, no divider set 11db for 3.9 max scale like the code. i hope i can get adc value ~~550 for 3.6v but it's always 3.9v.
2nd)i power up board by usb, gpio6 wiring to +battery, ground to ground, divider 4.2-->27k-->measure here-->10k-->ground. set 11db for 3.9 max scale like the code. get 3.66v seem good.
3rd)i power up board by usb, gpio6 wiring to +battery, ground to ground, no divider set 0db for 1.1 max scale like the code. it's always 2047 for max adc resolution, like 1st case.
I dont get the point, i think if i use max scale 3.9, i apply 3.6v in adc pin, i think i will get adc value <2047. or anny voltage above 3.3v i apply to adc will return 2047
Code: Select all
#include <driver/adc.h>
float battery_read()
{
//read battery voltage per %
long sum = 0; // sum of samples taken
float voltage = 0.0; // calculated voltage
float output = 0.0; //output value
const float battery_max = 3.6; //maximum voltage of battery
const float battery_min = 3.0; //minimum voltage of battery before shutdown
float R1 = 27000.0; // resistance of R1 (100K)
float R2 = 10000.0; // resistance of R2 (10K)
for (int i = 0; i < 500; i++)
{
sum += adc1_get_voltage(ADC1_CHANNEL_0);
delayMicroseconds(1000);
}
// calculate the voltage
voltage = sum / (float)500;
Serial.println(voltage);
voltage = (voltage * 3.9) / 2047.0; //for internal 3.9v reference
// use if added divider circuit
//voltage = voltage / (R2/(R1+R2));//uncomment for no divider
//round value by two precision
voltage = roundf(voltage * 100) / 100;
Serial.print("voltage: ");
Serial.println(voltage, 2);
output = ((voltage - battery_min) / (battery_max - battery_min)) * 100;
if (output < 100)
return output;
else
return 100.0f;
}
void setup()
{
adc1_config_width(ADC_WIDTH_11Bit);
adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11); //set reference voltage to internal
Serial.begin(9600);
}
void loop()
{
Serial.print("Battery Level: ");
Serial.println(battery_read(), 2);
delay(1000);
}