Update gitignore

This commit is contained in:
Carsten Schmiemann 2018-08-29 00:24:09 +02:00
parent f4a22880fe
commit 0748877113
255 changed files with 26668 additions and 2 deletions

2
.gitignore vendored
View File

@ -1,5 +1,3 @@
.pioenvs
.piolibdeps
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json

BIN
.pioenvs/.sconsign.dblite Normal file

Binary file not shown.

View File

@ -0,0 +1,3 @@
[InternetShortcut]
URL=https://docs.platformio.org/page/projectconf/section_platformio.html#build-dir

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
!<arch>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1 @@
!<arch>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 MiB

Binary file not shown.

1
.pioenvs/structure.hash Normal file
View File

@ -0,0 +1 @@
ed29a20d4d5440e7bc8a8339197df0ad37810755

View File

@ -0,0 +1 @@
Thumbs.db

View File

@ -0,0 +1,64 @@
{
"description": "INA219 hi-side i2c current/power sensor Library",
"repository": {
"url": "https://github.com/flav1972/ArduinoINA219",
"type": "git"
},
"platforms": [
"atmelavr",
"atmelsam",
"espressif8266",
"intel_arc32",
"microchippic32",
"nordicnrf51",
"teensy",
"timsp430"
],
"export": {
"exclude": [
"extras",
"docs",
"tests",
"test",
"*.doxyfile",
"*.pdf"
],
"include": null
},
"authors": [
{
"maintainer": false,
"name": "DeCristofaro John",
"url": null,
"email": "johngineer@yahoo.com"
},
{
"maintainer": false,
"name": "Jukka-Pekka Sarjanen",
"url": null,
"email": "jukka-Pekka.sarjanen@kone.com"
},
{
"maintainer": false,
"name": "gandy92",
"url": null,
"email": "gandy92@googlemail.com"
},
{
"maintainer": true,
"name": "Flavius Bindea",
"url": null,
"email": "flav@flav.com"
}
],
"keywords": [
"sensors"
],
"id": 411,
"name": "ArduinoINA219",
"frameworks": [
"arduino"
],
"version": "1.0.1",
"homepage": null
}

View File

