mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.1 INA219
This commit is contained in:
parent
7b7c828a92
commit
da07272e95
11
libraries/INA219/.arduino-ci.yml
Normal file
11
libraries/INA219/.arduino-ci.yml
Normal file
@ -0,0 +1,11 @@
|
||||
compile:
|
||||
# Choosing to run compilation tests on 2 different Arduino platforms
|
||||
platforms:
|
||||
- uno
|
||||
# - due
|
||||
# - zero
|
||||
# - leonardo
|
||||
- m4
|
||||
- esp32
|
||||
# - esp8266
|
||||
# - mega2560
|
4
libraries/INA219/.github/FUNDING.yml
vendored
Normal file
4
libraries/INA219/.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: RobTillaart
|
||||
|
13
libraries/INA219/.github/workflows/arduino-lint.yml
vendored
Normal file
13
libraries/INA219/.github/workflows/arduino-lint.yml
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
|
||||
name: Arduino-lint
|
||||
|
||||
on: [push, pull_request]
|
||||
jobs:
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: arduino/arduino-lint-action@v1
|
||||
with:
|
||||
library-manager: update
|
||||
compliance: strict
|
17
libraries/INA219/.github/workflows/arduino_test_runner.yml
vendored
Normal file
17
libraries/INA219/.github/workflows/arduino_test_runner.yml
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
---
|
||||
name: Arduino CI
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
runTest:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: 2.6
|
||||
- run: |
|
||||
gem install arduino_ci
|
||||
arduino_ci.rb
|
18
libraries/INA219/.github/workflows/jsoncheck.yml
vendored
Normal file
18
libraries/INA219/.github/workflows/jsoncheck.yml
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
name: JSON check
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- '**.json'
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: json-syntax-check
|
||||
uses: limitusus/json-syntax-check@v1
|
||||
with:
|
||||
pattern: "\\.json$"
|
||||
|
321
libraries/INA219/INA219.cpp
Normal file
321
libraries/INA219/INA219.cpp
Normal file
@ -0,0 +1,321 @@
|
||||
// FILE: INA219.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// DATE: 2021-05-18
|
||||
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
|
||||
// URL: https://github.com/RobTillaart/INA219
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2021-05-18 initial version
|
||||
// 0.1.1 2022-09-06 refactor and first public release
|
||||
|
||||
|
||||
#include "INA219.h"
|
||||
|
||||
|
||||
// REGISTER ADDRESSES
|
||||
#define INA219_CONFIGURATION 0x00
|
||||
#define INA219_SHUNT_VOLTAGE 0x01
|
||||
#define INA219_BUS_VOLTAGE 0x02
|
||||
#define INA219_POWER 0x03
|
||||
#define INA219_CURRENT 0x04
|
||||
#define INA219_CALIBRATION 0x05
|
||||
#define INA219_MASK_ENABLE 0x06
|
||||
#define INA219_ALERT_LIMIT 0x07
|
||||
#define INA219_MANUFACTURER 0xFE
|
||||
#define INA219_DIE_ID 0xFF
|
||||
|
||||
|
||||
// CONFIGURATION REGISTER MASKS
|
||||
#define INA219_CONF_RESET 0x8000
|
||||
#define INA219_CONF_BUS_RANGE_VOLTAGE 0x2000
|
||||
#define INA219_CONF_PROG_GAIN 0x1800
|
||||
#define INA219_CONF_BUS_ADC 0x0780
|
||||
#define INA219_CONF_SHUNT_ADC 0x0078
|
||||
#define INA219_CONF_MODE 0x0007
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// CONSTRUCTOR
|
||||
//
|
||||
INA219::INA219(const uint8_t address, TwoWire *wire)
|
||||
{
|
||||
_address = address;
|
||||
_wire = wire;
|
||||
// not calibrated values by default.
|
||||
_current_LSB = 0;
|
||||
_maxCurrent = 0;
|
||||
_shunt = 0;
|
||||
}
|
||||
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
bool INA219::begin(const uint8_t sda, const uint8_t scl)
|
||||
{
|
||||
_wire = &Wire;
|
||||
_wire->begin(sda, scl);
|
||||
if (! isConnected()) return false;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
bool INA219::begin()
|
||||
{
|
||||
_wire->begin();
|
||||
if (! isConnected()) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool INA219::isConnected()
|
||||
{
|
||||
_wire->beginTransmission(_address);
|
||||
return ( _wire->endTransmission() == 0);
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// CORE FUNCTIONS
|
||||
//
|
||||
float INA219::getShuntVoltage()
|
||||
{
|
||||
uint16_t value = _readRegister(INA219_SHUNT_VOLTAGE);
|
||||
return value * 1e-5; // fixed 10 uV
|
||||
}
|
||||
|
||||
|
||||
float INA219::getBusVoltage()
|
||||
{
|
||||
uint16_t value = _readRegister(INA219_BUS_VOLTAGE);
|
||||
uint8_t flags = value & 0x03;
|
||||
// overflow handling
|
||||
if (flags & 0x01) return -100;
|
||||
float voltage = (value >> 3) * 4e-3; // fixed 4 mV
|
||||
return voltage;
|
||||
}
|
||||
|
||||
|
||||
float INA219::getPower()
|
||||
{
|
||||
uint16_t value = _readRegister(INA219_POWER);
|
||||
return value * 20 * _current_LSB;
|
||||
}
|
||||
|
||||
// TODO CHECK
|
||||
// needs _current_LSB factor?
|
||||
float INA219::getCurrent()
|
||||
{
|
||||
int16_t value = _readRegister(INA219_CURRENT);
|
||||
return value * _current_LSB;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// CONFIGURATION
|
||||
//
|
||||
void INA219::reset()
|
||||
{
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config |= 0x8000;
|
||||
_writeRegister(INA219_CONFIGURATION, config);
|
||||
// reset calibration
|
||||
_current_LSB = 0;
|
||||
_maxCurrent = 0;
|
||||
_shunt = 0;
|
||||
}
|
||||
|
||||
|
||||
bool INA219::setBusVoltageRange(uint8_t voltage)
|
||||
{
|
||||
if (voltage > 32) return false;
|
||||
if (voltage > 16) voltage = 32;
|
||||
else voltage = 16;
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= ~INA219_CONF_BUS_RANGE_VOLTAGE;
|
||||
if (voltage == 32) config |= INA219_CONF_BUS_RANGE_VOLTAGE;
|
||||
_writeRegister(INA219_CONFIGURATION, config);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t INA219::getBusVoltageRange()
|
||||
{
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
if (config & INA219_CONF_BUS_RANGE_VOLTAGE) return 32;
|
||||
return 16;
|
||||
}
|
||||
|
||||
|
||||
bool INA219::setGain(uint8_t factor)
|
||||
{
|
||||
if (factor != 1 && factor != 2 && factor != 4 && factor != 8)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= ~INA219_CONF_PROG_GAIN;
|
||||
if (factor == 2) config |= (1 << 11);
|
||||
else if (factor == 4) config |= (2 << 11);
|
||||
else if (factor == 8) config |= (3 << 11);
|
||||
_writeRegister(INA219_CONFIGURATION, config);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t INA219::getGain()
|
||||
{
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
uint16_t mask = (config & INA219_CONF_PROG_GAIN);
|
||||
if (mask == 0x0000) return 1;
|
||||
else if (mask == 0x0800) return 2;
|
||||
else if (mask == 0x1000) return 4;
|
||||
return 8;
|
||||
}
|
||||
|
||||
|
||||
bool INA219::setBusADC(uint8_t mask)
|
||||
{
|
||||
if (mask > 0x000F) return false;
|
||||
|
||||
// TODO improve this one. datasheet.
|
||||
// two functions
|
||||
// setBusResolution + setBusSamples()
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= ~INA219_CONF_BUS_ADC;
|
||||
config |= (mask << 7);
|
||||
// if (bits == 10) config |= (1 << 7);
|
||||
// else if (bits == 11) config |= (2 << 7);
|
||||
// else if (bits == 12) config |= (3 << 7);
|
||||
_writeRegister(INA219_CONFIGURATION, config);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t INA219::getBusADC()
|
||||
{
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= INA219_CONF_BUS_ADC;
|
||||
return config >> 7;
|
||||
}
|
||||
|
||||
|
||||
bool INA219::setShuntADC(uint8_t mask)
|
||||
{
|
||||
if (mask > 0x000F) return false;
|
||||
|
||||
// TODO improve this one. datasheet.
|
||||
// two functions
|
||||
// setShuntResolution + setShuntSamples()
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= ~INA219_CONF_SHUNT_ADC;
|
||||
config |= (mask << 3);
|
||||
_writeRegister(INA219_CONFIGURATION, config);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t INA219::getShuntADC()
|
||||
{
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= INA219_CONF_SHUNT_ADC;
|
||||
return config >> 3;
|
||||
}
|
||||
|
||||
|
||||
bool INA219::setMode(uint8_t mode)
|
||||
{
|
||||
if (mode > 8) return false;
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= ~INA219_CONF_MODE;
|
||||
config |= mode;
|
||||
_writeRegister(INA219_CONFIGURATION, config);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t INA219::getMode()
|
||||
{
|
||||
uint16_t config = _readRegister(INA219_CONFIGURATION);
|
||||
config &= INA219_CONF_MODE;
|
||||
return config;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// CALIBRATION
|
||||
//
|
||||
bool INA219::setMaxCurrentShunt(float maxCurrent, float shunt)
|
||||
{
|
||||
#define printdebug true
|
||||
uint16_t calib = 0;
|
||||
|
||||
if (maxCurrent < 0.001) return false;
|
||||
if (shunt < 0.001) return false;
|
||||
|
||||
// _current_LSB = maxCurrent / 32768;
|
||||
_current_LSB = maxCurrent * 3.0517578125e-5;
|
||||
_maxCurrent = maxCurrent;
|
||||
_shunt = shunt;
|
||||
calib = round(0.04096 / (_current_LSB * shunt));
|
||||
_writeRegister(INA219_CALIBRATION, calib);
|
||||
|
||||
|
||||
#ifdef printdebug
|
||||
Serial.println();
|
||||
Serial.print("current_LSB:\t");
|
||||
Serial.print(_current_LSB, 8);
|
||||
Serial.println(" uA / bit");
|
||||
|
||||
Serial.print("Calibration:\t");
|
||||
Serial.println(calib);
|
||||
Serial.print("Max current:\t");
|
||||
Serial.print(_maxCurrent, 3);
|
||||
Serial.println(" A");
|
||||
Serial.print("Shunt:\t");
|
||||
Serial.print(_shunt, 8);
|
||||
Serial.println(" ohm Ω");
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
uint16_t INA219::_readRegister(uint8_t reg)
|
||||
{
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(reg);
|
||||
_wire->endTransmission();
|
||||
|
||||
_wire->requestFrom(_address, (uint8_t)2);
|
||||
uint16_t value = _wire->read();
|
||||
value <<= 8;
|
||||
value |= _wire->read();
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
uint16_t INA219::_writeRegister(uint8_t reg, uint16_t value)
|
||||
{
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(reg);
|
||||
_wire->write(value >> 8);
|
||||
_wire->write(value & 0xFF);
|
||||
return _wire->endTransmission();
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
113
libraries/INA219/INA219.h
Normal file
113
libraries/INA219/INA219.h
Normal file
@ -0,0 +1,113 @@
|
||||
#pragma once
|
||||
// FILE: INA219.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.1
|
||||
// DATE: 2021-05-18
|
||||
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
|
||||
// URL: https://github.com/RobTillaart/INA219
|
||||
//
|
||||
// Read the datasheet for the details
|
||||
//
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
|
||||
#define INA219_LIB_VERSION (F("0.1.1"))
|
||||
|
||||
|
||||
class INA219
|
||||
{
|
||||
public:
|
||||
// address between 0x40 and 0x4F
|
||||
explicit INA219(const uint8_t address, TwoWire *wire = &Wire);
|
||||
|
||||
#if defined (ESP8266) || defined(ESP32)
|
||||
bool begin(const uint8_t sda, const uint8_t scl);
|
||||
#endif
|
||||
bool begin();
|
||||
bool isConnected();
|
||||
|
||||
|
||||
// CORE FUNCTIONS // Register
|
||||
float getShuntVoltage(); // 01
|
||||
float getBusVoltage(); // 02
|
||||
float getPower(); // 03
|
||||
float getCurrent(); // 04
|
||||
|
||||
|
||||
// SCALE HELPERS
|
||||
float getBusVoltage_mV() { return getBusVoltage() * 1e3; };
|
||||
float getShuntVoltage_mV() { return getShuntVoltage() * 1e3; };
|
||||
float getCurrent_mA() { return getCurrent() * 1e3; };
|
||||
float getPower_mW() { return getPower() * 1e3; };
|
||||
float getShuntVoltage_uV() { return getShuntVoltage() * 1e6; };
|
||||
float getCurrent_uA() { return getCurrent() * 1e6; };
|
||||
float getPower_uW() { return getPower() * 1e6; };
|
||||
|
||||
|
||||
// CONFIGURATION
|
||||
// need improvement API wise.
|
||||
void reset();
|
||||
// voltage = 16, 32 (values below 32 are rounded to 16 or 32)
|
||||
bool setBusVoltageRange(uint8_t voltage = 16);
|
||||
uint8_t getBusVoltageRange(); // returns 16 or 32.
|
||||
// factor = 1, 2, 4, 8
|
||||
bool setGain(uint8_t factor = 1);
|
||||
uint8_t getGain();
|
||||
// mask
|
||||
bool setBusADC(uint8_t mask = 0x03);
|
||||
uint8_t getBusADC();
|
||||
bool setShuntADC(uint8_t mask = 0x03);
|
||||
uint8_t getShuntADC();
|
||||
// Operating mode = 0..7
|
||||
bool setMode(uint8_t mode = 7);
|
||||
uint8_t getMode();
|
||||
bool shutDown() { return setMode(0); };
|
||||
bool setModeShuntTrigger() { return setMode(1); };
|
||||
bool setModeBusTrigger() { return setMode(2); };
|
||||
bool setModeShuntBusTrigger() { return setMode(3); };
|
||||
bool setModeADCOff() { return setMode(4); };
|
||||
bool setModeShuntContinuous() { return setMode(5); };
|
||||
bool setModeBusContinuous() { return setMode(6); };
|
||||
bool setModeShuntBusContinuous() { return setMode(7); }; // default.
|
||||
|
||||
|
||||
// CALIBRATION
|
||||
// mandatory to set these! read datasheet.
|
||||
// maxCurrent >= 0.001
|
||||
// shunt >= 0.001
|
||||
bool setMaxCurrentShunt(float maxCurrent = 3.4,
|
||||
float shunt = 0.002);
|
||||
|
||||
bool isCalibrated() { return _current_LSB != 0.0; };
|
||||
|
||||
// these return zero if not calibrated!
|
||||
float getCurrentLSB() { return _current_LSB; };
|
||||
float getCurrentLSB_mA() { return _current_LSB * 1e3; };
|
||||
float getCurrentLSB_uA() { return _current_LSB * 1e6; };
|
||||
float getShunt() { return _shunt; };
|
||||
float getMaxCurrent() { return _maxCurrent; };
|
||||
|
||||
|
||||
// DEBUG
|
||||
uint16_t getRegister(uint8_t reg) { return _readRegister(reg); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
uint16_t _readRegister(uint8_t reg);
|
||||
uint16_t _writeRegister(uint8_t reg, uint16_t value);
|
||||
float _current_LSB;
|
||||
float _shunt;
|
||||
float _maxCurrent;
|
||||
|
||||
uint8_t _address;
|
||||
TwoWire * _wire;
|
||||
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
21
libraries/INA219/LICENSE
Normal file
21
libraries/INA219/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2021-2022 Rob Tillaart
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
226
libraries/INA219/README.md
Normal file
226
libraries/INA219/README.md
Normal file
@ -0,0 +1,226 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/INA219/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![Arduino-lint](https://github.com/RobTillaart/INA219/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/INA219/actions/workflows/arduino-lint.yml)
|
||||
[![JSON check](https://github.com/RobTillaart/INA219/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/INA219/actions/workflows/jsoncheck.yml)
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/INA219/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/INA219.svg?maxAge=3600)](https://github.com/RobTillaart/INA219/releases)
|
||||
|
||||
|
||||
# INA219
|
||||
|
||||
Arduino library for INA219 voltage, current and power sensor.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
**Experimental** library for the INA219 power sensor.
|
||||
|
||||
Not tested as I have no hardware available.
|
||||
So usage remarks and comments are welcome.
|
||||
|
||||
**USE WITH CARE**
|
||||
|
||||
The INA219 is a voltage, current and power measurement device.
|
||||
Maxima, see datasheet, chapter 7, esp 7.5
|
||||
|
||||
| description | max | unit | notes |
|
||||
|:--------------|------:|-------:|:------|
|
||||
| bus voltage | 32 | Volt | depends on BRNG setting
|
||||
| shunt voltage | 320 | mVolt | depends on PGA setting
|
||||
|
||||
#### special chars
|
||||
|
||||
- Ω == Ohm = ALT-234 (Windows)
|
||||
- µ == micro = ALT-0181 (Windows)
|
||||
|
||||
|
||||
## I2C
|
||||
|
||||
#### Address
|
||||
|
||||
The sensor can be configured to use 1 of 16 I2C addresses between 0x40 and 0x4F.
|
||||
The address depends on how the A0 and A1 address lines are connected to the SCL, SDA, GND and VCC pins.
|
||||
(datasheet chapter 8.5.5.1 Serial Bus Address)
|
||||
|
||||
|
||||
#### Performance
|
||||
|
||||
Datasheet states it supports 1 KHz .. 2.56 MHz. (to be verified).
|
||||
Note: higher speeds need smaller pull up resistors.
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
Read datasheet for details.
|
||||
|
||||
|
||||
### Constructor
|
||||
|
||||
- **INA219(const uint8_t address, TwoWire \*wire = Wire)** Constructor to set address and optional Wire interface.
|
||||
- **bool begin(const uint8_t sda, const uint8_t scl)** for ESP32 and ESP8266; initializes the class.
|
||||
sets I2C pins.
|
||||
Returns true if the INA219 address is on the I2C bus.
|
||||
- **bool begin()** UNO ea. initializes the class.
|
||||
Returns true if the INA219 address is on the I2C bus.
|
||||
- **bool isConnected()** returns true if the INA219 address is on the I2C bus.
|
||||
|
||||
|
||||
### Core Functions
|
||||
|
||||
Note the power and the current are not meaningful without calibrating
|
||||
the sensor. Also the value is not meaningful if there is no shunt connected.
|
||||
|
||||
- **float getShuntVoltage()** idem.
|
||||
- **float getBusVoltage()** idem. Max 32 Volt.
|
||||
- **float getPower()** is the current x BusVoltage in Watt.
|
||||
- **float getCurrent()** is the current through the shunt in Ampere.
|
||||
|
||||
Helper functions to convert to the right scale of units.
|
||||
|
||||
- **float getBusVoltage_mV()** idem, in millivolts.
|
||||
- **float getShuntVoltage_mV()** idem, in millivolts.
|
||||
- **float getCurrent_mA()** idem in milliAmpere.
|
||||
- **float getPower_mW()** idem in milliWatt.
|
||||
- **float getShuntVoltage_uV()** idem microVolt.
|
||||
- **float getCurrent_uA()** idem in microAmpere.
|
||||
- **float getPower_uW()** idem, in microWatt.
|
||||
|
||||
|
||||
### Configuration
|
||||
|
||||
Note: the conversion time runs in the background and if done value is stored in a register.
|
||||
The core functions read from the registers, so they are not blocked,
|
||||
but just get the same value if no new is ready.
|
||||
|
||||
- **void reset()** software power on reset.
|
||||
This implies calibration with **setMaxCurrentShunt()** needs to be redone.
|
||||
- **bool setBusVoltageRange(uint8_t voltage = 16)** set to 16 or 32.
|
||||
Values < 16 map to 16 an values between 16 and 32 map to 32.
|
||||
- **uint8_t getBusVoltageRange()** returns 16 or 32.
|
||||
- **bool setGain(uint8_t factor =, 1)** factor = 1, 2, 4, 8.
|
||||
Determines the shunt voltage range. 40, 80, 160 or 320 mV.
|
||||
- **uint8_t getGain()** returns set factor.
|
||||
- **bool setBusADC(uint8_t mask = 0x03)** check datasheet for meaning of mask.
|
||||
Returns false if mask > 0x0F.
|
||||
- **uint8_t getBusADC()** returns mask.
|
||||
- **bool setShuntADC(uint8_t mask = 0x03)** check datasheet for meaning of mask.
|
||||
Returns false if mask > 0x0F.
|
||||
- **uint8_t getShuntADC()** returns mask.
|
||||
|
||||
mask = both resolution + averaging multiple samples.
|
||||
minus - == don't care
|
||||
|
||||
| bit mask | value | description | conversion time |
|
||||
|:----------:|:-------:|:------------------|:---------------:|
|
||||
| 0-00 | 0 / 4 | 9 bit resolution | 84 μs |
|
||||
| 0-01 | 1 / 5 | 10 bit resolution | 148 μs |
|
||||
| 0-10 | 2 / 6 | 11 bit resolution | 276 μs |
|
||||
| 0-11 | 3 / 7 | 12 bit resolution | 532 μs |
|
||||
| 1000 | 8 | 12 bit 1 sample | 532 μs |
|
||||
| 1001 | 9 | 2 samples | 1.06 ms |
|
||||
| 1010 | 10 | 4 samples | 2.13 ms |
|
||||
| 1011 | 11 | 8 samples | 4.26 ms |
|
||||
| 1100 | 12 | 16 samples | 8.51 ms |
|
||||
| 1101 | 13 | 32 samples | 17.02 ms |
|
||||
| 1110 | 14 | 64 samples | 34.05 ms |
|
||||
| 1111 | 15 | 128 samples | 68.10 ms |
|
||||
|
||||
- note that a new value can take a while depending on value set.
|
||||
- note that you cannot set e.g. 9 bits and 16 samples.
|
||||
|
||||
|
||||
### Operating mode
|
||||
|
||||
See datasheet,
|
||||
|
||||
- **bool setMode(uint8_t mode = 7)** mode = 0 .. 7
|
||||
- **uint8_t getMode()** returns the mode (0..7) set.
|
||||
|
||||
Descriptive mode functions (wrappers).
|
||||
|
||||
- **bool shutDown()** mode 0
|
||||
- **bool setModeShuntTrigger()** mode 1 - how to trigger to be investigated
|
||||
- **bool setModeBusTrigger()** mode 2
|
||||
- **bool setModeShuntBusTrigger()** mode 3
|
||||
- **bool setModeADCOff()** mode 4 -
|
||||
- **bool setModeShuntContinuous()** mode 5
|
||||
- **bool setModeBusContinuous()** mode 6
|
||||
- **bool setModeShuntBusContinuous()** mode 7 - default
|
||||
|
||||
|
||||
### Calibration
|
||||
|
||||
See datasheet,
|
||||
|
||||
Calibration is mandatory for **getCurrent()** and **getPower()** to work.
|
||||
|
||||
- **bool setMaxCurrentShunt(float ampere = 20.0, float ohm = 0.002)**
|
||||
set the calibration register based upon the shunt and the max ampere.
|
||||
From this the LSB is derived.
|
||||
Note the function will round up the LSB to nearest round value by default.
|
||||
This may cause loss of precision. The function may force normalization if underflow detected.
|
||||
The user **must** check the return value == true, otherwise the calibration register is **not** set.
|
||||
- **bool isCalibrated()** returns true if CurrentLSB has been calculated by **setMaxCurrentShunt()**.
|
||||
- **float getCurrentLSB()** returns the LSB in Ampere == precision of the calibration.
|
||||
- **float getCurrentLSB_mA()** returns the LSB in milliampere.
|
||||
- **float getCurrentLSB_uA()** returns the LSB in microampere.
|
||||
- **float getShunt()** returns the value set for the shunt.
|
||||
- **float getMaxCurrent()** returns the value for the maxCurrent which can be corrected.
|
||||
|
||||
To print these values one might use https://github.com/RobTillaart/printHelpers
|
||||
to get scientific notation like "3.5e-6"
|
||||
|
||||
|
||||
### debugging
|
||||
|
||||
- **uint16_t getRegister(uint8_t reg)** fetch registers directly, meant for debugging only.
|
||||
|
||||
|
||||
## Operational
|
||||
|
||||
See examples..
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
|
||||
- get hardware (+ time) to test
|
||||
- test different loads
|
||||
- write and verify examples
|
||||
- all functions.
|
||||
- update documentation
|
||||
|
||||
#### Should
|
||||
|
||||
- create unit tests
|
||||
- performance
|
||||
- verify I2C bus speed
|
||||
- verify conversion time
|
||||
- Conversion ready flag (8.6.3.2)
|
||||
- Math overflow flag (8.6.3.2)
|
||||
- add a **setCurrentLSB(uint16_t mA)** function ?
|
||||
- maxAmpere as derived value
|
||||
|
||||
#### Could
|
||||
|
||||
- calibration
|
||||
- initial current 20A and shunt 0.002 Ω in **begin()** ??
|
||||
- autocorrect \_current_LSB round number
|
||||
- maxCurrent? how much?
|
||||
- can the calibration math be optimized
|
||||
- integer only?
|
||||
- less iterations?
|
||||
- local var for current_lsb?
|
||||
- ??
|
||||
- disconnected load,
|
||||
- can it be recognized? => current drop?
|
||||
- cache configuration ? ==> 2 bytes
|
||||
- what is gained? both getting and setting is faster.
|
||||
a few milliseconds per call?
|
||||
- about a dozen times used,
|
||||
- flag for forced read in functions **setMode(uint8_t mode, bool forced = false)**
|
||||
- separate release notes.
|
||||
|
||||
|
||||
|
63
libraries/INA219/examples/INA219_demo/INA219_demo.ino
Normal file
63
libraries/INA219/examples/INA219_demo/INA219_demo.ino
Normal file
@ -0,0 +1,63 @@
|
||||
//
|
||||
// FILE: INA219_demo.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo
|
||||
// DATE: 2021-05-18
|
||||
// URL: https://github.com/RobTillaart/INA219
|
||||
|
||||
|
||||
#include "INA219.h"
|
||||
#include "Wire.h"
|
||||
|
||||
INA219 INA(0x40);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("INA219_LIB_VERSION: ");
|
||||
Serial.println(INA219_LIB_VERSION);
|
||||
|
||||
Wire.begin();
|
||||
if (!INA.begin() )
|
||||
{
|
||||
Serial.println("could not connect. Fix and Reboot");
|
||||
}
|
||||
INA.setMaxCurrentShunt(1, 0.002);
|
||||
delay(1000);
|
||||
INA.setMaxCurrentShunt(2.5, 0.002);
|
||||
delay(1000);
|
||||
INA.setMaxCurrentShunt(5, 0.002);
|
||||
delay(1000);
|
||||
INA.setMaxCurrentShunt(7.5, 0.002);
|
||||
delay(1000);
|
||||
INA.setMaxCurrentShunt(10, 0.002);
|
||||
delay(1000);
|
||||
INA.setMaxCurrentShunt(15, 0.002);
|
||||
delay(1000);
|
||||
INA.setMaxCurrentShunt(20, 0.002);
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
Serial.println("\nBUS\tSHUNT\tCURRENT\tPOWER");
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
Serial.print(INA.getBusVoltage(), 3);
|
||||
Serial.print("\t");
|
||||
Serial.print(INA.getShuntVoltage_mV(), 3);
|
||||
Serial.print("\t");
|
||||
Serial.print(INA.getCurrent_mA(), 3);
|
||||
Serial.print("\t");
|
||||
Serial.print(INA.getPower_mW(), 3);
|
||||
Serial.println();
|
||||
delay(1000);
|
||||
}
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
40
libraries/INA219/examples/INA219_minimal/INA219_minimal.ino
Normal file
40
libraries/INA219/examples/INA219_minimal/INA219_minimal.ino
Normal file
@ -0,0 +1,40 @@
|
||||
//
|
||||
// FILE: INA219_minimal.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo
|
||||
// DATE: 2022-09-06
|
||||
// URL: https://github.com/RobTillaart/INA219
|
||||
|
||||
|
||||
#include "INA219.h"
|
||||
#include "Wire.h"
|
||||
|
||||
INA219 INA(0x40);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("INA219_LIB_VERSION: ");
|
||||
Serial.println(INA219_LIB_VERSION);
|
||||
|
||||
Wire.begin();
|
||||
if (!INA.begin() )
|
||||
{
|
||||
Serial.println("could not connect. Fix and Reboot");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// these two can be read without further configuration.
|
||||
Serial.print(INA.getBusVoltage(), 3);
|
||||
Serial.print("\t");
|
||||
Serial.print(INA.getShuntVoltage_mV(), 3);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
52
libraries/INA219/keywords.txt
Normal file
52
libraries/INA219/keywords.txt
Normal file
@ -0,0 +1,52 @@
|
||||
# Syntax Colouring Map For INA219
|
||||
|
||||
|
||||
# Data types (KEYWORD1)
|
||||
INA219 KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
begin KEYWORD2
|
||||
isConnected KEYWORD2
|
||||
|
||||
getShuntVoltage KEYWORD2
|
||||
getBusVoltage KEYWORD2
|
||||
getPower KEYWORD2
|
||||
getCurrent KEYWORD2
|
||||
|
||||
getBusVoltage_mV KEYWORD2
|
||||
getShuntVoltage_uV KEYWORD2
|
||||
getCurrent_mA KEYWORD2
|
||||
getPower_mW KEYWORD2
|
||||
getShuntVoltage_uV KEYWORD2
|
||||
getCurrent_uA KEYWORD2
|
||||
getPower_uW KEYWORD2
|
||||
|
||||
|
||||
reset KEYWORD2
|
||||
setBusVoltageRange KEYWORD2
|
||||
getBusVoltageRange KEYWORD2
|
||||
setGain KEYWORD2
|
||||
getGain KEYWORD2
|
||||
setBusADC KEYWORD2
|
||||
getBusADC KEYWORD2
|
||||
setShuntADC KEYWORD2
|
||||
getShuntADC KEYWORD2
|
||||
|
||||
setMode KEYWORD2
|
||||
getMode KEYWORD2
|
||||
|
||||
shutDown KEYWORD2
|
||||
setModeShuntTrigger KEYWORD2
|
||||
setModeBusTrigger KEYWORD2
|
||||
setModeShuntBusTrigger KEYWORD2
|
||||
setModeADCOff KEYWORD2
|
||||
setModeShuntContinuous KEYWORD2
|
||||
setModeBusContinuous KEYWORD2
|
||||
setModeShuntBusContinuous KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
INA219_LIB_VERSION LITERAL1
|
||||
|
||||
|
23
libraries/INA219/library.json
Normal file
23
libraries/INA219/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "INA219",
|
||||
"keywords": "power ampere voltage volts current",
|
||||
"description": "AArduino library for INA219 voltage, current and power sensor.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/INA219.git"
|
||||
},
|
||||
"version": "0.1.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"headers": "INA219.h"
|
||||
}
|
11
libraries/INA219/library.properties
Normal file
11
libraries/INA219/library.properties
Normal file
@ -0,0 +1,11 @@
|
||||
name=INA219
|
||||
version=0.1.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for INA219 voltage, current and power sensor.
|
||||
paragraph=Voltage current Volt Ampere
|
||||
category=Data Processing
|
||||
url=https://github.com/RobTillaart/INA219
|
||||
architectures=*
|
||||
includes=INA219.h
|
||||
depends=
|
114
libraries/INA219/test/unit_test_001.cpp
Normal file
114
libraries/INA219/test/unit_test_001.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
//
|
||||
// FILE: unit_test_001.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2021-05-18
|
||||
// PURPOSE: unit tests for the INA219 library
|
||||
// https://github.com/RobTillaart/INA219
|
||||
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
|
||||
//
|
||||
|
||||
// supported assertions
|
||||
// ----------------------------
|
||||
// assertEqual(expected, actual); // a == b
|
||||
// assertNotEqual(unwanted, actual); // a != b
|
||||
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
|
||||
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
|
||||
// assertLess(upperBound, actual); // a < b
|
||||
// assertMore(lowerBound, actual); // a > b
|
||||
// assertLessOrEqual(upperBound, actual); // a <= b
|
||||
// assertMoreOrEqual(lowerBound, actual); // a >= b
|
||||
// assertTrue(actual);
|
||||
// assertFalse(actual);
|
||||
// assertNull(actual);
|
||||
|
||||
// // special cases for floats
|
||||
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
|
||||
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
|
||||
// assertInfinity(actual); // isinf(a)
|
||||
// assertNotInfinity(actual); // !isinf(a)
|
||||
// assertNAN(arg); // isnan(a)
|
||||
// assertNotNAN(arg); // !isnan(a)
|
||||
|
||||
#include <ArduinoUnitTests.h>
|
||||
|
||||
|
||||
#include "INA219.h"
|
||||
|
||||
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "\n INA219_LIB_VERSION: %s\n", (char *) INA219_LIB_VERSION);
|
||||
}
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
INA219 INA(0x40);
|
||||
|
||||
Wire.begin();
|
||||
assertTrue(INA.begin());
|
||||
assertTrue(INA.isConnected());
|
||||
|
||||
// default is not calibrated.
|
||||
assertFalse(INA.isCalibrated());
|
||||
assertEqualFloat(0.0, INA.getCurrentLSB(), 0.001);
|
||||
assertEqualFloat(0.0, INA.getShunt(), 0.001);
|
||||
assertEqualFloat(0.0, INA.getMaxCurrent(), 0.001);
|
||||
|
||||
}
|
||||
|
||||
|
||||
unittest(test_core_functions)
|
||||
{
|
||||
INA219 INA(0x40);
|
||||
// assertTrue(INA.begin());
|
||||
|
||||
fprintf(stderr, "need mock up\n");
|
||||
/*
|
||||
fprintf(stderr, "%f\n", INA.getShuntVoltage());
|
||||
fprintf(stderr, "%f\n", INA.getBusVoltage());
|
||||
fprintf(stderr, "%f\n", INA.getPower());
|
||||
fprintf(stderr, "%f\n", INA.getCurrent());
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
unittest(test_configuration)
|
||||
{
|
||||
INA219 INA(0x40);
|
||||
// assertTrue(INA.begin());
|
||||
|
||||
// only errors can be tested
|
||||
assertFalse(INA.setBusVoltageRange(33));
|
||||
assertFalse(INA.setGain(3));
|
||||
assertFalse(INA.setGain(5));
|
||||
assertFalse(INA.setGain(6));
|
||||
assertFalse(INA.setGain(7));
|
||||
assertFalse(INA.setGain(9));
|
||||
assertFalse(INA.setBusADC(16));
|
||||
assertFalse(INA.setShuntADC(16));
|
||||
}
|
||||
|
||||
|
||||
unittest(test_calibration)
|
||||
{
|
||||
INA219 INA(0x40);
|
||||
// assertTrue(INA.begin());
|
||||
|
||||
// only errors can be tested
|
||||
assertFalse(INA.setMaxCurrentShunt(0.0009));
|
||||
assertFalse(INA.setMaxCurrentShunt(0));
|
||||
assertFalse(INA.setMaxCurrentShunt(-1));
|
||||
assertFalse(INA.setMaxCurrentShunt(10, 0));
|
||||
assertFalse(INA.setMaxCurrentShunt(10, 0.0009));
|
||||
}
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
// --------
|
Loading…
Reference in New Issue
Block a user