0.1.4 INA219

This commit is contained in:
Rob Tillaart 2023-06-13 13:21:10 +02:00
parent eb8bc6741e
commit a927652f89
7 changed files with 124 additions and 29 deletions

View File

@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.4] - 2023-06-12
- improve RP2040 support
- add address test in isConnected()
- update readme.md
- add array example
## [0.1.3] - 2023-03-31
- fix setBusADC() range check
- fix setShuntADC() range check
@ -19,7 +26,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- update license 2023
- minor edits
## [0.1.2] - 2022-11-14
- Add RP2040 support to build-CI.
- Add CHANGELOG.md

View File

@ -1,6 +1,6 @@
// FILE: INA219.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
// URL: https://github.com/RobTillaart/INA219
@ -55,6 +55,15 @@ bool INA219::begin(const uint8_t sda, const uint8_t scl)
if (! isConnected()) return false;
return true;
}
#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__)
bool INA219::begin(const uint8_t sda, const uint8_t scl)
{
_wire->setSDA(sda);
_wire->setSCL(scl);
_wire->begin();
if (! isConnected()) return false;
return true;
}
#endif
@ -68,6 +77,7 @@ bool INA219::begin()
bool INA219::isConnected()
{
if ((_address < 0x40) || (_address > 0x4F)) return false;
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
@ -90,6 +100,7 @@ float INA219::getBusVoltage()
uint8_t flags = value & 0x03;
// math overflow handling
if (flags & 0x01) return -100;
// if flags && 0x02 ==> convert flag; not handled
float voltage = (value >> 3) * 4e-3; // fixed 4 mV
return voltage;
}
@ -157,7 +168,7 @@ uint8_t INA219::getBusVoltageRange()
{
uint16_t config = _readRegister(INA219_CONFIGURATION);
if (config & INA219_CONF_BUS_RANGE_VOLTAGE) return 32;
return 16;
return 16; // volts
}

View File

@ -1,7 +1,7 @@
#pragma once
// FILE: INA219.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
// URL: https://github.com/RobTillaart/INA219
@ -14,7 +14,7 @@
#include "Wire.h"
#define INA219_LIB_VERSION (F("0.1.3"))
#define INA219_LIB_VERSION (F("0.1.4"))
class INA219
@ -25,6 +25,8 @@ public:
#if defined (ESP8266) || defined(ESP32)
bool begin(const uint8_t sda, const uint8_t scl);
#elif defined (ARDUINO_ARCH_RP2040) && !defined(__MBED__)
bool begin(const uint8_t sda, const uint8_t scl);
#endif
bool begin();
bool isConnected();
@ -39,11 +41,14 @@ public:
bool getConversionFlag(); // 02
// SCALE HELPERS
// SCALE HELPERS - milli range
float getBusVoltage_mV() { return getBusVoltage() * 1e3; };
float getShuntVoltage_mV() { return getShuntVoltage() * 1e3; };
float getCurrent_mA() { return getCurrent() * 1e3; };
float getPower_mW() { return getPower() * 1e3; };
// SCALE HELPERS - micro range
float getBusVoltage_uV() { return getBusVoltage() * 1e6; };
float getShuntVoltage_uV() { return getShuntVoltage() * 1e6; };
float getCurrent_uA() { return getCurrent() * 1e6; };
float getPower_uW() { return getPower() * 1e6; };
@ -63,6 +68,7 @@ public:
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();

View File

