GithubHelp home page GithubHelp logo

adafruit_ina219's Introduction

Adafruit INA219 Library Build StatusDocumentation

This is a library for the Adafruit INA219 high side DC current sensor boards:

Check out the links above for our tutorials and wiring diagrams. This chip uses I2C to communicate.

To install, use the Arduino Library Manager and search for 'Adafruit INA219' and install the library.

Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

Written by Ktown for Adafruit Industries. MIT license, all text above must be included in any redistribution

adafruit_ina219's People

Contributors

andydoro avatar colindgrant avatar deanm1278 avatar driverblock avatar evaherrada avatar flavorj avatar hathach avatar hoffmannjan avatar ladyada avatar niabassey avatar outlookhazy avatar paintyourdragon avatar siddacious avatar skurrier avatar tdicola avatar tyeth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

adafruit_ina219's Issues

Power divider should be 0.5 in setCalibration_32V_2A

PowerLSB is 2mW in this method, then should be 0.5 the divider: 1 bit / 2mW.

  // 6. Calculate the power LSB
  // PowerLSB = 20 * CurrentLSB
  // PowerLSB = 0.002 (2mW per bit)
...
 ina219_powerDivider_mW = 2; // Power LSB = 1mW per bit (2/1)

Should be:

 ina219_powerDivider_mW = 0.5; // Power LSB = 2mW per bit (1/2)

You can check the results doing the operation getBusVoltage_V()*getCurrent_mA()

Confusion between "load voltage" and "bus voltage".

There is confusion between "load voltage" and "bus voltage" that is used in the library.
I made this issue because of this forum topic: https://forum.arduino.cc/index.php?topic=735283.0

Example:
When a 12V car battery provides power to the devices in a car and a rear window heater is the load, then the 12V from the car battery is the "bus voltage" and the voltage over the heater is the "load voltage".
The 12V is connected to VIN+ and the heater is connected to VIN-.

On the website of Texas Instruments I found references that the "load voltage" is supposed to be the power supply, the input voltage at VIN+
However, they also call that the "bus voltage".

The datasheet of the INA219 is confusing. They say that the bus voltage is measured at pin VIN-
That is not okay.

I even found the phrase "load voltage drop", where they meant "shunt voltage".

So now the confusion is complete.
I suggest to make new names, and add explanation every time the old names are used.

Note: I have been editing this issue after finding "load voltage" references on the Texas Instruments website.

Are the considerations for setCalibration_16V_400mA() correct?

Hi, thanks for your library!

First a minor issue: In Adafruit_INA219.cpp, in function setCalibration_16V_400mA(), you say you are using the highest precision 0.1 mA.

