adafruit / adafruit_ina219 Goto Github PK
View Code? Open in Web Editor NEWINA219 Current Sensor
License: Other
INA219 Current Sensor
License: Other
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.
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;
}```
Would be nice to have it ported by someone capable... :)
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.
Adafruit_INA219/Adafruit_INA219.cpp
Lines 391 to 395 in ce59837
But according to line 416 the current LSB you are using is 0.00005:
Adafruit_INA219/Adafruit_INA219.cpp
Line 416 in ce59837
My main question is about the voltage limitation. In these lines you calculate the maximum power:
Adafruit_INA219/Adafruit_INA219.cpp
Lines 454 to 457 in ce59837
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?
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: 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 this library be added to so that it can use the power measuring capability of the chip?
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.
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;
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:
Adafruit_INA219/Adafruit_INA219.cpp
Line 174 in f6f7d9f
Adafruit_INA219/Adafruit_INA219.cpp
Line 199 in f6f7d9f
Adafruit_INA219/Adafruit_INA219.cpp
Line 201 in f6f7d9f
Adafruit_INA219/Adafruit_INA219.cpp
Line 205 in f6f7d9f
Adafruit_INA219/Adafruit_INA219.cpp
Line 215 in f6f7d9f
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!
What would it take to port this over to to work in the Johnny Five environment? (https://github.com/rwaldron/johnny-five) (http://johnny-five.io/)
spooky
line 212 should be
uint16_t data, ///< Data to be writen.
?
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
).
Currently only 16V max bus voltage is settable because of the typo contrary to the name of the default config
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
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
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.
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.
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.
Can you add PowerSaving ?
https://forums.adafruit.com/viewtopic.php?f=8&t=104262#p524875
This will save about 0.7mA per INA chip. a serious amount.
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!
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.
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:
Thanks!
Martin Boissonneault
library imports Adafruit_BusIO_Register.h that seems no where to be found?!
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()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.