I grabbed the example code from the sparkfun website under the Documents section:
https://www.sparkfun.com/products/10711
<LINK_TEXT text="
http://dlnmh9ip6v2uc.cloudfront.net/da … ample.ino
">
http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Dev/Arduino/Shields/LiPower_Shield_Example.ino
</LINK_TEXT>
Then I modified it to work with my LCD screen and not the serial monitor, since the code provided is inherently dangerous. Please see my code attached.
clang | last year 1
What may happen if I plug the Arduino to USB to upload some sketch with connected LiPower Shield with battery attached?
tim7 | last year * 1
That would not be a healthy thing to do. The Uno’s power circuitry connects the 5V pin to USB power whenever Vin is less than 6.6V. The LiPower shield doesn’t supply Vin, so you’d end up with the output of the 5V step-up regulator on the shield shorted to the USB power on the Uno.
Member #338104 | about 6 months ago 1
Doesn’t that mean the example code is inherently dangerous? (Sorry to zombie your reply.)
I run into problems with the i2cWrite16(unsigned int data, unsigned char address) function. There is something screwy in this function that causes my USB connection to port 7 (Arduino Port) to bounce, meaning I can no longer communicate with my arduino anymore until I unplug and reboot computer. It probably has to do with
// R/W =~ 0x6D/0x6C
#define MAX17043_ADDRESS 0x36
All I can say is my R/W pin on LCD screen is wired to ground. What does this code mean? I tried all values, so how do I know what will work for me? Please help!
/*********************************************************************************
LiPower Shield Example Code
By: Jim Lindblom
SparkFun Electronics
Date: 1/3/2012
Modified By: Evan Johnson
Date: 12/23/2012
license: Creative Commons Attribution-ShareAlike 3.0 (CC BY-SA 3.0)
Do whatever you'd like with this code, use it for any purpose.
Please attribute and keep this license.
This is an example for the MAX17043G chip on the LiPower Shield.
The MAX17043G+U is a compact, low-cost 1S LiPo fuel gauge. The
Arduino talks with the MAX17043 over an I2C (two-wire) interface,
so we'll use the Wire.h library to talk with it.
The program reads the battery voltage, its percentage full, and
prints the results to the LCD screen. You'll learn how to how to
configure the MAX17043G chip. Further you'll learn how to read
and manipulate the voltage values.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 10
* LCD D5 pin to digital pin 9
* LCD D6 pin to digital pin 8
* LCD D7 pin to digital pin 7
* LCD R/W pin to GND
* Analog output of 10K potentiometer to LCD VO
*************************************************************************
************************************************************************/
// include the wire library:
#include <Wire.h>
// include the liquid crystal library:
#include <LiquidCrystal.h>
// R/W =~ 0x6D/0x6C
#define MAX17043_ADDRESS 0x36
// initialize the alert interrupt pin, connected to pin 2 on the LiPower Shield
int alertPin = 2;
// global variables
float batVoltage = 0.0f;
float batPercentage = 0.0f;
int alertStatus = 0;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
//***************************SETUP************************************************
//********************************************************************************
void setup()
{
pinMode(alertPin, INPUT);
digitalWrite(alertPin, HIGH);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// clear screen and set cursor to upper left corner
lcd.clear();
// print sentence to LCD screen
lcd.print("Hello World!");
// delay 5 second
delay(5000);
// clear screen and set cursor to upper left corner
lcd.clear();
// start I2C (two-wire) interface
Wire.begin();
delay(100);
// 1. configure the MAX17043 chip alert percentage to 32%. Therefore,
// if the battery's power runs under 32%, then the alert flag will be triggered.
configMAX17043(32);
}/*
// 2. restart fuel-gauge calculations
qsMAX17043();
}
*/
//********************START OF LOOP********************************************
//*****************************************************************************
void loop()
{}
/*
// 3. call single return function to retrieve battery's power percentage
// and save as a float
batPercentage = percentMAX17043();
// 4. call single return function to retrieve battery's voltage
// note: vcell reports battery in 1.25mV increments
batVoltage = (float) vcellMAX17043() * 1/800;
// call digitalRead function using input pin 2 (the alertPin)
// and store result as an integer.
// NOTE: (1 signifies low battery power, 0 signifies high battery power
alertStatus = digitalRead(alertPin);
// print the battery percentage (accuracy of .01 - 2 spots after the decimal)
lcd.print(batPercentage, 2);
lcd.print(" %");
// set cursor at beginning of row 1
lcd.setCursor(0,1);
// print battery voltage (accuracy of .01 - 2 spots after the decimal)
lcd.print(batVoltage, 2);
lcd.print(" volts");
// hold battery results on screen for 5 seconds
delay(5000);
// clear screen and set cursor to upper left corner
lcd.clear();
// print 0 if battery percentage is below 32%
// print 1 if battery percentage is above 32%
lcd.print("Alert Status = ");
lcd.print(alertStatus, DEC);
// hold battery results on screen for 5 seconds
delay(5000);
// clear screen and set cursor to upper left corner
lcd.clear();
}
//*****************************END OF LOOP*********************************
//*************************************************************************
/***************************************************************
1. void/value function configures the config register of
the MAX170143 chip, specifically the alert threshold therein. Pass a
value between 1 and 32 to set the alert threshold to a value between
1 and 32%. Any other values will set the threshold to 32%.
***************************************************************/
void configMAX17043(byte percent)
{
// anything 32 or greater will set to 32%
if ((percent >= 32)||(percent == 0))
{
//5. void/values function to write data to address.
// essentially it's writing result of battery to Arduino Uno board
i2cWrite16(0x9700, 0x0C);
}
else
{
byte percentBits = 32 - percent;
//5. void/values function to write data to address.
// essentially it's writing result of battery to Arduino Uno board
i2cWrite16((0x9700 | percentBits), 0x0C);
}
}
/************************************************************************
2. void/void function issues a quick-start command to the MAX17043. A quick
start allows the MAX17043 chip to restart fuel-gauge calculations
in the same manner as initial power-up of the IC. If an application's
power-up sequence is very noisy, such that excess error is introduced
into the IC's first guess of SOC, the Arduino can issue a quick-start
to reduce the error.
************************************************************************/
void qsMAX17043()
{
// write a 0x4000 to the MODE register
i2cWrite16(0x4000, 0x06);
}
/*********************************************************************
3. single return function returns a float value of the battery's power
percentage reported from the SOC register of the MAX17043 chip.
*********************************************************************/
float percentMAX17043()
{
unsigned int soc;
float percent;
// 6. read SOC register of MAX17043 chip
soc = i2cRead16(0x04);
// high byte of SOC is percentage
percent = (byte) (soc >> 8);
// low byte is (1/256)%
percent += ((float)((byte)soc))/256;
return percent;
}
/*********************************************************************
4. single return function returns a 12-bit Analog to Digital Converter (ADC)
reading of the battery voltage, as reported by the MAX17043's VCELL register.
This does not return a voltage value. To convert this to a voltage,
multiply by 5 and divide by 4096.
**********************************************************************/
unsigned int vcellMAX17043()
{
unsigned int vcell;
// 6. read vcell register of MAX17043 chip
vcell = i2cRead16(0x02);
// last 4 bits of vcell are nothing
vcell = vcell >> 4;
return vcell;
}
/************************************************************************
5. i2cWrite16(unsigned int data, unsigned char address) writes 16 bits
of data beginning at an 8-bit address, and continuing to the next.
************************************************************************/
void i2cWrite16(unsigned int data, unsigned char address)
{
Wire.beginTransmission(MAX17043_ADDRESS);
//Wire.write(address);
//Wire.write((byte)((data >> 8) & 0x00FF)); //PROBLEM SOMEWHERE HERE!!!!!!!
//Wire.write((byte)(data & 0x00FF));
Wire.endTransmission();
}
/*******************************************************************
6. i2cRead16(unsigned char address) reads a 16-bit value beginning
at the 8-bit address, and continuing to the next address. A 16-bit
value is returned.
*******************************************************************/
unsigned int i2cRead16(unsigned char address)
{
int data = 0;
Wire.beginTransmission(MAX17043_ADDRESS);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom(MAX17043_ADDRESS, 2);
while (Wire.available() < 2)
;
data = ((int) Wire.read()) << 8;
data |= Wire.read();
return data;
}