@ -0,0 +1,298 @@
/******************************************************************************
* TI INA219 hi-side i2c current/power monitor Library
*
* http://www.ti.com/product/ina219
*
* 6 May 2012 by John De Cristofaro
*
*
* Tested at standard i2c 100kbps signaling rate.
*
* This library does not handle triggered conversion modes. It uses the INA219
* in continuous conversion mode. All reads are from continous conversions.
*
* A note about the gain (PGA) setting:
* The gain of the ADC pre-amplifier is programmable in the INA219, and can
* be set between 1/8x (default) and unity. This allows a shunt voltage
* range of +/-320mV to +/-40mV respectively. Something to keep in mind,
* however, is that this change in gain DOES NOT affect the resolution
* of the ADC, which is fixed at 1uV. What it does do is increase noise
* immunity by exploiting the integrative nature of the delta-sigma ADC.
* For the best possible reading, you should set the gain to the range
* of voltages that you expect to see in your particular circuit. See
* page 15 in the datasheet for more info about the PGA.
*
* Known bugs:
* * may return unreliable values if not connected to a bus or at
* bus currents below 10uA.
*
* Arduino 1.0 compatible as of 6/6/2012
*
* Dependencies:
* * Arduino Wire library
*
* MIT license
******************************************************************************/
#include "INA219.h"
#define _delay_ms(ms) delayMicroseconds((ms) * 1000)
namespace{
// config. register bit labels
const uint8_t RST = 15;
const uint8_t BRNG = 13;
const uint8_t PG1 = 12;
const uint8_t PG0 = 11;
const uint8_t BADC4 = 10;
const uint8_t BADC3 = 9;
const uint8_t BADC2 = 8;
const uint8_t BADC1 = 7;
const uint8_t SADC4 = 6;
const uint8_t SADC3 = 5;
const uint8_t SADC2 = 4;
const uint8_t SADC1 = 3;
const uint8_t MODE3 = 2;
const uint8_t MODE2 = 1;
const uint8_t MODE1 = 0;
};
#define CNVR_B 1 // conversion ready bit in bus voltage register V_BUS_R
#define OVF_B 0 // math overflow bit in bus voltage register V_BUS_R
INA219::INA219(t_i2caddr addr): i2c_address(addr) {
}
void INA219::begin() {
Wire.begin();
configure();
calibrate();
}
void INA219::calibrate(float shunt_val, float v_shunt_max, float v_bus_max, float i_max_expected) {
uint16_t digits;
float i_max_possible, min_lsb, max_lsb, swap;
#if (INA219_DEBUG == 1)
float max_current,max_before_overflow,max_shunt_v,max_shunt_v_before_overflow,max_power;
#endif
r_shunt = shunt_val;
i_max_possible = v_shunt_max / r_shunt;
min_lsb = i_max_expected / 32767;
max_lsb = i_max_expected / 4096;
current_lsb = min_lsb;
digits=0;
/* From datasheet: This value was selected to be a round number near the Minimum_LSB.
* This selection allows for good resolution with a rounded LSB.
* eg. 0.000610 -> 0.000700
*/
while( current_lsb > 0.0 ){//If zero there is something weird...
if( (uint16_t)current_lsb / 1){
current_lsb = (uint16_t) current_lsb + 1;
current_lsb /= pow(10,digits);
break;
}
else{
digits++;
current_lsb *= 10.0;
}
};
swap = (0.04096)/(current_lsb*r_shunt);
cal = (uint16_t)swap;
power_lsb = current_lsb * 20;
#if (INA219_DEBUG == 1)
max_current = current_lsb*32767;
max_before_overflow = max_current > i_max_possible?i_max_possible:max_current;
max_shunt_v = max_before_overflow*r_shunt;
max_shunt_v_before_overflow = max_shunt_v > v_shunt_max?v_shunt_max:max_shunt_v;
max_power = v_bus_max * max_before_overflow;
Serial.print("v_bus_max: "); Serial.println(v_bus_max, 8);
Serial.print("v_shunt_max: "); Serial.println(v_shunt_max, 8);
Serial.print("i_max_possible: "); Serial.println(i_max_possible, 8);
Serial.print("i_max_expected: "); Serial.println(i_max_expected, 8);
Serial.print("min_lsb: "); Serial.println(min_lsb, 12);
Serial.print("max_lsb: "); Serial.println(max_lsb, 12);
Serial.print("current_lsb: "); Serial.println(current_lsb, 12);
Serial.print("power_lsb: "); Serial.println(power_lsb, 8);
Serial.println(" ");
Serial.print("cal: "); Serial.println(cal);
Serial.print("r_shunt: "); Serial.println(r_shunt, 6);
Serial.print("max_before_overflow: "); Serial.println(max_before_overflow,8);
Serial.print("max_shunt_v_before_overflow: "); Serial.println(max_shunt_v_before_overflow,8);
Serial.print("max_power: "); Serial.println(max_power,8);
Serial.println(" ");
#endif
write16(CAL_R, cal);
}
void INA219::configure( t_range range, t_gain gain, t_adc bus_adc, t_adc shunt_adc, t_mode mode) {
config = 0;
config |= (range << BRNG | gain << PG0 | bus_adc << BADC1 | shunt_adc << SADC1 | mode);
#if (INA219_DEBUG == 1)
Serial.print("Config: 0x"); Serial.println(config,HEX);
#endif
write16(CONFIG_R, config);
}
#define INA_RESET 0xFFFF // send to CONFIG_R to reset unit
void INA219::reset(){
write16(CONFIG_R, INA_RESET);
_delay_ms(5);
}
int16_t INA219::shuntVoltageRaw() const {
return read16(V_SHUNT_R);
}
float INA219::shuntVoltage() const {
float temp;
temp = read16(V_SHUNT_R);
return (temp / 100000);
}
int16_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() {
int16_t temp;
temp = busVoltageRaw();
temp >>= 3;
return (temp * 0.004);
}
int16_t INA219::shuntCurrentRaw() const {
return (read16(I_SHUNT_R));
}
float INA219::shuntCurrent() const {
return (read16(I_SHUNT_R) * current_lsb);
}
float INA219::busPower() const {
return (read16(P_BUS_R) * power_lsb);
}
/**************************************************************************/
/*!
@brief Rewrites the last config register
*/
/**************************************************************************/
void INA219::reconfig() const {
#if (INA219_DEBUG == 1)
Serial.print("Reconfigure with Config: 0x"); Serial.println(config,HEX);
#endif
write16(CONFIG_R, config);
}
/**************************************************************************/
/*!
@brief Rewrites the last calibration
*/
/**************************************************************************/
void INA219::recalibrate() const {
#if (INA219_DEBUG == 1)
Serial.print("Recalibrate with cal: "); Serial.println(cal);
#endif
write16(CAL_R, cal);
}
/**************************************************************************/
/*!
@brief returns conversion ready bite from last bus voltage read
@note page 30:
Although the data from the last conversion can be read at any time,
the INA219 Conversion Ready bit (CNVR) indicates when data from
a conversion is available in the data output registers.
The CNVR bit is set after all conversions, averaging,
and multiplications are complete.
CNVR will clear under the following conditions:
1.) Writing a new mode into the Operating Mode bits in the
Configuration Register (except for Power-Down or Disable)
2.) Reading the Power Register
page 15:
The Conversion Ready bit clears under these
conditions:
1. Writing to the Configuration Register, except
when configuring the MODE bits for Power Down
or ADC off (Disable) modes;
2. Reading the Status Register;
3. Triggering a single-shot conversion with the
Convert pin.
*/
/**************************************************************************/
bool INA219::ready() const {
return _ready;
}
/**************************************************************************/
/*!
@brief returns overflow bite from last bus voltage read
@note The Math Overflow Flag (OVF) is set when the Power or
Current calculations are out of range. It indicates that
current and power data may be meaningless.
*/
/**************************************************************************/
bool INA219::overflow() const {
return _overflow;
}
/**********************************************************************
* INTERNAL I2C FUNCTIONS *
**********************************************************************/
void INA219::write16(t_reg a, uint16_t d) const {
uint8_t temp;
temp = (uint8_t)d;
d >>= 8;
Wire.beginTransmission(i2c_address); // start transmission to device
#if ARDUINO >= 100
Wire.write(a); // sends register address to read from
Wire.write((uint8_t)d); // write data hibyte
Wire.write(temp); // write data lobyte;
#else
Wire.send(a); // sends register address to read from
Wire.send((uint8_t)d); // write data hibyte
Wire.send(temp); // write data lobyte;
#endif
Wire.endTransmission(); // end transmission
delay(1);
}
int16_t INA219::read16(t_reg a) const {
uint16_t ret;
// move the pointer to reg. of interest, null argument
write16(a, 0);
Wire.requestFrom((int)i2c_address, 2); // request 2 data bytes
#if ARDUINO >= 100
ret = Wire.read(); // rx hi byte
ret <<= 8;
ret |= Wire.read(); // rx lo byte
#else
ret = Wire.receive(); // rx hi byte
ret <<= 8;
ret |= Wire.receive(); // rx lo byte
#endif
Wire.endTransmission(); // end transmission
return ret;
}

