Code: Select all
void setup() {
esp_adc_cal_value_t val_type =
esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_0, ADC_WIDTH_BIT_11, REF_VOLTAGE, adc_chars);
}
float battery_read(uint8_t channel, adc_atten_t attenuation) {
float voltage = 0.0; // calculated voltage
float output = 0.0; //output value
const float battery_max = 4.2; //maximum voltage of battery
const float battery_min = 3.0; //minimum voltage of battery before shutdown
float R1 = 100000; // resistance of R1 (100K)
float R2 = 27000; // resistance of R2 (27K)
adc1_channel_t channelNum;
switch (channel) {
case (36):
channelNum = ADC1_CHANNEL_0;
break;
case (39):
channelNum = ADC1_CHANNEL_3;
break;
case (34):
channelNum = ADC1_CHANNEL_6;
break;
case (35):
channelNum = ADC1_CHANNEL_7;
break;
case (32):
channelNum = ADC1_CHANNEL_4;
break;
case (33):
channelNum = ADC1_CHANNEL_5;
break;
}
adc1_config_channel_atten(channelNum, attenuation);
voltage = esp_adc_cal_raw_to_voltage(analogRead(channel), adc_chars);
Serial.println(voltage);
return output = voltage*(R1+R2)/R2;
}