void Adafruit_INA219::setCalibration_16V_400mA() {
// Calibration which uses the highest precision for
// current measurement (0.1mA), at the expense of
// only supporting 16V at 400mA max.

But according to line 416 the current LSB you are using is 0.00005:

// CurrentLSB = 0.00005 (50uA per bit)

My main question is about the voltage limitation. In these lines you calculate the maximum power:

// 8. Compute the Maximum Power
// MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
// MaximumPower = 0.4 * 16V
// MaximumPower = 6.4W

Why is 16 V the limit? Here, some calculations using 400 mA / 32 V:

  • The LSB of the Bus Voltage Register is 4mV, so the Bus Voltage Register would be 8000 at 32 V.
  • Using an 0.1 ohm shunt the value in the shunt voltage register would be 4000 at 400 mA (= 40 mV shunt voltage)
  • The value in the Current Register would be : Shunt Voltage Register x Calibration Register / 4096 = 4000 * 8192 / 4096 = 8000.
  • With these values, the Bus Power Register would be: Current Register * Bus Voltage Register / 5000 = 8000 * 8000 / 5000 = 12800, which is 12.8 W.

So, I can't see any overflow or other issue and therefore I would say there is no limitation to 16 V in this mode. Or have I forgotten anything?

Providing for custom shunt resistors

I had to de-solder 0.1 Ohm resistor and put 0.04 in. Now I need to change the code of the library to make it work. As alternative could you change assess modifier like so?:

protected:
    Adafruit_I2CDevice *i2c_dev = NULL;
    uint8_t ina219_i2caddr = -1;
    uint32_t ina219_calValue;
    // The following multipliers are used to convert raw current and power
    // values to mA and mW, taking into account the current config settings
    uint32_t ina219_currentDivider_mA;
    float ina219_powerMultiplier_mW;

private:
    void init();
    int16_t getBusVoltage_raw();
    int16_t getShuntVoltage_raw();
    int16_t getCurrent_raw();
    int16_t getPower_raw();

Protected fields are accessible in child class and thus I could use it like so:

class MyINA219 : public Adafruit_INA219 {
  public:
  void setCalibration_32_5A() {
    
    ...
    
    ina219_calValue = 5012;
    ina219_currentDivider_mA = 20; 
    ina219_powerMultiplier_mW = 4;
   
    ...
    
  }
};

Note, that this allows to add custom calibration methods to accommodate any resistor values.
This change will allow for more flexibility.
I can add Pull Request should you accept this change.

Power measurement

Can this library be added to so that it can use the power measuring capability of the chip?

No "include guards" in Adafruit_INA219.h

Using this overall great class gave me the problem of multiple declaration (of this class).

The reason for this is in my opinion the missing include guards in the header file:
using lines like

#ifndef ADAFRUIT_INA219_H
#define ADAFRUIT_INA219_H
...
#endif

did solve the problem for me. In my opinion should such a include guard be added to the header file.

On quickly changing voltages and currents, the values are uncorrelated to each other

  • Arduino board: Feather M0 RFM69
  • Arduino IDE version: 1.8.5
  • Library version: 1.0.2

Issue:
Each measurement is made separately. In fast changing voltage and current situations, the reading of voltage is not synchronized with it's current measurement. Power calculation is difficult because of this.

Suggestion:
Allow a triggered measurement instead of continous. The user code would Trigger, Wait for the measurement, and Read the synchronized values.

The changes involved:

    • Modifying the setCalibration_* to pass a parameter
      OR
    • better, create a setMode command and remove the mode from setCalibration.
    • Create a command to trigger a measurement.
    • The user would implement the delay after triggering, (maybe return this delay with the calibration?)

Thanks!
Martin Boissonneault

Adafruit_INA219.cpp contains unrecognized characters

Hello,

I noticed that the Arduino IDE would issue the following warning when I opened my project containing Adafruit_INA219.cpp:

"Adafruit_INA219.cpp" contains unrecognized characters. If this code was created with an older version of Arduino, you may need to use Tools -> Fix Encoding & Reload to update the sketch to use UTF-8 encoding. If not, you may need to delete the bad characters to get rid of this warning.

The suggested fix did not make a difference (Arudino IDE 1.8.11), so I did some poking around. I wanted to let you know what I found, and then you can decide if you want to make a change or not.

First, I noticed the encoding of the two library files are different (this isn't strictly a problem):

$ file Adafruit_INA219.cpp Adafruit_INA219.h
Adafruit_INA219.cpp: c program text, UTF-8 Unicode text
Adafruit_INA219.h: C++ source text, ASCII text

Next I used iconv (MacOS 10.14.6, GNU libiconv 1.11) to convert Adafruit_INA219.cpp to a new ASCII file, discarding unrecognized character(s) in the process:

iconv -c -f UTF8 -t ASCII Adafruit_INA219.cpp > Adafruit_INA219.cpp.fixed

That let me diff the original and .fixed files to reveal the unrecognized characters. I think it's the character 'mu', used for for micro watts/micro amps. I found 5 instances of the character:

* unit of power corresponds to 800�W. Counter overflow occurs at

// MinimumLSB = 0.0000305 (30.5�A per bit)

// MaximumLSB = 0.000244 (244�A per bit)

// CurrentLSB = 0.0000400 (40�A per bit)

// PowerLSB = 0.0008 (800�W per bit)

There are lots of different types of 'mu' in UTF-8 (https://en.wikipedia.org/wiki/Mu_(letter)#Character_encodings). So in my case, I just replaced these instances with US-ASCII lower case 'u' instead, and Arduino stopped complaining after that:

iconv --unicode-subst=u -f UTF8 -t ASCII Adafruit_INA219.cpp > Adafruit_INA219.cpp.fixed && mv Adafruit_INA219.cpp.fixed Adafruit_INA219.cpp

Fortunately the unrecognized character was in comments only, so the code compiled fine, but that error message could throw off new users.

Thanks for the awesome breakout board and library!

Library properties need updating

  • Arduino board: n/a

  • Arduino IDE version: 1.8.5

  • Library reported version in Arduino IDE: 1.0.2

Hi!

Issue:
The changes made on Jan 17 2018 are not reflected in Arduino IDE.

Thanks!
Martin

Bad ina219_powerMultiplier_mW for Calibration_32V_1A

ina219_powerMultiplier_mW in .setCalibration_32V_1A() should be 0.8. Also it needs to be changed from uint32_t to float (or combination of multiplier/divider).

Also in the same line (255) in comments there is a mistake. Now it is 800mW per bit, it should be 800uW per bit (mW to uW).

busVoltage should be handled as unsigned int

Error: measured voltages over 16.3V are returned as negative value.
Problem: the i2c value is treaded as signed INT
Proposal: change it to unsigned INT as done below and the .h file accordingly.

Andre

uint16_t INA219::busVoltageRaw() {
_bus_voltage_register = read16(V_BUS_R);
_overflow = bitRead(_bus_voltage_register, OVF_B); // overflow bit
_ready = bitRead(_bus_voltage_register, CNVR_B); // ready bit
return _bus_voltage_register;
}

float INA219::busVoltage() {
uint16_t temp;
temp = busVoltageRaw();
temp >>= 3;
return (temp * 0.004);
}

Can I use INA219 I2C with UWP

Hi,
Can I use this sensor with UWP and Raspberry pi? I saw examples python and arduino but I don't figure out How Can I use with UWP? Do you have any suggestion?
Thank you. Best Regards.

Wire.setClock(highspeed) implementation unclear

Hi,
I'm working on a robotic experiment and want high speedy watt measurement rate. I'm using the Adafruit INA291 with no resistor modifications. I'd like to speed up the I2C to the fastest rate possible, which is 3.4MhZ from the datasheet (https://cdn-shop.adafruit.com/datasheets/ina219.pdf), or at least the fastest the Arduino Uno can handle. I see that this Adafruit_INA219 library uses the Wire package to set up the connection to the INA219, but setting Wire.setClock(3400000); does not appear to increase the rate. I get ~500s/s when printing to Serial when 1000 samples have been observed (via hand timing of 10000 samples).

How can I increase the I2C rate for the INA219 from Arduino? Is there a function/parameter I can call or pass?

  • Arduino board: Arduino Uno

  • Arduino IDE version 1.8.5

  • Use case: Adding watts over time for a variety of loads on a power supply. I will not be printing incrementally to Serial.

Thanks so much!

Brian

#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;
int counter;
float power_mW;
void setup(void) 
{
  pinMode(12, INPUT_PULLUP); 
  Serial.begin(115200);
  Wire.setClock(3400000);
  while (!Serial) {
      // will pause Zero, Leonardo, etc until serial console opens
      delay(1);
  }
  uint32_t currentFrequency;
  ina219.begin();
  power_mW = 0;
}

void loop(void) 
{
  if (counter % 1000 == 0){
    Serial.println(counter);
    }
  power_mW = ina219.getPower_mW();
  counter += 1;
}```

Small negative currents returned as large positive ones

The library returns strange readings when the current is flickering around zero, and it appears to be due to a minor bug in the Adafruit library. In the function:

uint16_t Adafruit_INA219::getCurrent_raw() {
  uint16_t value;
  wireReadRegister(INA219_REG_CURRENT, &value);
  return value;
}

it is treating the value in the current register as an unsigned int, but in fact it is a signed value. This means that small negative currents are being treated as large positive ones.

By changing the function to return a signed int:

int16_t Adafruit_INA219::getCurrent_raw() {
  uint16_t value;
  wireReadRegister(INA219_REG_CURRENT, &value);
  return (int16_t)value;
}

it can return positive and negative currents correctly.

The function getShuntVoltage_raw() looks like it suffers from the same problem, but that has not been tested.

Error in BADC register bits

I believe you have got an error in the following register bit defines:
#define INA219_CONFIG_BADCRES_9BIT (0x0080) // 9-bit bus res = 0..511
#define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023
#define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047

According to the datasheet, 9 bit resolution needs to be 0x0000. 10 bit should be 0x0080 and 11 bit should be 0x0100. The value you define for 11 bit (0x0200) actually selects 9 bit resolution.

Very low shuntvoltage

Hello,

Any idea what can be causing a very low shunt (and so, load) voltage? just the same as if I didn't connect anything to the shunt resistor.

I'm using this code to get the values:


//using setCalibration_32V_1A resolution in setup()

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  energy = energy +  loadvoltage * current_mA / 3600; 

I have a 4W 3.2Ohm resistance in the negative VIN, the current measurement seems to be correct, around 800mA for Lion battery testing. I am also using an arduino UNO for measurement.

Thanks in advance for any answer!

library.properties carries too much dependencies

Hi.

When used with platformIO, this library fetches many useless dependencies (Adafruit NeoPixel, Adafruit GFX Library, Adafruit SSD1306):

Dependency Graph
|-- <Adafruit BMP085 Library> 1.0.1
|   |-- <Wire> 1.0
|-- <Adafruit INA219> 1.0.7
|   |-- <Adafruit NeoPixel> 1.3.5
|   |-- <Adafruit GFX Library> 1.7.5
|   |   |-- <Adafruit ILI9341> 1.5.4
|   |   |   |-- <Adafruit STMPE610> 1.1.0
|   |   |   |   |-- <Wire> 1.0
|   |   |   |-- <Adafruit TouchScreen> 1.0.4
|   |-- <Adafruit SSD1306> 2.2.1
|   |   |-- <Adafruit GFX Library> 1.7.5
|   |   |   |-- <Adafruit ILI9341> 1.5.4
|   |   |   |   |-- <Adafruit STMPE610> 1.1.0
|   |   |   |   |   |-- <Wire> 1.0
|   |   |   |   |-- <Adafruit TouchScreen> 1.0.4
|   |   |-- <Wire> 1.0
|   |-- <Wire> 1.0

And thus provokes undesired errors at compile time:

Building in release mode
Compiling .pio/build/esp12e/src/main.cpp.o
Compiling .pio/build/esp12e/lib794/Adafruit STMPE610_ID377/Adafruit_STMPE610.cpp.o
Compiling .pio/build/esp12e/libd92/Adafruit TouchScreen_ID5430/TouchScreen.cpp.o
Compiling .pio/build/esp12e/lib223/Adafruit ILI9341_ID571/Adafruit_ILI9341.cpp.o
Compiling .pio/build/esp12e/libea9/Adafruit GFX Library_ID13/Adafruit_SPITFT.cpp.o
.pio/libdeps/esp12e/Adafruit STMPE610_ID377/Adafruit_STMPE610.cpp:31:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

 #include <SPI.h>
                 ^
compilation terminated.
Compiling .pio/build/esp12e/lib483/Adafruit SSD1306_ID135/Adafruit_SSD1306.cpp.o
In file included from .pio/libdeps/esp12e/Adafruit ILI9341_ID571/Adafruit_ILI9341.h:42:0,
                 from .pio/libdeps/esp12e/Adafruit ILI9341_ID571/Adafruit_ILI9341.cpp:49:
.pio/libdeps/esp12e/Adafruit GFX Library_ID13/Adafruit_SPITFT.h:26:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

 #include <SPI.h>
                 ^
compilation terminated.
Archiving .pio/build/esp12e/libb2f/libAdafruit INA219_ID160.a
*** [.pio/build/esp12e/lib794/Adafruit STMPE610_ID377/Adafruit_STMPE610.cpp.o] Error 1
Indexing .pio/build/esp12e/libb2f/libAdafruit INA219_ID160.a
*** [.pio/build/esp12e/lib223/Adafruit ILI9341_ID571/Adafruit_ILI9341.cpp.o] Error 1
Archiving .pio/build/esp12e/lib1ad/libPubSubClient_ID89.a
In file included from .pio/libdeps/esp12e/Adafruit GFX Library_ID13/Adafruit_SPITFT.cpp:36:0:
.pio/libdeps/esp12e/Adafruit GFX Library_ID13/Adafruit_SPITFT.h:26:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

 #include <SPI.h>
                 ^
compilation terminated.
*** [.pio/build/esp12e/libea9/Adafruit GFX Library_ID13/Adafruit_SPITFT.cpp.o] Error 1
Indexing .pio/build/esp12e/lib1ad/libPubSubClient_ID89.a
In file included from .pio/libdeps/esp12e/Adafruit SSD1306_ID135/Adafruit_SSD1306.cpp:52:0:
.pio/libdeps/esp12e/Adafruit SSD1306_ID135/Adafruit_SSD1306.h:40:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

 #include <SPI.h>
                 ^
compilation terminated.
*** [.pio/build/esp12e/lib483/Adafruit SSD1306_ID135/Adafruit_SSD1306.cpp.o] Error 1
In file included from .pio/libdeps/esp12e/Adafruit TouchScreen_ID5430/TouchScreen.cpp:14:0:
.pio/libdeps/esp12e/Adafruit TouchScreen_ID5430/TouchScreen.h:51:12: error: 'RwReg' does not name a type
   volatile RwReg *xp_port, *yp_port, *xm_port, *ym_port;
            ^
.pio/libdeps/esp12e/Adafruit TouchScreen_ID5430/TouchScreen.h:52:3: error: 'RwReg' does not name a type
   RwReg xp_pin, xm_pin, yp_pin, ym_pin;
   ^
*** [.pio/build/esp12e/libd92/Adafruit TouchScreen_ID5430/TouchScreen.cpp.o] Error 1
============================================================================== [FAILED] Took 2.26 seconds ==============================================================================

Environment    Status    Duration
-------------  --------  ------------
esp12e         FAILED    00:00:02.263
esp01          IGNORED
esp32dev       IGNORED
======================================================================== 1 failed, 0 succeeded in 00:00:02.263 ========================================================================
The terminal process terminated with exit code: 1

Incorrect values from getShuntVoltage_Raw() and getCurrent_raw() when bidirectional current flow is measured.

  • Arduino board: Custom ESP32 board

  • Arduino IDE version (found in Arduino -> About Arduino menu): Usin Platform IO with Arduino 1.813

  • The problem lies in the functions getShuntVoltage_raw() and getCurrent_raw() both of which return an unsigned integer from the BusIO_Register read() function. and return this value from a function intended to return a signed integer but do not deal with the flags CNVR and OVF so give some very odd readings when used bidirectionally as I am doing monitoring the current flowing from and into the battery in a circuit that includes a charging circuit as well as using the device away from the charger.

  • The function getBusVoltage_raw() does not have this problem as the return value there is

  • // Shift to the right 3 to drop CNVR and OVF and multiply by LSB
    return (int16_t)((value >> 3) * 4);

This works correctly and if used in the other two functions provides correct values for current flow in either direction.

Error in included example

  • Arduino board: Nano

  • Arduino IDE version 1.8.5

  • Load the only example code "getcurrent" and compile. It will fail with the message:
    exit status 1 expected initializer before 'float'

Code will not compile as is because line 22 is incomplete. It reads:

float power_mW

Should be:

float power_mW = 0;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.