Elixir INA219 Sensor Driver
Go to file
Renovate Bot 274409f0bd
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
chore(deps): update dependency wafer to v1.1.0
2024-04-24 14:35:41 +12:00
config chore: migrate to local (#23) 2024-02-14 14:55:23 +13:00
lib chore: migrate to local (#23) 2024-02-14 14:55:23 +13:00
test Derive the release protocol. 2020-01-23 11:31:35 +13:00
.drone.yml chore: fix docs release. 2024-03-14 19:23:06 +13:00
.formatter.exs Empty Mix application. 2019-10-05 11:22:12 +13:00
.gitignore Empty Mix application. 2019-10-05 11:22:12 +13:00
.tool-versions chore(deps): update dependency erlang to v26.2.4 2024-04-13 00:29:02 +12:00
CHANGELOG.md chore: release version v2.0.1 2024-02-23 20:05:50 +13:00
LICENSE.md chore!: Relicense to HL3-FULL. 2023-01-17 10:22:20 +13:00
mix.exs docs: Include README and CHANGELOG in hexdocs. 2024-03-07 20:03:16 +13:00
mix.lock chore(deps): update dependency wafer to v1.1.0 2024-04-24 14:35:41 +12:00
README.md chore: fix typo in readme 2024-03-08 14:45:25 +13:00
renovate.json chore: Configure Renovate (#24) 2024-02-14 17:43:33 +13:00

INA219

Build Status Hex.pm Hippocratic License HL3-FULL

A library for interacting with the Texas Instruments INA219 high side current and power monitoring chip via I2C using Elixir ALE.

I'm using the Adafruit INA219 breakout for prototyping, so am using their calibrations straight from their Arduino library, but you can do the maths and calibrate it yourself if you're using a different shunt resistor value.

Usage

This library uses the Wafer project to connect to the I2C device - this allows you to bring your own driver.

Example using Elixir Circuits

iex> {:ok, conn} = Wafer.Driver.CircuitsI2C.acquire(bus: "i2c-1", address: 0x41)
...> {:ok, conn} = INA219.acquire(conn: conn, current_divisor: 10, power_divisor: 2)
...> :ok = INA219.calibrate_32V_2A(conn)
...> INA219.bus_voltage(conn)
{:ok, 12.0}

Calibration

Calibrating these wee chips is a bit of a pain in the donkey, but is easily achieved by following the equation in the data sheet. Once you have the calibration and divisor values you wish to use you can configure the device manually. For example:

iex> {:ok, conn} = Wafer.Driver.CircuitsI2C.acquire(bus: "i2c-1", address: 0x41)
...> {:ok, conn} = INA219.acquire(conn: conn, current_divisor: 10, power_divisor: 2)
...> INA219.calibrate(conn, 4096)
...> INA219.bus_voltage_range(conn, 32)
...> INA219.shunt_voltage_pga(conn, 8)
...> INA219.bus_adc_resolution_and_averaging(conn, {1, 12})
...> INA219.shunt_adc_resolution_and_averaging(conn, {1, 12})
...> INA219.mode(conn, :shunt_and_bus_voltage_continuous)

The current_divisor and power_divisor values are stored in the connection struct because there's no way to store them in the device's registers. You can alter them as required.

Installation

The ina219 package is available on hex so it can be installed by adding ina219 to your list of dependencies in mix.exs. You'll also need to install an appropriate connection driver.

def deps do
  [
    {:ina219, "~> 2.0.1"}
  ]
end

Documentation for the latest release is always available on HexDocs and for the main branch on docs.harton.nz.

Github Mirror

This repository is mirrored on Github from it's primary location on my Forgejo instance. Feel free to raise issues and open PRs on Github.

License

This software is licensed under the terms of the HL3-FULL, see the LICENSE.md file included with this package for the terms.

This license actively proscribes this software being used by and for some industries, countries and activities. If your usage of this software doesn't comply with the terms of this license, then contact me with the details of your use-case to organise the purchase of a license - the cost of which may include a donation to a suitable charity or NGO.