Reading Battery Data With an Arduino

Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
I sourced mine from a dead solo. The triple pin is actually two separate molex ten60 power connectors. The two lower pins are one connector, which is the same that is found on the charger. The one with the data line is a ten60 split.

I think you will have to source each component separately...

Molex RSD-171088-0048.pdf

The level shifter I got from sparkfun: SparkFun Level Translator Breakout - PCA9306 - BOB-11955 - SparkFun Electronics, there are probably cheaper options from china.
Thanks yes i have an inquiry into Dijikey.ca for that drawing i have already sent it in to them.They are going to source for me that very split with double attached. Thanks.
 
Joined
Jun 18, 2015
Messages
2,898
Reaction score
1,514
Location
Chouston, Tejas
I have made my own

fairly simple

use a double sided 1/16 copper clad pcb board cut three strips
(optional just for looks) get a small relay take cover off and use cover

solder your wires to both sides on power connector
solder one wire to each side for communication port

take blue painters tape cover battery terminals
slice small slot in tape with razor blade the slide PCB pieces in battery thru cuts in tape

Take JB Weld Putty mold it around connectors slide cover over wait 15 minutes pull out of battery.
Just throwing this out there and may not ever be an issue, but... The copper clad board is abraded to a certain degree for photo resist to adhere. It may not be a bad idea to polish out the copper cladding or diy power terminals to insure you're not wearing down the battery's contacts at a faster rate.

Just a thought towards longer life for the battery's connector, as a bright surface will be less abrasive.

The relay cover and epoxy idea is a great solution. I was trying to figure out how to contain the epoxy on my diy plug build. fwiw, I sandwiched hdpe with copper psa tape for the data connector...stuff I had on hand and seems to be holding up well.
 
Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
@pious_greek Heard back from Digikey today. Not good, the quote I got was for $3400.00CAD I needed to order 650 at $5.25CAD each.13-15 week delivery!! BALLS! I sent an email to Molex Canada. See what they can do for me! :) This home made garbage might be the only way to here soon. Jesus.
 
Joined
Nov 15, 2016
Messages
189
Reaction score
111
Location
Fresno, CA
@pious_greek Heard back from Digikey today. Not good, the quote I got was for $3400.00CAD I needed to order 650 at $5.25CAD each.13-15 week delivery!! BALLS! I sent an email to Molex Canada. See what they can do for me! :) This home made garbage might be the only way to here soon. Jesus.
Molex will tell you minimum order is 650 and give you a list of recommended distributors to order from. I guess it's time for plan b... anybody know where @PdxSteve keeps his stash? :)
 
Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
Bump. @pious_greek , might need your help again....? I have been struggling with this code a bit again. I got the battery data out of the battery no problem. But when I try to add my LCD screen i2c on the bus it reads the battery one time and then stops and spits out garbage data. The code stops reading the battery when ever i put in LCD.init(); soon as i comment that out. Pooof its ok. I dont know how to interface the LCD i2c properly with this battery sketch. Any chance you could help me out again? Thank you!

As well, the i2c wont print anything to the LCD either, If I open a new sketch with just LCD code, no problem. Try to add script to battery sketch. No dice.
 
Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
Code:
/**********************************

 * SMBus FOR 3DR SOLO
 * STAVROPOULOS
 * Code Version 0.01 beta
 *
 * MUCH OF THIS CODE WAS COPIED FROM
 * https://github.com/PowerCartel/PackProbe/blob/master/PackProbe/PackProbe.ino
 * https://github.com/ArduPilot/PX4Firmware/blob/master/src/drivers/batt_smbus/batt_smbus.cpp
 *
 **********************************/

/**********************************
 * CONFIGURE I2C/SERIAL ON ARDUINO
 **********************************/
 
//DEFINE SDA AND SCL PINS
  #define SCL_PIN 5                 //COMMUNICATION PIN 20 ON MEGA
  #define SCL_PORT PORTC

  #define SDA_PIN 4                 //COMMUNICATION PIN 21 ON MEGA
  #define SDA_PORT PORTC