View File

@ -0,0 +1,215 @@
/******************************************************************************
* TI INA219 hi-side i2c current/power monitor Library
*
* http://www.ti.com/product/ina219
*
* 6 May 2012 by John De Cristofaro
*
*
* Tested at standard i2c 100kbps signaling rate.
*
* This library does not handle triggered conversion modes. It uses the INA219
* in continuous conversion mode. All reads are from continous conversions.
*
* A note about the gain (PGA) setting:
* The gain of the ADC pre-amplifier is programmable in the INA219, and can
* be set between 1/8x (default) and unity. This allows a shunt voltage
* range of +/-320mV to +/-40mV respectively. Something to keep in mind,
* however, is that this change in gain DOES NOT affect the resolution
* of the ADC, which is fixed at 1uV. What it does do is increase noise
* immunity by exploiting the integrative nature of the delta-sigma ADC.
* For the best possible reading, you should set the gain to the range
* of voltages that you expect to see in your particular circuit. See
* page 15 in the datasheet for more info about the PGA.
*
* Known bugs:
* * may return unreliable values if not connected to a bus or at
* bus currents below 10uA.
*
* Arduino 1.0 compatible as of 6/6/2012
*
* Dependencies:
* * Arduino Wire library
*
* MIT license
******************************************************************************/
#ifndef ina219_h
#define ina219_h
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#define INA219_DEBUG 0
// default values
#define D_SHUNT 0.1
#define D_V_BUS_MAX 32
#define D_V_SHUNT_MAX 0.2
#define D_I_MAX_EXPECTED 2
class INA219
{
public:
///I2C address definations.
/// Adafruit breakout board have two jumppers to set unique
/// address for each board ( in case there is more than one in Your system)
/// base address is 0x40, no jumpers needed)
enum t_i2caddr{
I2C_ADDR_40 = 0x40, ///< address 0x40 no jumpers required.
I2C_ADDR_41 = 0x41, ///< address 0x41 bridge A0.
I2C_ADDR_44 = 0x44, ///< address 0x44 bridge A1.
I2C_ADDR_45 = 0x45 ///< address 0x45 bridge A0 & A1.
};
///Sets PGA gain and range. Note that the PGA defaults to ÷8 (320mV range).
///Configuration reister bits 11, 12 are used for this.
enum t_gain{
GAIN_1_40MV = 0,
GAIN_2_80MV = 1,
GAIN_4_160MV = 2,
GAIN_8_320MV = 3
};
///Bus voltage range.
///Configuration register bit 13 defines this.
enum t_range{
RANGE_16V = 0, ///< Range 0-16 volts
RANGE_32V = 1 ///< Range 0-32 volts
};
///ADC resolution (9-, 10-, 11-, or 12-bit) or set the number of samples.
///Same definations are used both shunt and bus adc.
///Configuration register bits 36 are used for averaging results for the Shunt Voltage Register (01h).
///And bits 7-10 are used averaging results for the Bus Voltage Register (02h).
/// \see t_reg
enum t_adc{
ADC_9BIT = 0, ///< 9bit converion time 84us.
ADC_10BIT = 1, ///< 10bit converion time 148us.
ADC_11BIT = 2, ///< 11bit converion time 2766us.
ADC_12BIT = 3, ///< 12bit converion time 532us.
ADC_2SAMP = 9, ///< 2 samples converion time 1.06ms.
ADC_4SAMP = 10, ///< 4 samples converion time 2.13ms.
ADC_8SAMP = 11, ///< 8 samples converion time 4.26ms.
ADC_16SAMP = 12, ///< 16 samples converion time 8.51ms
ADC_32SAMP = 13, ///< 32 samples converion time 17.02ms.
ADC_64SAMP = 14, ///< 64 samples converion time 34.05ms.
ADC_128SAMP = 15, ///< 128 samples converion time 68.10ms.
};
///Selects continuous, triggered, or power-down mode of operation. These bits default to continuous shunt and bus measurement mode.
///\n Please note: <b>Thus library does not support triggered converion I have comment out values to set trigged mode</b>.\n
///Configuration register bits 0-2 are used for this. \see t_reg
enum t_mode{
PWR_DOWN = 0,
/*
TRIG_SH = 1,
TRIG_BUS = 2,
TRIG_SH_BUS = 3,
*/
ADC_OFF = 4,
CONT_SH = 5, ///<Shunt Continuous.
CONT_BUS = 6, ///<Bus Continuous.
CONT_SH_BUS = 7 ///<Shunt and Bus, Continuous.
};
///Constructor
INA219( t_i2caddr addr = I2C_ADDR_40 ///< Device address.
);
/// Start i2 communication with actual device.
void begin( );
/// calibration of equations and device.
/// default values are for a 0.25 Ohm shunt on a 5V bus with max current of 1A
void calibrate( float r_shunt = D_SHUNT, ///< Value of shunt in Ohms.
float v_shunt_max = D_V_SHUNT_MAX, ///< Maximum value of voltage across shunt.
float v_bus_max = D_V_BUS_MAX, ///< Maximum voltage of bus.
float i_max_expected = D_I_MAX_EXPECTED ///< Maximum current draw of bus + shunt.
);
/// Config values (range, gain, bus adc, shunt adc, mode).
void configure( t_range range = RANGE_32V, ///< Range for bus voltage.
t_gain gain = GAIN_8_320MV,///< Set Gain for shunt voltage.
t_adc bus_adc = ADC_12BIT, ///< Configure bus voltage conversion.
t_adc shunt_adc = ADC_12BIT, ///< Configure shun voltage conversion.
t_mode mode = CONT_SH_BUS ///< Sets operation mode.
);
/// Resets the INA219.
void reset();
/// Returns the raw binary value of the shunt voltage
int16_t shuntVoltageRaw() const;
/// Returns raw bus voltage binary value.
int16_t busVoltageRaw();
/// Returns raw current binary value.
int16_t shuntCurrentRaw() const;
/// Returns the shunt voltage in volts.
float shuntVoltage() const;
/// Returns the bus voltage in volts.
float busVoltage();
/// Returns the shunt current in amps.
float shuntCurrent() const;
/// Returns the bus power in watts.
float busPower() const;
/// Rewrites last config value to INA219 register
void reconfig() const;
/// Rewrites last calibration value to INA219 register
void recalibrate() const;
/// conversion is ready
bool ready() const;
/// conversion is ready
bool overflow() const;
private:
/// INA219 memory registers.
enum t_reg{
CONFIG_R = 0x00, ///< configuration register.
V_SHUNT_R = 0x01, ///< differential shunt voltage.
V_BUS_R = 0x02, ///< bus voltage (wrt to system/chip GND).
P_BUS_R = 0x03, ///< system power draw (= V_BUS * I_SHUNT).
I_SHUNT_R = 0x04, ///< shunt current.
CAL_R = 0x05 ///< calibration register.
};
const uint8_t i2c_address;
float r_shunt, current_lsb, power_lsb;
uint16_t config, cal;
bool _ready, _overflow;
uint16_t _bus_voltage_register;
///Read 16 word from given register address.
int16_t read16( t_reg addr ///< Register address.
) const;
/// Writes a 16-bit word (d) to register pointer (a).
/// When selecting a register pointer to read from, (d) = 0
void write16( t_reg addr, ///< Register address.
uint16_t data ///< Data to be writen.
) const;
};
#endif

