Skip to content

Ina238 + 237 via a refactor of INA260 too. #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions .github/workflows/build-clang-doxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,34 @@ jobs:
[
"funhouse_noota",
"esp32s3_devkitc_1_n8",
"magtag",
"metroesp32s2",
"feather_esp32s2",
"feather_esp32s2_tft",
"feather_esp32s2_reverse_tft",
"qtpy_esp32s2",
"feather_esp32s3_reverse_tft",
"qtpy_esp32s3_n4r2",
"feather_esp32s3_4mbflash_2mbpsram",
"feather_esp32s3_tft",
"xiao_esp32s3",
]
include:
- offset: "0x0"
- offset: "0x1000"
arduino-platform: "funhouse_noota"
- offset: "0x1000"
arduino-platform: "magtag"
- offset: "0x1000"
arduino-platform: "metroesp32s2"
- offset: "0x1000"
arduino-platform: "feather_esp32s2"
- offset: "0x1000"
arduino-platform: "feather_esp32s2_tft"
- offset: "0x1000"
arduino-platform: "feather_esp32s2_reverse_tft"
- offset: "0x1000"
arduino-platform: "qtpy_esp32s2"
steps:
- name: "skip if unwanted"
continue-on-error: true
Expand All @@ -54,13 +76,14 @@ jobs:
- uses: actions/checkout@v4
with:
repository: adafruit/ci-arduino
ref: ci-wippersnapper
ref: ci-wippersnapper-2025-07-10-noota
path: ci
- name: Checkout Board Definitions
uses: actions/checkout@v4
with:
repository: adafruit/Wippersnapper_Boards
path: ws-boards
ref: migrate-4mb-to-esptool
- name: Install CI-Arduino
run: bash ci/actions_install.sh
- name: Install extra Arduino libraries
Expand Down Expand Up @@ -200,19 +223,9 @@ jobs:
matrix:
arduino-platform:
[
"magtag",
"metroesp32s2",
"metro_esp32s3",
"feather_esp32s2",
"feather_esp32s2_tft",
"feather_esp32s2_reverse_tft",
"feather_esp32s3",
"feather_esp32s3_4mbflash_2mbpsram",
"feather_esp32s3_tft",
"qtpy_esp32s3",
"qtpy_esp32s2",
"feather_esp32s3_reverse_tft",
"qtpy_esp32s3_n4r2",
]
steps:
- name: "skip if unwanted"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
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
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
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lib_deps =
adafruit/Adafruit DS248x
adafruit/Adafruit INA219
adafruit/Adafruit INA260 Library
adafruit/Adafruit INA237 and INA238 Library
adafruit/Adafruit HDC302x
adafruit/Adafruit HTS221
adafruit/Adafruit HTU21DF Library
Expand Down
22 changes: 22 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,28 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_scd30->configureDriver(msgDeviceInitReq);
drivers.push_back(_scd30);
WS_DEBUG_PRINTLN("SCD30 Initialized Successfully!");
} else if (strcmp("ina237", msgDeviceInitReq->i2c_device_name) == 0) {
_ina237 = new WipperSnapper_I2C_Driver_INA237(this->_i2c, i2cAddress);
if (!_ina237->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA237");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_ina237->configureDriver(msgDeviceInitReq);
drivers.push_back(_ina237);
WS_DEBUG_PRINTLN("INA237 Initialized Successfully!");
} else if (strcmp("ina238", msgDeviceInitReq->i2c_device_name) == 0) {
_ina238 = new WipperSnapper_I2C_Driver_INA238(this->_i2c, i2cAddress);
if (!_ina238->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize INA238");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_ina238->configureDriver(msgDeviceInitReq);
drivers.push_back(_ina238);
WS_DEBUG_PRINTLN("INA238 Initialized Successfully!");
} else if (strcmp("ina219", msgDeviceInitReq->i2c_device_name) == 0) {
_ina219 = new WipperSnapper_I2C_Driver_INA219(this->_i2c, i2cAddress);
if (!_ina219->begin()) {
Expand Down
7 changes: 7 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "drivers/WipperSnapper_I2C_Driver_HTU21D.h"
#include "drivers/WipperSnapper_I2C_Driver_HTU31D.h"
#include "drivers/WipperSnapper_I2C_Driver_INA219.h"
#include "drivers/WipperSnapper_I2C_Driver_INA237.h"
#include "drivers/WipperSnapper_I2C_Driver_INA238.h"
#include "drivers/WipperSnapper_I2C_Driver_INA260.h"
#include "drivers/WipperSnapper_I2C_Driver_LC709203F.h"
#include "drivers/WipperSnapper_I2C_Driver_LPS22HB.h"
Expand Down Expand Up @@ -86,6 +88,9 @@

// forward decl.
class Wippersnapper;
class WipperSnapper_I2C_Driver_INA260;
class WipperSnapper_I2C_Driver_INA237;
class WipperSnapper_I2C_Driver_INA238;

/**************************************************************************/
/*!
Expand Down Expand Up @@ -165,6 +170,8 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_HTU21D *_htu21d = nullptr;
WipperSnapper_I2C_Driver_HTU31D *_htu31d = nullptr;
WipperSnapper_I2C_Driver_INA219 *_ina219 = nullptr;
WipperSnapper_I2C_Driver_INA237 *_ina237 = nullptr;
WipperSnapper_I2C_Driver_INA238 *_ina238 = nullptr;
WipperSnapper_I2C_Driver_INA260 *_ina260 = nullptr;
WipperSnapper_I2C_Driver_LTR329_LTR303 *_ltr329 = nullptr;
WipperSnapper_I2C_Driver_LTR390 *_ltr390 = nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#ifndef WipperSnapper_I2C_Driver_H
#define WipperSnapper_I2C_Driver_H

#include "wippersnapper/i2c/v1/i2c.pb.h"
#include <Adafruit_Sensor.h>
#include <Arduino.h>
#include <Wire.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this added?

Copy link
Member Author

@tyeth tyeth Jul 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous chain of header includes get broken / ignored when switching scope to the cpp file, so as the includes from the ina2xx cpp call back up to this file it needs the Wire included to then refer to these twowire instances.
Just retested with it removed and platformIO fails to build. Letting CI run now to see if arduino likes it still. Then will reinstate #include <Wire.h>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000))
///< Used for last sensor read time, initially set 24hrs ago (max period)
Expand Down
114 changes: 114 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*!
* @file WipperSnapper_I2C_Driver_INA237.cpp
*
* Device driver implementation for the INA237 DC Current and Voltage Monitor
* (Avoids import conflict with INA260 typedef enum _mode etc)
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/

#include "WipperSnapper_I2C_Driver_INA237.h"
#include "Wippersnapper.h"
#include <Adafruit_INA237.h>

/*******************************************************************************/
/*!
@brief Constructor for a INA237 sensor.
@param i2c
The I2C interface.
@param sensorAddress
The 7-bit I2C address of the sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_INA237::WipperSnapper_I2C_Driver_INA237(
TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress), _ina237(nullptr) {
_i2c = i2c;
_sensorAddress = sensorAddress;
}

/*******************************************************************************/
/*!
@brief Destructor for an INA237 sensor.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_INA237::~WipperSnapper_I2C_Driver_INA237() {
delete _ina237;
}

/*******************************************************************************/
/*!
@brief Initializes the INA237 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool WipperSnapper_I2C_Driver_INA237::begin() {
_ina237 = new Adafruit_INA237();
if (!_ina237->begin(_sensorAddress, _i2c)) {
return false;
}

// Configuration based on INA237 example sketch
// Set default shunt resistance and maximum current
// Default 0.015 ohm shunt, 10A max current
_ina237->setShunt(0.015, 10.0);

// Set averaging for better accuracy (16 samples)
_ina237->setAveragingCount(INA2XX_COUNT_16);

// Set conversion times as per example
_ina237->setVoltageConversionTime(INA2XX_TIME_150_us);
_ina237->setCurrentConversionTime(INA2XX_TIME_280_us);

return true;
}

/*******************************************************************************/
/*!
@brief Reads a voltage sensor and converts the
reading into the expected SI unit.
@param voltageEvent
voltage sensor reading, in volts.
@returns True if the sensor event was obtained successfully, False
otherwise.
*/
/*******************************************************************************/
bool WipperSnapper_I2C_Driver_INA237::getEventVoltage(
sensors_event_t *voltageEvent) {
voltageEvent->voltage = _ina237->getBusVoltage_V();
return true;
}

/**
* @brief Get the current sensor event.
*
* @param currentEvent Pointer to the current sensor event.
*
* @returns True if the sensor event was obtained successfully, False
* otherwise.
*/
bool WipperSnapper_I2C_Driver_INA237::getEventCurrent(
sensors_event_t *currentEvent) {
currentEvent->current = _ina237->getCurrent_mA();
return true;
}

/**
* @brief Get the raw (power) sensor event.
*
* @param powerEvent Pointer to the power sensor event.
*
* @returns True if the sensor event was obtained successfully, False
* otherwise.
*/
bool WipperSnapper_I2C_Driver_INA237::getEventRaw(sensors_event_t *powerEvent) {
powerEvent->data[0] = _ina237->getPower_mW();
return true;
}
45 changes: 45 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_INA237.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
* @file WipperSnapper_I2C_Driver_INA237.h
*
* Device driver for the INA237 DC Current and Voltage Monitor
* 16-bit ADC with ±0.3% gain error, ±50µV offset voltage
* Cost-effective version, lower precision than INA238
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry 2025 for Adafruit Industries.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef WipperSnapper_I2C_Driver_INA237_H
#define WipperSnapper_I2C_Driver_INA237_H

#include "WipperSnapper_I2C_Driver.h"
#include "Wippersnapper.h"

// Forward declaration
class Adafruit_INA237;

/**************************************************************************/
/*!
@brief Class that provides a driver interface for a INA237 sensor.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_INA237 : public WipperSnapper_I2C_Driver {
public:
WipperSnapper_I2C_Driver_INA237(TwoWire *i2c, uint16_t sensorAddress);
~WipperSnapper_I2C_Driver_INA237();

bool begin();
bool getEventVoltage(sensors_event_t *voltageEvent);
bool getEventCurrent(sensors_event_t *currentEvent);
bool getEventRaw(sensors_event_t *powerEvent);

protected:
Adafruit_INA237 *_ina237; ///< Pointer to INA237 sensor object
};

#endif // WipperSnapper_I2C_Driver_INA237
Loading
Loading