A compilation of some hardware sensors and their shared programming interfaces.

Embed: (wiki syntax)

« Back to documentation index

INA219 Class Reference

This class allows for easy control over a INA219 current/power sensing IC. More...

#include <INA219.h>

Inherits I2CSensor, and PeriodicSensor.

Public Member Functions

 INA219 (PinName sda, PinName scl, int i2c_addr)
 ~INA219 ()
 Destroys this object.
void selfInit ()
 Initializes the device to some nice state.
void reset ()
 Performs a software reset of the device.
void setBusVoltageRange32V (bool enable)
 Sets the bus voltage range to either 16V or 32V.
bool isBusVoltageRange32V ()
void setShuntAmpGain (unsigned int gain)
 Sets the PGA amplifier gain for shunt voltage measurement.
unsigned int getShuntAmpGain ()
 Gets the PGA amplifier gain for shunt voltage measurement.
void setADCResolutionAndAveraging (bool shunt, bool resolution, unsigned int value)
 Sets the resolution and sample averaging of the bus or shunt ADC.
unsigned int getADCResolutionAndAveraging (bool shunt, bool &resolution)
 Gets the resolution or number of samples in an average of the bus or shunt ADC.
void setMode (bool shuntVoltage, bool busVoltage, bool continuous)
 Sets the operating mode of the device.
void getMode (bool &shuntVoltage, bool &busVoltage, bool &continuous)
 Gets the operating mode of the device.
int16_t getShuntVoltage (bool sampleNow)
 Gets the shunt voltage across the current sensing resistor.
float getShuntVoltageFloat (bool sampleNow)
 Gets the shunt voltage across the current sensing resistor.
int16_t getBusVoltage (bool sampleNow)
 Gets the bus voltage at the negative terminal of the current sensing resistor (V-).
float getBusVoltageFloat (bool sampleNow)
 Gets the bus voltage across the current sensing resistor.
int16_t getPower (bool sampleNow)
 Gets the power consumed by the load.
float getPowerFloat (bool sampleNow)
 Gets the power consumed by the load.
int16_t getCurrent (bool sampleNow)
 Gets the current sunk by the load.
float getCurrentFloat (bool sampleNow)
 Gets the current delivered to the load.
uint16_t getCalibration ()
 Gets the calibration configuration setting for current range.
PinName getSDAPin ()
PinName getSCLPin ()
uint8_t getDeviceI2CAddress ()
uint8_t getRegister (const uint8_t reg_addr)
 Read an 8-bit register.
uint16_t getRegister16b (const uint8_t reg_addr)
 Read a 16-bit register.
void setRegister (const uint8_t reg_addr, const uint8_t data)
 Set an 8-bit register.
void setRegister16b (const uint8_t reg_addr, const uint16_t data)
 Set a 16-bit register.
bool isDataReady ()
void enableBackgroundSampling (bool enable, float sample_period)
 Control the background sampling of the device via interrupts.
bool isBackgroundSamplingEnabled ()
float getSamplePeriod ()
float getMinSamplePeriod ()

Protected Member Functions

int __readReg (const uint8_t reg_addr, uint8_t *data, int len)
int __writeReg (const uint8_t *data, int total_len)

Detailed Description

This class allows for easy control over a INA219 current/power sensing IC.

Definition at line 17 of file INA219.h.


Constructor & Destructor Documentation

INA219 ( PinName  sda,
PinName  scl,
int  i2c_addr 
)
Parameters:
sdathe pin identifier for SDA I2C signal
sclthe pin identifier for SCL I2C signal
i2c_addrthe 8-bit I2C address for this device. Note that LSB is a don't care.

Definition at line 11 of file INA219.cpp.

~INA219 (  )

Destroys this object.

Definition at line 35 of file INA219.cpp.


Member Function Documentation

int __readReg ( const uint8_t  reg_addr,
uint8_t *  data,
int  len 
) [protected, inherited]
Parameters:
reg_addr8-bit register address inside the device
data8-bit data that will be read from the register. This pointer MUST be valid. This array MUST be at least as long as len.
lentotal number of bytes to read. len must be >= 1.
Returns:
0 on success, otherwise error code from I2C

Definition at line 57 of file I2CSensor.cpp.

