Skip to content

Commit 895bd13

Browse files
authored
Merge pull request #765 from adafruit/ina238
Ina238 + 237 via a refactor of INA260 too.
2 parents 029602a + 2e97574 commit 895bd13

11 files changed

+448
-68
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=OmronD6T - Community Fork, 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 SH110X, Adafruit SSD1306
10+
depends=OmronD6T - Community Fork, 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 SH110X, 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
@@ -391,6 +391,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
391391
_scd30->configureDriver(msgDeviceInitReq);
392392
drivers.push_back(_scd30);
393393
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
394+
} else if (strcmp("ina237", msgDeviceInitReq->i2c_device_name) == 0) {
395+
_ina237 = new WipperSnapper_I2C_Driver_INA237(this->_i2c, i2cAddress);
396+
if (!_ina237->begin()) {
397+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA237");
398+
_busStatusResponse =
399+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
400+
return false;
401+
}
402+
_ina237->configureDriver(msgDeviceInitReq);
403+
drivers.push_back(_ina237);
404+
WS_DEBUG_PRINTLN("INA237 Initialized Successfully!");
405+
} else if (strcmp("ina238", msgDeviceInitReq->i2c_device_name) == 0) {
406+
_ina238 = new WipperSnapper_I2C_Driver_INA238(this->_i2c, i2cAddress);
407+
if (!_ina238->begin()) {
408+
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA238");
409+
_busStatusResponse =
410+
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
411+
return false;
412+
}
413+
_ina238->configureDriver(msgDeviceInitReq);
414+
drivers.push_back(_ina238);
415+
WS_DEBUG_PRINTLN("INA238 Initialized Successfully!");
394416
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
395417
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
396418
if (!_ina219->begin()) {

src/components/i2c/WipperSnapper_I2C.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
3737
#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h"
3838
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
39+
#include "drivers/WipperSnapper_I2C_Driver_INA237.h"
40+
#include "drivers/WipperSnapper_I2C_Driver_INA238.h"
3941
#include "drivers/WipperSnapper_I2C_Driver_INA260.h"
4042
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
4143
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
@@ -86,6 +88,9 @@
8688

8789
// forward decl.
8890
class Wippersnapper;
91+
class WipperSnapper_I2C_Driver_INA260;
92+
class WipperSnapper_I2C_Driver_INA237;
93+
class WipperSnapper_I2C_Driver_INA238;
8994

9095
/**************************************************************************/
9196
/*!
@@ -165,6 +170,8 @@ class WipperSnapper_Component_I2C {
165170
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
166171
WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr;
167172
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
173+
WipperSnapper_I2C_Driver_INA237 *_ina237 = nullptr;
174+
WipperSnapper_I2C_Driver_INA238 *_ina238 = nullptr;
168175
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
169176
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
170177
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;

src/components/i2c/drivers/WipperSnapper_I2C_Driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#ifndef WipperSnapper_I2C_Driver_H
1717
#define WipperSnapper_I2C_Driver_H
1818

19+
#include "wippersnapper/i2c/v1/i2c.pb.h"
1920
#include <Adafruit_Sensor.h>
2021
#include <Arduino.h>
22+
#include <Wire.h>
2123

2224
#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000))
2325
///< Used for last sensor read time, initially set 24hrs ago (max period)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA237.cpp
3+
*
4+
* Device driver implementation for the INA237 DC Current and Voltage Monitor
5+
* (Avoids import conflict with INA260 typedef enum _mode etc)
6+
*
7+
* Adafruit invests time and resources providing this open source code,
8+
* please support Adafruit and open-source hardware by purchasing
9+
* products from Adafruit!
10+
*
11+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
12+
*
13+
* MIT license, all text here must be included in any redistribution.
14+
*
15+
*/
16+
17+
#include "WipperSnapper_I2C_Driver_INA237.h"
18+
#include "Wippersnapper.h"
19+
#include <Adafruit_INA237.h>
20+
21+
/*******************************************************************************/
22+
/*!
23+
@brief Constructor for a INA237 sensor.
24+
@param i2c
25+
The I2C interface.
26+
@param sensorAddress
27+
The 7-bit I2C address of the sensor.
28+
*/
29+
/*******************************************************************************/
30+
WipperSnapper_I2C_Driver_INA237::WipperSnapper_I2C_Driver_INA237(
31+
TwoWire *i2c, uint16_t sensorAddress)
32+
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina237(nullptr) {
33+
_i2c = i2c;
34+
_sensorAddress = sensorAddress;
35+
}
36+
37+
/*******************************************************************************/
38+
/*!
39+
@brief Destructor for an INA237 sensor.
40+
*/
41+
/*******************************************************************************/
42+
WipperSnapper_I2C_Driver_INA237::~WipperSnapper_I2C_Driver_INA237() {
43+
delete _ina237;
44+
}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Initializes the INA237 sensor and begins I2C.
49+
@returns True if initialized successfully, False otherwise.
50+
*/
51+
/*******************************************************************************/
52+
bool WipperSnapper_I2C_Driver_INA237::begin() {
53+
_ina237 = new Adafruit_INA237();
54+
if (!_ina237->begin(_sensorAddress, _i2c)) {
55+
return false;
56+
}
57+
58+
// Configuration based on INA237 example sketch
59+
// Set default shunt resistance and maximum current
60+
// Default 0.015 ohm shunt, 10A max current
61+
_ina237->setShunt(0.015, 10.0);
62+
63+
// Set averaging for better accuracy (16 samples)
64+
_ina237->setAveragingCount(INA2XX_COUNT_16);
65+
66+
// Set conversion times as per example
67+
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
68+
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);
69+
70+
return true;
71+
}
72+
73+
/*******************************************************************************/
74+
/*!
75+
@brief Reads a voltage sensor and converts the
76+
reading into the expected SI unit.
77+
@param voltageEvent
78+
voltage sensor reading, in volts.
79+
@returns True if the sensor event was obtained successfully, False
80+
otherwise.
81+
*/
82+
/*******************************************************************************/
83+
bool WipperSnapper_I2C_Driver_INA237::getEventVoltage(
84+
sensors_event_t *voltageEvent) {
85+
voltageEvent->voltage = _ina237->getBusVoltage_V();
86+
return true;
87+
}
88+
89+
/**
90+
* @brief Get the current sensor event.
91+
*
92+
* @param currentEvent Pointer to the current sensor event.
93+
*
94+
* @returns True if the sensor event was obtained successfully, False
95+
* otherwise.
96+
*/
97+
bool WipperSnapper_I2C_Driver_INA237::getEventCurrent(
98+
sensors_event_t *currentEvent) {
99+
currentEvent->current = _ina237->getCurrent_mA();
100+
return true;
101+
}
102+
103+
/**
104+
* @brief Get the raw (power) sensor event.
105+
*
106+
* @param powerEvent Pointer to the power sensor event.
107+
*
108+
* @returns True if the sensor event was obtained successfully, False
109+
* otherwise.
110+
*/
111+
bool WipperSnapper_I2C_Driver_INA237::getEventRaw(sensors_event_t *powerEvent) {
112+
powerEvent->data[0] = _ina237->getPower_mW();
113+
return true;
114+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 "Wippersnapper.h"
22+
23+
// Forward declaration
24+
class Adafruit_INA237;
25+
26+
/**************************************************************************/
27+
/*!
28+
@brief Class that provides a driver interface for a INA237 sensor.
29+
*/
30+
/**************************************************************************/
31+
class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
32+
public:
33+
WipperSnapper_I2C_Driver_INA237(TwoWire *i2c, uint16_t sensorAddress);
34+
~WipperSnapper_I2C_Driver_INA237();
35+
36+
bool begin();
37+
bool getEventVoltage(sensors_event_t *voltageEvent);
38+
bool getEventCurrent(sensors_event_t *currentEvent);
39+
bool getEventRaw(sensors_event_t *powerEvent);
40+
41+
protected:
42+
Adafruit_INA237 *_ina237; ///< Pointer to INA237 sensor object
43+
};
44+
45+
#endif // WipperSnapper_I2C_Driver_INA237
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*!
2+
* @file WipperSnapper_I2C_Driver_INA238.cpp
3+
*
4+
* Device driver implementation for the INA238 High-precision DC Current and
5+
* Voltage Monitor (Avoids import conflict with INA260 typedef enum _mode etc)
6+
*
7+
* Adafruit invests time and resources providing this open source code,
8+
* please support Adafruit and open-source hardware by purchasing
9+
* products from Adafruit!
10+
*
11+
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
12+
*
13+
* MIT license, all text here must be included in any redistribution.
14+
*
15+
*/
16+
17+
#include "WipperSnapper_I2C_Driver_INA238.h"
18+
#include "Wippersnapper.h"
19+
#include <Adafruit_INA238.h>
20+
21+
/*******************************************************************************/
22+
/*!
23+
@brief Constructor for a INA238 sensor.
24+
@param i2c
25+
The I2C interface.
26+
@param sensorAddress
27+
The 7-bit I2C address of the sensor.
28+
*/
29+
/*******************************************************************************/
30+
WipperSnapper_I2C_Driver_INA238::WipperSnapper_I2C_Driver_INA238(
31+
TwoWire *i2c, uint16_t sensorAddress)
32+
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina238(nullptr) {
33+
_i2c = i2c;
34+
_sensorAddress = sensorAddress;
35+
}
36+
37+
/*******************************************************************************/
38+
/*!
39+
@brief Destructor for an INA238 sensor.
40+
*/
41+
/*******************************************************************************/
42+
WipperSnapper_I2C_Driver_INA238::~WipperSnapper_I2C_Driver_INA238() {
43+
delete _ina238;
44+
}
45+
46+
/*******************************************************************************/
47+
/*!
48+
@brief Initializes the INA238 sensor and begins I2C.
49+
@returns True if initialized successfully, False otherwise.
50+
*/
51+
/*******************************************************************************/
52+
bool WipperSnapper_I2C_Driver_INA238::begin() {
53+
_ina238 = new Adafruit_INA238();
54+
if (!_ina238->begin(_sensorAddress, _i2c)) {
55+
return false;
56+
}
57+
58+
// Configuration based on INA238 example sketch
59+
// Set default shunt resistance and maximum current
60+
// Default 0.015 ohm shunt, 10A max current
61+
_ina238->setShunt(0.015, 10.0);
62+
63+
// Set averaging for better accuracy (16 samples)
64+
_ina238->setAveragingCount(INA2XX_COUNT_16);
65+
66+
// Set conversion times as per example
67+
_ina238->setVoltageConversionTime(INA2XX_TIME_150_us);
68+
_ina238->setCurrentConversionTime(INA2XX_TIME_280_us);
69+
70+
return true;
71+
}
72+
73+
/*******************************************************************************/
74+
/*!
75+
@brief Reads a voltage sensor and converts the
76+
reading into the expected SI unit.
77+
@param voltageEvent
78+
voltage sensor reading, in volts.
79+
@returns True if the sensor event was obtained successfully, False
80+
otherwise.
81+
*/
82+
/*******************************************************************************/
83+
bool WipperSnapper_I2C_Driver_INA238::getEventVoltage(
84+
sensors_event_t *voltageEvent) {
85+
voltageEvent->voltage = _ina238->getBusVoltage_V();
86+
return true;
87+
}
88+
89+
/**
90+
* @brief Get the current sensor event.
91+
*
92+
* @param currentEvent Pointer to the current sensor event.
93+
*
94+
* @returns True if the sensor event was obtained successfully, False
95+
* otherwise.
96+
*/
97+
bool WipperSnapper_I2C_Driver_INA238::getEventCurrent(
98+
sensors_event_t *currentEvent) {
99+
currentEvent->current = _ina238->getCurrent_mA();
100+
return true;
101+
}
102+
103+
/**
104+
* @brief Get the Raw (power) sensor event.
105+
*
106+
* @param powerEvent Pointer to the power sensor event.
107+
*
108+
* @returns True if the sensor event was obtained successfully, False
109+
* otherwise.
110+
*/
111+
bool WipperSnapper_I2C_Driver_INA238::getEventRaw(sensors_event_t *powerEvent) {
112+
powerEvent->data[0] = _ina238->getPower_mW();
113+
return true;
114+
}

0 commit comments

Comments
 (0)