//CONFIGURE I2C MODES
  #define I2C_TIMEOUT 100           //PREVENT SLAVE DEVICES FROM STRETCHING LOW PERIOD OF THE CLOCK INDEFINITELY AND LOCKING UP MCU BY DEFINING TIMEOUT
  //#define I2C_NOINTERRUPT 1       //SET TO 1 IF SMBus DEVICE CAN TIMEOUT
  //#define I2C_FASTMODE 1          //THE STANDARD I2C FREQ IS 100kHz.  USE THIS TO PERMIT FASTER UP TO 400kHz.
  //#define I2C_SLOWMODE 1            //THE STANDARD I2C FREQ IS 100kHz.  USE THIS TO PERMIT SLOWER, DOWN TO 25kHz.
  #define BAUD_RATE 115200
  #include <SoftI2CMaster.h>

/**********************************
 * CONFIGURE SERIAL LIBRARY
 **********************************/
  //#include <SoftwareSerial.h>
  //#include <Serial.h>
  #include <Wire.h>
  #include <LiquidCrystal_I2C.h> //i2c library for my LCD display that works.
 
 
/**********************************
 * DEFINE VARIABLES AND SMBus MAPPINGS
 **********************************/

  #define BATT_SMBUS_ADDR                     0x0B                ///< I2C address
  #define BATT_SMBUS_ADDR_MIN                 0x08                ///< lowest possible address
  #define BATT_SMBUS_ADDR_MAX                 0x7F                ///< highest possible address
//BUS MAPPINGS FROM DEV.3DR
  #define BATT_SMBUS_TEMP                     0x08                ///< temperature register
  #define BATT_SMBUS_VOLTAGE                  0x09                ///< voltage register
  #define BATT_SMBUS_REMAINING_CAPACITY       0x0f                ///< predicted remaining battery capacity as a percentage
  #define BATT_SMBUS_FULL_CHARGE_CAPACITY     0x10                ///< capacity when fully charged
  #define BATT_SMBUS_DESIGN_CAPACITY          0x18                ///< design capacity register
  #define BATT_SMBUS_DESIGN_VOLTAGE           0x19                ///< design voltage register
  #define BATT_SMBUS_SERIALNUM                0x1c                ///< serial number register
  #define BATT_SMBUS_MANUFACTURE_NAME         0x20                ///< manufacturer name
  #define BATT_SMBUS_MANUFACTURE_DATA         0x23                ///< manufacturer data
  #define BATT_SMBUS_MANUFACTURE_INFO         0x25                ///< cell voltage register
  #define BATT_SMBUS_CURRENT                  0x2a                ///< current register
  #define BATT_SMBUS_MEASUREMENT_INTERVAL_US  (1000000 / 10)      ///< time in microseconds, measure at 10hz
  #define BATT_SMBUS_TIMEOUT_US               10000000            ///< timeout looking for battery 10seconds after startup
  #define BATT_SMBUS_BUTTON_DEBOUNCE_MS       300                 ///< button holds longer than this time will cause a power off event
 
  #define BATT_SMBUS_PEC_POLYNOMIAL           0x07                ///< Polynomial for calculating PEC
  #define BATT_SMBUS_I2C_BUS                  PX4_I2C_BUS_EXPANSION
//BUS MAPPINGS FROM SMBus PROTOCOL DOCUMENTATION
#define BATTERY_MODE             0x03
#define CURRENT                  0x0A
#define RELATIVE_SOC             0x0D
#define ABSOLUTE_SOC             0x0E
#define TIME_TO_FULL             0x13
#define CHARGING_CURRENT         0x14
#define CHARGING_VOLTAGE         0x15
#define BATTERY_STATUS           0x16
#define CYCLE_COUNT              0x17
#define SPEC_INFO                0x1A
#define MFG_DATE                 0x1B
#define DEV_NAME                 0x21   // String
#define CELL_CHEM                0x22   // String
#define CELL4_VOLTAGE            0x3C   // Indidual cell voltages don't work on Lenovo and Dell Packs
#define CELL3_VOLTAGE            0x3D
#define CELL2_VOLTAGE            0x3E
#define CELL1_VOLTAGE            0x3F
#define STATE_OF_HEALTH          0x4F
//END BUS MAPPINGS
 
  #define bufferLen 32
  uint8_t i2cBuffer[bufferLen];