int __writeReg ( const uint8_t *  data,
int  total_len 
) [protected, inherited]
Parameters:
data8-bit data that will be written to the register. This pointer MUST be valid. This array MUST be at least as long as total_len. data[0] should be set to the 8-bit register address. The data payload should start at index 1.
total_lentotal length of the data array, which is the length of the payload + 1 for the register address. total_len must be >= 2.
Returns:
0 on success, otherwise error code from I2C

Definition at line 72 of file I2CSensor.cpp.

void enableBackgroundSampling ( bool  enable,
float  sample_period 
) [inherited]

Control the background sampling of the device via interrupts.

If disabled, the device can still be sampled using the getter methods directly.

Parameters:
enableif true, enables background sampling with the given sample period.
sample_periodsampling period in seconds. Must be at least 0.005 sec unless enable is false, in which case it is don't care.

Definition at line 36 of file PeriodicSensor.cpp.

unsigned int getADCResolutionAndAveraging ( bool  shunt,
bool &  resolution 
)

Gets the resolution or number of samples in an average of the bus or shunt ADC.

Parameters:
shuntif true, gets for shunt ADC. if false, gets for bus ADC.
resolutionsets this to true if the return value represents resolution in bits. else, sets it to false if return value represents sample averaging in # samples.

Definition at line 195 of file INA219.cpp.

int16_t getBusVoltage ( bool  sampleNow )

Gets the bus voltage at the negative terminal of the current sensing resistor (V-).

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
16-bit integer representing the bus voltage.

Definition at line 267 of file INA219.cpp.

float getBusVoltageFloat ( bool  sampleNow )

Gets the bus voltage across the current sensing resistor.

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
the bus voltage in Volts

Definition at line 281 of file INA219.cpp.

uint16_t getCalibration (  )

Gets the calibration configuration setting for current range.

Returns:
16-bit unsigned integer representing calibration register value

Definition at line 319 of file INA219.cpp.

int16_t getCurrent ( bool  sampleNow )

Gets the current sunk by the load.

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
16-bit integer representing current sunk by the load.

Definition at line 305 of file INA219.cpp.

float getCurrentFloat ( bool  sampleNow )

Gets the current delivered to the load.

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
the load current in Amperes

Definition at line 315 of file INA219.cpp.

uint8_t getDeviceI2CAddress (  ) [inherited]
Returns:
Device I2C address (LSB always 0 in this case, it is don't care)

Definition at line 28 of file I2CSensor.cpp.

float getMinSamplePeriod (  ) [inherited]
Returns:
the minimum sample period in seconds

Definition at line 57 of file PeriodicSensor.cpp.

void getMode ( bool &  shuntVoltage,
bool &  busVoltage,
bool &  continuous 
)

Gets the operating mode of the device.

If all parameters are false, the device is powered down. If shuntVoltage and busVoltage are false but continuous is true, the device is on but the ADC is powered down.

Parameters:
shuntVoltagesets true if shunt voltage is being measured.
busVoltagesets true if bus voltage is being measured.
continuoussets true if continuous readings are being done.

Definition at line 246 of file INA219.cpp.

int16_t getPower ( bool  sampleNow )

Gets the power consumed by the load.

Note that calling this method with sampleNow == true will also implicitly sample current as well, which can be retrieved with a call to getCurrent(false) after calling getPower(true).

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
16-bit integer representing power consumption of the load.

Definition at line 285 of file INA219.cpp.

float getPowerFloat ( bool  sampleNow )

Gets the power consumed by the load.

Note that calling this method with sampleNow == true will also implicitly sample current as well, which can be retrieved with a call to getCurrentFloat(false) after calling getPowerFloat(true).

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
the load power in Watts

Definition at line 301 of file INA219.cpp.

uint8_t getRegister ( const uint8_t  reg_addr ) [inherited]

Read an 8-bit register.

Parameters:
reg_addrthe register in the device
Returns:
The raw value from the register specified by reg_addr.

Definition at line 30 of file I2CSensor.cpp.

uint16_t getRegister16b ( const uint8_t  reg_addr ) [inherited]

Read a 16-bit register.

Parameters:
reg_addrthe register in the device
Returns:
The raw value from the register specified by reg_addr.

Definition at line 36 of file I2CSensor.cpp.

float getSamplePeriod (  ) [inherited]
Returns:
the sample period in seconds, if background sampling is enabled. Else, non-positive return.

Definition at line 53 of file PeriodicSensor.cpp.

PinName getSCLPin (  ) [inherited]
Returns:
I2C SCL pin ID

Definition at line 26 of file I2CSensor.cpp.

PinName getSDAPin (  ) [inherited]
Returns:
I2C SDA pin ID

Definition at line 24 of file I2CSensor.cpp.

unsigned int getShuntAmpGain (  )

Gets the PGA amplifier gain for shunt voltage measurement.

Returns:
the gain level

Definition at line 111 of file INA219.cpp.

int16_t getShuntVoltage ( bool  sampleNow )

Gets the shunt voltage across the current sensing resistor.

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
16-bit integer representing the shunt voltage.

Definition at line 253 of file INA219.cpp.

float getShuntVoltageFloat ( bool  sampleNow )

Gets the shunt voltage across the current sensing resistor.

Parameters:
sampleNowif true, queries the device for the sample and returns it. if false, gets the last queried value. The latter is preferred if this object is set up to sample using interrupts.
Returns:
the shunt voltage in Volts

Definition at line 263 of file INA219.cpp.

bool isBackgroundSamplingEnabled (  ) [inherited]
Returns:
true if background sampling is enabled.

Definition at line 49 of file PeriodicSensor.cpp.

bool isBusVoltageRange32V (  )
Returns:
true if the bus voltage range is set to 32V. if false, bus voltage range is 16V.

Definition at line 81 of file INA219.cpp.

bool isDataReady (  ) [inherited]
Returns:
true if there is new data ready to be retrieved.

Definition at line 32 of file PeriodicSensor.cpp.

void reset (  )

Performs a software reset of the device.

Definition at line 43 of file INA219.cpp.

void selfInit (  )

Initializes the device to some nice state.

Definition at line 37 of file INA219.cpp.

void setADCResolutionAndAveraging ( bool  shunt,
bool  resolution,
unsigned int  value 
)

Sets the resolution and sample averaging of the bus or shunt ADC.

If resolution is set, sample averaging is disabled. If sample averaging is set, resolution setting is disabled.

Parameters:
shuntif true, sets for shunt ADC. if false, sets for bus ADC.
resolutionif true, sets ADC resolution using the third parameter. if false, sets the ADC sample averaging using the third parameter.
valueif resolution is true, then this is the resolution in bits to use. Allowed values: 9, 10, 11, or 12 bits. If resolution is false, then this is the number of samples to average in each data sample. Allowed values: 2, 4, 8, 16, 32, 64, or 128 samples. Any other values will cause this method to have no effect. Default is 12-bit resolution mode.

Definition at line 115 of file INA219.cpp.

void setBusVoltageRange32V ( bool  enable )

Sets the bus voltage range to either 16V or 32V.

Parameters:
enableif true, sets the bus voltage range to 32V. Else, sets it to 16V.

Definition at line 68 of file INA219.cpp.

void setMode ( bool  shuntVoltage,
bool  busVoltage,
bool  continuous 
)

Sets the operating mode of the device.

If all parameters are false, the device powers down. If shuntVoltage and busVoltage are false but continuous is true, the device remains on but the ADC is powered down.

Parameters:
shuntVoltageif true, samples the shunt voltage.
busVoltageif true, samples the bus voltage.
continuousif true, samples continuously, otherwise only on triggered reads.

Definition at line 220 of file INA219.cpp.

void setRegister ( const uint8_t  reg_addr,
const uint8_t  data 
) [inherited]

Set an 8-bit register.

Parameters:
reg_addrthe register in the device
datathe byte to write to the register

Definition at line 43 of file I2CSensor.cpp.

void setRegister16b ( const uint8_t  reg_addr,
const uint16_t  data 
) [inherited]

Set a 16-bit register.

Parameters:
reg_addrthe register in the device
datathe byte to write to the register

Definition at line 48 of file I2CSensor.cpp.

void setShuntAmpGain ( unsigned int  gain )

Sets the PGA amplifier gain for shunt voltage measurement.

Parameters:
gainthe gain level to set. Allowed values: 1, 2, 4, 8. Default is 8. Any other values will have no effect.

Definition at line 85 of file INA219.cpp.