View File

@ -0,0 +1,181 @@
TI INA219 hi-side i2c current/power monitor Library for Arduino
===============================================================
# Authors
## John De Cristofaro
* Initial releases
## Jukka-Pekka Sarjanen
* Quite much small changes to Johns's original lib mostly documentation and
C++ "mambo jambo" Doxy documentation etc.
* One major change / fix:
calibrate() function fixed to give correct ratings.
## gandy92
* Fix compiling issue due to case sensitivity on linux systems
## Flavius Bindea
* More examples
* added shuntCurrentRaw()
* changed begin() and default values for ADAFRUIT INA219 breakout
* Added rewrite modes, read conversion ready, overflow functions
* Fixed bug in calibrate() in order to use class variable cal
# Tests and fonctionalities
* Tested at standard i2c 100kbps signaling rate.
* This library does not handle triggered conversion modes. It uses the INA219
in continuous conversion mode. All reads are from continous conversions.
* A note about the gain (PGA) setting:
The gain of the ADC pre-amplifier is programmable in the INA219, and can
be set between 1/8x (default) and unity. This allows a shunt voltage
range of +/-320mV to +/-40mV respectively. Something to keep in mind,
however, is that this change in gain DOES NOT affect the resolution
of the ADC, which is fixed at 1uV. What it does do is increase noise
immunity by exploiting the integrative nature of the delta-sigma ADC.
For the best possible reading, you should set the gain to the range
of voltages that you expect to see in your particular circuit. See
page 15 in the datasheet for more info about the PGA.
# Known bugs:
* may return unreliable values if not connected to a bus or at bus currents below 10uA.
# Dependencies:
* Arduino Wire library
# Usage
## Basic
Look at [ina219_test](example/ina219_test/ina219_test.ino) exemple sketch.
This example works out of the box for Adafruit's INA219 Breakout.
Include defintions and define needed object:
```
#include <Wire.h>
#include <INA219.h>
INA219 monitor;
```
In the `setup()` function initialise the INA219:
```
monitor.begin();
```
Then in the `loop()` function make calls to different functions that are returning the values:
```
Serial.print("raw shunt voltage: ");
Serial.println(monitor.shuntVoltageRaw());
Serial.print("raw bus voltage: ");
Serial.println(monitor.busVoltageRaw());
Serial.println("--");
Serial.print("shunt voltage: ");
Serial.print(monitor.shuntVoltage() * 1000, 4);
Serial.println(" mV");
Serial.print("shunt current: ");
Serial.print(monitor.shuntCurrent() * 1000, 4);
Serial.println(" mA");
Serial.print("bus voltage: ");
Serial.print(monitor.busVoltage(), 4);
Serial.println(" V");
Serial.print("bus power: ");
Serial.print(monitor.busPower() * 1000, 4);
Serial.println(" mW");
```
## Enhanced setup
If you want to use a different setup or if you do not use the Adafruit's breakout then in the `setup()` function you need to call `configure()` and `calibrate()`.
An exemple is in [ina219_test_nondefault](examples/ina219_test_nondefault/ina219_test_nondefault.ino).
Extract of the `setup()` function:
```
monitor.begin();
// setting up our configuration
monitor.configure(INA219::RANGE_16V, INA219::GAIN_2_80MV, INA219::ADC_64SAMP, INA219::ADC_64SAMP, INA219::CONT_SH_BUS);
// calibrate with our values
monitor.calibrate(SHUNT_R, SHUNT_MAX_V, BUS_MAX_V, MAX_CURRENT);
```
## functions
All the functions are well comented in [INA219.h](INA219.h) and [INA219.cpp](INA219.cpp)
* `begin()`
starts the communication with the INA219 and does the default setup.
* `configure()`
setups the INA219 mode.
The args are: `(range, gain, bus_adc, shunt_adc, mode)`
* range : Range for bus voltage
* RANGE_16V : Range 0-16 volts
* RANGE_32V (default): Range 0-32 volts
* gain : Set Gain for shunt voltage (choose the lowest possible depending on your hardware)
* GAIN_1_40MV : 40mV
* GAIN_2_80MV : 80mV
* GAIN_4_160MV : 160mV
* GAIN_8_320MV (default): 320mV
* bus_adc : Configure bus voltage conversion
* ADC_9BIT : 9bit, converion time 84us.
* ADC_10BIT : 10bit, converion time 148us.
* ADC_11BIT : 11bit, converion time 2766us.
* ADC_12BIT (default): 12bit converion time 532us.
* ADC_2SAMP : 2 samples converion time 1.06ms.
* ADC_4SAMP : 4 samples converion time 2.13ms.
* ADC_8SAMP : 8 samples converion time 4.26ms.
* ADC_16SAMP : 16 samples converion time 8.51ms
* ADC_32SAMP : 32 samples converion time 17.02ms.
* ADC_64SAMP : 64 samples converion time 34.05ms.
* ADC_128SAMP : 128 samples converion time 68.10ms.
* shunt_adc: Configure shun voltage conversion. Same values as for bus_adc
* mode: Sets operation mode.
* PWR_DOWN : Power Down
* ADC_OFF
* CONT_SH : Shunt Continuous
* CONT_BUS : Bus Continuous
* CONT_SH_BUS (default): Shunt and Bus, Continuous.
* `calibrate(r_shunt, v_shunt_max, v_bus_max, i_max_expected)` function is doing the calculations as described in the INA219 datasheet and sets up the calibration registers.
The args are:
* r_shunt : Value of shunt in Ohms.
* v_shunt_max : Maximum value of voltage across shunt.
* v_bus_max : Maximum voltage of bus.
* i_max_expected : Maximum current draw of bus + shunt.
* `reconfig()` rewrites the last used config to the INA219.
* `recalibrate()` rewrites the calibration registers as they were calculated by `calibrate()` function.
* `ready()` returns the value of the ready bit. It's value is updated according to the last call to `busVoltageRaw()` or `busVoltage()`.
based on the datasheet page 30:
Although the data from the last conversion can be read at any time,
the INA219 Conversion Ready bit (CNVR) indicates when data from
a conversion is available in the data output registers.
The CNVR bit is set after all conversions, averaging,
and multiplications are complete.
CNVR will clear under the following conditions:
1.) Writing a new mode into the Operating Mode bits in the
Configuration Register (except for Power-Down or Disable)
2.) Reading the Power Register
page 15:
The Conversion Ready bit clears under these
conditions:
1. Writing to the Configuration Register, except
when configuring the MODE bits for Power Down
or ADC off (Disable) modes;
2. Reading the Status Register;
3. Triggering a single-shot conversion with the
Convert pin.
* `overflow()` returns the value of the overflow bit. It's value is updated according to the last call to `busVoltageRaw()` or `busVoltage()`.
* `reset()` : Resets the INA219.
* `shuntVoltageRaw()` : Returns the raw binary value of the shunt voltage
* `busVoltageRaw()` : Returns raw bus voltage binary value.
* `shuntCurrentRaw()` : Returns raw bus voltage binary value.
* `shuntVoltage()` : Returns the shunt voltage in volts.
* `busVoltage()` : Returns the bus voltage in volts.
* `shuntCurrent()` : Returns the shunt current in amps.
* `busPower()` : Returns the bus power in watts
# Licence
MIT license

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View File

