Thursday, February 23, 2017

Battery Percentage Algorithm

Hi guys, I just completed the battery percentage feature on the controller board and I'm going to share it with you.
First of all lets start with the 6v Battery pack for the controller. It is consists of 4 1.5V batteries in series. I believe its operating voltages are from around 4.2V to 6.2V ( I could be wrong) 4.2V means it is empty and 6.2V means it is fully charged. Well mine are not rechargeable so I'll have to run to the store and get new ones when it runs out. Anyhow, since your micro-controller requires 5V to operate so it is safe to say that the operating voltages of your battery pack is pretty much some where around 4.9V to 6.2V. And if you perform linear mapping (which I'm going to go over in a minute) to this you'll get your percentage. 4.9V means 0% and 6.2V means 100%. Pretty simple right? However there is a slight problem: Your micro controller does not read Voltages that are above 5V. Anything above 5V will just be map to the maximum value of the 10-bit ADC, which is 1023.

Best way to solve this is to drop the voltages down using a simple voltage divider that consist of 2 Resistors in series R1 and R2. Lets say you have 6.2V and you want to drop down to 5V. You'll need a drop of 1.2V across R1 and then you can read across R2 to get your 5V. If you are lazy just like me (high 5) then you'll go to google and pull up a voltage divider calculator to enter your desirable values and get the Resistances you need. But if you are not, then this is how the calculation goes.
Let's use a 1K Resistor for R1 (I just happen to have these laying around). Then according to the formula, your Vout = Vin*R2/(R2+R1) which is the equivalent of:
5 = 6.2*R2/(R2+1000). Solving for R2 you'll get R2 = 4166.67Ohm. You can use a 4.3K Ohm resistor and you'll be fine or if you have a 10K potentiometer, you can use that as well. I would recommend the pot since you can easily fine tune your resistance. So you dropped your max to 5V, what about your min? Well you gotta run it through the same equation too. Applying the same equation, but this time for your Vout. Vmin = Vin*R2(R1+R2) which is the equivalent of:
Vmin = 4.9*4166(4166+1000) which gives you Vmin = 3.95V, pretty close to 4.0V. So there you go your new Min and Max are 3.95V and 5V, mapping this to 0 to 100% would be easy right but WAITTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT...........................................................................................
Here's a downer at 4V, my ADC already maps it to 1023 so if you use the above values. Your battery will always be at 100%.
Bad ADC.
Well the solution is simple you just gotta test out your nano to see at what voltage it will give you the maximum digitized level of 1023. For me its at about 4V. Yours might be different. When you do figure it out, then you'd repeat the same steps as above to figure out your R2. Then adjust your R2 using a pot, and then use those values to calculate the lower limit voltages.

So the next step is to Map your Voltages into actual percentage. Lets say your min and Max are 3V to 4V. You'll need to convert these back to the corresponding digital levels multiplying the ratio (1023/4). This is the slope of your ADC mapping (run by 4-0V and rise by 1023-0). Once you have your digitized voltages you'll need to map them into percentage. For 3V - 4V the levels are 767 and 1023. If you graph this digitized levels as x axis and 0 to 100% as y axis. You can calculate the slope and your y intercept. And then in the code it is going to look something like this:

float  x = analogRead(A7); //(Read across your R2)
float percentage = x*slope - yIntercept;
Then you can display your percentage on the OLED by casting it into an integer
display.display((int) percentage);


No comments:

Post a Comment