-
Notifications
You must be signed in to change notification settings - Fork 52
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
af62bcc
b391585
1c6813f
b4bf436
7f0f1a2
c5b8706
3e2f7e4
00deb17
50b924c
a1cdc01
c9e728c
08fa3ac
29ef4c2
860822a
51b9335
398fb7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this added? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failed in https://github.com/adafruit/Adafruit_Wippersnapper_Arduino/actions/runs/16222764678/job/45807199135 |
||
|
||
#define PERIOD_24HRS_AGO_MILLIS (millis() - (24 * 60 * 60 * 1000)) | ||
///< Used for last sensor read time, initially set 24hrs ago (max period) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/*! | ||
* @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) | ||
* | ||
tyeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
|
||
#include "WipperSnapper_I2C_Driver_INA237.h" | ||
#include "../../../Wippersnapper.h" | ||
tyeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#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)) { | ||
WS_DEBUG_PRINTLN("INA237 failed to initialise!"); | ||
tyeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/*! | ||
* @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: | ||
/*******************************************************************************/ | ||
/*! | ||
@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(TwoWire *i2c, uint16_t sensorAddress); | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Destructor for an INA237 sensor. | ||
*/ | ||
/*******************************************************************************/ | ||
~WipperSnapper_I2C_Driver_INA237(); | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Initializes the INA237 sensor and begins I2C. | ||
@returns True if initialized successfully, False otherwise. | ||
*/ | ||
/*******************************************************************************/ | ||
bool begin(); | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@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 getEventVoltage(sensors_event_t *voltageEvent); | ||
|
||
/** | ||
* @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 getEventCurrent(sensors_event_t *currentEvent); | ||
|
||
/** | ||
tyeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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 getEventRaw(sensors_event_t *powerEvent); | ||
|
||
protected: | ||
Adafruit_INA237 *_ina237; ///< Pointer to INA237 sensor object | ||
}; | ||
|
||
#endif // WipperSnapper_I2C_Driver_INA237 |
Uh oh!
There was an error while loading. Please reload this page.