@ -0,0 +1,123 @@
/*
* Baterry Monitor
* by Flavius Bindea
* this sketch uses averaging and check of ready status
*/
#include <Wire.h>
#include <INA219.h>
// Current sensor and shunt used
INA219 ina219;
#define R_SHUNT 0.00375
#define V_SHUNT_MAX 0.075
#define V_BUS_MAX 16
#define I_MAX_EXPECTED 20
boolean led=true;
// current and voltage readings
float shuntvoltage = 0;
float busvoltage = 0;
float current_A = 0;
float batvoltage = 0;
float power = 0;
float Ah = 0;
unsigned long lastread = 0; // used to calculate Ah
unsigned long tick; // current read time - last read
// different intervals for each Task
int intervalReadData = 50;
int intervalDisplay = 1000;
// last taks call
unsigned long previousMillisReadData = 0;
unsigned long previousMillisDisplay = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
Serial.println("Hello - Arduino_INA219 !");
ina219.begin();
// configure INA219 for averaging at 16 samples (8.51ms)
ina219.configure(INA219::RANGE_16V, INA219::GAIN_2_80MV, INA219::ADC_16SAMP, INA219::ADC_16SAMP, INA219::CONT_SH_BUS);
// configure INA219 for averaging at 128 samples
//ina219.configure(INA219::RANGE_16V, INA219::GAIN_2_80MV, INA219::ADC_128SAMP, INA219::ADC_128SAMP, INA219::CONT_SH_BUS);
lastread = millis();
// calibrate INA219 with out shunt values
ina219.calibrate(R_SHUNT, V_SHUNT_MAX, V_BUS_MAX, I_MAX_EXPECTED);
// for led blinking
pinMode(13, OUTPUT);
/*
// use timer 1 to launch current reading
// this is a test only and not sure it works
Timer1.initialize(READFREQ); // 100ms reading interval
Timer1.attachInterrupt(readCurrent);
*/
delay(1000);
}
void loop() {
// get current time stamp
// only need one for both if-statements
unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillisReadData) >= intervalReadData) {
previousMillisReadData = millis();
readCurrent();
Serial.print("tick: "); Serial.print(tick); Serial.println(" ms");
}
if ((unsigned long)(currentMillis - previousMillisDisplay) >= intervalDisplay) {
previousMillisDisplay = millis();
// displays data
Serial.print("Bus Voltage: "); Serial.print(busvoltage,3); Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage,3); Serial.println(" mV");
Serial.print("Bat Voltage: "); Serial.print(batvoltage,3); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_A,3); Serial.println(" A");
Serial.print("Power: "); Serial.print(power,3); Serial.println(" W");
Serial.print("Ah: "); Serial.print(Ah,3); Serial.println(" Ah");
Serial.println("");
}
// blink led
digitalWrite(13, led);
led = !led;
delay(10);
}
void readCurrent() {
uint32_t count = 0;
unsigned long newtime;
// Serial.println("waiting data ready");
// reads busVoltage
busvoltage = ina219.busVoltage();
// waits for conversion ready
while(!ina219.ready() && count < 500) {
count++;
delay(1);
busvoltage = ina219.busVoltage();
}
// Serial.print("Count: "); Serial.println(count);
// read the other values
shuntvoltage = ina219.shuntVoltage() * 1000;
current_A = ina219.shuntCurrent();
batvoltage = busvoltage + (shuntvoltage / 1000);
power = ina219.busPower();
newtime = millis();
tick = newtime - lastread;
Ah += (current_A * tick)/3600000.0;
lastread = newtime;
// prepare for next read -- this is security just in case the ina219 is reset by transient curent
ina219.recalibrate();
ina219.reconfig();
}

