How to Li-ion'ise your Arduino Project
Its easy, if you ask me!
I was apprehensive before my current project. But no longer. I do not have many photos, but will edit once I have them.
This is a highly involved project!
This is a series of post I am planning to write on projects I am doing, and my learnings from them.
Li-ion charger ( see here https://www.ebay.in/itm/TP4056-5v-1a-Lithium-Battery-Charging-Module-lipo-lion-charger-mini-USB-port-/171827715868?pt=LH_DefaultDomain_203&hash=item2801badf1c)
A Li-ion battery, choose a 1200mAh+ battery for a good amount of backup. Mostly these go for 100/- at Lamington Road.
An Arduino ofcourse, but any microcontroller with a spare ADC pin will suffice.
A couple of resistors, I used 10k and 39k 5% resistors. These are used to create a voltage divider network which senses battery voltage. Dont spend a lot on 1% MFR resistors, it is possible to have accurate results in software.
A switch with three pins, one common, two inputs. this is used to seperate the battery from the arduino into the charger and vice versa.
Firstly, your Arduino can get permanently damaged if the ADC input voltage is greater than the supply voltage.
A Li-ion battery highest voltage should not cross 4.2 volts. Its full discharged when it reaches 3.3V. Effectively, our ADC has to measure this range for the Arduino to know how far along will the battery last.
Here is what has been connected.
TP4056 Bat+ to one extreme terminal of switch.
TP4056 Bat- to Battery Negative terminal
Arduino 5V pin to other extreme terminal of switch.
Arduino GND to battery Negative terminal
40k and 10k resistor divider between battery terminals. 40k Resistor connected to Battery positive, 10k to battery GND
Center junction of voltage divider connected to A0 on the Arduino.
Center pin of switch connected to Battery (+) terminal.
Okay! I hope you have drawn a good circuit, please verify again.
One flick of switch will disconnect battery from the arduino and connect it to the charger, the other flick will be vice versa.
The TP4056 has two LED's, Red is charging, Blue is Charged
Change the ADC reference to Internal 1.1 V, see here https://www.arduino.cc/en/Reference/AnalogReference
At 4.2v, the A0 pin should ideally see a voltage below 1.1v, about 0.84 volts. This is the premise to choose those resistor values. With the 1.1v reference, this should be easily measurable.
Use the AnalogInOutSerial example, in the Arduino IDE, Change the ADC reference in the setup function, and note values. Lets say it is 650, this is ADC count. This has to be converted to voltage.
A0 pin voltage= 650*1.1/1023=0.69V. With expected 100% accurate resistor values, the battery voltage should be 3.38V( I leave this as an excercise for the reader)
Measure the Voltage of the battery. Lets say its 3.34V.
Why is there this difference? Because
1) The 1.1V reference is not purely 1.1V
2) The resistor values are not exactly 39k and 10k.
We add correction to this now. Charge the battery, and then let it discharge. Measure a couple of these ADC and Battery voltage values, and find the average multiplier to be provided in the calculation. For our example above, the multiplier is (3.34/3.38)=0.988
You now have the correct battery voltage
Battery voltage= ADC reading * 1.1 * multiplier * ( sum of resistor values)/1023;
Problems that can occur while running on batteries
1) Batteries will drain.
2) The Atmega chips have a certain max clock speed at which they run at different voltages. the datasheet will tell you this voltage speed relation. This means that unless you have a very stable voltage, a very strict timing cannot be achieved, because the system clock can be not so accurate
3) Brown Out- If the battery dips too low, the system will reset itself, can be mitigated by changing a couple of fuses.