@@ -18,13 +18,15 @@ static adc_oneshot_unit_handle_t adc_handle;
1818static adc_cali_handle_t adc_cal_handle = NULL ;
1919static bool adc_calibrated = false;
2020static int measurementCount = 0 ;
21- static int sumVoltage = 0 ; //Voltage in mV
21+ static int sumVoltage = 0 ; //voltage in mV
2222
2323static bool adc_calibration (adc_unit_t unit , adc_atten_t atten , adc_cali_handle_t * out_handle );
2424static int32_t getVoltage ();
2525
2626
27- //determine the esp ADC reference voltage which is around 1100 mV
27+ /**
28+ * determine the esp ADC reference voltage which is around 1100 mV
29+ */
2830static bool adc_calibration (adc_unit_t unit , adc_atten_t atten , adc_cali_handle_t * return_handle ){
2931 adc_cali_handle_t handle = NULL ;
3032 esp_err_t ret = ESP_FAIL ;
@@ -47,18 +49,19 @@ static bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_
4749}
4850
4951void initBatteryRead (){
50- //adc init
52+ //ADC init
5153 adc_oneshot_unit_init_cfg_t init_config = {
5254 .unit_id = ADC_UNIT_1 ,
5355 .ulp_mode = ADC_ULP_MODE_DISABLE ,
5456 };
5557
5658 ESP_ERROR_CHECK (adc_oneshot_new_unit (& init_config , & adc_handle ));
5759
58- //adc configuration
59- //wenn .atten auf 0db Dämpfung gesetzt ist, ist der mögliche Wertebereich von 0 bis 1.1V
60- //bei .atten 12db wird der Wertebereich von 0 bis theoretisch 3.9V (3.55*1.1V) verwendet aber durch VDD auf 3.3V begrenzt
61- //recommended range between 150 to 2450 mV at 12db
60+ /*ADC configuration
61+ * if .atten is set to 0db attenuation, the possible value range is from 0 to 1.1V
62+ * at .atten 12db the value range from 0 to theoretically 3.9V (3.55*1.1V) is used but limited by VDD to 3.3V
63+ * recommended range between 150 to 2450 mV at 12db
64+ */
6265 adc_oneshot_chan_cfg_t config = {
6366 .bitwidth = ADC_BITWIDTH_12 ,
6467 .atten = ADC_ATTEN_DB_11 ,
@@ -70,7 +73,9 @@ void initBatteryRead(){
7073 adc_calibrated = adc_calibration (ADC_UNIT_1 , ADC_ATTEN_DB_11 , & adc_cal_handle );
7174}
7275
73- //Voltage in mV
76+ /**
77+ * determines the applied voltage at the ADC. Either calibrated or raw.
78+ */
7479static int32_t getVoltage (){
7580 int raw_val , voltage ;
7681 adc_oneshot_read (adc_handle , ADC_CHANNEL_4 , & raw_val );
@@ -86,7 +91,10 @@ static int32_t getVoltage(){
8691}
8792
8893void battery_Timer_Event (TimerHandle_t ev ){
89- //https://esp32.com/viewtopic.php?t=1459
94+ /* If there is too much output, the ESP crashes
95+ * https://esp32.com/viewtopic.php?t=1459
96+ */
97+
9098
9199 getVoltage ();
92100 if (measurementCount < 10 ){
@@ -98,7 +106,7 @@ void battery_Timer_Event(TimerHandle_t ev){
98106 measurementCount = 0 ;
99107
100108 //battery range between 3.4 and 4.2V
101- //r2/(r1+r2) = 0.755 akutell
109+ //r2/(r1+r2) = 0.755 with the used resistors
102110 //vmax_in = 4.2*0.755 = 3.171
103111 //vmin_in = 3.8*0.755 = 2.869
104112 //steps = (3171-2869)/100 = 302/100
0 commit comments