View File

@ -0,0 +1,63 @@
/**********************************************
* INA219 library example
* 10 May 2012 by johngineer
*
* 9 January 2016 Flavius Bindea: changed default values and begin()
*
* this code is public domain.
**********************************************/
#include <Wire.h>
#include <INA219.h>
INA219 monitor;
void setup()
{
Serial.begin(9600);
monitor.begin();
// begin calls:
// configure() with default values RANGE_32V, GAIN_8_320MV, ADC_12BIT, ADC_12BIT, CONT_SH_BUS
// calibrate() with default values D_SHUNT=0.1, D_V_BUS_MAX=32, D_V_SHUNT_MAX=0.2, D_I_MAX_EXPECTED=2
// in order to work directly with ADAFruit's INA219B breakout
}
void loop()
{
Serial.println("******************");
Serial.print("raw shunt voltage: ");
Serial.println(monitor.shuntVoltageRaw());
Serial.print("raw bus voltage: ");
Serial.println(monitor.busVoltageRaw());
Serial.println("--");
Serial.print("shunt voltage: ");
Serial.print(monitor.shuntVoltage() * 1000, 4);
Serial.println(" mV");
Serial.print("shunt current: ");
Serial.print(monitor.shuntCurrent() * 1000, 4);
Serial.println(" mA");
Serial.print("bus voltage: ");
Serial.print(monitor.busVoltage(), 4);
Serial.println(" V");
Serial.print("bus power: ");
Serial.print(monitor.busPower() * 1000, 4);
Serial.println(" mW");
Serial.println(" ");
Serial.println(" ");
delay(10000);
}

