Skip to content

Commit c4a4636

Browse files
authored
Merge pull request #1501 from fl43/patch-1
Fix voltage calculation - Update mkr-battery-app-note.md
2 parents 5e8cbfb + d9097a7 commit c4a4636

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

content/hardware/01.mkr/01.boards/mkr-wifi-1010/tutorials/mkr-battery-app-note/mkr-battery-app-note.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ We will go through the lines needed to create a Sketch to read the battery value
124124
**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`.
125125

126126
```arduino
127-
int max_Source_voltage;
127+
float max_Source_voltage;
128128
129129
float batteryFullVoltage = 4.2;
130130
float batteryEmptyVoltage = 3.3;
@@ -185,12 +185,13 @@ void loop()
185185
**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).
186186

187187
```arduino
188-
voltADC = rawADC * (3.3/4095.0);
188+
voltADC = rawADC * 3.3 / 4096.0;
189189
```
190190

191-
**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.
191+
**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 equivalent battery voltage as follows.
192+
192193
```arduino
193-
voltBat = voltADC * (max_Source_voltage/3.3);
194+
voltBat = max_Source_voltage * rawADC / 4096.0;
194195
```
195196

196197
**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 +267,7 @@ float voltBat; //calculated voltage on battery
266267
int R1 = 330000; // resistor between battery terminal and SAMD pin PB09
267268
int R2 = 1000000; // resistor between SAMD pin PB09 and ground
268269
269-
int max_Source_voltage; // upper source voltage for the battery
270+
float max_Source_voltage; // upper source voltage for the battery
270271
271272
// define voltage at which battery is full/empty
272273
float batteryFullVoltage = 4.2; //upper voltage limit for battery
@@ -303,8 +304,8 @@ void loop()
303304
{
304305
305306
rawADC = analogRead(ADC_BATTERY); //the value obtained directly at the PB09 input pin
306-
voltADC = rawADC * (3.3/4095.0); //convert ADC value to the voltage read at the pin
307-
voltBat = voltADC * (max_Source_voltage/3.3); //we cannot use map since it requires int inputs/outputs
307+
voltADC = rawADC * 3.3 / 4096.0; //convert ADC value to the voltage read at the pin
308+
voltBat = max_Source_voltage * rawADC / 4096.0; //we cannot use map since it requires int inputs/outputs
308309
309310
int new_batt = (voltBat - batteryEmptyVoltage) * (100) / (batteryFullVoltage - batteryEmptyVoltage); //custom float friendly map function
310311

0 commit comments

Comments
 (0)