Skip to content
Snippets Groups Projects
Commit 29cb498a authored by Bilal Hassan's avatar Bilal Hassan
Browse files

Resulting documentation after working with Betka on the ina219

parent f75525ed
No related branches found
No related tags found
1 merge request!10merge serial comm and sd write into espnow
Showing with 290 additions and 0 deletions
#include <Wire.h>
#include <INA219_WE.h>
/* There are several ways to create your INA219 object:
* INA219_WE ina219 = INA219_WE() -> uses Wire / I2C Address = 0x40
* INA219_WE ina219 = INA219_WE(ICM20948_ADDR) -> uses Wire / I2C_ADDRESS
* INA219_WE ina219 = INA219_WE(&wire2) -> uses the TwoWire object wire2 / I2C_ADDRESS
* INA219_WE ina219 = INA219_WE(&wire2, I2C_ADDRESS) -> all together
* Successfully tested with two I2C busses on an ESP32
*/
INA219_WE ina219 = INA219_WE();
int sda_pin = 5; // GPIO16 as I2C SDA
int scl_pin = 4; // GPIO17 as I2C SCL
void setup() {
Serial.begin(9600);
Wire.setPins(sda_pin, scl_pin);
Wire.begin();
if(!ina219.init()){
Serial.println("INA219 not connected!");
}
/* Set ADC Mode for Bus and ShuntVoltage
* Mode * * Res / Samples * * Conversion Time *
BIT_MODE_9 9 Bit Resolution 84 µs
BIT_MODE_10 10 Bit Resolution 148 µs
BIT_MODE_11 11 Bit Resolution 276 µs
BIT_MODE_12 12 Bit Resolution 532 µs (DEFAULT)
SAMPLE_MODE_2 Mean Value 2 samples 1.06 ms
SAMPLE_MODE_4 Mean Value 4 samples 2.13 ms
SAMPLE_MODE_8 Mean Value 8 samples 4.26 ms
SAMPLE_MODE_16 Mean Value 16 samples 8.51 ms
SAMPLE_MODE_32 Mean Value 32 samples 17.02 ms
SAMPLE_MODE_64 Mean Value 64 samples 34.05 ms
SAMPLE_MODE_128 Mean Value 128 samples 68.10 ms
*/
//ina219.setADCMode(SAMPLE_MODE_128); // choose mode and uncomment for change of default
/* Set measure mode
POWER_DOWN - INA219 switched off
TRIGGERED - measurement on demand
ADC_OFF - Analog/Digital Converter switched off
CONTINUOUS - Continuous measurements (DEFAULT)
*/
// ina219.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
/* Set PGain
* Gain * * Shunt Voltage Range * * Max Current (if shunt is 0.1 ohms) *
PG_40 40 mV 0.4 A
PG_80 80 mV 0.8 A
PG_160 160 mV 1.6 A
PG_320 320 mV 3.2 A (DEFAULT)
*/
// ina219.setPGain(PG_320); // choose gain and uncomment for change of default
/* Set Bus Voltage Range
BRNG_16 -> 16 V
BRNG_32 -> 32 V (DEFAULT)
*/
// ina219.setBusRange(BRNG_32); // choose range and uncomment for change of default
Serial.println("INA219 Current Sensor Example Sketch - Continuous");
/* If the current values delivered by the INA219 differ by a constant factor
from values obtained with calibrated equipment you can define a correction factor.
Correction factor = current delivered from calibrated equipment / current delivered by INA219
*/
// ina219.setCorrectionFactor(0.98); // insert your correction factor if necessary
/* If you experience a shunt voltage offset, that means you detect a shunt voltage which is not
zero, although the current should be zero, you can apply a correction. For this, uncomment the
following function and apply the offset you have detected.
*/
// ina219.setShuntVoltOffset_mV(0.5); // insert the shunt voltage (millivolts) you detect at zero current
}
void loop() {
float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getBusPower();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV/1000);
ina219_overflow = ina219.getOverflow();
Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
Serial.print("Current[mA]: "); Serial.println(current_mA);
Serial.print("Bus Power [mW]: "); Serial.println(power_mW);
if(!ina219_overflow){
Serial.println("Values OK - no overflow");
}
else{
Serial.println("Overflow! Choose higher PGAIN");
}
Serial.println();
delay(3000);
}
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:esp32-c3-devkitm-1]
platform = espressif32
board = esp32-c3-devkitm-1
framework = arduino
lib_deps = wollewald/INA219_WE@^1.3.1
#include <Wire.h>
#include <INA219_WE.h>
/* There are several ways to create your INA219 object:
* INA219_WE ina219 = INA219_WE() -> uses Wire / I2C Address = 0x40
* INA219_WE ina219 = INA219_WE(ICM20948_ADDR) -> uses Wire / I2C_ADDRESS
* INA219_WE ina219 = INA219_WE(&wire2) -> uses the TwoWire object wire2 / I2C_ADDRESS
* INA219_WE ina219 = INA219_WE(&wire2, I2C_ADDRESS) -> all together
* Successfully tested with two I2C busses on an ESP32
*/
INA219_WE ina219 = INA219_WE();
int sda_pin = 5; // GPIO16 as I2C SDA
int scl_pin = 4; // GPIO17 as I2C SCL
void setup() {
Serial.begin(9600);
Wire.setPins(sda_pin, scl_pin);
Wire.begin();
if(!ina219.init()){
Serial.println("INA219 not connected!");
}
/* Set ADC Mode for Bus and ShuntVoltage
* Mode * * Res / Samples * * Conversion Time *
BIT_MODE_9 9 Bit Resolution 84 µs
BIT_MODE_10 10 Bit Resolution 148 µs
BIT_MODE_11 11 Bit Resolution 276 µs
BIT_MODE_12 12 Bit Resolution 532 µs (DEFAULT)
SAMPLE_MODE_2 Mean Value 2 samples 1.06 ms
SAMPLE_MODE_4 Mean Value 4 samples 2.13 ms
SAMPLE_MODE_8 Mean Value 8 samples 4.26 ms
SAMPLE_MODE_16 Mean Value 16 samples 8.51 ms
SAMPLE_MODE_32 Mean Value 32 samples 17.02 ms
SAMPLE_MODE_64 Mean Value 64 samples 34.05 ms
SAMPLE_MODE_128 Mean Value 128 samples 68.10 ms
*/
//ina219.setADCMode(SAMPLE_MODE_128); // choose mode and uncomment for change of default
/* Set measure mode
POWER_DOWN - INA219 switched off
TRIGGERED - measurement on demand
ADC_OFF - Analog/Digital Converter switched off
CONTINUOUS - Continuous measurements (DEFAULT)
*/
// ina219.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
/* Set PGain
* Gain * * Shunt Voltage Range * * Max Current (if shunt is 0.1 ohms) *
PG_40 40 mV 0.4 A
PG_80 80 mV 0.8 A
PG_160 160 mV 1.6 A
PG_320 320 mV 3.2 A (DEFAULT)
*/
// ina219.setPGain(PG_320); // choose gain and uncomment for change of default
/* Set Bus Voltage Range
BRNG_16 -> 16 V
BRNG_32 -> 32 V (DEFAULT)
*/
// ina219.setBusRange(BRNG_32); // choose range and uncomment for change of default
Serial.println("INA219 Current Sensor Example Sketch - Continuous");
/* If the current values delivered by the INA219 differ by a constant factor
from values obtained with calibrated equipment you can define a correction factor.
Correction factor = current delivered from calibrated equipment / current delivered by INA219
*/
// ina219.setCorrectionFactor(0.98); // insert your correction factor if necessary
/* If you experience a shunt voltage offset, that means you detect a shunt voltage which is not
zero, although the current should be zero, you can apply a correction. For this, uncomment the
following function and apply the offset you have detected.
*/
// ina219.setShuntVoltOffset_mV(0.5); // insert the shunt voltage (millivolts) you detect at zero current
}
void loop() {
float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getBusPower();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV/1000);
ina219_overflow = ina219.getOverflow();
Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV);
Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V);
Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V);
Serial.print("Current[mA]: "); Serial.println(current_mA);
Serial.print("Bus Power [mW]: "); Serial.println(power_mW);
if(!ina219_overflow){
Serial.println("Values OK - no overflow");
}
else{
Serial.println("Overflow! Choose higher PGAIN");
}
Serial.println();
delay(3000);
}
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html
code-snippets/client/INA219/circuit_diagramm_pic.png

348 KiB

File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment