This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

INA219: How to tune the INA219 for 0 - 20 mA

Part Number: INA219

I have a pressure sensor that signals via current and draws between 4 and 20 mA: https://www.amazon.com/gp/product/B093ZNL53C 

Using the Arduino library (https://github.com/adafruit/Adafruit_INA219/blob/master/Adafruit_INA219.h#L169), most configurations are in the range from 400 mA to 2 A, and I am trying to get a better fidelity.

I see the datasheet and the library walks you through a 8 step process: https://github.com/adafruit/Adafruit_INA219/blob/master/Adafruit_INA219.cpp#L300, but I am unable to get the setting optimized for exactly what I am looking for.

This was my attempt and I would appreciate any guidance:

void Adafruit_INA219::setCalibration_32V_20mA() {
// By default we use a pretty huge range for the input voltage,
// which probably isn't the most appropriate choice for system
// that don't use a lot of power. But all of the calculations
// are shown below if you want to change the settings. You will
// also need to change any relevant register settings, such as
// setting the VBUS_MAX to 16V instead of 32V, etc.

// VBUS_MAX = 32V (Assumes 32V, can also be set to 16V)
// VSHUNT_MAX = 0.04 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
// RSHUNT = 0.1 (Resistor value in ohms)

// 1. Determine max possible current
// MaxPossible_I = VSHUNT_MAX / RSHUNT
// MaxPossible_I = .4A                 // kevin saye modification

// 2. Determine max expected current
// MaxExpected_I = .012A                 // kevin saye modification

// 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
// MinimumLSB = MaxExpected_I/32767 
// MinimumLSB = 0.000000366                 // kevin saye modification
// MaximumLSB = MaxExpected_I/4096
// MaximumLSB = 0.000002629                 // kevin saye modification

// 4. Choose an LSB between the min and max values
// (Preferrably a roundish number close to MinLSB)
// CurrentLSB = 0.0000004                 // kevin saye modification

// 5. Compute the calibration register
// Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
// Cal = 4096 (0x1000)

ina219_calValue = 1240000;                 // kevin saye modification

// 6. Calculate the power LSB
// PowerLSB = 20 * CurrentLSB
// PowerLSB = 0.000008 (.008mW per bit)                 // kevin saye modification

// 7. Compute the maximum current and shunt voltage values before overflow
//
// Max_Current = Current_LSB * 32767
// Max_Current = .032767A before overflow                 // kevin saye modification
//
// If Max_Current > Max_Possible_I then
// Max_Current_Before_Overflow = MaxPossible_I
// Else
// Max_Current_Before_Overflow = Max_Current .032767A                 // kevin saye modification
// End If
//
// Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
// Max_ShuntVoltage = .0032767V
//
// If Max_ShuntVoltage >= VSHUNT_MAX
// Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
// Else
// Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage .0032767V                 // kevin saye modification
// End If

// 8. Compute the Maximum Power
// MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
// MaximumPower = .0032767 * 32V
// MaximumPower = 0.1048544W                 // kevin saye modification

// Set multipliers to convert raw current/power values
// ina219_currentDivider_mA = 10; // Current LSB = 100uA per bit (1000/100 = 10)
ina219_currentDivider_mA = 2500; // Current LSB = 400pA per bit (1,000,000/400 = 10) //0.0000004                    // kevin saye modification
//ina219_powerMultiplier_mW = 2; // Power LSB = 1mW per bit (2/1)
ina219_powerMultiplier_mW = 250; // Power LSB = .008mW per bit (2/.008)                  // kevin saye modification

// Set Calibration register to 'Cal' calculated above
Adafruit_BusIO_Register calibration_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);

// Set Config register to take into account the settings above
uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
INA219_CONFIG_GAIN_1_40MV | INA219_CONFIG_BADCRES_12BIT_128S_69MS |
INA219_CONFIG_SADCRES_12BIT_128S_69MS |
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
_success = config_reg.write(config, 2);
}

  • Hello,

    We have a great tool to help you go through the steps you mentioned above highlighted below:

    This can be found on the INA219 product page under the design and development tab.

    I am unable to get the setting optimized for exactly what I am looking for.

    Does this mean the output of the INA219 does not satisfy your error requirements?

    With a shunt resistor of 100mR and trying to measure 4mA-20mA should be possible for the INA219.

    I will go through your code and see if there are any errors. Expect a reply soon.

    Regards,

    Cas

  • Cas,
    Thanks for the Power Monitor Tool recommendation.  It appears that it does not work for the INA219 or the current I input, as shown below.

    Any review of the code or my calculation would be greatly appreciated.

  • Hello,

    If your supply voltage is 32V then none of our current sensors will work. I think you made a mistake should be around 5V. Also the INA219 will not be able to do 32V common mode.

    I still have not had a chance to review the code I will reply by end of day.

    Regards,

    Cas

  • Hello,

    I see you have chosen the 4mV input PGA setting this will limit you to the max rshunt you can use. I suggest you use the full PGA setting so 320mV and a higher shunt resistance. Making sure that you are okay with the power loss of the shunt. If you are trying to measure closer to 0 mA then the below method will be  better as the INA219's offset voltage is lower in the lower PGA ranges.

    You could also just use a bigger shunt resistor to increase the usage of the INA219 PGA. Right now you can measure up to 400mA but you placed 12mA as your max, this is a very low range you are using vs what you could use. 

    Also you did not mention what exactly is concerning you about this setup? is the error too large or is there another problem. 

    A schematic could be helpful to see if there is anything else wrong.

    Regards,

    Cas