// standard I2C address for Smart Battery packs
  byte deviceAddress = BATT_SMBUS_ADDR;
 
  LiquidCrystal_I2C LCD(0x3B,16,2);
 
int i2c;
 
void setup()
{
 
  

  //INITIATE SERIAL CONSOLE
    Serial.begin(BAUD_RATE);
  /*
    Serial.println(i2c_init());
    
    
  //SETUP I2C INPUT PINS
   // pinMode(5,INPUT_PULLUP);  // commented this out becauase it cant be used on R3 UNO
   // pinMode(4,INPUT_PULLUP);
    Serial.flush();

    while (!Serial) {
    ;                                                       //wait for Console port to connect.
    }

    Serial.println("Console Initialized");
 */
    i2c_init();                                             //i2c_start initialized the I2C system.  will return false if bus is locked.
    Serial.println("I2C Inialized");
 
}

int fetchWord(byte func)
{
    i2c_start(deviceAddress<<1 | I2C_WRITE);                //Initiates a transfer to the slave device with the (8-bit) I2C address addr.
                                                            //Alternatively, use i2c_start_wait which tries repeatedly to start transfer until acknowledgment received
    //i2c_start_wait(deviceAddress<<1 | I2C_WRITE);
    i2c_write(func);                                        //Sends a byte to the previously addressed device. Returns true if the device replies with an ACK.
    i2c_rep_start(deviceAddress<<1 | I2C_READ);             //Sends a repeated start condition, i.e., it starts a new transfer without sending first a stop condition.
    byte b1 = i2c_read(false);                              //i2c_read Requests to receive a byte from the slave device. If last is true,
                                                            //then a NAK is sent after receiving the byte finishing the read transfer sequence.
    byte b2 = i2c_read(true);
    i2c_stop();                                             //Sends a stop condition and thereby releases the bus.
    return (int)b1|((( int)b2)<<8);
}

uint8_t i2c_smbus_read_block ( uint8_t command, uint8_t* blockBuffer, uint8_t blockBufferLen )
{
    uint8_t x, num_bytes;
    i2c_start((deviceAddress<<1) + I2C_WRITE);
    i2c_write(command);
    i2c_rep_start((deviceAddress<<1) + I2C_READ);         
    num_bytes = i2c_read(false);                              //num of bytes; 1 byte will be index 0
    num_bytes = constrain(num_bytes,0,blockBufferLen-2);      //room for null at the end
    for (x=0; x<num_bytes-1; x++) {                           //-1 because x=num_bytes-1 if x<y; last byte needs to be "nack"'d, x<y-1
      blockBuffer[x] = i2c_read(false);
    }
    blockBuffer[x++] = i2c_read(true);                        //this will nack the last byte and store it in x's num_bytes-1 address.
    blockBuffer[x] = 0;                                       //and null it at last_byte+1
    i2c_stop();
    return num_bytes;
}



