All Projects → chrisb2 → pyb_ina219

chrisb2 / pyb_ina219

Licence: MIT license
This library for MicroPython makes it easy to leverage the complex functionality of the Texas Instruments INA219 sensor to measure voltage, current and power.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to pyb ina219

MicroPython Examples
MicroPython Examples For 01Studio Development Board
Stars: ✭ 86 (+138.89%)
Mutual labels:  esp32, pyboard
esp8266-upy
MicroPython Cross-platform Drivers - collection of code, wiring and sample for various breakout boards - Works with ESP8266, Pyboard, PYB405, Wemos, etc))
Stars: ✭ 36 (+0%)
Mutual labels:  esp32, pyboard
micropython-i2s-examples
Examples for I2S support on microcontrollers that run MicroPython
Stars: ✭ 40 (+11.11%)
Mutual labels:  esp32, pyboard
Homekit
Homekit for ESP32 with Arduino framework
Stars: ✭ 80 (+122.22%)
Mutual labels:  esp32
IMU-VR-Full-Body-Tracker
Inertial Measurement Unit (IMU) based full body tracker for Steam VR.
Stars: ✭ 46 (+27.78%)
Mutual labels:  esp32
esp32-transpiler
Transpile Golang into Arduino code to use fully automated testing at your IoT projects.
Stars: ✭ 53 (+47.22%)
Mutual labels:  esp32
esphome-flasher
Simple GUI tool to flash ESPs over USB
Stars: ✭ 619 (+1619.44%)
Mutual labels:  esp32
esp32-ota
ESP32 OTA based on ThingsBoard Open-source IoT Platform
Stars: ✭ 45 (+25%)
Mutual labels:  esp32
uPyCam
Take a photo with an ESP32-CAM running MicroPython
Stars: ✭ 97 (+169.44%)
Mutual labels:  esp32
opcua-esp32
Embedded OPC UA Server on ESP32 based on open62541 stack
Stars: ✭ 82 (+127.78%)
Mutual labels:  esp32
canairio sensorlib
Particle sensor manager for multiple sensors: Honeywell, Plantower, Panasonic, Sensirion, etc. This is sensors layer of CanAirIO project too.
Stars: ✭ 24 (-33.33%)
Mutual labels:  esp32
ESP-Alerts-for-Android
Send Android Notifications to an ESP32 with OLED display
Stars: ✭ 42 (+16.67%)
Mutual labels:  esp32
pikascript
Ultralightweight Python engine that can run with 4KB of RAM and 32KB of Flash (such as STM32G030C8 and STM32F103C8), and is very easy to deploy and expand.
Stars: ✭ 855 (+2275%)
Mutual labels:  esp32
owlos
DIY Open Source OS for building IoT ecosystems
Stars: ✭ 43 (+19.44%)
Mutual labels:  esp32
nestronic
Nestronic Game Music Synthesizer Alarm Clock
Stars: ✭ 24 (-33.33%)
Mutual labels:  esp32
idf-installer
ESP IDF Windows Installer
Stars: ✭ 56 (+55.56%)
Mutual labels:  esp32
stack-chan
A JavaScript-driven M5Stack-embedded super-kawaii robot.
Stars: ✭ 242 (+572.22%)
Mutual labels:  esp32
hman-stomper
Stomp Box based on ESP32
Stars: ✭ 26 (-27.78%)
Mutual labels:  esp32
ESP DoubleResetDetector
ESP_DoubleResetDetector is a library for the ESP32/ESP8266 Arduino platform to enable trigger configure mode by resetting twice.
Stars: ✭ 34 (-5.56%)
Mutual labels:  esp32
PMserial
Arduino library for PM sensors with serial interface
Stars: ✭ 41 (+13.89%)
Mutual labels:  esp32

MicroPython Library for Voltage and Current Sensors Using the INA219

This MicroPython library for the INA219 voltage, current and power monitor sensor from Texas Instruments. The intent of the library is to make it easy to use the quite complex functionality of this sensor.

The functionality is currently under development and is based on my INA219 library for the Raspberry Pi.

The library currently only supports continuous reads of voltage and power, but not triggered reads.

The library supports the detection of overflow in the current/power calculations which results in meaningless values for these readings.

The low power mode of the INA219 is supported, so if only occasional reads are being made in a battery based system, current consumption can be minimised.

The library has been tested with the Adafruit INA219 Breakout and the pyboard, as well as a NodeMCU (esp8266 12e) clone. and a Lolin32 Lite (esp32). For specific instructions for the esp8266 and esp32, see sub-directories.

If you successfully use this library with an WiPy, etc, please let me know.

Usage

If you want to give it a try then copy ina219.py and logging.py onto the flash drive of your pyboard, connect the sensor to the I2C(1) or I2C(2) interfaces on the pyboard, then from a REPL prompt execute:

from ina219 import INA219
from machine import I2C

