From 0887ca0309d7d7d039998074f66910b12c1b4065 Mon Sep 17 00:00:00 2001 From: Falk Lehmann Date: Thu, 16 Nov 2023 11:30:26 +0100 Subject: [PATCH 1/2] Update mkr-battery-app-note.md Fix data type of variable max_Source_voltage. Float is correct here to get correct values. Made calculation of the two voltages better. No real change, but more accurate results this way. --- .../tutorials/mkr-battery-app-note/mkr-battery-app-note.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md b/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md index 70b17a117b..fc461fa777 100644 --- a/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md +++ b/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md @@ -124,7 +124,7 @@ We will go through the lines needed to create a Sketch to read the battery value **4.** We will now create a variable to store the maximum source voltage `max_Source_voltage` as well as the upper (`batteryFullVoltage`) and lower (`batteryEmptyVoltage`) values for the battery. We will also define the battery capacity as `batteryCapacity` so as to determine the charging current. Since we are using a 750 mAh battery in this example, we will set the value to `0.750`. ```arduino - int max_Source_voltage; + float max_Source_voltage; float batteryFullVoltage = 4.2; float batteryEmptyVoltage = 3.3; @@ -303,8 +303,8 @@ void loop() { rawADC = analogRead(ADC_BATTERY); //the value obtained directly at the PB09 input pin - voltADC = rawADC * (3.3/4095.0); //convert ADC value to the voltage read at the pin - voltBat = voltADC * (max_Source_voltage/3.3); //we cannot use map since it requires int inputs/outputs + voltADC = rawADC * 3.3 / 4096.0; //convert ADC value to the voltage read at the pin + voltBat = max_Source_voltage * rawADC / 4096.0; //we cannot use map since it requires int inputs/outputs int new_batt = (voltBat - batteryEmptyVoltage) * (100) / (batteryFullVoltage - batteryEmptyVoltage); //custom float friendly map function From 8b347022fc4173c820b7488b737e47b6c9242826 Mon Sep 17 00:00:00 2001 From: Falk Lehmann Date: Thu, 16 Nov 2023 14:57:18 +0100 Subject: [PATCH 2/2] Update mkr-battery-app-note.md Added previous fix in all places. --- .../tutorials/mkr-battery-app-note/mkr-battery-app-note.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md b/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md index fc461fa777..1d3a9dca2a 100644 --- a/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md +++ b/content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md @@ -185,12 +185,12 @@ void loop() **13.** In order to convert `rawADC` into a voltage reading (`voltADC`) we will divide `rawADC` by 4095 and then multiply it by the analog reference voltage (3.3V). ```arduino -voltADC = rawADC * (3.3/4095.0); +voltADC = rawADC * 3.3 / 4096.0; ``` **14.** The `voltADC` variable gives us the voltage sensed directly on the PB09 pin. This voltage is passed through the voltage divider, so it is a fraction of the actual battery voltage. We can then calculate the equivilanet battery voltage as follows. ```arduino -voltBat = voltADC * (max_Source_voltage/3.3); +voltBat = max_Source_voltage * rawADC / 4096.0; ``` **15.** We can approximate the battery voltage to be proportional to the capacity level. Since the `map()` function does not work with float variables, we will manually map the values. @@ -266,7 +266,7 @@ float voltBat; //calculated voltage on battery int R1 = 330000; // resistor between battery terminal and SAMD pin PB09 int R2 = 1000000; // resistor between SAMD pin PB09 and ground -int max_Source_voltage; // upper source voltage for the battery +float max_Source_voltage; // upper source voltage for the battery // define voltage at which battery is full/empty float batteryFullVoltage = 4.2; //upper voltage limit for battery