void loop()
{

 
    uint8_t length_read = 0;
    delay (750);
    Serial.write(12);
    Serial.print("Manufacturer Name: ");
    length_read = i2c_smbus_read_block(BATT_SMBUS_MANUFACTURE_NAME, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");
 
    //Serial.print("Manufacturer Data: ");
   // length_read = i2c_smbus_read_block(BATT_SMBUS_MANUFACTURE_DATA, i2cBuffer, bufferLen);
    //Serial.write(i2cBuffer, length_read);
   // Serial.println("");
 
   // Serial.print("Manufacturer Info: ");
    //length_read = i2c_smbus_read_block(BATT_SMBUS_MANUFACTURE_INFO, i2cBuffer, bufferLen);
    //Serial.write(i2cBuffer, length_read);
   // Serial.println("");
 
    Serial.print("Design Capacity: " );
    Serial.println(fetchWord(BATT_SMBUS_DESIGN_CAPACITY));
 
    Serial.print("Design Voltage: " );
    Serial.println(fetchWord(BATT_SMBUS_DESIGN_VOLTAGE));
 
    Serial.print("Serial Number: ");
    Serial.println(fetchWord(BATT_SMBUS_SERIALNUM));
 
    Serial.print("Voltage: ");
    Serial.println((float)fetchWord(BATT_SMBUS_VOLTAGE)/1000);
 
    Serial.print("Full Charge Capacity mAh: " );
    Serial.println(fetchWord(BATT_SMBUS_FULL_CHARGE_CAPACITY));
 
    Serial.print("Remaining Capacity mAh: " );
    Serial.println(fetchWord(BATT_SMBUS_REMAINING_CAPACITY));
 
    Serial.print("Temp: ");
    unsigned int tempk = fetchWord(BATT_SMBUS_TEMP);
    Serial.println((float)tempk/10.0-273.15);
 
    Serial.print("Current (mA): " );
    Serial.println(fetchWord(BATT_SMBUS_CURRENT));

    Serial.print("Device Name: ");
    length_read = i2c_smbus_read_block(DEV_NAME, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");

    Serial.print("Chemistry ");
    length_read = i2c_smbus_read_block(CELL_CHEM, i2cBuffer, bufferLen);
    Serial.write(i2cBuffer, length_read);
    Serial.println("");

    String formatted_date = "Manufacture Date (Y-M-D): ";
    int mdate = fetchWord(MFG_DATE);
    int mday = B00011111 & mdate;
    int mmonth = mdate>>5 & B00001111;
    int myear = 1980 + (mdate>>9 & B01111111);
    formatted_date += myear;
    formatted_date += "-";
    formatted_date += mmonth;
    formatted_date += "-";
    formatted_date += mday;
    Serial.println(formatted_date);

   // Serial.print("Specification Info: ");
   // Serial.println(fetchWord(SPEC_INFO));
 
    Serial.print("Cycle Count: " );
    Serial.println(fetchWord(CYCLE_COUNT));
 
    Serial.print("Relative Charge(%): ");
    Serial.println(fetchWord(RELATIVE_SOC));
 
    Serial.print("Absolute Charge(%): ");
    Serial.println(fetchWord(ABSOLUTE_SOC));
 
    Serial.print("Minutes remaining for full charge: ");
    Serial.println(fetchWord(TIME_TO_FULL));
 
    // These aren't part of the standard, but work with some packs.
    // They don't work with the Lenovo and Dell packs we've tested
    Serial.print("Cell 1 Voltage: ");
    Serial.println(fetchWord(CELL1_VOLTAGE));
    Serial.print("Cell 2 Voltage: ");
    Serial.println(fetchWord(CELL2_VOLTAGE));
    Serial.print("Cell 3 Voltage: ");
    Serial.println(fetchWord(CELL3_VOLTAGE));
    Serial.print("Cell 4 Voltage: ");
    Serial.println(fetchWord(CELL4_VOLTAGE));
 
   // Serial.print("State of Health: ");
   // Serial.println(fetchWord(STATE_OF_HEALTH));
 
    //Serial.print("Battery Mode (BIN): 0b");
    //Serial.println(fetchWord(BATTERY_MODE),BIN);
 
    //Serial.print("Battery Status (BIN): 0b");
    //Serial.println(fetchWord(BATTERY_STATUS),BIN);
 
    Serial.print("Charging Current: ");
    Serial.println(fetchWord(CHARGING_CURRENT));
 
    Serial.print("Charging Voltage: ");
    Serial.println(fetchWord(CHARGING_VOLTAGE));
 
    Serial.print("Charging Current (mA): " );
    Serial.println(fetchWord(CURRENT));

    Serial.println(".");



    LCD.init();
    LCD.backlight();//this turns the backlight on

    LCD.setCursor(0,0);
    LCD.print("Charge V:");
    LCD.println(fetchWord(CHARGING_VOLTAGE));
    LCD.setCursor(0,1);
    LCD.print("Charge mA:");
    LCD.print(fetchWord(CHARGING_CURRENT));
    

    delay(1500);

    LCD.setCursor(0,0);
    LCD.print("C1:");
    LCD.print(fetchWord(CELL1_VOLTAGE));
    LCD.print("    ");
    LCD.print("C2:");
    LCD.print(fetchWord(CELL2_VOLTAGE));
  

    LCD.setCursor(0,1);
    LCD.print("C3:");
    LCD.print(fetchWord(CELL3_VOLTAGE));
    LCD.print("    ");
    LCD.print("C4:");
    LCD.print(fetchWord(CELL4_VOLTAGE));
    
i2c_stop();
Wire.endTransmission();
  
    delay(750);
}
 
Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
So the above code is what Im trying to use... ugh... It compiles ok, but when it runs down to the LCD.init(); it pretty much hates having 2 devices on the i2c bus. I dont know how to properly let 2 devices co-exist in the code. Sorry for nooby questions, I know enough about coding to be just dangerous! haha

Thanks everyone!
 
Joined
Nov 10, 2016
Messages
18
Reaction score
7
Age
42
@CanadianSolo - have you managed to solve this issue yet?
I seem to have exactly the same issue with my setup even though I'm using a different screen - I can either read the Solo battery info and display it via the serial monitor or I can display info on my OLED screen over i2c but never both at the same time - as soon as I power up the lipo, the screen freezes and the serial console starts spitting out useful data or vice versa.

I'm fairly certain it is just my lack of understanding of this type of coding - any tips would be appreciated!

I have both the battery SDA and SCL lines hooked to pins 4/5 on an Arduino Uno with 10K pullup resistors to the 5V line and the ground of the lipo also connected to the Uno ground.

@PdxSteve - I'm trying to do something very similar to your project, if you can shed any light on this that would be great :)
 

Attachments

Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
@CanadianSolo - have you managed to solve this issue yet?
I seem to have exactly the same issue with my setup even though I'm using a different screen - I can either read the Solo battery info and display it via the serial monitor or I can display info on my OLED screen over i2c but never both at the same time - as soon as I power up the lipo, the screen freezes and the serial console starts spitting out useful data or vice versa.

I'm fairly certain it is just my lack of understanding of this type of coding - any tips would be appreciated!

I have both the battery SDA and SCL lines hooked to pins 4/5 on an Arduino Uno with 10K pullup resistors to the 5V line and the ground of the lipo also connected to the Uno ground.

@PdxSteve - I'm trying to do something very similar to your project, if you can shed any light on this that would be great :)
No im still stuck. I honestly just havn't had any time. I asked some co-workers that know some code, and they think that the battery code is latching onto the bus some how and not releasing it. And im in the same boat as you i know enough code to alter a few things, but I just cant both running on the same bus. From what you describe im hooked up the same as you! I have been waiting patiently for someone to chime in here and see if they can look at the above shared code come up with a resolution to fix the issue.
 
Joined
Nov 10, 2016
Messages
18
Reaction score
7
Age
42
No im still stuck. I honestly just havn't had any time. I asked some co-workers that know some code, and they think that the battery code is latching onto the bus some how and not releasing it. And im in the same boat as you i know enough code to alter a few things, but I just cant both running on the same bus. From what you describe im hooked up the same as you! I have been waiting patiently for someone to chime in here and see if they can look at the above shared code come up with a resolution to fix the issue.
I think I've cracked it - I changed the value of my pullup resistors and fiddled some more and now have a rough version working - I'll post the code up l tomorrow once I've got it displaying a few more lines :)
 

Attachments

Joined
Nov 10, 2016
Messages
18
Reaction score
7
Age
42
Here's what I've got for now. The code is likely far from optimal but it's essentially doing exactly what I wanted so it's a start!

 
Last edited:
Joined
Jan 2, 2017
Messages
40
Reaction score
8
Age
59
Here's what I've got for now. The code is likely far from optimal but it's essentially doing exactly what I wanted so it's a start!

Awesome! I can't wait to hear what you did with resistors and see the code.
 
Joined
Nov 10, 2016
Messages
18
Reaction score
7
Age
42
Here's the code and what it should look like, with added splash screen!

I've chosen only to display the information I'm interested in - I am not planning on using this whilst charging so that info is available but ignored.

I'd still like to sort out the cell voltages which are currently in mV not V as my code was truncating the full voltages from 4 to 2 decimal places - easy enough to fix it but was getting tired.

Might also add a button or 2 to enable scrolling between the different screens, any other suggestions welcome :)

The small change I had to make to my setup was moving back to the 4.7K resistors instead of the 10K ones that I was initially using on the basis that I needed a higher value due to having 2 devices on the same I2C line. The other changes are visible in the code attached...

 

Attachments

Last edited:
  • Like
Reactions: pious_greek
Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
Here's the code and what it should look like, with added splash screen!

I've chosen only to display the information I'm interested in - I am not planning on using this whilst charging so that info is available but ignored.

I'd still like to sort out the cell voltages which are currently in mV not V as my code was truncating the full voltages from 4 to 2 decimal places - easy enough to fix it but was getting tired.

Might also add a button or 2 to enable scrolling between the different screens, any other suggestions welcome :)

