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