@ -80,12 +80,12 @@ use **INA219_test_I2C.ino**
#### 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(const uint8_t sda, const uint8_t scl)** for ESP32 and ESP8266 et al.
Initializes the class and sets I2C pins.
Returns true if the INA219 address (set in the constructor) 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.
Returns true if the INA219 address (set in the constructor) is on the I2C bus.
- **bool isConnected()** Returns true if the INA219 address (set in the constructor) is on the I2C bus.
#### Core Functions
@ -98,10 +98,11 @@ Also the value is not meaningful if there is no shunt connected.
- **float getPower()** returns the current times BusVoltage in Watt.
- **float getCurrent()** returns the current through the shunt in Ampere.
Helper functions to convert above output to a more appropriate scale of units.
The library has helper functions to convert above output to a more appropriate scale of units.
- **float getBusVoltage_mV()** idem, in millivolts.
- **float getShuntVoltage_mV()** idem, in millivolts.
- **float getBusVoltage_mV()** idem, returns millivolts.
Note: returns -100 if the math overflow bit is set.
- **float getShuntVoltage_mV()** idem, returns millivolts.
- **float getCurrent_mA()** idem in milliAmpere.
- **float getPower_mW()** idem in milliWatt.
- **float getShuntVoltage_uV()** idem microVolt.
@ -109,10 +110,17 @@ Helper functions to convert above output to a more appropriate scale of units.
- **float getPower_uW()** idem, in microWatt.
##### Indicator flags
- **bool getMathOverflowFlag()** internal math overflow.
- **bool getConversionFlag()** conversion is ready.
Especially useful in non-continuous modi.
#### Configuration
Note: the conversion time runs in the background and if done the value is stored in a register.
The core functions always read from the registers, so they are not blocked.
Note: the conversion runs in the background and if done the value is stored in a register.
The core functions can always be read from the registers, so they will not block.
Result can be that you get the very same value if no new value is ready.
- **void reset()** software power on reset.
@ -121,7 +129,7 @@ See section below.
- **bool setBusVoltageRange(uint8_t voltage = 16)** set to 16 or 32.
Values < 16 map to 16 and values between 16 and 32 map to 32.
Values above 32 return false.
- **uint8_t getBusVoltageRange()** returns 16 or 32.
- **uint8_t getBusVoltageRange()** returns 16 or 32. (Volts)
- **bool setGain(uint8_t factor = 1)** factor = 1, 2, 4, 8.
Determines the shunt voltage range. 40, 80, 160 or 320 mV.
Returns false if factor is not a valid value.
@ -163,7 +171,7 @@ See details datasheet,
The value 7 == ShuntBusContinuous mode
- **uint8_t getMode()** returns the mode (0..7) set.
Descriptive mode functions (wrappers).
Descriptive mode functions (convenience wrappers).
- **bool shutDown()** mode 0
- **bool setModeShuntTrigger()** mode 1 - how to trigger to be investigated.
@ -177,7 +185,7 @@ Descriptive mode functions (wrappers).
#### Calibration
See details datasheet,
See details datasheet.
Calibration is mandatory for **getCurrent()** and **getPower()** to work.
@ -200,12 +208,7 @@ to get the values in scientific notation like "3.5e-6"
#### debugging
- **uint16_t getRegister(uint8_t reg)** fetch registers directly, meant for debugging only.
## Operational
See examples..
- **uint16_t getRegister(uint8_t reg)** fetch registers directly, meant for debugging only. Check datasheet.
## Future
@ -216,7 +219,6 @@ See examples..
- test different loads
- all functions.
- update documentation
- Math overflow flag (8.6.3.2)
#### Should
@ -225,7 +227,6 @@ See examples..
- test performance
- verify conversion time
- write and verify examples
- Conversion ready flag (8.6.3.2)
- add a **setCurrentLSB(uint16_t mA)** function ?
- maxAmpere as derived value
@ -247,6 +248,7 @@ See examples..
a few milliseconds per call?
- about a dozen times used,
- flag for forced read in functions **setMode(uint8_t mode, bool forced = false)**
- create defines for several masks / magic numbers
#### Wont

View File

@ -0,0 +1,70 @@
//
// FILE: INA219_array.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo array of INA219 sensors
// URL: https://github.com/RobTillaart/INA219
#include "INA219.h"
#include "Wire.h"
INA219 INA(0x40);
INA219 arr_ina[3] = { INA219(0x40), INA219(0x41), INA219(0x42) };
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("INA219_LIB_VERSION: ");
Serial.println(INA219_LIB_VERSION);
Wire.begin();
for (int i = 0; i < 3; i++)
{
if (! arr_ina[i].begin())
{
Serial.print("Could not connect: ");
Serial.print(i);
Serial.println(". Fix and Reboot");
}
}
for (int i = 0; i < 3; i++)
{
arr_ina[i].setMaxCurrentShunt(5, 0.002);
delay(1000);
Serial.println(arr_ina[i].getBusVoltageRange());
}
}
void loop()
{
Serial.println("\n\t#\tBUS\t\tSHUNT\t\tCURRENT\t\tPOWER\t\tOVF\t\tCNVR");
for (int i = 0; i < 3; i++)
{
Serial.print("\t");
Serial.print(i);
Serial.print("\t");
Serial.print(arr_ina[i].getBusVoltage(), 2);
Serial.print("\t\t");
Serial.print(arr_ina[i].getShuntVoltage_mV(), 2);
Serial.print("\t\t");
Serial.print(arr_ina[i].getCurrent_mA(), 2);
Serial.print("\t\t");
Serial.print(arr_ina[i].getPower_mW(), 2);
Serial.print("\t\t");
Serial.print(arr_ina[i].getMathOverflowFlag());
Serial.print("\t\t");
Serial.print(arr_ina[i].getConversionFlag());
Serial.println();
delay(1000);
}
delay(1000);
}
// -- END OF FILE --

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/INA219.git"
},
"version": "0.1.3",
"version": "0.1.4",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=INA219
version=0.1.3
version=0.1.4
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for INA219 voltage, current and power sensor.