From 80c71283a1129e0c3856f565879bd882f12a974a Mon Sep 17 00:00:00 2001 From: Ilario Gelmetti Date: Tue, 15 Oct 2024 17:43:26 +0200 Subject: [PATCH 1/4] rewritten calibration functions --- adafruit_ina219.py | 488 ++++++++++++++++++++++----------------------- 1 file changed, 233 insertions(+), 255 deletions(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 16a9ac2..9cc895a 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -232,304 +232,282 @@ def power(self) -> float: return self.raw_power * self._power_lsb def set_calibration_32V_2A(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 32V and 2A of current. Counter - overflow occurs at 3.2A. + """Configures to INA219 to be able to measure up to 32V and 2A of current. Actual max current: 3.2 A. .. note:: These calculations assume a 0.1 shunt ohm resistor is present """ - # 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.32 (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 = 3.2A - - # 2. Determine max expected current - # MaxExpected_I = 2.0A - - # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) - # MinimumLSB = MaxExpected_I/32767 - # MinimumLSB = 0.000061 (61uA per bit) - # MaximumLSB = MaxExpected_I/4096 - # MaximumLSB = 0,000488 (488uA per bit) - - # 4. Choose an LSB between the min and max values - # (Preferrably a roundish number close to MinLSB) - # CurrentLSB = 0.0001 (100uA per bit) - self._current_lsb = 0.1 # Current LSB = 100uA per bit - - # 5. Compute the calibration register - # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) - # Cal = 4096 (0x1000) - - self._cal_value = 4096 - - # 6. Calculate the power LSB - # PowerLSB = 20 * CurrentLSB - # PowerLSB = 0.002 (2mW per bit) - self._power_lsb = 0.002 # Power LSB = 2mW per bit - - # 7. Compute the maximum current and shunt voltage values before overflow - # - # Max_Current = Current_LSB * 32767 - # Max_Current = 3.2767A before overflow - # - # If Max_Current > Max_Possible_I then - # Max_Current_Before_Overflow = MaxPossible_I - # Else - # Max_Current_Before_Overflow = Max_Current - # End If + # 1. Determine max possible bus voltage, 16 or 32 V + #self.bus_voltage_range = BusVoltageRange.RANGE_16V + self.bus_voltage_range = BusVoltageRange.RANGE_32V + + # 2. Determine the installed shunt resistor value + # By default, a 0.1 Ohm resistor is installed + RSHUNT = 0.1 # (Resistor value in ohms) + + # 2. Estimate the max expected current + # MaxExpected_I = 2 A + + # 3. Calculate maximum possible current for each gain value + # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.4 A + # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.8 A + # MaxI_gain4_160mV = 0.16 / RSHUNT = 1.6 A + # MaxI_gain8_320mV = 0.32 / RSHUNT = 3.2 A + + # 4. Evaluate whether to replace the shunt resistor # - # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT - # Max_ShuntVoltage = 0.32V + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. # - # If Max_ShuntVoltage >= VSHUNT_MAX - # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX - # Else - # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage - # End If + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V + + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I + #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + + # 6. Select a calibration value + # Values below 4096 will harm the resolution + # + # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + self.calibration = 4096 + + # 6. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.1) = 0.0001 A + self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + + # 7. Calculate the power LSB in W + # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0001 = 0.002 (2 mW per bit) + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A # 8. Compute the Maximum Power - # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX - # MaximumPower = 3.2 * 32V - # MaximumPower = 102.4W + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # MaximumPower = 32 V * MaxI_gain8_320mV = 32 V * 3.2 A = 102.4 W - # Set Calibration register to 'Cal' calculated above - self._raw_calibration = self._cal_value - - # Set Config register to take into account the settings above - self.bus_voltage_range = BusVoltageRange.RANGE_32V - self.gain = Gain.DIV_8_320MV + # 9. Select the resolution + # Increasing the bits will increase the measurement time but will give better resolution + # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + + # 10. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one ended self.mode = Mode.SANDBVOLT_CONTINUOUS + # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + #self.mode = Mode.SANDBVOLT_TRIGGERED + # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked def set_calibration_32V_1A(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow - occurs at 1.3A. + """Configures to INA219 to be able to measure up to 32V and 1A of current. Actual max current: 1.6 A. .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" - # 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.32 (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 = 3.2A - - # 2. Determine max expected current - # MaxExpected_I = 1.0A - - # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) - # MinimumLSB = MaxExpected_I/32767 - # MinimumLSB = 0.0000305 (30.5uA per bit) - # MaximumLSB = MaxExpected_I/4096 - # MaximumLSB = 0.000244 (244uA per bit) - - # 4. Choose an LSB between the min and max values - # (Preferrably a roundish number close to MinLSB) - # CurrentLSB = 0.0000400 (40uA per bit) - self._current_lsb = 0.04 # In milliamps - - # 5. Compute the calibration register - # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) - # Cal = 10240 (0x2800) - - self._cal_value = 10240 - - # 6. Calculate the power LSB - # PowerLSB = 20 * CurrentLSB - # PowerLSB = 0.0008 (800uW per bit) - self._power_lsb = 0.0008 - - # 7. Compute the maximum current and shunt voltage values before overflow - # - # Max_Current = Current_LSB * 32767 - # Max_Current = 1.31068A before overflow - # - # If Max_Current > Max_Possible_I then - # Max_Current_Before_Overflow = MaxPossible_I - # Else - # Max_Current_Before_Overflow = Max_Current - # End If - # - # ... In this case, we're good though since Max_Current is less than MaxPossible_I + # 1. Determine max possible bus voltage, 16 or 32 V + #self.bus_voltage_range = BusVoltageRange.RANGE_16V + self.bus_voltage_range = BusVoltageRange.RANGE_32V + + # 2. Determine the installed shunt resistor value + # By default, a 0.1 Ohm resistor is installed + RSHUNT = 0.1 # (Resistor value in ohms) + + # 2. Estimate the max expected current + # MaxExpected_I = 1 A + + # 3. Calculate maximum possible current for each gain value + # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.4 A + # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.8 A + # MaxI_gain4_160mV = 0.16 / RSHUNT = 1.6 A + # MaxI_gain8_320mV = 0.32 / RSHUNT = 3.2 A + + # 4. Evaluate whether to replace the shunt resistor # - # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT - # Max_ShuntVoltage = 0.131068V + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. # - # If Max_ShuntVoltage >= VSHUNT_MAX - # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX - # Else - # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage - # End If + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V + + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I + #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + + # 6. Select a calibration value + # Values below 4096 will harm the resolution + # + # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + self.calibration = 4096 + + # 6. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.1) = 0.0001 A + self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + + # 7. Calculate the power LSB in W + # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0001 = 0.002 (2 mW per bit) + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A # 8. Compute the Maximum Power - # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX - # MaximumPower = 1.31068 * 32V - # MaximumPower = 41.94176W - - # Set Calibration register to 'Cal' calculated above - self._raw_calibration = self._cal_value + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # MaximumPower = 32 V * MaxI_gain4_160mV = 32 V * 1.6 A = 51.2 W - # Set Config register to take into account the settings above - self.bus_voltage_range = BusVoltageRange.RANGE_32V - self.gain = Gain.DIV_8_320MV + # 9. Select the resolution + # Increasing the bits will increase the measurement time but will give better resolution + # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + + # 10. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one ended self.mode = Mode.SANDBVOLT_CONTINUOUS + # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + #self.mode = Mode.SANDBVOLT_TRIGGERED + # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter - overflow occurs at 1.6A. + """Configures to INA219 to be able to measure up to 16V and 400mA of current. .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" - # Calibration which uses the highest precision for - # current measurement (0.1mA), at the expense of - # only supporting 16V at 400mA max. - - # VBUS_MAX = 16V - # 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.00005 (50uA per bit) - self._current_lsb = 0.05 # in milliamps - - # 5. Compute the calibration register - # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) - # Cal = 8192 (0x2000) - - self._cal_value = 8192 - - # 6. Calculate the power LSB - # PowerLSB = 20 * CurrentLSB - # PowerLSB = 0.001 (1mW per bit) - self._power_lsb = 0.001 - - # 7. Compute the maximum current and shunt voltage values before overflow - # - # Max_Current = Current_LSB * 32767 - # Max_Current = 1.63835A 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 + # 1. Determine max possible bus voltage, 16 or 32 V + self.bus_voltage_range = BusVoltageRange.RANGE_16V + #self.bus_voltage_range = BusVoltageRange.RANGE_32V + + # 2. Determine the installed shunt resistor value + # By default, a 0.1 Ohm resistor is installed + RSHUNT = 0.1 # (Resistor value in ohms) + + # 2. Estimate the max expected current + # MaxExpected_I = 0.4 A + + # 3. Calculate maximum possible current for each gain value + # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.4 A + # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.8 A + # MaxI_gain4_160mV = 0.16 / RSHUNT = 1.6 A + # MaxI_gain8_320mV = 0.32 / RSHUNT = 3.2 A + + # 4. Evaluate whether to replace the shunt resistor # - # If Max_ShuntVoltage >= VSHUNT_MAX - # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX - # Else - # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage - # End If + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. # - # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX - # Max_ShuntVoltage_Before_Overflow = 0.04V + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V + + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I + self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + + # 6. Select a calibration value + # Values below 4096 will harm the resolution + # + # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + self.calibration = 4096 + + # 6. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.1) = 0.0001 A + self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + + # 7. Calculate the power LSB in W + # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0001 = 0.002 (2 mW per bit) + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A # 8. Compute the Maximum Power - # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX - # MaximumPower = 0.4 * 16V - # MaximumPower = 6.4W - - # Set Calibration register to 'Cal' calculated above - self._raw_calibration = self._cal_value + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # MaximumPower = 16 V * MaxI_gain1_40mV = 16 V * 0.4 A = 6.4 W - # Set Config register to take into account the settings above - self.bus_voltage_range = BusVoltageRange.RANGE_16V - self.gain = Gain.DIV_1_40MV + # 9. Select the resolution + # Increasing the bits will increase the measurement time but will give better resolution + # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + + # 10. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one ended self.mode = Mode.SANDBVOLT_CONTINUOUS + # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + #self.mode = Mode.SANDBVOLT_TRIGGERED + # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 16V and 5000mA of current. Counter - overflow occurs at 8.0A. + """Configures to INA219 to be able to measure up to 16V and 5000mA of current. Actual max current: 8 A. .. note:: These calculations assume a 0.02 ohm shunt resistor is present""" - # Calibration which uses the highest precision for - # current measurement (0.1mA), at the expense of - # only supporting 16V at 5000mA max. - - # VBUS_MAX = 16V - # VSHUNT_MAX = 0.16 (Assumes Gain 3, 160mV) - # RSHUNT = 0.02 (Resistor value in ohms) - - # 1. Determine max possible current - # MaxPossible_I = VSHUNT_MAX / RSHUNT - # MaxPossible_I = 8.0A - - # 2. Determine max expected current - # MaxExpected_I = 5.0A - - # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) - # MinimumLSB = MaxExpected_I/32767 - # MinimumLSB = 0.0001529 (uA per bit) - # MaximumLSB = MaxExpected_I/4096 - # MaximumLSB = 0.0012207 (uA per bit) - - # 4. Choose an LSB between the min and max values - # (Preferrably a roundish number close to MinLSB) - # CurrentLSB = 0.00016 (uA per bit) - self._current_lsb = 0.1524 # in milliamps - - # 5. Compute the calibration register - # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) - # Cal = 13434 (0x347a) - - self._cal_value = 13434 - - # 6. Calculate the power LSB - # PowerLSB = 20 * CurrentLSB - # PowerLSB = 0.003 (3.048mW per bit) - self._power_lsb = 0.003048 - - # 7. Compute the maximum current and shunt voltage values before overflow + # 1. Determine max possible bus voltage, 16 or 32 V + self.bus_voltage_range = BusVoltageRange.RANGE_16V + #self.bus_voltage_range = BusVoltageRange.RANGE_32V + + # 2. Determine the installed shunt resistor value + # By default, a 0.1 Ohm resistor is installed + RSHUNT = 0.02 # (Resistor value in ohms) + + # 2. Estimate the max expected current + # MaxExpected_I = 5 A + + # 3. Calculate maximum possible current for each gain value + # MaxI_gain1_40mV = 0.04 / RSHUNT = 2 A + # MaxI_gain2_80mV = 0.08 / RSHUNT = 4 A + # MaxI_gain4_160mV = 0.16 / RSHUNT = 8 A + # MaxI_gain8_320mV = 0.32 / RSHUNT = 16 A + + # 4. Evaluate whether to replace the shunt resistor # - # 8. Compute the Maximum Power + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. # + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V + + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I + #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + + # 6. Select a calibration value + # Values below 4096 will harm the resolution + # + # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + self.calibration = 4096 + + # 6. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.02) = 0.0005 A + self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + + # 7. Calculate the power LSB in W + # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0005 = 0.01 (10 mW per bit) + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A - # Set Calibration register to 'Cal' calcutated above - self._raw_calibration = self._cal_value + # 8. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # MaximumPower = 16 V * MaxI_gain4_160mV = 16 V * 8 A = 128 W - # Set Config register to take into account the settings above - self.bus_voltage_range = BusVoltageRange.RANGE_16V - self.gain = Gain.DIV_4_160MV + # 9. Select the resolution + # Increasing the bits will increase the measurement time but will give better resolution + # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + + # 10. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one ended self.mode = Mode.SANDBVOLT_CONTINUOUS + # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + #self.mode = Mode.SANDBVOLT_TRIGGERED + # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked From b5d858a33401f494922c2db0f20031103985089f Mon Sep 17 00:00:00 2001 From: Ilario Gelmetti Date: Tue, 15 Oct 2024 17:55:04 +0200 Subject: [PATCH 2/4] added small current calibration functions --- adafruit_ina219.py | 140 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 9cc895a..1d38118 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -511,3 +511,143 @@ def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + + def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name + """Configures to INA219 to be able to measure up to 16V and 80mA of current. + + .. note:: These calculations assume a 1 ohm shunt resistor is present""" + # 1. Determine max possible bus voltage, 16 or 32 V + self.bus_voltage_range = BusVoltageRange.RANGE_16V + #self.bus_voltage_range = BusVoltageRange.RANGE_32V + + # 2. Determine the installed shunt resistor value + # By default, a 0.1 Ohm resistor is installed + RSHUNT = 1 # (Resistor value in ohms) + + # 2. Estimate the max expected current + # MaxExpected_I = 0.08 A + + # 3. Calculate maximum possible current for each gain value + # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.04 A + # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.08 A + # MaxI_gain4_160mV = 0.16 / RSHUNT = 0.16 A + # MaxI_gain8_320mV = 0.32 / RSHUNT = 0.32 A + + # 4. Evaluate whether to replace the shunt resistor + # + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V + + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I + #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + + # 6. Select a calibration value + # Values below 4096 will harm the resolution + # + # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + self.calibration = 4096 + + # 6. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 1) = 0.00001 A + self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + + # 7. Calculate the power LSB in W + # Power_LSB = 20 * Current_LSB in A = 20 * 0.00001 = 0.0002 W (0.2 mW per bit) + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + + # 8. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # MaximumPower = 16 V * MaxI_gain2_80mV = 16 V * 0.08 A = 1.28 W + + # 9. Select the resolution + # Increasing the bits will increase the measurement time but will give better resolution + # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S + self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + + # 10. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one ended + self.mode = Mode.SANDBVOLT_CONTINUOUS + # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + #self.mode = Mode.SANDBVOLT_TRIGGERED + # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + + def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name + """Configures to INA219 to be able to measure up to 16V and 4mA of current. + + .. note:: These calculations assume a 10 ohm shunt resistor is present""" + # 1. Determine max possible bus voltage, 16 or 32 V + self.bus_voltage_range = BusVoltageRange.RANGE_16V + #self.bus_voltage_range = BusVoltageRange.RANGE_32V + + # 2. Determine the installed shunt resistor value + # By default, a 0.1 Ohm resistor is installed + RSHUNT = 10 # (Resistor value in ohms) + + # 2. Estimate the max expected current + # MaxExpected_I = 0.004 A + + # 3. Calculate maximum possible current for each gain value + # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.004 A + # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.008 A + # MaxI_gain4_160mV = 0.16 / RSHUNT = 0.016 A + # MaxI_gain8_320mV = 0.32 / RSHUNT = 0.032 A + + # 4. Evaluate whether to replace the shunt resistor + # + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V + + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I + self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + + # 6. Select a calibration value + # Values below 4096 will harm the resolution + # + # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + self.calibration = 4096 + + # 6. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 10) = 0.000001 A + self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + + # 7. Calculate the power LSB in W + # Power_LSB = 20 * Current_LSB in A = 20 * 0.000001 = 0.00002 W (0.02 mW per bit) + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + + # 8. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # MaximumPower = 16 V * MaxI_gain1_40mV = 16 V * 0.004 A = 0.064 W + + # 9. Select the resolution + # Increasing the bits will increase the measurement time but will give better resolution + # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S + self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S + + # 10. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one ended + self.mode = Mode.SANDBVOLT_CONTINUOUS + # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + #self.mode = Mode.SANDBVOLT_TRIGGERED + # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked From deb5417903488edba364ad9fee00f839460d54dc Mon Sep 17 00:00:00 2001 From: Ilario Gelmetti Date: Wed, 16 Oct 2024 14:25:08 +0200 Subject: [PATCH 3/4] make pylint happy again --- adafruit_ina219.py | 469 +++++++++++++++++++++++++++------------------ 1 file changed, 278 insertions(+), 191 deletions(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 1d38118..4eb1b6b 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -117,7 +117,7 @@ def _to_signed(num: int) -> int: return num -class INA219: +class INA219: # pylint: disable=too-many-instance-attributes """Driver for the INA219 current sensor""" # Basic API: @@ -232,36 +232,41 @@ def power(self) -> float: return self.raw_power * self._power_lsb def set_calibration_32V_2A(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 32V and 2A of current. Actual max current: 3.2 A. + """Configures to INA219 to be able to measure up to 32V and 2A of current. + Actual max current: 3.2 A. .. note:: These calculations assume a 0.1 shunt ohm resistor is present """ # 1. Determine max possible bus voltage, 16 or 32 V #self.bus_voltage_range = BusVoltageRange.RANGE_16V self.bus_voltage_range = BusVoltageRange.RANGE_32V - + # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed - RSHUNT = 0.1 # (Resistor value in ohms) - + rshunt = 0.1 # (Resistor value in ohms) + # 2. Estimate the max expected current # MaxExpected_I = 2 A # 3. Calculate maximum possible current for each gain value - # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.4 A - # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.8 A - # MaxI_gain4_160mV = 0.16 / RSHUNT = 1.6 A - # MaxI_gain8_320mV = 0.32 / RSHUNT = 3.2 A + # MaxI_gain1_40mV = 0.04 / rshunt = 0.4 A + # MaxI_gain2_80mV = 0.08 / rshunt = 0.8 A + # MaxI_gain4_160mV = 0.16 / rshunt = 1.6 A + # MaxI_gain8_320mV = 0.32 / rshunt = 3.2 A # 4. Evaluate whether to replace the shunt resistor # - # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. - # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt + # resistor and soldering another one with a higher resistance. # - # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. - # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. - # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V - + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller + # resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) + # of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can + # stand is 26 V + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV @@ -270,68 +275,83 @@ def set_calibration_32V_2A(self) -> None: # pylint: disable=invalid-name # 6. Select a calibration value # Values below 4096 will harm the resolution - # - # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) - # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) - # - # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + # + # Too high values will limit the maximum measurable current without any advantage (causing + # an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for + # gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a + # reliable current measured with a better equipment. self.calibration = 4096 - # 6. Calculate the current LSB (least significant bit) value in mA - # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.1) = 0.0001 A - self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + # 7. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * rshunt) = 0.04096 / (4096 * 0.1) = 0.0001 A + # "1000*" is for having the output in milliAmps + self._current_lsb = 1000 * 0.04096 / (self.calibration * rshunt) - # 7. Calculate the power LSB in W + # 8. Calculate the power LSB in W # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0001 = 0.002 (2 mW per bit) - self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + # "/1000" is for converting mA to A + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts - # 8. Compute the Maximum Power - # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # 9. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the + # chosen gain and resistor: # MaximumPower = 32 V * MaxI_gain8_320mV = 32 V * 3.2 A = 102.4 W - # 9. Select the resolution + # 10. Select the resolution # Increasing the bits will increase the measurement time but will give better resolution - # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + # Increasing the samples to be averaged will further increase the measurement time + # resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S - # 10. Select the operation mode - # With continuous mode, the a new reading will be performed as soon as the previous one ended + # 11. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one + # ended self.mode = Mode.SANDBVOLT_CONTINUOUS - # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + # With triggered mode, a new measurement is performed each time the triggered mode is + # configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED - # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + # In order to know if the triggered measurement is complete, the status of + # conversion_ready can be checked def set_calibration_32V_1A(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 32V and 1A of current. Actual max current: 1.6 A. + """Configures to INA219 to be able to measure up to 32V and 1A of current. + Actual max current: 1.6 A. .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V #self.bus_voltage_range = BusVoltageRange.RANGE_16V self.bus_voltage_range = BusVoltageRange.RANGE_32V - + # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed - RSHUNT = 0.1 # (Resistor value in ohms) - + rshunt = 0.1 # (Resistor value in ohms) + # 2. Estimate the max expected current # MaxExpected_I = 1 A # 3. Calculate maximum possible current for each gain value - # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.4 A - # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.8 A - # MaxI_gain4_160mV = 0.16 / RSHUNT = 1.6 A - # MaxI_gain8_320mV = 0.32 / RSHUNT = 3.2 A + # MaxI_gain1_40mV = 0.04 / rshunt = 0.4 A + # MaxI_gain2_80mV = 0.08 / rshunt = 0.8 A + # MaxI_gain4_160mV = 0.16 / rshunt = 1.6 A + # MaxI_gain8_320mV = 0.32 / rshunt = 3.2 A # 4. Evaluate whether to replace the shunt resistor # - # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. - # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt + # resistor and soldering another one with a higher resistance. # - # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. - # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. - # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V - + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller + # resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) + # of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can + # stand is 26 V + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV @@ -340,37 +360,47 @@ def set_calibration_32V_1A(self) -> None: # pylint: disable=invalid-name # 6. Select a calibration value # Values below 4096 will harm the resolution - # - # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) - # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) - # - # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + # + # Too high values will limit the maximum measurable current without any advantage (causing + # an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for + # gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a + # reliable current measured with a better equipment. self.calibration = 4096 - # 6. Calculate the current LSB (least significant bit) value in mA - # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.1) = 0.0001 A - self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + # 7. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * rshunt) = 0.04096 / (4096 * 0.1) = 0.0001 A + # "1000*" is for having the output in milliAmps + self._current_lsb = 1000 * 0.04096 / (self.calibration * rshunt) - # 7. Calculate the power LSB in W + # 8. Calculate the power LSB in W # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0001 = 0.002 (2 mW per bit) - self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + # "/1000" is for converting mA to A + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts - # 8. Compute the Maximum Power - # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # 9. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the + # chosen gain and resistor: # MaximumPower = 32 V * MaxI_gain4_160mV = 32 V * 1.6 A = 51.2 W - # 9. Select the resolution + # 10. Select the resolution # Increasing the bits will increase the measurement time but will give better resolution - # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + # Increasing the samples to be averaged will further increase the measurement time + # resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S - # 10. Select the operation mode - # With continuous mode, the a new reading will be performed as soon as the previous one ended + # 11. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one + # ended self.mode = Mode.SANDBVOLT_CONTINUOUS - # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + # With triggered mode, a new measurement is performed each time the triggered mode is + # configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED - # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + # In order to know if the triggered measurement is complete, the status of + # conversion_ready can be checked def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name """Configures to INA219 to be able to measure up to 16V and 400mA of current. @@ -379,29 +409,33 @@ def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V #self.bus_voltage_range = BusVoltageRange.RANGE_32V - + # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed - RSHUNT = 0.1 # (Resistor value in ohms) - + rshunt = 0.1 # (Resistor value in ohms) + # 2. Estimate the max expected current # MaxExpected_I = 0.4 A # 3. Calculate maximum possible current for each gain value - # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.4 A - # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.8 A - # MaxI_gain4_160mV = 0.16 / RSHUNT = 1.6 A - # MaxI_gain8_320mV = 0.32 / RSHUNT = 3.2 A + # MaxI_gain1_40mV = 0.04 / rshunt = 0.4 A + # MaxI_gain2_80mV = 0.08 / rshunt = 0.8 A + # MaxI_gain4_160mV = 0.16 / rshunt = 1.6 A + # MaxI_gain8_320mV = 0.32 / rshunt = 3.2 A # 4. Evaluate whether to replace the shunt resistor # - # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. - # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt + # resistor and soldering another one with a higher resistance. # - # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. - # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. - # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V - + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller + # resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) + # of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can + # stand is 26 V + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV @@ -410,68 +444,83 @@ def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name # 6. Select a calibration value # Values below 4096 will harm the resolution - # - # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) - # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) - # - # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + # + # Too high values will limit the maximum measurable current without any advantage (causing + # an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for + # gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a + # reliable current measured with a better equipment. self.calibration = 4096 - # 6. Calculate the current LSB (least significant bit) value in mA - # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.1) = 0.0001 A - self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + # 7. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * rshunt) = 0.04096 / (4096 * 0.1) = 0.0001 A + # "1000*" is for having the output in milliAmps + self._current_lsb = 1000 * 0.04096 / (self.calibration * rshunt) - # 7. Calculate the power LSB in W + # 8. Calculate the power LSB in W # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0001 = 0.002 (2 mW per bit) - self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + # "/1000" is for converting mA to A + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts - # 8. Compute the Maximum Power - # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # 9. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for + # the chosen gain and resistor: # MaximumPower = 16 V * MaxI_gain1_40mV = 16 V * 0.4 A = 6.4 W - # 9. Select the resolution + # 10. Select the resolution # Increasing the bits will increase the measurement time but will give better resolution - # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + # Increasing the samples to be averaged will further increase the measurement time + # resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S - # 10. Select the operation mode - # With continuous mode, the a new reading will be performed as soon as the previous one ended + # 11. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one + # ended self.mode = Mode.SANDBVOLT_CONTINUOUS - # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + # With triggered mode, a new measurement is performed each time the triggered mode is + # configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED - # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + # In order to know if the triggered measurement is complete, the status of + # conversion_ready can be checked def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name - """Configures to INA219 to be able to measure up to 16V and 5000mA of current. Actual max current: 8 A. + """Configures to INA219 to be able to measure up to 16V and 5000mA of current. + Actual max current: 8 A. .. note:: These calculations assume a 0.02 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V #self.bus_voltage_range = BusVoltageRange.RANGE_32V - + # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed - RSHUNT = 0.02 # (Resistor value in ohms) - + rshunt = 0.02 # (Resistor value in ohms) + # 2. Estimate the max expected current # MaxExpected_I = 5 A # 3. Calculate maximum possible current for each gain value - # MaxI_gain1_40mV = 0.04 / RSHUNT = 2 A - # MaxI_gain2_80mV = 0.08 / RSHUNT = 4 A - # MaxI_gain4_160mV = 0.16 / RSHUNT = 8 A - # MaxI_gain8_320mV = 0.32 / RSHUNT = 16 A + # MaxI_gain1_40mV = 0.04 / rshunt = 2 A + # MaxI_gain2_80mV = 0.08 / rshunt = 4 A + # MaxI_gain4_160mV = 0.16 / rshunt = 8 A + # MaxI_gain8_320mV = 0.32 / rshunt = 16 A # 4. Evaluate whether to replace the shunt resistor # - # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. - # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt + # resistor and soldering another one with a higher resistance. # - # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. - # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. - # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V - + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller + # resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) + # of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can + # stand is 26 V + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV @@ -480,37 +529,47 @@ def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name # 6. Select a calibration value # Values below 4096 will harm the resolution - # - # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) - # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) - # - # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + # + # Too high values will limit the maximum measurable current without any advantage (causing + # an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for + # gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a + # reliable current measured with a better equipment. self.calibration = 4096 - # 6. Calculate the current LSB (least significant bit) value in mA - # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 0.02) = 0.0005 A - self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + # 7. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * rshunt) = 0.04096 / (4096 * 0.02) = 0.0005 A + # "1000*" is for having the output in milliAmps + self._current_lsb = 1000 * 0.04096 / (self.calibration * rshunt) - # 7. Calculate the power LSB in W + # 8. Calculate the power LSB in W # Power_LSB = 20 * CurrentLSB in A = 20 * 0.0005 = 0.01 (10 mW per bit) - self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + # "/1000" is for converting mA to A + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts - # 8. Compute the Maximum Power - # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # 9. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for + # the chosen gain and resistor: # MaximumPower = 16 V * MaxI_gain4_160mV = 16 V * 8 A = 128 W - # 9. Select the resolution + # 10. Select the resolution # Increasing the bits will increase the measurement time but will give better resolution - # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + # Increasing the samples to be averaged will further increase the measurement time + # resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S - # 10. Select the operation mode - # With continuous mode, the a new reading will be performed as soon as the previous one ended + # 11. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one + # ended self.mode = Mode.SANDBVOLT_CONTINUOUS - # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + # With triggered mode, a new measurement is performed each time the triggered mode is + # configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED - # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + # In order to know if the triggered measurement is complete, the status of + # conversion_ready can be checked def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name """Configures to INA219 to be able to measure up to 16V and 80mA of current. @@ -519,29 +578,33 @@ def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V #self.bus_voltage_range = BusVoltageRange.RANGE_32V - + # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed - RSHUNT = 1 # (Resistor value in ohms) - + rshunt = 1 # (Resistor value in ohms) + # 2. Estimate the max expected current # MaxExpected_I = 0.08 A # 3. Calculate maximum possible current for each gain value - # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.04 A - # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.08 A - # MaxI_gain4_160mV = 0.16 / RSHUNT = 0.16 A - # MaxI_gain8_320mV = 0.32 / RSHUNT = 0.32 A + # MaxI_gain1_40mV = 0.04 / rshunt = 0.04 A + # MaxI_gain2_80mV = 0.08 / rshunt = 0.08 A + # MaxI_gain4_160mV = 0.16 / rshunt = 0.16 A + # MaxI_gain8_320mV = 0.32 / rshunt = 0.32 A # 4. Evaluate whether to replace the shunt resistor # - # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. - # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt + # resistor and soldering another one with a higher resistance. # - # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. - # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. - # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V - + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller + # resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) + # of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can + # stand is 26 V + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV @@ -550,38 +613,48 @@ def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name # 6. Select a calibration value # Values below 4096 will harm the resolution - # - # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) - # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) - # - # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + # + # Too high values will limit the maximum measurable current without any advantage (causing + # an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for + # gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a + # reliable current measured with a better equipment. self.calibration = 4096 - # 6. Calculate the current LSB (least significant bit) value in mA - # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 1) = 0.00001 A - self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + # 7. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * rshunt) = 0.04096 / (4096 * 1) = 0.00001 A + # "1000*" is for having the output in milliAmps + self._current_lsb = 1000 * 0.04096 / (self.calibration * rshunt) - # 7. Calculate the power LSB in W + # 8. Calculate the power LSB in W # Power_LSB = 20 * Current_LSB in A = 20 * 0.00001 = 0.0002 W (0.2 mW per bit) - self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + # "/1000" is for converting mA to A + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts - # 8. Compute the Maximum Power - # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # 9. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for + # the chosen gain and resistor: # MaximumPower = 16 V * MaxI_gain2_80mV = 16 V * 0.08 A = 1.28 W - # 9. Select the resolution + # 10. Select the resolution # Increasing the bits will increase the measurement time but will give better resolution - # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + # Increasing the samples to be averaged will further increase the measurement time + # resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S - # 10. Select the operation mode - # With continuous mode, the a new reading will be performed as soon as the previous one ended + # 11. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one + # ended self.mode = Mode.SANDBVOLT_CONTINUOUS - # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + # With triggered mode, a new measurement is performed each time the triggered mode is + # configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED - # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked - + # In order to know if the triggered measurement is complete, the status of + # conversion_ready can be checked + def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name """Configures to INA219 to be able to measure up to 16V and 4mA of current. @@ -589,29 +662,33 @@ def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V #self.bus_voltage_range = BusVoltageRange.RANGE_32V - + # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed - RSHUNT = 10 # (Resistor value in ohms) - + rshunt = 10 # (Resistor value in ohms) + # 2. Estimate the max expected current # MaxExpected_I = 0.004 A # 3. Calculate maximum possible current for each gain value - # MaxI_gain1_40mV = 0.04 / RSHUNT = 0.004 A - # MaxI_gain2_80mV = 0.08 / RSHUNT = 0.008 A - # MaxI_gain4_160mV = 0.16 / RSHUNT = 0.016 A - # MaxI_gain8_320mV = 0.32 / RSHUNT = 0.032 A + # MaxI_gain1_40mV = 0.04 / rshunt = 0.004 A + # MaxI_gain2_80mV = 0.08 / rshunt = 0.008 A + # MaxI_gain4_160mV = 0.16 / rshunt = 0.016 A + # MaxI_gain8_320mV = 0.32 / rshunt = 0.032 A # 4. Evaluate whether to replace the shunt resistor # - # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. - # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt resistor and soldering another one with a higher resistance. + # If MaxExpected_I << MaxI_gain1_40mV, expect poor resolution. + # If a good resolution is important for you, consider de-soldering the 0.1 Ohm shunt + # resistor and soldering another one with a higher resistance. # - # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller resistance. - # Either replacing the one currently in place or soldering another one on top (in parallel) of the current one. - # Remember that the maximum voltage across the shunt resistor that the INA219 chip can stand is 26 V - + # If MaxExpected_I > MaxI_gain8_320mV, consider soldering a shunt resistor with a smaller + # resistance. + # Either replacing the one currently in place or soldering another one on top (in parallel) + # of the current one. + # Remember that the maximum voltage across the shunt resistor that the INA219 chip can + # stand is 26 V + # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV @@ -620,34 +697,44 @@ def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name # 6. Select a calibration value # Values below 4096 will harm the resolution - # - # Too high values will limit the maximum measurable current without any advantage (causing an overflow to happen earlier) - # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for gain 8) - # - # Use a value different from 4096 only if you are actually calibrating the board versus a reliable current measured with a better equipment. + # + # Too high values will limit the maximum measurable current without any advantage (causing + # an overflow to happen earlier) + # (above 32768 for gain 1, above 16384 for gain 2, above 8192 for gain 4, above 4096 for + # gain 8) + # + # Use a value different from 4096 only if you are actually calibrating the board versus a + # reliable current measured with a better equipment. self.calibration = 4096 - # 6. Calculate the current LSB (least significant bit) value in mA - # Current_LSB = 0.04096 / (calibration * RSHUNT) = 0.04096 / (4096 * 10) = 0.000001 A - self._current_lsb = 1000 * 0.04096 / (self.calibration * RSHUNT) # the "1000 *" is for having the output in milliAmps + # 7. Calculate the current LSB (least significant bit) value in mA + # Current_LSB = 0.04096 / (calibration * rshunt) = 0.04096 / (4096 * 10) = 0.000001 A + # "1000*" is for having the output in milliAmps + self._current_lsb = 1000 * 0.04096 / (self.calibration * rshunt) - # 7. Calculate the power LSB in W + # 8. Calculate the power LSB in W # Power_LSB = 20 * Current_LSB in A = 20 * 0.000001 = 0.00002 W (0.02 mW per bit) - self._power_lsb = 20 * self._current_lsb / 1000 # in Watts. The "/ 1000" is for converting mA to A + # "/1000" is for converting mA to A + self._power_lsb = 20 * self._current_lsb / 1000 # in Watts - # 8. Compute the Maximum Power - # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for the chosen gain and resistor: + # 9. Compute the Maximum Power + # Multiplying the maximum possible bus voltage (16 or 32 V) by the maximum current for + # the chosen gain and resistor: # MaximumPower = 16 V * MaxI_gain1_40mV = 16 V * 0.004 A = 0.064 W - # 9. Select the resolution + # 10. Select the resolution # Increasing the bits will increase the measurement time but will give better resolution - # Increasing the samples to be averaged will further increase the measurement time resulting in less noisy measurements + # Increasing the samples to be averaged will further increase the measurement time + # resulting in less noisy measurements self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S - # 10. Select the operation mode - # With continuous mode, the a new reading will be performed as soon as the previous one ended + # 11. Select the operation mode + # With continuous mode, the a new reading will be performed as soon as the previous one + # ended self.mode = Mode.SANDBVOLT_CONTINUOUS - # With triggered mode, a new measurement is performed each time the triggered mode is configured (the following line works both as configuration and as trigger) + # With triggered mode, a new measurement is performed each time the triggered mode is + # configured (the following line works both as configuration and as trigger) #self.mode = Mode.SANDBVOLT_TRIGGERED - # In order to know if the triggered measurement is complete, the status of conversion_ready can be checked + # In order to know if the triggered measurement is complete, the status of + # conversion_ready can be checked From f7bfdc5e9f39c7e450f0414d84f359aa7c2ddb9b Mon Sep 17 00:00:00 2001 From: Ilario Gelmetti Date: Wed, 16 Oct 2024 14:31:53 +0200 Subject: [PATCH 4/4] reformat with Black --- adafruit_ina219.py | 82 +++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/adafruit_ina219.py b/adafruit_ina219.py index 4eb1b6b..0f52419 100644 --- a/adafruit_ina219.py +++ b/adafruit_ina219.py @@ -117,7 +117,7 @@ def _to_signed(num: int) -> int: return num -class INA219: # pylint: disable=too-many-instance-attributes +class INA219: # pylint: disable=too-many-instance-attributes """Driver for the INA219 current sensor""" # Basic API: @@ -238,7 +238,7 @@ def set_calibration_32V_2A(self) -> None: # pylint: disable=invalid-name .. note:: These calculations assume a 0.1 shunt ohm resistor is present """ # 1. Determine max possible bus voltage, 16 or 32 V - #self.bus_voltage_range = BusVoltageRange.RANGE_16V + # self.bus_voltage_range = BusVoltageRange.RANGE_16V self.bus_voltage_range = BusVoltageRange.RANGE_32V # 2. Determine the installed shunt resistor value @@ -268,10 +268,12 @@ def set_calibration_32V_2A(self) -> None: # pylint: disable=invalid-name # stand is 26 V # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I - #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV - #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV - #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV - self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + # self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + # self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + # self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + self.gain = ( + Gain.DIV_8_320MV + ) # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV # 6. Select a calibration value # Values below 4096 will harm the resolution @@ -313,7 +315,7 @@ def set_calibration_32V_2A(self) -> None: # pylint: disable=invalid-name self.mode = Mode.SANDBVOLT_CONTINUOUS # With triggered mode, a new measurement is performed each time the triggered mode is # configured (the following line works both as configuration and as trigger) - #self.mode = Mode.SANDBVOLT_TRIGGERED + # self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of # conversion_ready can be checked @@ -323,7 +325,7 @@ def set_calibration_32V_1A(self) -> None: # pylint: disable=invalid-name .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V - #self.bus_voltage_range = BusVoltageRange.RANGE_16V + # self.bus_voltage_range = BusVoltageRange.RANGE_16V self.bus_voltage_range = BusVoltageRange.RANGE_32V # 2. Determine the installed shunt resistor value @@ -353,10 +355,12 @@ def set_calibration_32V_1A(self) -> None: # pylint: disable=invalid-name # stand is 26 V # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I - #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV - #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV - self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV - #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + # self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + # self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + self.gain = ( + Gain.DIV_4_160MV + ) # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + # self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV # 6. Select a calibration value # Values below 4096 will harm the resolution @@ -398,7 +402,7 @@ def set_calibration_32V_1A(self) -> None: # pylint: disable=invalid-name self.mode = Mode.SANDBVOLT_CONTINUOUS # With triggered mode, a new measurement is performed each time the triggered mode is # configured (the following line works both as configuration and as trigger) - #self.mode = Mode.SANDBVOLT_TRIGGERED + # self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of # conversion_ready can be checked @@ -408,7 +412,7 @@ def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V - #self.bus_voltage_range = BusVoltageRange.RANGE_32V + # self.bus_voltage_range = BusVoltageRange.RANGE_32V # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed @@ -437,10 +441,10 @@ def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name # stand is 26 V # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I - self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV - #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV - #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV - #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + # self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + # self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + # self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV # 6. Select a calibration value # Values below 4096 will harm the resolution @@ -482,7 +486,7 @@ def set_calibration_16V_400mA(self) -> None: # pylint: disable=invalid-name self.mode = Mode.SANDBVOLT_CONTINUOUS # With triggered mode, a new measurement is performed each time the triggered mode is # configured (the following line works both as configuration and as trigger) - #self.mode = Mode.SANDBVOLT_TRIGGERED + # self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of # conversion_ready can be checked @@ -493,7 +497,7 @@ def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name .. note:: These calculations assume a 0.02 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V - #self.bus_voltage_range = BusVoltageRange.RANGE_32V + # self.bus_voltage_range = BusVoltageRange.RANGE_32V # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed @@ -522,10 +526,12 @@ def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name # stand is 26 V # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I - #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV - #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV - self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV - #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + # self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + # self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + self.gain = ( + Gain.DIV_4_160MV + ) # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + # self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV # 6. Select a calibration value # Values below 4096 will harm the resolution @@ -567,7 +573,7 @@ def set_calibration_16V_5A(self) -> None: # pylint: disable=invalid-name self.mode = Mode.SANDBVOLT_CONTINUOUS # With triggered mode, a new measurement is performed each time the triggered mode is # configured (the following line works both as configuration and as trigger) - #self.mode = Mode.SANDBVOLT_TRIGGERED + # self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of # conversion_ready can be checked @@ -577,7 +583,7 @@ def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name .. note:: These calculations assume a 1 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V - #self.bus_voltage_range = BusVoltageRange.RANGE_32V + # self.bus_voltage_range = BusVoltageRange.RANGE_32V # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed @@ -606,10 +612,12 @@ def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name # stand is 26 V # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I - #self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV - self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV - #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV - #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + # self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + self.gain = ( + Gain.DIV_2_80MV + ) # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + # self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + # self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV # 6. Select a calibration value # Values below 4096 will harm the resolution @@ -651,7 +659,7 @@ def set_calibration_16V_80mA(self) -> None: # pylint: disable=invalid-name self.mode = Mode.SANDBVOLT_CONTINUOUS # With triggered mode, a new measurement is performed each time the triggered mode is # configured (the following line works both as configuration and as trigger) - #self.mode = Mode.SANDBVOLT_TRIGGERED + # self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of # conversion_ready can be checked @@ -661,7 +669,7 @@ def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name .. note:: These calculations assume a 10 ohm shunt resistor is present""" # 1. Determine max possible bus voltage, 16 or 32 V self.bus_voltage_range = BusVoltageRange.RANGE_16V - #self.bus_voltage_range = BusVoltageRange.RANGE_32V + # self.bus_voltage_range = BusVoltageRange.RANGE_32V # 2. Determine the installed shunt resistor value # By default, a 0.1 Ohm resistor is installed @@ -690,10 +698,10 @@ def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name # stand is 26 V # 5. Select a gain for which MaxI_gainX_XXmV > MaxExpected_I - self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV - #self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV - #self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV - #self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV + self.gain = Gain.DIV_1_40MV # For 0 < MaxExpected_I < MaxI_gain1_40mV + # self.gain = Gain.DIV_2_80MV # For MaxI_gain1_40mV < MaxExpected_I < MaxI_gain2_80mV + # self.gain = Gain.DIV_4_160MV # For MaxI_gain2_80mV < MaxExpected_I < MaxI_gain4_160mV + # self.gain = Gain.DIV_8_320MV # For MaxI_gain4_160mV < MaxExpected_I < MaxI_gain8_320mV # 6. Select a calibration value # Values below 4096 will harm the resolution @@ -735,6 +743,6 @@ def set_calibration_16V_4mA(self) -> None: # pylint: disable=invalid-name self.mode = Mode.SANDBVOLT_CONTINUOUS # With triggered mode, a new measurement is performed each time the triggered mode is # configured (the following line works both as configuration and as trigger) - #self.mode = Mode.SANDBVOLT_TRIGGERED + # self.mode = Mode.SANDBVOLT_TRIGGERED # In order to know if the triggered measurement is complete, the status of # conversion_ready can be checked