So, the red positive wire should be on the left nearest the USB connector, and with the ‘fin’ on the JST connector uppermost.
If you already have a battery, but the wires are the wrong way round, they can be swapped over. Turn the JST connector over (so that the ‘fin’ is pointing downwards). The wires are held in place by two plastic tags. You can lift these tags with a sharp instrument such as an awl or small screwdriver, and the wires can be pulled out. Swap the wires around, then push back the tags to hold them in. Make sure the wires do not touch as you remove them from the JST, or you will short out the battery.
Plug the lithium polymer battery into the JST socket on the Arduino, and plug the USB cable into the board and your computer. Nothing will happen.
However, if you plug in a brand-new Arduino for the first time, you may see the built in LED blinking once per second as the board runs a factory programmed test code to verify functionality.
The board needs to be told to charge the battery, and for that we need a sketch and yet another library. Click this link and download the ZIP file to your computer: https://downloads.arduino.cc/libraries/github.com/arduino-libraries/Arduino_BQ24195-0.9.1.zip
You do not need to extract the contents of the ZIP file.
Open your Arduino IDE and install the library. Go to Sketch > Include Library > Add .ZIP Library. At the top of the drop-down list, select the option to ‘Add .ZIP Library’. You will now be prompted to navigate to where you saved the ZIP file. Click on it and select ‘Open’. The library will be installed and will appear in the ‘Include Library’ drop-down list.
Now you can extract the ZIP file. Look in the folder you have just extracted until you find the folder called ‘Examples’. Open this, open BatteryCharger, then open BatteryCharger.ino. You should see this:
/*
Battery charge Example
This example shows how to configure and enable charge mode on Arduino MKR boards
Circuit:
- Arduino MKR board
- 750 mAh lipo battery
created 21 Aug 2019
by Riccardo Rizzo
This sample code is part of the public domain.
*/
#include
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
if (!PMIC.begin()) {
Serial.println("Failed to initialize PMIC!");
while (1);
}
// Set the input current limit to 2 A and the overload input voltage to 3.88 V
if (!PMIC.setInputCurrentLimit(2.0)) {
Serial.println("Error in set input current limit");
}
if (!PMIC.setInputVoltageLimit(3.88)) {
Serial.println("Error in set input voltage limit");
}
// set the minimum voltage used to feeding the module embed on Board
if (!PMIC.setMinimumSystemVoltage(3.5)) {
Serial.println("Error in set minimum system volage");
}
// Set the desired charge voltage to 4.11 V
if (!PMIC.setChargeVoltage(4.2)) {
Serial.println("Error in set charge volage");
}
// Set the charge current to 375 mA
// the charge current should be definde as maximum at (C for hour)/2h
// to avoid battery explosion (for example for a 750mAh battery set to 0.375 A)
if (!PMIC.setChargeCurrent(0.375)) {
Serial.println("Error in set charge current");
}
Serial.println("Initialization done!");
}
void loop() {
// Enable the Charger
if (!PMIC.enableCharge()) {
Serial.println("Error enabling Charge mode");
}
// canRunOnBattery() returns true if the battery voltage is < the minimum
// systems voltage
if (PMIC.canRunOnBattery()) {
// loop until charge is done
while (PMIC.chargeStatus() != CHARGE_TERMINATION_DONE) {
delay(1000);
}
// Disable the charger and loop forever
Serial.println("Disable Charge mode");
if (!PMIC.disableCharge()) {
Serial.println("Error disabling Charge mode");
}
while (1);
// if you really want to detach the battery call
// PMIC.disableBATFET();
//isbatteryconnected = false;
}
delay(100);
}
Making sure that you have the correct board and port selected, upload the sketch to your Arduino.
How much power is left in the battery? For this you need another sketch and the Adafruit MAX17048 fuel gauge.
You will also need a 4 Pin Sh1.0 connector from the Elechawk I2C Qwiic cable kit. Choose the one that has the four male Dupont connectors. Plug this cable into the MAX17048 fuel gauge:
We can now connect the JST-PH 2-pin Jumper Cable. There are two battery terminals on the MAX17048 and they are connected together. So, if a Li Po battery is connected to one terminal, the other terminal becomes live. We can now use the second ‘live’ terminal to connect the MAX17048 to our Arduino to power the board.
Your circuit should now look like this:
A note on I2C
I ‘two’ C or sometimes I ‘squared’ C stands for Inter-Integrated Circuit. It is a communications protocol invented in 1982 by Philips Semiconductors. It allows peripheral circuits to connect to micro controllers using just two wires.
Pin 11 SDA stands for Serial Data, and pin 12 SCL stands for Serial Clock. The job of the serial clock is to make sure that the information sent from the MAX17048 is sent at the same data rate that the Arduino is expecting to receive it. Otherwise, you could end up with garbled data received by the SDA pin.
We can now power our Arduino from a rechargeable battery. Our next task is to monitor the state of charge of the battery once it has been disconnected from your computer. Click here for the next steps.