I2C_INTERFACE_NO = 2
SHUNT_OHMS = 0.1  # Check value of shunt used with your INA219

ina = INA219(SHUNT_OHMS, I2C(I2C_INTERFACE_NO))
ina.configure()
print("Bus Voltage: %.3f V" % ina.voltage())
print("Current: %.3f mA" % ina.current())
print("Power: %.3f mW" % ina.power())

Alternatively copy ina219.py, logging.py and example.py to the flash drive and from the REPL prompt execute:

execfile('example.py')

The address of the sensor unless otherwise specified is the default of 0x40.

Note that the bus voltage is that on the load side of the shunt resister, if you want the voltage on the supply side then you should add the bus voltage and shunt voltage together, or use the supply_voltage() function.

Simple - Auto Gain

This mode is great for getting started, as it will provide valid readings until the device current capability is exceeded for the value of the shunt resistor connected (3.2A for 0.1Ω shunt resistor). It does this by automatically adjusting the gain as required until the maximum is reached, when a DeviceRangeError exception is thrown to avoid invalid readings being taken.

The downside of this approach is reduced current and power resolution.

from ina219 import INA219
from ina219 import DeviceRangeError
from machine import I2C

I2C_INTERFACE_NO = 2
SHUNT_OHMS = 0.1  # Check value of shunt used with your INA219

ina = INA219(SHUNT_OHMS, I2C(I2C_INTERFACE_NO))
ina.configure()

print("Bus Voltage: %.3f V" % ina.voltage())
try:
    print("Bus Current: %.3f mA" % ina.current())
    print("Power: %.3f mW" % ina.power())
    print("Shunt voltage: %.3f mV" % ina.shunt_voltage())
except DeviceRangeError as e:
    # Current out of device range with specified shunt resister
    print e

Advanced - Auto Gain, High Resolution

In this mode by understanding the maximum current expected in your system and specifying this in the script you can achieve the best possible current and power resolution. The library will calculate the best gain to achieve the highest resolution based on the maximum expected current.

In this mode if the current exceeds the maximum specified, the gain will be automatically increased, so a valid reading will still result, but at a lower resolution.

As above when the maximum gain is reached, an exception is thrown to avoid invalid readings being taken.

from ina219 import INA219
from ina219 import DeviceRangeError
from machine import I2C

I2C_INTERFACE_NO = 2
SHUNT_OHMS = 0.1  # Check value of shunt used with your INA219
MAX_EXPECTED_AMPS = 0.2

ina = INA219(SHUNT_OHMS, I2C(I2C_INTERFACE_NO), MAX_EXPECTED_AMPS)
ina.configure(ina.RANGE_16V)

print("Bus Voltage: %.3f V" % ina.voltage())
try:
    print("Bus Current: %.3f mA" % ina.current())
    print("Power: %.3f mW" % ina.power())
    print("Shunt voltage: %.3f mV" % ina.shunt_voltage())
except DeviceRangeError as e:
    # Current out of device range with specified shunt resister
    print e

Advanced - Manual Gain, High Resolution

In this mode by understanding the maximum current expected in your system and specifying this and the gain in the script you can always achieve the best possible current and power resolution, at the price of missing current and power values if a current overflow occurs.

from ina219 import INA219
from ina219 import DeviceRangeError
from machine import I2C

I2C_INTERFACE_NO = 2
SHUNT_OHMS = 0.1  # Check value of shunt used with your INA219
MAX_EXPECTED_AMPS = 0.2

ina = INA219(SHUNT_OHMS, I2C(I2C_INTERFACE_NO), MAX_EXPECTED_AMPS)
ina.configure(ina.RANGE_16V, ina.GAIN_1_40MV)

print("Bus Voltage: %.3f V" % ina.voltage())
try:
    print("Bus Current: %.3f mA" % ina.current())
    print("Power: %.3f mW" % ina.power())
    print("Shunt voltage: %.3f mV" % ina.shunt_voltage())
except DeviceRangeError as e:
    print("Current overflow")

Sensor Address

The sensor address may be altered as follows:

ina = INA219(SHUNT_OHMS, I2C(2), MAX_EXPECTED_AMPS, address=0x41)

Low Power Mode

The sensor may be put in low power mode between reads as follows:

ina.configure(ina.RANGE_16V)
while True:
    print("Voltage : %.3f V" % ina.voltage())
    ina.sleep()
    time.sleep(60)
    ina.wake()

Note that if you do not wake the device after sleeping, the value returned from a read will be the previous value taken before sleeping.