The small change I had to make to my setup was moving back to the 4.7K resistors instead of the 10K ones that I was initially using on the basis that I needed a higher value due to having 2 devices on the same I2C line. The other changes are visible in the code attached...

Thanks for posting your findings. I never though of changing the resistor values... ugh so stupid of me. Hey could you change your video security settings I cant see them it says private. Thanks bud!
 
Joined
Nov 10, 2016
Messages
18
Reaction score
7
Age
42
Thanks for posting your findings. I never though of changing the resistor values... ugh so stupid of me. Hey could you change your video security settings I cant see them it says private. Thanks bud!
Easily done! I2C devices are apparently quite fiddly in this regard...I certainly had lots of issues with intermittent connections on the breadboard, wire lengths and resistor requirements...
Video should now be available:
 
Last edited:
  • Like
Reactions: pious_greek
Joined
Jul 10, 2015
Messages
243
Reaction score
91
Location
CANADA
Easily done! I2C devices are apparently quite fiddly in this regard...I certainly had lots of issues with intermittent connections on the breadboard, wire lengths and resistor requirements...
Video should now be available:
Ok I finally had some time over the past weekend to have a look at the code you posted. Interesting...I removed a bunch of crap like that i2C scanning stuff, all the batteries will be the same Address LOL. What you added was an integer to fetch and store the value requested ie. CellV1. I modified it for my self for a float and then divided it by 1000 so that i get 3.81v displayed. But your bit of code helped me! I am now able to get the battery read and output to the Serial monitor as well as output to the LCD screen. my (16x2).

