From df2f257ca827c7dfcac37dc12853a2558a25aeba Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Wed, 17 Oct 2018 10:48:36 +0200 Subject: [PATCH 1/9] Add files via upload --- Adafruit_INA219.cpp | 88 +++++++++++++++++++++++++++++++++++---------- Adafruit_INA219.h | 7 ++-- 2 files changed, 74 insertions(+), 21 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index d2b6e6d..64c4ae8 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -92,7 +92,7 @@ void Adafruit_INA219::wireReadRegister(uint8_t reg, uint16_t *value) @note These calculations assume a 0.1 ohm resistor is present */ /**************************************************************************/ -void Adafruit_INA219::setCalibration_32V_2A(void) +void Adafruit_INA219::setCalibration_32V_2A(bool triggered) { // By default we use a pretty huge range for the input voltage, // which probably isn't the most appropriate choice for system @@ -165,11 +165,22 @@ void Adafruit_INA219::setCalibration_32V_2A(void) wireWriteRegister(INA219_REG_CALIBRATION, ina219_calValue); // Set Config register to take into account the settings above - uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + uint16_t config; + + if (triggered) { + config = INA219_CONFIG_BVOLTAGERANGE_32V | + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + } else { + config = INA219_CONFIG_BVOLTAGERANGE_32V | + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + } + wireWriteRegister(INA219_REG_CONFIG, config); } @@ -183,7 +194,7 @@ void Adafruit_INA219::setCalibration_32V_2A(void) @note These calculations assume a 0.1 ohm resistor is present */ /**************************************************************************/ -void Adafruit_INA219::setCalibration_32V_1A(void) +void Adafruit_INA219::setCalibration_32V_1A(bool triggered) { // By default we use a pretty huge range for the input voltage, // which probably isn't the most appropriate choice for system @@ -258,11 +269,22 @@ void Adafruit_INA219::setCalibration_32V_1A(void) wireWriteRegister(INA219_REG_CALIBRATION, ina219_calValue); // Set Config register to take into account the settings above - uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + uint16_t config; + + if (triggered) { + config = INA219_CONFIG_BVOLTAGERANGE_32V | + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + } else { + config = INA219_CONFIG_BVOLTAGERANGE_32V | + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + } + wireWriteRegister(INA219_REG_CONFIG, config); } @@ -273,7 +295,7 @@ void Adafruit_INA219::setCalibration_32V_1A(void) only supporting 16V at 400mA max. */ /**************************************************************************/ -void Adafruit_INA219::setCalibration_16V_400mA(void) { +void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { // Calibration which uses the highest precision for // current measurement (0.1mA), at the expense of @@ -349,11 +371,21 @@ void Adafruit_INA219::setCalibration_16V_400mA(void) { wireWriteRegister(INA219_REG_CALIBRATION, ina219_calValue); // Set Config register to take into account the settings above - uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V | - INA219_CONFIG_GAIN_1_40MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + uint16_t config; + + if (triggered) { + config = INA219_CONFIG_BVOLTAGERANGE_16V | + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + } else { + config = INA219_CONFIG_BVOLTAGERANGE_16V | + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + } wireWriteRegister(INA219_REG_CONFIG, config); } @@ -517,3 +549,23 @@ float Adafruit_INA219::getPower_mW() { valueDec *= ina219_powerMultiplier_mW; return valueDec; } + +/**************************************************************************/ +/*! + @brief Enter the Power Down mode + using 1 uA instead of 1 mA current in continous mode. + Just call your calibration routine to turn it on again + @return void + +*/ +void Adafruit_INA219::enterPowerSave() { + + uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V | + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_POWERDOWN; + + wireWriteRegister(INA219_REG_CONFIG, config); + +} \ No newline at end of file diff --git a/Adafruit_INA219.h b/Adafruit_INA219.h index be8cb35..01da43a 100644 --- a/Adafruit_INA219.h +++ b/Adafruit_INA219.h @@ -213,13 +213,14 @@ class Adafruit_INA219{ Adafruit_INA219(uint8_t addr = INA219_ADDRESS); void begin(void); void begin(TwoWire *theWire); - void setCalibration_32V_2A(void); - void setCalibration_32V_1A(void); - void setCalibration_16V_400mA(void); + void setCalibration_32V_2A(bool triggered = false); + void setCalibration_32V_1A(bool triggered = false); + void setCalibration_16V_400mA(bool triggered = false); float getBusVoltage_V(void); float getShuntVoltage_mV(void); float getCurrent_mA(void); float getPower_mW(void); + void enterPowerSave(void); private: TwoWire *_i2c; From 1beb0deea1372d4e8c7f5124b8e7cf07bdd23d25 Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Wed, 17 Oct 2018 10:49:48 +0200 Subject: [PATCH 2/9] Add files via upload --- .../getcurrent_asynchronous.ino | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 examples/getcurrent_asynchronous/getcurrent_asynchronous.ino diff --git a/examples/getcurrent_asynchronous/getcurrent_asynchronous.ino b/examples/getcurrent_asynchronous/getcurrent_asynchronous.ino new file mode 100644 index 0000000..e4f6e4d --- /dev/null +++ b/examples/getcurrent_asynchronous/getcurrent_asynchronous.ino @@ -0,0 +1,66 @@ +/* The difference with getcurrent example is that + * INA219 will not be constantly measuring values but only when triggered by calling the configuration function + * hence it will be measuring for 69 ms every 2 seconds (delay(200) + delay(1800) in this example) + * During the 1800 ms timelap the INA219 is put to power dowm using enterPowerSave() + * It would be particularly helpfull if you put the MCU in sleep mode during this period so both MCU and INA219 would consumme close to 0. + */ + +#include +#include + +Adafruit_INA219 ina219; + + +void setup(void) +{ + Serial.begin(115200); + while (!Serial) { + // will pause Zero, Leonardo, etc until serial console opens + delay(1); + } + + uint32_t currentFrequency; + + Serial.println("Hello!"); + + // Initialize the INA219. + // By default the initialization will use the largest range (32V, 2A). However + // you can call a setCalibration function to change this range (see comments). + ina219.begin(); + + Serial.println("Measuring voltage and current with INA219 ..."); +} + +void loop(void) +{ + float shuntvoltage = 0; + float busvoltage = 0; + float current_mA = 0; + float loadvoltage = 0; + float power_mW = 0; + + // Configuring the INA (in trigger mode) will trigger measurement. Use the appropriate calibration function + ina219.setCalibration_32V_2A(true); + //ina219.setCalibration_32V_1A(true); + //ina219.setCalibration_16V_400mA(true); + + // Some time to do the measurement (more than 69 ms) + delay(200); + + shuntvoltage = ina219.getShuntVoltage_mV(); + busvoltage = ina219.getBusVoltage_V(); + current_mA = ina219.getCurrent_mA(); + power_mW = ina219.getPower_mW(); + loadvoltage = busvoltage + (shuntvoltage / 1000); + + Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V"); + Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV"); + Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V"); + Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA"); + Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW"); + Serial.println(""); + + // Now INA219 is sleeping, using a couple of uA instead of 1mA + ina219.enterPowerSave(); + delay(1800); +} From db2e44a71b9a82890e87b2718916494a94e30883 Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Wed, 17 Oct 2018 10:53:27 +0200 Subject: [PATCH 3/9] Add files via upload --- Adafruit_INA219.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 64c4ae8..271a7c3 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -168,13 +168,13 @@ void Adafruit_INA219::setCalibration_32V_2A(bool triggered) uint16_t config; if (triggered) { - config = INA219_CONFIG_BVOLTAGERANGE_32V | + config = INA219_CONFIG_BVOLTAGERANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { - config = INA219_CONFIG_BVOLTAGERANGE_32V | + config = INA219_CONFIG_BVOLTAGERANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US | @@ -272,13 +272,13 @@ void Adafruit_INA219::setCalibration_32V_1A(bool triggered) uint16_t config; if (triggered) { - config = INA219_CONFIG_BVOLTAGERANGE_32V | + config = INA219_CONFIG_BVOLTAGERANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { - config = INA219_CONFIG_BVOLTAGERANGE_32V | + config = INA219_CONFIG_BVOLTAGERANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US | @@ -374,13 +374,13 @@ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { uint16_t config; if (triggered) { - config = INA219_CONFIG_BVOLTAGERANGE_16V | + config = INA219_CONFIG_BVOLTAGERANGE_16V | INA219_CONFIG_GAIN_1_40MV | INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { - config = INA219_CONFIG_BVOLTAGERANGE_16V | + config = INA219_CONFIG_BVOLTAGERANGE_16V | INA219_CONFIG_GAIN_1_40MV | INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US | From eaf3a6ec1f24af86526ec0cc4a1102b1e9fdae3b Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Wed, 17 Oct 2018 10:54:56 +0200 Subject: [PATCH 4/9] Add files via upload From 8aaa6ff0045740d1d52f13e07f455bf5b2598789 Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Wed, 17 Oct 2018 10:56:48 +0200 Subject: [PATCH 5/9] Update Adafruit_INA219.cpp --- Adafruit_INA219.cpp | 50 ++++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 271a7c3..01ed0ba 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -169,16 +169,16 @@ void Adafruit_INA219::setCalibration_32V_2A(bool triggered) if (triggered) { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; } wireWriteRegister(INA219_REG_CONFIG, config); @@ -273,16 +273,16 @@ void Adafruit_INA219::setCalibration_32V_1A(bool triggered) if (triggered) { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; } wireWriteRegister(INA219_REG_CONFIG, config); @@ -375,16 +375,16 @@ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { if (triggered) { config = INA219_CONFIG_BVOLTAGERANGE_16V | - INA219_CONFIG_GAIN_1_40MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { config = INA219_CONFIG_BVOLTAGERANGE_16V | - INA219_CONFIG_GAIN_1_40MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; } wireWriteRegister(INA219_REG_CONFIG, config); } @@ -568,4 +568,4 @@ void Adafruit_INA219::enterPowerSave() { wireWriteRegister(INA219_REG_CONFIG, config); -} \ No newline at end of file +} From da0ebde321a43c46c1eb6f0cdc7ce5471a8a5cdd Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Wed, 17 Oct 2018 14:06:41 +0200 Subject: [PATCH 6/9] Add files via upload --- Adafruit_INA219.cpp | 61 ++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 01ed0ba..bd73a93 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -89,6 +89,9 @@ void Adafruit_INA219::wireReadRegister(uint8_t reg, uint16_t *value) each unit of power corresponds to 2mW. Counter overflow occurs at 3.2A. + @param triggered : a boolean to configure INA219 in triggered (true) + or continuous (false) mode. Optional with default value false + @note These calculations assume a 0.1 ohm resistor is present */ /**************************************************************************/ @@ -169,16 +172,16 @@ void Adafruit_INA219::setCalibration_32V_2A(bool triggered) if (triggered) { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; } wireWriteRegister(INA219_REG_CONFIG, config); @@ -191,6 +194,9 @@ void Adafruit_INA219::setCalibration_32V_2A(bool triggered) unit of power corresponds to 800�W. Counter overflow occurs at 1.3A. + @param triggered : a boolean to configure INA219 in triggered (true) + or continuous (false) mode. Optional with default value false + @note These calculations assume a 0.1 ohm resistor is present */ /**************************************************************************/ @@ -273,16 +279,16 @@ void Adafruit_INA219::setCalibration_32V_1A(bool triggered) if (triggered) { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { config = INA219_CONFIG_BVOLTAGERANGE_32V | - INA219_CONFIG_GAIN_8_320MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + INA219_CONFIG_GAIN_8_320MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; } wireWriteRegister(INA219_REG_CONFIG, config); @@ -293,6 +299,11 @@ void Adafruit_INA219::setCalibration_32V_1A(bool triggered) @brief set device to alibration which uses the highest precision for current measurement (0.1mA), at the expense of only supporting 16V at 400mA max. + + @param triggered : a boolean to configure INA219 in triggered (true) + or continuous (false) mode. Optional with default value false + + @note These calculations assume a 0.1 ohm resistor is present */ /**************************************************************************/ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { @@ -375,16 +386,16 @@ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { if (triggered) { config = INA219_CONFIG_BVOLTAGERANGE_16V | - INA219_CONFIG_GAIN_1_40MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_128S_69MS | - INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; } else { config = INA219_CONFIG_BVOLTAGERANGE_16V | - INA219_CONFIG_GAIN_1_40MV | - INA219_CONFIG_BADCRES_12BIT | - INA219_CONFIG_SADCRES_12BIT_1S_532US | - INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; } wireWriteRegister(INA219_REG_CONFIG, config); } @@ -568,4 +579,4 @@ void Adafruit_INA219::enterPowerSave() { wireWriteRegister(INA219_REG_CONFIG, config); -} +} \ No newline at end of file From 33fa1f62831991961b4b01ff664ea4697bf44394 Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Mon, 19 Nov 2018 15:43:55 +0100 Subject: [PATCH 7/9] Add files via upload --- Adafruit_INA219.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++- Adafruit_INA219.h | 1 + 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index bd73a93..3f08975 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -296,8 +296,8 @@ void Adafruit_INA219::setCalibration_32V_1A(bool triggered) /**************************************************************************/ /*! - @brief set device to alibration which uses the highest precision for - current measurement (0.1mA), at the expense of + @brief set device to calibration which uses the highest precision for + current measurement (0.05mA), at the expense of only supporting 16V at 400mA max. @param triggered : a boolean to configure INA219 in triggered (true) @@ -400,6 +400,111 @@ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { wireWriteRegister(INA219_REG_CONFIG, config); } +/**************************************************************************/ +/*! + @brief set device to alibration which uses the highest precision for + current measurement (to reduce offset), a very mall LSB (20 uA) + and a maximum Bus voltage of 32V + + @param triggered : a boolean to configure INA219 in triggered (true) + or continuous (false) mode. Optional with default value false + + @note These calculations assume a 0.1 ohm resistor is present +*/ +/**************************************************************************/ +void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { + + // Calibration which uses the highest precision for + // current measurement (0.1mA), at the expense of + // only supporting 16V at 400mA max. + + // VBUS_MAX = 32V + // VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV) + // RSHUNT = 0.1 (Resistor value in ohms) + + // 1. Determine max possible current + // MaxPossible_I = VSHUNT_MAX / RSHUNT + // MaxPossible_I = 0.4A + + // 2. Determine max expected current + // MaxExpected_I = 0.4A + + // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) + // MinimumLSB = MaxExpected_I/32767 + // MinimumLSB = 0.0000122 (12uA per bit) + // MaximumLSB = MaxExpected_I/4096 + // MaximumLSB = 0.0000977 (98uA per bit) + + // 4. Choose an LSB between the min and max values + // (Preferrably a roundish number close to MinLSB) + // CurrentLSB = 0.00002 (20uA per bit) + + // 5. Compute the calibration register + // Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) + // Cal = 20480 + + ina219_calValue = 20480; + + // 6. Calculate the power LSB + // PowerLSB = 20 * CurrentLSB + // PowerLSB = 0.0004 (0.4mW per bit) + + // 7. Compute the maximum current and shunt voltage values before overflow + // + // Max_Current = Current_LSB * 32767 + // Max_Current = 0.65534A before overflow + // + // If Max_Current > Max_Possible_I then + // Max_Current_Before_Overflow = MaxPossible_I + // Else + // Max_Current_Before_Overflow = Max_Current + // End If + // + // Max_Current_Before_Overflow = MaxPossible_I + // Max_Current_Before_Overflow = 0.4 + // + // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT + // Max_ShuntVoltage = 0.04V + // + // If Max_ShuntVoltage >= VSHUNT_MAX + // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX + // Else + // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage + // End If + // + // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX + // Max_ShuntVoltage_Before_Overflow = 0.04V + + // 8. Compute the Maximum Power + // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX + // MaximumPower = 0.4 * 32V + // MaximumPower = 12.8W + + // Set multipliers to convert raw current/power values + ina219_currentDivider_mA = 50; // Current LSB = 20uA per bit (1000/20 = 50) + ina219_powerMultiplier_mW = 0.4f; // Power LSB = 1mW per bit + + // Set Calibration register to 'Cal' calculated above + wireWriteRegister(INA219_REG_CALIBRATION, ina219_calValue); + + // Set Config register to take into account the settings above + uint16_t config; + + if (triggered) { + config = INA219_CONFIG_BVOLTAGERANGE_32V | + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_128S_69MS | + INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED; + } else { + config = INA219_CONFIG_BVOLTAGERANGE_32V | + INA219_CONFIG_GAIN_1_40MV | + INA219_CONFIG_BADCRES_12BIT | + INA219_CONFIG_SADCRES_12BIT_1S_532US | + INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS; + } + wireWriteRegister(INA219_REG_CONFIG, config); +} /**************************************************************************/ /*! @brief Instantiates a new INA219 class diff --git a/Adafruit_INA219.h b/Adafruit_INA219.h index 01da43a..1112c2f 100644 --- a/Adafruit_INA219.h +++ b/Adafruit_INA219.h @@ -216,6 +216,7 @@ class Adafruit_INA219{ void setCalibration_32V_2A(bool triggered = false); void setCalibration_32V_1A(bool triggered = false); void setCalibration_16V_400mA(bool triggered = false); + void setCalibration_32V_400mA(bool triggered = false); float getBusVoltage_V(void); float getShuntVoltage_mV(void); float getCurrent_mA(void); From 124285ddfeee53ad412b05b5fea49a2ee19c6793 Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Mon, 19 Nov 2018 16:35:36 +0100 Subject: [PATCH 8/9] Add files via upload --- Adafruit_INA219.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 3f08975..bc25a18 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -402,9 +402,8 @@ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { /**************************************************************************/ /*! - @brief set device to alibration which uses the highest precision for - current measurement (to reduce offset), a very mall LSB (20 uA) - and a maximum Bus voltage of 32V + @brief set device to calibration which uses the highest precision for + current measurement (0.1mA), Gain 1 and 32V max bus voltage @param triggered : a boolean to configure INA219 in triggered (true) or continuous (false) mode. Optional with default value false @@ -418,7 +417,7 @@ void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { // current measurement (0.1mA), at the expense of // only supporting 16V at 400mA max. - // VBUS_MAX = 32V + // VBUS_MAX = 16V // VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV) // RSHUNT = 0.1 (Resistor value in ohms) @@ -437,22 +436,22 @@ void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { // 4. Choose an LSB between the min and max values // (Preferrably a roundish number close to MinLSB) - // CurrentLSB = 0.00002 (20uA per bit) + // CurrentLSB = 0.00005 (50uA per bit) // 5. Compute the calibration register // Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) - // Cal = 20480 + // Cal = 8192 (0x2000) - ina219_calValue = 20480; + ina219_calValue = 8192; // 6. Calculate the power LSB // PowerLSB = 20 * CurrentLSB - // PowerLSB = 0.0004 (0.4mW per bit) + // PowerLSB = 0.001 (1mW per bit) // 7. Compute the maximum current and shunt voltage values before overflow // // Max_Current = Current_LSB * 32767 - // Max_Current = 0.65534A before overflow + // Max_Current = 1.63835A before overflow // // If Max_Current > Max_Possible_I then // Max_Current_Before_Overflow = MaxPossible_I @@ -477,12 +476,12 @@ void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { // 8. Compute the Maximum Power // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX - // MaximumPower = 0.4 * 32V - // MaximumPower = 12.8W + // MaximumPower = 0.4 * 16V + // MaximumPower = 6.4W // Set multipliers to convert raw current/power values - ina219_currentDivider_mA = 50; // Current LSB = 20uA per bit (1000/20 = 50) - ina219_powerMultiplier_mW = 0.4f; // Power LSB = 1mW per bit + ina219_currentDivider_mA = 20; // Current LSB = 50uA per bit (1000/50 = 20) + ina219_powerMultiplier_mW = 1.0f; // Power LSB = 1mW per bit // Set Calibration register to 'Cal' calculated above wireWriteRegister(INA219_REG_CALIBRATION, ina219_calValue); @@ -505,6 +504,7 @@ void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { } wireWriteRegister(INA219_REG_CONFIG, config); } + /**************************************************************************/ /*! @brief Instantiates a new INA219 class From 3b896d2004002b1aee80f54b239da479d26cee6e Mon Sep 17 00:00:00 2001 From: Thierry GUENNOU <31097359+mrguen@users.noreply.github.com> Date: Mon, 19 Nov 2018 16:49:15 +0100 Subject: [PATCH 9/9] Add files via upload --- Adafruit_INA219.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index bc25a18..3f172a9 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -414,10 +414,9 @@ void Adafruit_INA219::setCalibration_16V_400mA(bool triggered) { void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { // Calibration which uses the highest precision for - // current measurement (0.1mA), at the expense of - // only supporting 16V at 400mA max. + // current measurement (0.1mA) - // VBUS_MAX = 16V + // VBUS_MAX = 32V // VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV) // RSHUNT = 0.1 (Resistor value in ohms) @@ -476,8 +475,8 @@ void Adafruit_INA219::setCalibration_32V_400mA(bool triggered) { // 8. Compute the Maximum Power // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX - // MaximumPower = 0.4 * 16V - // MaximumPower = 6.4W + // MaximumPower = 0.4 * 32V + // MaximumPower = 12.8W // Set multipliers to convert raw current/power values ina219_currentDivider_mA = 20; // Current LSB = 50uA per bit (1000/50 = 20)