0

I'm measuring battery reading and sending it to the master using nrf24l01 module. But for the first time after reset, battery measurement always gives incorrect value. It's same as if wake up from deep sleep. I don't know how to fix it.

Monitor

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;
}
Shubham
  • 628
  • 1
  • 9
  • 19
Quang Minh Lê
  • 169
  • 1
  • 2
  • 13
  • 1
    There is generally some time required to change the ADC channels by the controller. You should check the datasheet and add a small delay after changing the channel and before reading the voltage. – Rishikesh Raje Jul 05 '19 at 05:55
  • i try to add update but notthing happen. then i add `analogRead(channlel)` before `float voltage = 0.0` and everything is ok – Quang Minh Lê Jul 05 '19 at 07:15

0 Answers0