Outils pour utilisateurs

Outils du site


informatique:projets:python-ina219

Python INA219

python-ina219 est une classe d'abstraction permettant de communiquer avec un INA219 via le bus i2c (d'un Raspberry Pi par exemple).

Le INA219 est un capteur de courant (high side) et de tension fabriqué par Texas Instruments.

Cette classe possède un mode 'debug' qui permet d'obtenir un certain nombre de messages utiles (communication avec le circuit, résultat de mesures, etc…).

Une version pour MicroPython est également disponible (uina219). Cette version reprend le même code que la version classique mais sans les commentaires et options de debug afin d'être la plus légère possible.

INA219 Datasheet

Documentation

L'ensemble du code est commenté de manière suffisamment explicite, et le code est compatible PyDOC. Un simple 'help(ina219.ina219)' devrait permettre d'avoir un minimum d'informations sur la syntaxe.

Un programme de test est fourni à titre d'exemple et permet de voir le fonctionnement de la classe.

test_ina219.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
# Test program to illustrate INA219 class usage
 
import ina219
 
if __name__ == "__main__":
 
  # Connecting to INA219 in Debug mode
  chip = ina219.ina219(debug=True)
 
  # Perform a soft reset on chip
  chip.SoftReset()
 
  # Retrieve bus voltage
  chip.GetBusVoltage()
 
  # Retrieve shunt voltage
  chip.GetShuntVoltage()
 
  # Calibrate chip with an expected max current of 3.2A
  # and a shunt resistor or 0.1ohms
  chip.Calibrate(r_shunt=0.1, i_max=3.2)
 
  # Force INA219 to trigger a new measurement
  # (if configured in triggered mode)
  chip.TriggerMeasurement()
 
  # Retrieve calculated current
  chip.GetCurrent()
 
  # Retrieve calculated power
  chip.GetPower()
 
  # Configure chip using full parameters
  conf = chip.CONF_BRNG_32V | chip.CONF_PGA_DIV8 | chip.CONF_BADC_12BIT | \
         chip.CONF_SADC_12BIT | chip.CONF_MODE_SHUNT_BUS_CONT
  chip.Configure(config=conf)
 
  # Configure chip by changing a single value
  # -> Set bus voltage range to 16V
  chip.Configure(single_value=chip.CONF_BRNG_16V, mask=chip.CONF_BRNG_MASK)
Résultat
[DEBUG] Accessing INA219 at address 0x40 on bus 1
[DEBUG] Reading configuration from INA219
[DEBUG] > Reading 2 bytes from register 0x0
[DEBUG] > INA219 response : [0x19 0x9F]
 
[DEBUG] Performing a soft reset of the INA219
[DEBUG] > Writing 2 bytes to register 0x0 [0x80 0x00]
 
[DEBUG] Reading Bus Voltage register
[DEBUG] > Reading 2 bytes from register 0x2
[DEBUG] > INA219 response : [0x21 0xDA]
[DEBUG] Measured Bus voltage is 4332mV (OVF=0 CNVR=1)
 
[DEBUG] Reading Shunt Voltage register
[DEBUG] > Reading 2 bytes from register 0x1
[DEBUG] > INA219 response : [0xF7 0x3D]
[DEBUG] Measured Shunt voltage is -2.243mV (resolution 16 bits)
 
[DEBUG] Calibrating INA219 with Imax=3.2A RShunt=0.1Ω
[DEBUG] > Writing 2 bytes to register 0x5 [0x20 0xC4]
 
[DEBUG] Triggering a new measurement
[DEBUG] Reading configuration from INA219
[DEBUG] > Reading 2 bytes from register 0x0
[DEBUG] > INA219 response : [0x39 0x9F]
[DEBUG] INA219 not configured using a triggered mode, skipping...
 
[DEBUG] Reading Current register
[DEBUG] > Reading 2 bytes from register 0x4
[DEBUG] > INA219 response : [0xED 0x92]
[DEBUG] Measured current is -460.7421875mA
 
[DEBUG] Reading Power register
[DEBUG] > Reading 2 bytes from register 0x3
[DEBUG] > INA219 response : [0x03 0xAC]
[DEBUG] Measured power is 1835.9375mW
 
[DEBUG] Configuring INA219 using full parameters
[DEBUG] > Writing 2 bytes to register 0x0 [0x39 0x9F]
 
[DEBUG] Updating single value on INA219 configuration
[DEBUG] Reading configuration from INA219
[DEBUG] > Reading 2 bytes from register 0x0
[DEBUG] > INA219 response : [0x39 0x9F]
[DEBUG] > Writing 2 bytes to register 0x0 [0x19 0x9F]
test_uina219.py
# Test program to illustrate uINA219 class usage
 
import uina219
import machine
 
# Example using an ESP8266 (ESP-01)
i2c = machine.I2C(freq=400000, scl=machine.Pin(0),sda=machine.Pin(2))
 
# Connecting to INA219
chip = uina219.uina219(i2c)
 
# Perform a soft reset on chip
chip.SoftReset()
 
# Retrieve bus voltage
chip.GetBusVoltage()
 
# Retrieve shunt voltage
chip.GetShuntVoltage()
 
# Calibrate chip with an expected max current of 3.2A
# and a shunt resistor or 0.1ohms
chip.Calibrate(r_shunt=0.1, i_max=3.2)
 
# Force INA219 to trigger a new measurement
# (if configured in triggered mode)
chip.TriggerMeasurement()
 
# Retrieve calculated current
chip.GetCurrent()
 
# Retrieve calculated power
chip.GetPower()
 
# Configure chip using full parameters
conf = chip.CONF_BRNG_32V | chip.CONF_PGA_DIV8 | chip.CONF_BADC_12BIT | \
       chip.CONF_SADC_12BIT | chip.CONF_MODE_SHUNT_BUS_CONT
chip.Configure(config=conf)
 
# Configure chip by changing a single value
# -> Set bus voltage range to 16V
chip.Configure(single_value=chip.CONF_BRNG_16V, mask=chip.CONF_BRNG_MASK)

Téléchargement

Changelog

informatique/projets/python-ina219.txt · Dernière modification : 2022/11/27 17:21 de lestat

Outils de la page