I have the same issue as well, It will only run ONCE! I see your bit of code there for the watchdog reset....I tried it in my R3, and it locked it up tigher than a FART and it also tied up my Com port on my PC. I had to short the reset pins on the R3 to get it back to life. Brief moment of panic there. So I guess the last thing would be to figure out why its stopping after one run and outputting CRAP.

Thanks for your post @bassmission !! Maybe @pious_greek could have a look at the code above and see? Im going to work on it as well....still rusty with coding.

As for the Resistor values. I was always using 4.7k (well resistors in parallel to give me 4.9k) and its worked just fine on the 5v line.
 
  • Like
Reactions: bassmission
Joined
Jan 2, 2017
Messages
40
Reaction score
8
Age
59
I have it running through the paces on a nano at least. I'm using a 128x32 OLED. I haven't gotten a battery to read yet but it's mostly because I don't have a good interface for the battery. I've got this sad little slip of mylar with a couple wires taped on. What thickness 2 sided PCB material are folks using?

Does the size of the resistors matter at all? I have some teeny 1/8 watt 4.7k jobbies.
 

Attachments

Joined
Nov 10, 2016
Messages
18
Reaction score
7
Age
42
Glad some other people are making progress! I managed to kill my OLED screen somehow so haven't got much further!

@dneelands - if you have some copper tape, I made some pretty reliable connectors using cut up credit card, adding a few extra layers of copper tape until the connection was tight, but not too tight. Until I did this I had very unpredictable results just by jamming wires in.
 
Last edited:

New Threads

Members online

Forum statistics

Threads
11,804
Messages
138,919
Members
13,605
Latest member
antippc