/ examples / ina219_simpletest.py
ina219_simpletest.py
 1  """Sample code and test for adafruit_ina219"""
 2  
 3  import time
 4  import board
 5  from adafruit_ina219 import ADCResolution, BusVoltageRange, INA219
 6  
 7  
 8  i2c_bus = board.I2C()
 9  
10  ina219 = INA219(i2c_bus)
11  
12  print("ina219 test")
13  
14  # display some of the advanced field (just to test)
15  print("Config register:")
16  print("  bus_voltage_range:    0x%1X" % ina219.bus_voltage_range)
17  print("  gain:                 0x%1X" % ina219.gain)
18  print("  bus_adc_resolution:   0x%1X" % ina219.bus_adc_resolution)
19  print("  shunt_adc_resolution: 0x%1X" % ina219.shunt_adc_resolution)
20  print("  mode:                 0x%1X" % ina219.mode)
21  print("")
22  
23  # optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage
24  ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S
25  ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S
26  # optional : change voltage range to 16V
27  ina219.bus_voltage_range = BusVoltageRange.RANGE_16V
28  
29  # measure and display loop
30  while True:
31      bus_voltage = ina219.bus_voltage  # voltage on V- (load side)
32      shunt_voltage = ina219.shunt_voltage  # voltage between V+ and V- across the shunt
33      current = ina219.current  # current in mA
34  
35      # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage
36      print("PSU Voltage:   {:6.3f} V".format(bus_voltage + shunt_voltage))
37      print("Shunt Voltage: {:9.6f} V".format(shunt_voltage))
38      print("Load Voltage:  {:6.3f} V".format(bus_voltage))
39      print("Current:       {:9.6f} A".format(current / 1000))
40      print("")
41  
42      time.sleep(2)