View File

@ -0,0 +1,66 @@
/**********************************************
* INA219 library example
* 9 January 2016 by Flavius Bindea
*
* this code is public domain.
**********************************************/
#include <Wire.h>
#include <INA219.h>
#define SHUNT_MAX_V 0.04 /* Rated max for our shunt is 75mv for 50 A current:
we will mesure only up to 20A so max is about 75mV*20/50 lets put some more*/
#define BUS_MAX_V 16.0 /* with 12v lead acid battery this should be enough*/
#define MAX_CURRENT 20 /* In our case this is enaugh even shunt is capable to 50 A*/
#define SHUNT_R 0.015 /* Shunt resistor in ohm */
INA219 monitor;
void setup()
{
Serial.begin(57600);
monitor.begin();
// setting up our configuration
// default values are RANGE_32V, GAIN_8_320MV, ADC_12BIT, ADC_12BIT, CONT_SH_BUS
monitor.configure(INA219::RANGE_16V, INA219::GAIN_2_80MV, INA219::ADC_64SAMP, INA219::ADC_64SAMP, INA219::CONT_SH_BUS);
// calibrate with our values
monitor.calibrate(SHUNT_R, SHUNT_MAX_V, BUS_MAX_V, MAX_CURRENT);
}
void loop()
{
Serial.println("******************");
Serial.print("raw shunt voltage: ");
Serial.println(monitor.shuntVoltageRaw());
Serial.print("raw bus voltage: ");
Serial.println(monitor.busVoltageRaw());
Serial.println("--");
Serial.print("shunt voltage: ");
Serial.print(monitor.shuntVoltage() * 1000, 4);
Serial.println(" mV");
Serial.print("shunt current: ");
Serial.print(monitor.shuntCurrent() * 1000, 4);
Serial.println(" mA");
Serial.print("bus voltage: ");
Serial.print(monitor.busVoltage(), 4);
Serial.println(" V");
Serial.print("bus power: ");
Serial.print(monitor.busPower() * 1000, 4);
Serial.println(" mW");
Serial.println(" ");
Serial.println(" ");
delay(1000);
}

