-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathRAK16000_DcCurrent_INA219.ino
108 lines (96 loc) · 3.76 KB
/
RAK16000_DcCurrent_INA219.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
@file RAK16000_DcCurrent_INA219.ino
@author rakwireless.com
@brief Get DC current from sensor
@version 0.1
@date 2021-7-28
@copyright Copyright (c) 2020
**/
#include <Wire.h>
#include <INA219_WE.h>// Click here to get the library: http://librarymanager/All#INA219_WE
#define I2C_ADDRESS 0x41
INA219_WE ina219 = INA219_WE(I2C_ADDRESS);
void setup()
{
Serial.begin(115200);
Serial.println("Initializing...");
pinMode(WB_IO2, OUTPUT);
digitalWrite(WB_IO2, HIGH);
delay(300);
// Initialize sensor
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
ina219.setShuntSizeInOhms(0.1); // we use 100ohm
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.99); // insert your correction factor if necessary
}
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();
//here we use the I=U/R to calculate, here the Resistor is 100mΩ, accuracy can reach to 0.5%.
current_mA = shuntVoltage_mV/0.1;
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(1000);
}