Functions

  • INA219() constructs the class. The arguments, are:
    • shunt_ohms: The value of the shunt resistor in Ohms (mandatory).
    • i2c: an instance of the I2C class from the machine module, either I2C(1) or I2C(2) (mandatory).
    • max_expected_amps: The maximum expected current in Amps (optional).
    • address: The I2C address of the INA219, defaults to 0x40 (optional).
    • log_level: Set to logging.INFO to see the detailed calibration calculations and logging.DEBUG to see register operations (optional).
  • configure() configures and calibrates how the INA219 will take measurements. The arguments, which are all optional, are:
    • voltage_range: The full scale voltage range, this is either 16V or 32V, represented by one of the following constants (optional).
      • RANGE_16V: Range zero to 16 volts
      • RANGE_32V: Range zero to 32 volts (default). Device only supports upto 26V.
    • gain: The gain, which controls the maximum range of the shunt voltage, represented by one of the following constants (optional).
      • GAIN_1_40MV: Maximum shunt voltage 40mV
      • GAIN_2_80MV: Maximum shunt voltage 80mV
      • GAIN_4_160MV: Maximum shunt voltage 160mV
      • GAIN_8_320MV: Maximum shunt voltage 320mV
      • GAIN_AUTO: Automatically calculate the gain (default)
    • bus_adc: The bus ADC resolution (9, 10, 11, or 12-bit), or set the number of samples used when averaging results, represented by one of the following constants (optional).
      • ADC_9BIT: 9 bit, conversion time 84us.
      • ADC_10BIT: 10 bit, conversion time 148us.
      • ADC_11BIT: 11 bit, conversion time 276us.
      • ADC_12BIT: 12 bit, conversion time 532us (default).
      • ADC_2SAMP: 2 samples at 12 bit, conversion time 1.06ms.
      • ADC_4SAMP: 4 samples at 12 bit, conversion time 2.13ms.
      • ADC_8SAMP: 8 samples at 12 bit, conversion time 4.26ms.
      • ADC_16SAMP: 16 samples at 12 bit, conversion time 8.51ms
      • ADC_32SAMP: 32 samples at 12 bit, conversion time 17.02ms.
      • ADC_64SAMP: 64 samples at 12 bit, conversion time 34.05ms.
      • ADC_128SAMP: 128 samples at 12 bit, conversion time 68.10ms.
    • shunt_adc: The shunt ADC resolution (9, 10, 11, or 12-bit), or set the number of samples used when averaging results, represented by one of the following constants (optional).
      • ADC_9BIT: 9 bit, conversion time 84us.
      • ADC_10BIT: 10 bit, conversion time 148us.
      • ADC_11BIT: 11 bit, conversion time 276us.
      • ADC_12BIT: 12 bit, conversion time 532us (default).
      • ADC_2SAMP: 2 samples at 12 bit, conversion time 1.06ms.
      • ADC_4SAMP: 4 samples at 12 bit, conversion time 2.13ms.
      • ADC_8SAMP: 8 samples at 12 bit, conversion time 4.26ms.
      • ADC_16SAMP: 16 samples at 12 bit, conversion time 8.51ms
      • ADC_32SAMP: 32 samples at 12 bit, conversion time 17.02ms.
      • ADC_64SAMP: 64 samples at 12 bit, conversion time 34.05ms.
      • ADC_128SAMP: 128 samples at 12 bit, conversion time 68.10ms.
  • voltage() Returns the bus voltage in volts (V).
  • supply_voltage() Returns the bus supply voltage in volts (V). This is the sum of the bus voltage and shunt voltage. A DeviceRangeError exception is thrown if current overflow occurs.
  • current() Returns the bus current in milliamps (mA). A DeviceRangeError exception is thrown if current overflow occurs.
  • power() Returns the bus power consumption in milliwatts (mW). A DeviceRangeError exception is thrown if current overflow occurs.
  • shunt_voltage() Returns the shunt voltage in millivolts (mV). A DeviceRangeError exception is thrown if current overflow occurs.
  • current_overflow() Returns 'True' if an overflow has occured. Alternatively handle the DeviceRangeError exception as shown in the examples above.
  • sleep() Put the INA219 into power down mode.
  • wake() Wake the INA219 from power down mode.
  • reset() Reset the INA219 to its default configuration.

Performance

On v1.1 pyboard reading a bus voltage in a loop, a read occurred approximately every 270μs. Given that in continuous mode a single 12-bit ADC conversion takes 532μs (p27 of the specification) each value returned by the voltage() function will likely be the result of a new conversion.

If multiple ADC conversions are configured (e.g. ADC_2SAMP, takes 1060μs) then the values returned by the voltage() function will often be the result of the same conversion and therefore identical.

Debugging

Add the following to the imports

import logging

To understand the calibration calculation results and automatic gain increases, informational output can be enabled with:

ina = INA219(SHUNT_OHMS, I2C(2), log_level=logging.INFO)

Detailed logging of device register operations can be enabled with:

ina = INA219(SHUNT_OHMS, I2C(2), log_level=logging.DEBUG)

Coding Standard

This library adheres to the PEP8 standard and follows the idiomatic style described in the book Writing Idiomatic Python by Jeff Knupp.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].