Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Adafruit_INA219.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,86 @@ float Adafruit_INA219::getCurrent_mA() {
valueDec /= ina219_currentDivider_mA;
return valueDec;
}

/**************************************************************************/
/*!
@brief Change the config register so that the INA219 returns current
measurement values from single samples.
*/
/**************************************************************************/
void Adafruit_INA219::setAmpInstant() {
uint16_t value;

// get the current configuration data
wireReadRegister(INA219_REG_CONFIG, &value);

// change out the bits to average 1 samples at 12 bits per sample
value = value & ~INA219_CONFIG_SADCRES_MASK | INA219_CONFIG_SADCRES_12BIT_1S_532US ;

// write the changed config value back again
wireWriteRegister(INA219_REG_CONFIG, value);
}

/**************************************************************************/
/*!
@brief Change the config register so that the INA219 returns a current
measurement value that is an average over 128 samples.
*/
/**************************************************************************/
void Adafruit_INA219::setAmpAverage() {
uint16_t value;

// get the current configuration data
wireReadRegister(INA219_REG_CONFIG, &value);

// change out the bits to average 128 samples at 12 bits per sample
value = value & ~INA219_CONFIG_SADCRES_MASK | INA219_CONFIG_SADCRES_12BIT_128S_69MS;

// write the changed config value back again
wireWriteRegister(INA219_REG_CONFIG, value);

delay(69); // Max 12-bit 128S conversion time is 69mS per sample, but
// read can happen more frequently
}

/**************************************************************************/
/*!
@brief Change the config register so that the INA219 returns voltage
measurement values from single samples.
*/
/**************************************************************************/
void Adafruit_INA219::setVoltInstant() {
uint16_t value;

// get the current configuration data
wireReadRegister(INA219_REG_CONFIG, &value);

// change out the bits to average 1 samples at 12 bits per sample
value = value & ~INA219_CONFIG_BADCRES_MASK | INA219_CONFIG_BADCRES_12BIT ;

// write the changed config value back again
wireWriteRegister(INA219_REG_CONFIG, value);
}

/**************************************************************************/
/*!
@brief Change the config register so that the INA219 returns a current
measurement value that is an average over 128 samples.
*/
/**************************************************************************/
void Adafruit_INA219::setVoltAverage() {
uint16_t value;

// get the current configuration data
wireReadRegister(INA219_REG_CONFIG, &value);

// change out the bits to average 128 samples at 12 bits per sample
value = value & ~INA219_CONFIG_BADCRES_MASK | INA219_CONFIG_BADCRES_12BIT_128S_69MS ;

// write the changed config value back again
wireWriteRegister(INA219_REG_CONFIG, value);

delay(69); // Max 12-bit 128S conversion time is 69mS per sample, but
// read can happen more frequently
}

5 changes: 5 additions & 0 deletions Adafruit_INA219.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023
#define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047
#define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097
#define INA219_CONFIG_BADCRES_12BIT_128S_69MS (0x0780) // 128 x 12-bit bus samples averaged together

#define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask
#define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample
Expand Down Expand Up @@ -116,6 +117,10 @@ class Adafruit_INA219{
float getBusVoltage_V(void);
float getShuntVoltage_mV(void);
float getCurrent_mA(void);
void setAmpInstant(void);
void setAmpAverage(void);
void setVoltInstant(void);
void setVoltAverage(void);

private:
uint8_t ina219_i2caddr;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Adafruit_INA219
===============

INA219 Current Sensor
Arduino library to talk to the INA219 Current Sensor over I2C.