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