Simple test
Ensure your device works with this simple test.
examples/ina219_simpletest.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4"""Sample code and test for adafruit_ina219"""
5
6import time
7
8import board
9
10from adafruit_ina219 import INA219, ADCResolution, BusVoltageRange
11
12i2c_bus = board.I2C() # uses board.SCL and board.SDA
13# i2c_bus = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
14
15ina219 = INA219(i2c_bus)
16
17print("ina219 test")
18
19# display some of the advanced field (just to test)
20print("Config register:")
21print(f" bus_voltage_range: 0x{ina219.bus_voltage_range:1X}")
22print(f" gain: 0x{ina219.gain:1X}")
23print(f" bus_adc_resolution: 0x{ina219.bus_adc_resolution:1X}")
24print(f" shunt_adc_resolution: 0x{ina219.shunt_adc_resolution:1X}")
25print(f" mode: 0x{ina219.mode:1X}")
26print("")
27
28# optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage
29ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S
30ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S
31# optional : change voltage range to 16V
32ina219.bus_voltage_range = BusVoltageRange.RANGE_16V
33
34# measure and display loop
35while True:
36 bus_voltage = ina219.bus_voltage # voltage on V- (load side)
37 shunt_voltage = ina219.shunt_voltage # voltage between V+ and V- across the shunt
38 current = ina219.current # current in mA
39 power = ina219.power # power in watts
40
41 # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage
42 print(f"Voltage (VIN+) : {bus_voltage + shunt_voltage:6.3f} V")
43 print(f"Voltage (VIN-) : {bus_voltage:6.3f} V")
44 print(f"Shunt Voltage : {shunt_voltage:8.5f} V")
45 print(f"Shunt Current : {current / 1000:7.4f} A")
46 print(f"Power Calc. : {bus_voltage * (current / 1000):8.5f} W")
47 print(f"Power Register : {power:6.3f} W")
48 print("")
49
50 # Check internal calculations haven't overflowed (doesn't detect ADC overflows)
51 if ina219.overflow:
52 print("Internal Math Overflow Detected!")
53 print("")
54
55 time.sleep(2)