Skip to content

Commit 38af67b

Browse files
committed
Add INA228, set INA2xx shunt/avg/conv.times
1 parent 0c34fe5 commit 38af67b

File tree

7 files changed

+216
-7
lines changed

7 files changed

+216
-7
lines changed

src/components/i2c/controller.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ static const std::map<std::string, FnCreateI2CSensorDriver> I2cFactorySensor = {
133133
const char *driver_name) -> drvBase * {
134134
return new drvIna219(i2c, addr, mux_channel, driver_name);
135135
}},
136+
{"ina228",
137+
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
138+
const char *driver_name) -> drvBase * {
139+
return new drvIna228(i2c, addr, mux_channel, driver_name);
140+
}},
136141
{"ina237",
137142
[](TwoWire *i2c, uint16_t addr, uint32_t mux_channel,
138143
const char *driver_name) -> drvBase * {
@@ -387,11 +392,11 @@ static const std::unordered_map<uint16_t, std::vector<const char *>>
387392
{0x38, {"aht20", "max17048"}},
388393
{0x39, {"tsl2591"}},
389394
{0x40,
390-
{"htu21d", "htu31d", "ina219", "ina237", "ina238", "ina260",
395+
{"htu21d", "htu31d", "ina219", "ina228", "ina237", "ina238", "ina260",
391396
"ms8607", "si7021", "stemma_soil"}},
392-
{0x41, {"htu31d", "ina219", "ina237", "ina238", "ina260"}},
393-
{0x44, {"hdc302x", "ina237", "ina238", "ina260", "sht3x", "sht4x"}},
394-
{0x45, {"hdc302x", "ina237", "ina238", "ina260", "sht3x"}},
397+
{0x41, {"htu31d", "ina219", "ina228", "ina237", "ina238", "ina260"}},
398+
{0x44, {"hdc302x", "ina228", "ina237", "ina238", "ina260", "sht3x", "sht4x"}},
399+
{0x45, {"hdc302x", "ina228", "ina237", "ina238", "ina260", "sht3x"}},
395400
{0x46, {"hdc302x"}},
396401
{0x47, {"hdc302x"}},
397402
{0x48, {"adt7410", "pct2075", "tmp117"}},

src/components/i2c/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "drivers/drvHts221.h"
3434
#include "drivers/drvHtu21d.h"
3535
#include "drivers/drvIna219.h"
36+
#include "drivers/drvIna228.h"
3637
#include "drivers/drvIna237.h"
3738
#include "drivers/drvIna238.h"
3839
#include "drivers/drvIna260.h"
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*!
2+
* @file drvIna228.cpp
3+
*
4+
* Device driver for the INA228 High Precision DC Current and Voltage Monitor
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#include "drvIna228.h"
17+
#include <Adafruit_INA228.h>
18+
19+
/*******************************************************************************/
20+
/*!
21+
@brief Destructor for an INA228 sensor.
22+
*/
23+
/*******************************************************************************/
24+
drvIna228::~drvIna228() {
25+
if (_ina228) {
26+
delete _ina228;
27+
_ina228 = nullptr;
28+
}
29+
}
30+
31+
/*******************************************************************************/
32+
/*!
33+
@brief Initializes the INA228 sensor and begins I2C.
34+
@returns True if initialized successfully, False otherwise.
35+
*/
36+
/*******************************************************************************/
37+
bool drvIna228::begin() {
38+
_ina228 = new Adafruit_INA228();
39+
if (!_ina228->begin(_address, _i2c)) {
40+
WS_DEBUG_PRINTLN("INA228 failed to initialise!");
41+
return false;
42+
}
43+
44+
_ina228->setShunt(0.015, 10.0);
45+
if (_ina228->getCurrentConversionTime() != INA228_TIME_280_us) {
46+
_ina228->setCurrentConversionTime(INA228_TIME_280_us);
47+
}
48+
if (_ina228->getAveragingCount() != INA228_COUNT_16) {
49+
_ina228->setAveragingCount(INA228_COUNT_16);
50+
}
51+
if (_ina228->getVoltageConversionTime() != INA228_TIME_150_us) {
52+
_ina228->setVoltageConversionTime(INA228_TIME_150_us);
53+
}
54+
return true;
55+
}
56+
57+
/*******************************************************************************/
58+
/*!
59+
@brief Reads a voltage sensor and converts the
60+
reading into the expected SI unit.
61+
@param voltageEvent
62+
voltage sensor reading, in volts.
63+
@returns True if the sensor event was obtained successfully, False
64+
otherwise.
65+
*/
66+
/*******************************************************************************/
67+
bool drvIna228::getEventVoltage(sensors_event_t *voltageEvent) {
68+
voltageEvent->voltage = _ina228->getBusVoltage_V();
69+
return true;
70+
}
71+
72+
/**
73+
* @brief Get the current sensor event.
74+
*
75+
* @param currentEvent Pointer to the current sensor event.
76+
*
77+
* @returns True if the sensor event was obtained successfully, False
78+
* otherwise.
79+
*/
80+
bool drvIna228::getEventCurrent(sensors_event_t *currentEvent) {
81+
currentEvent->current = _ina228->getCurrent_mA();
82+
return true;
83+
}
84+
85+
void drvIna228::ConfigureDefaultSensorTypes() {
86+
_default_sensor_types_count = 2;
87+
_default_sensor_types[0] =
88+
wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE;
89+
_default_sensor_types[1] =
90+
wippersnapper_sensor_SensorType_SENSOR_TYPE_CURRENT;
91+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*!
2+
* @file drvIna228.h
3+
*
4+
* Device driver for the INA228 High Precision DC Current and Voltage Monitor
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
#ifndef DRV_INA228_H
16+
#define DRV_INA228_H
17+
18+
#include "drvBase.h"
19+
20+
class Adafruit_INA228;
21+
22+
/**************************************************************************/
23+
/*!
24+
@brief Class that provides a driver interface for a INA228 sensor.
25+
*/
26+
/**************************************************************************/
27+
class drvIna228 : public drvBase {
28+
public:
29+
/*******************************************************************************/
30+
/*!
31+
@brief Constructor for a INA228 sensor.
32+
@param i2c
33+
The I2C interface.
34+
@param sensorAddress
35+
7-bit device address.
36+
@param mux_channel
37+
The I2C multiplexer channel.
38+
@param driver_name
39+
The name of the driver.
40+
*/
41+
/*******************************************************************************/
42+
drvIna228(TwoWire *i2c, uint16_t sensorAddress, uint32_t mux_channel,
43+
const char *driver_name)
44+
: drvBase(i2c, sensorAddress, mux_channel, driver_name) {}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Destructor for an INA228 sensor.
49+
*/
50+
/*******************************************************************************/
51+
~drvIna228();
52+
53+
/*******************************************************************************/
54+
/*!
55+
@brief Initializes the INA228 sensor and begins I2C.
56+
@returns True if initialized successfully, False otherwise.
57+
*/
58+
/*******************************************************************************/
59+
bool begin();
60+
61+
/*******************************************************************************/
62+
/*!
63+
@brief Reads a voltage sensor and converts the
64+
reading into the expected SI unit.
65+
@param voltageEvent
66+
voltage sensor reading, in volts.
67+
@returns True if the sensor event was obtained successfully, False
68+
otherwise.
69+
*/
70+
/*******************************************************************************/
71+
bool getEventVoltage(sensors_event_t *voltageEvent);
72+
73+
/**
74+
* @brief Get the current sensor event.
75+
*
76+
* @param currentEvent Pointer to the current sensor event.
77+
*
78+
* @returns True if the sensor event was obtained successfully, False
79+
* otherwise.
80+
*/
81+
bool getEventCurrent(sensors_event_t *currentEvent);
82+
83+
void ConfigureDefaultSensorTypes() override;
84+
85+
protected:
86+
Adafruit_INA228 *_ina228; ///< Pointer to INA228 sensor object
87+
};
88+
89+
#endif // DRV_INA228_H

src/components/i2c/drivers/drvIna237.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ bool drvIna237::begin() {
4040
WS_DEBUG_PRINTLN("INA237 failed to initialise!");
4141
return false;
4242
}
43-
// TODO: use setCalibration()
43+
44+
_ina237->setShunt(0.015, 10.0);
45+
if (_ina237->getCurrentConversionTime() != INA2XX_TIME_280_us) {
46+
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);
47+
}
48+
if (_ina237->getAveragingCount() != INA2XX_COUNT_16) {
49+
_ina237->setAveragingCount(INA2XX_COUNT_16);
50+
}
51+
if (_ina237->getVoltageConversionTime() != INA2XX_TIME_150_us) {
52+
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
53+
}
4454

4555
return true;
4656
}

src/components/i2c/drivers/drvIna238.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,17 @@ bool drvIna238::begin() {
4040
WS_DEBUG_PRINTLN("INA238 failed to initialise!");
4141
return false;
4242
}
43-
// TODO: use setCalibration()
43+
44+
_ina238->setShunt(0.015, 10.0);
45+
if (_ina238->getCurrentConversionTime() != INA2XX_TIME_280_us) {
46+
_ina238->setCurrentConversionTime(INA2XX_TIME_280_us);
47+
}
48+
if (_ina238->getAveragingCount() != INA2XX_COUNT_16) {
49+
_ina238->setAveragingCount(INA2XX_COUNT_16);
50+
}
51+
if (_ina238->getVoltageConversionTime() != INA2XX_TIME_150_us) {
52+
_ina238->setVoltageConversionTime(INA2XX_TIME_150_us);
53+
}
4454

4555
return true;
4656
}

src/components/i2c/drivers/drvIna260.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ bool drvIna260::begin() {
4040
WS_DEBUG_PRINTLN("INA260 failed to initialise!");
4141
return false;
4242
}
43-
// TODO: use setCalibration()
43+
44+
_ina260->setAveragingCount(INA260_COUNT_16);
45+
_ina260->setVoltageConversionTime(INA260_TIME_140_us);
46+
_ina260->setCurrentConversionTime(INA260_TIME_140_us);
4447

4548
return true;
4649
}

0 commit comments

Comments
 (0)