View File

@ -0,0 +1,16 @@
INA219 KEYWORD1
begin KEYWORD2
calibrate KEYWORD2
configure KEYWORD2
reset KEYWORD2
shuntVoltageRaw KEYWORD2
busVoltageRaw KEYWORD2
shuntCurrentRaw KEYWORD2
shuntVoltage KEYWORD2
busVoltage KEYWORD2
shuntCurrent KEYWORD2
busPower KEYWORD2
reconfig KEYWORD2
recalibrate KEYWORD2
ready KEYWORD2
overflow KEYWORD2

View File

@ -0,0 +1,12 @@
{
"name": "INA219",
"keywords": "i2c, driver, sensor, battery, monitor, current, voltage",
"description": "TI INA219 hi-side i2c current/power monitor Library",
"repository":
{
"type": "git",
"url": "https://github.com/johngineer/ArduinoINA219"
},
"frameworks": "arduino",
"platforms": "atmelavr"
}

View File

@ -0,0 +1,9 @@
name=ArduinoINA219
version=1.0.1
author=DeCristofaro John <johngineer@yahoo.com>, Jukka-Pekka Sarjanen <jukka-Pekka.sarjanen@kone.com>, gandy92 <gandy92@googlemail.com>, Flavius Bindea <flav@flav.com>
maintainer=Flavius Bindea <flav@flav.com>
sentence=INA219 hi-side i2c current/power sensor Library
paragraph=INA219 hi-side i2c current/power sensor Library
category=Sensors
url=https://github.com/flav1972/ArduinoINA219
architectures=*

View File

@ -0,0 +1,32 @@
{
"license": "LGPL-3.0",
"name": "AsyncTCP",
"repository": {
"url": "https://github.com/me-no-dev/AsyncTCP.git",
"type": "git"
},
"platforms": [
"espressif32"
],
"frameworks": [
"arduino"
],
"version": "1.0.1",
"build": {
"libCompatMode": 2
},
"authors": [
{
"url": null,
"maintainer": true,
"email": null,
"name": "Hristo Gochkov"
}
],
"keywords": [
"async",
"tcp"
],
"id": 1826,
"description": "Asynchronous TCP Library for ESP32"
}

Some files were not shown because too many files have changed in this diff Show More