Skip to content

Commit b391585

Browse files
committed
Add INA237 + INA238
1 parent af62bcc commit b391585

File tree

6 files changed

+278
-1
lines changed

6 files changed

+278
-1
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
99
architectures=*
10-
depends=SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SSD1306
10+
depends=SdFat - Adafruit Fork, Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit HDC302x, Adafruit INA219, Adafruit INA260 Library, Adafruit INA237 and INA238 Library, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit DS248x, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, Sensirion I2C SEN66, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, STM32duino VL53L4CX, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VCNL4200 Library, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS28, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork, Adafruit BusIO, Adafruit Unified Sensor, Sensirion Core, Adafruit GFX Library, Adafruit LED Backpack Library, Adafruit LiquidCrystal, Adafruit SSD1306

platformio.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ lib_deps =
3939
adafruit/Adafruit DS248x
4040
adafruit/Adafruit INA219
4141
adafruit/Adafruit INA260 Library
42+
adafruit/Adafruit INA237 and INA238 Library
4243
adafruit/Adafruit HDC302x
4344
adafruit/Adafruit HTS221
4445
adafruit/Adafruit HTU21DF Library

src/components/i2c/WipperSnapper_I2C.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
380380
_scd30->configureDriver(msgDeviceInitReq);
381381
drivers.push_back(_scd30);
382382
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
383+
} else if (strcmp("ina237", msgDeviceInitReq->i2c_device_name) == 0) {
384+
_ina237 = new WipperSnapper_I2C_Driver_INA237(this->_i2c, i2cAddress);
385+
if (!_ina237->begin()) {
386+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA237");
387+
_busStatusResponse =
388+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
389+
return false;
390+
}
391+
_ina237->configureDriver(msgDeviceInitReq);
392+
drivers.push_back(_ina237);
393+
WS_DEBUG_PRINTLN("INA237 Initialized Successfully!");
394+
} else if (strcmp("ina238", msgDeviceInitReq->i2c_device_name) == 0) {
395+
_ina238 = new WipperSnapper_I2C_Driver_INA238(this->_i2c, i2cAddress);
396+
if (!_ina238->begin()) {
397+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA238");
398+
_busStatusResponse =
399+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
400+
return false;
401+
}
402+
_ina238->configureDriver(msgDeviceInitReq);
403+
drivers.push_back(_ina238);
404+
WS_DEBUG_PRINTLN("INA238 Initialized Successfully!");
383405
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
384406
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
385407
if (!_ina219->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
3636
#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h"
3737
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
38+
#include "drivers/WipperSnapper_I2C_Driver_INA237.h"
39+
#include "drivers/WipperSnapper_I2C_Driver_INA238.h"
3840
#include "drivers/WipperSnapper_I2C_Driver_INA260.h"
3941
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
4042
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
@@ -162,6 +164,8 @@ class WipperSnapper_Component_I2C {
162164
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
163165
WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr;
164166
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
167+
WipperSnapper_I2C_Driver_INA237 *_ina237 = nullptr;
168+
WipperSnapper_I2C_Driver_INA238 *_ina238 = nullptr;
165169
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
166170
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
167171
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA237.h
3+
*
4+
* Device driver for the INA237 DC Current and Voltage Monitor
5+
* 16-bit ADC with ±0.3% gain error, ±50µV offset voltage
6+
* Cost-effective version, lower precision than INA238
7+
*
8+
* Adafruit invests time and resources providing this open source code,
9+
* please support Adafruit and open-source hardware by purchasing
10+
* products from Adafruit!
11+
*
12+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
13+
*
14+
* MIT license, all text here must be included in any redistribution.
15+
*
16+
*/
17+
#ifndef WipperSnapper_I2C_Driver_INA237_H
18+
#define WipperSnapper_I2C_Driver_INA237_H
19+
20+
#include "WipperSnapper_I2C_Driver.h"
21+
#include <Adafruit_INA237.h>
22+
23+
/**************************************************************************/
24+
/*!
25+
@brief Class that provides a driver interface for a INA237 sensor.
26+
*/
27+
/**************************************************************************/
28+
class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
29+
public:
30+
/*******************************************************************************/
31+
/*!
32+
@brief Constructor for a INA237 sensor.
33+
@param i2c
34+
The I2C interface.
35+
@param sensorAddress
36+
The 7-bit I2C address of the sensor.
37+
*/
38+
/*******************************************************************************/
39+
WipperSnapper_I2C_Driver_INA237(TwoWire *i2c, uint16_t sensorAddress)
40+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41+
_i2c = i2c;
42+
_sensorAddress = sensorAddress;
43+
}
44+
45+
/*******************************************************************************/
46+
/*!
47+
@brief Destructor for an INA237 sensor.
48+
*/
49+
/*******************************************************************************/
50+
~WipperSnapper_I2C_Driver_INA237() { delete _ina237; }
51+
52+
/*******************************************************************************/
53+
/*!
54+
@brief Initializes the INA237 sensor and begins I2C.
55+
@returns True if initialized successfully, False otherwise.
56+
*/
57+
/*******************************************************************************/
58+
bool begin() {
59+
_ina237 = new Adafruit_INA237();
60+
if (!_ina237->begin(_sensorAddress, _i2c)) {
61+
WS_DEBUG_PRINTLN("INA237 failed to initialise!");
62+
return false;
63+
}
64+
65+
// Configuration based on INA237 example sketch
66+
// Set default shunt resistance and maximum current
67+
// Default 0.015 ohm shunt, 10A max current
68+
_ina237->setShunt(0.015, 10.0);
69+
70+
// Set averaging for better accuracy (16 samples)
71+
_ina237->setAveragingCount(INA2XX_COUNT_16);
72+
73+
// Set conversion times as per example
74+
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
75+
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);
76+
77+
return true;
78+
}
79+
80+
/*******************************************************************************/
81+
/*!
82+
@brief Reads a voltage sensor and converts the
83+
reading into the expected SI unit.
84+
@param voltageEvent
85+
voltage sensor reading, in volts.
86+
@returns True if the sensor event was obtained successfully, False
87+
otherwise.
88+
*/
89+
/*******************************************************************************/
90+
bool getEventVoltage(sensors_event_t *voltageEvent) {
91+
voltageEvent->voltage = _ina237->getBusVoltage_V();
92+
return true;
93+
}
94+
95+
/**
96+
* @brief Get the current sensor event.
97+
*
98+
* @param currentEvent Pointer to the current sensor event.
99+
*
100+
* @returns True if the sensor event was obtained successfully, False
101+
* otherwise.
102+
*/
103+
bool getEventCurrent(sensors_event_t *currentEvent) {
104+
currentEvent->current = _ina237->getCurrent_mA();
105+
return true;
106+
}
107+
108+
/**
109+
* @brief Get the raw (power) sensor event.
110+
*
111+
* @param powerEvent Pointer to the power sensor event.
112+
*
113+
* @returns True if the sensor event was obtained successfully, False
114+
* otherwise.
115+
*/
116+
bool getEventRaw(sensors_event_t *powerEvent) {
117+
powerEvent->data[0] = _ina237->getPower_mW();
118+
return true;
119+
}
120+
121+
protected:
122+
Adafruit_INA237 *_ina237 = nullptr; ///< Pointer to INA237 sensor object
123+
};
124+
125+
#endif // WipperSnapper_I2C_Driver_INA237
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA238.h
3+
*
4+
* Device driver for the INA238 High-precision DC Current and Voltage Monitor
5+
* 16-bit ADC with ±0.1% gain error, ±5µV offset voltage
6+
* Higher precision version compared to INA237
7+
*
8+
* Adafruit invests time and resources providing this open source code,
9+
* please support Adafruit and open-source hardware by purchasing
10+
* products from Adafruit!
11+
*
12+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
13+
*
14+
* MIT license, all text here must be included in any redistribution.
15+
*
16+
*/
17+
#ifndef WipperSnapper_I2C_Driver_INA238_H
18+
#define WipperSnapper_I2C_Driver_INA238_H
19+
20+
#include "WipperSnapper_I2C_Driver.h"
21+
#include <Adafruit_INA238.h>
22+
23+
/**************************************************************************/
24+
/*!
25+
@brief Class that provides a driver interface for a INA238 sensor.
26+
*/
27+
/**************************************************************************/
28+
class WipperSnapper_I2C_Driver_INA238 : public WipperSnapper_I2C_Driver {
29+
public:
30+
/*******************************************************************************/
31+
/*!
32+
@brief Constructor for a INA238 sensor.
33+
@param i2c
34+
The I2C interface.
35+
@param sensorAddress
36+
The 7-bit I2C address of the sensor.
37+
*/
38+
/*******************************************************************************/
39+
WipperSnapper_I2C_Driver_INA238(TwoWire *i2c, uint16_t sensorAddress)
40+
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41+
_i2c = i2c;
42+
_sensorAddress = sensorAddress;
43+
}
44+
45+
/*******************************************************************************/
46+
/*!
47+
@brief Destructor for an INA238 sensor.
48+
*/
49+
/*******************************************************************************/
50+
~WipperSnapper_I2C_Driver_INA238() { delete _ina238; }
51+
52+
/*******************************************************************************/
53+
/*!
54+
@brief Initializes the INA238 sensor and begins I2C.
55+
@returns True if initialized successfully, False otherwise.
56+
*/
57+
/*******************************************************************************/
58+
bool begin() {
59+
_ina238 = new Adafruit_INA238();
60+
if (!_ina238->begin(_sensorAddress, _i2c)) {
61+
WS_DEBUG_PRINTLN("INA238 failed to initialise!");
62+
return false;
63+
}
64+
65+
// Configuration based on INA238 example sketch
66+
// Set default shunt resistance and maximum current
67+
// Default 0.015 ohm shunt, 10A max current
68+
_ina238->setShunt(0.015, 10.0);
69+
70+
// Set averaging for better accuracy (16 samples)
71+
_ina238->setAveragingCount(INA2XX_COUNT_16);
72+
73+
// Set conversion times as per example
74+
_ina238->setVoltageConversionTime(INA2XX_TIME_150_us);
75+
_ina238->setCurrentConversionTime(INA2XX_TIME_280_us);
76+
77+
return true;
78+
}
79+
80+
/*******************************************************************************/
81+
/*!
82+
@brief Reads a voltage sensor and converts the
83+
reading into the expected SI unit.
84+
@param voltageEvent
85+
voltage sensor reading, in volts.
86+
@returns True if the sensor event was obtained successfully, False
87+
otherwise.
88+
*/
89+
/*******************************************************************************/
90+
bool getEventVoltage(sensors_event_t *voltageEvent) {
91+
voltageEvent->voltage = _ina238->getBusVoltage_V();
92+
return true;
93+
}
94+
95+
/**
96+
* @brief Get the current sensor event.
97+
*
98+
* @param currentEvent Pointer to the current sensor event.
99+
*
100+
* @returns True if the sensor event was obtained successfully, False
101+
* otherwise.
102+
*/
103+
bool getEventCurrent(sensors_event_t *currentEvent) {
104+
currentEvent->current = _ina238->getCurrent_mA();
105+
return true;
106+
}
107+
108+
/**
109+
* @brief Get the Raw (power) sensor event.
110+
*
111+
* @param powerEvent Pointer to the power sensor event.
112+
*
113+
* @returns True if the sensor event was obtained successfully, False
114+
* otherwise.
115+
*/
116+
bool getEventRaw(sensors_event_t *powerEvent) {
117+
powerEvent->data[0] = _ina238->getPower_mW();
118+
return true;
119+
}
120+
121+
protected:
122+
Adafruit_INA238 *_ina238 = nullptr; ///< Pointer to INA238 sensor object
123+
};
124+
125+
#endif // WipperSnapper_I2C_Driver_INA238

0 commit comments

Comments
 (0)