-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcalibration.cpp
144 lines (128 loc) · 5.67 KB
/
calibration.cpp
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// FILE: INA226_calibrate.ino
// AUTHOR: Prashant Kumar
// PURPOSE: accurate calibration of shunt resistance, current zero offset, bus voltage scaling and full user control
// url: https://github.com/pk17r/INA226
#include "INA226.h"
INA226 INA(0x40);
void setup()
{
Serial.begin(115200);
delay(4000);
Serial.println(__FILE__);
Serial.print("INA226_LIB_VERSION: ");
Serial.println(INA226_LIB_VERSION);
pinMode(1, OUTPUT); // Load Switch
digitalWrite(1, HIGH);
Wire.setSCL(5);
Wire.setSDA(4);
Wire.begin();
if (!INA.begin())
{
Serial.println("could not connect. Fix and Reboot");
}
/* STEPS TO CALIBRATE INA226
* 1. Set shunt equal to shunt resistance in ohms. This is the shunt resistance between IN+ and IN- pins of INA226 in your setup.
* 2. Set current_LSB_mA (Current Least Significant Bit mA) equal to your desired least count resolution for IOUT in milli amps. Expected value to be in multiples of 0.050 o 0.010. Recommended values: 0.050, 0.100, 0.250, 0.500, 1, 2, 2.5 (in milli Ampere units).
* 3. Set current_zero_offset_mA = 0, bus_V_scaling_e4 = 10000.
* 4. Build firmware and flash microcontroller.
* 5. Attach a power supply with voltage 5-10V to INA226 on VBUS/IN+ and GND pins, without any load.
* 6. Start Serial Monitor and note Current values. Update current_zero_offset_mA = current_zero_offset_mA + average of 10 Current values in milli Amperes.
* NOTE: Following adjustments shouldn't change values by more than 15-20%.
* 7. Now measure Bus Voltage using a reliable Digital MultiMeter (DMM). Update bus_V_scaling_e4 = bus_V_scaling_e4 / (Displayed Bus Voltage on Serial Monitor) * (DMM Measured Bus Voltage). Can only be whole numbers.
* 8. Now set DMM in current measurement mode. Use a resistor that will generate around 50-100mA IOUT measurement between IN- and GND pins with DMM in series with load. Note current measured on DMM.
* 9. Update shunt = shunt * (Displayed IOUT on Serial Monitor) / (DMM Measured IOUT).
* 10. Build firmware and flash microcontroller. Your INA 226 is now calibrated. It should have less than 1% error in Current and Voltage measurements over a wide range like [5mA, 1A] and [5V, 20V].
*/
/* USER SET VALUES */
float shunt = 0.01023; /* shunt (Shunt Resistance in Ohms). Lower shunt gives higher accuracy but lower current measurement range. Recommended value 0.020 Ohm. Min 0.001 Ohm */
float current_LSB_mA = 0.25; /* current_LSB_mA (Current Least Significant Bit in milli Amperes). Recommended values: 0.050, 0.100, 0.250, 0.500, 1, 2, 2.5 (in milli Ampere units) */
float current_zero_offset_mA = 6.4; /* current_zero_offset_mA (Current Zero Offset in milli Amperes, default = 0) */
uint16_t bus_V_scaling_e4 = 10026; /* bus_V_scaling_e4 (Bus Voltage Scaling Factor, default = 10000) */
if (INA.configure(shunt, current_LSB_mA, current_zero_offset_mA, bus_V_scaling_e4))
Serial.println("\n***** Configuration Error! Chosen values outside range *****\n");
else
Serial.println("\n***** INA 226 CONFIGURE *****");
Serial.print("Shunt:\t");
Serial.print(shunt, 4);
Serial.println(" Ohm");
Serial.print("current_LSB_mA:\t");
Serial.print(current_LSB_mA * 1e+3, 1);
Serial.println(" uA / bit");
Serial.print("\nMax Measurable Current:\t");
Serial.print(INA.getMaxCurrent(), 3);
Serial.println(" A");
/* CALIBRATION */
float bv = 0, cu = 0;
for (int i = 0; i < 10; i++)
{
bv += INA.getBusVoltage();
cu += INA.getCurrent_mA();
delay(150);
}
bv /= 10;
cu /= 10;
Serial.println("\nAverage Bus and Current values for use in Shunt Resistance, Bus Voltage and Current Zero Offset calibration:");
bv = 0;
for (int i = 0; i < 10; i++)
{
bv += INA.getBusVoltage();
delay(100);
}
bv /= 10;
Serial.print("\nAverage of 10 Bus Voltage values = ");
Serial.print(bv, 3);
Serial.println("V");
cu = 0;
for (int i = 0; i < 10; i++)
{
cu += INA.getCurrent_mA();
delay(100);
}
cu /= 10;
Serial.print("Average of 10 Current values = ");
Serial.print(cu, 3);
Serial.println("mA");
Serial.println("\nCALIBRATION VALUES TO USE:\t(DMM = Digital MultiMeter)");
Serial.println("Step 5. Attach a power supply with voltage 5-10V to INA226 on VBUS/IN+ and GND pins, without any load.");
Serial.print("\tcurrent_zero_offset_mA = ");
Serial.print(current_zero_offset_mA + cu, 3);
Serial.println("mA");
if (cu > 5)
Serial.println("********** NOTE: No resistive load needs to be present during current_zero_offset_mA calibration. **********");
Serial.print("\tbus_V_scaling_e4 = ");
Serial.print(bus_V_scaling_e4);
Serial.print(" / ");
Serial.print(bv, 3);
Serial.println(" * (DMM Measured Bus Voltage)");
Serial.println("Step 8. Set DMM in current measurement mode. Use a resistor that will generate around 50-100mA IOUT measurement between IN- and GND pins with DMM in series with load. Note current measured on DMM.");
Serial.print("\tshunt = ");
Serial.print(shunt);
Serial.print(" * ");
Serial.print(cu, 3);
Serial.println(" / (DMM Measured IOUT)");
if (cu < 40)
Serial.println("********** NOTE: IOUT needs to be more than 50mA for better shunt resistance calibration. **********");
delay(1000);
/* MEASUREMENTS */
Serial.println("\nBUS(V) SHUNT(mV) CURRENT(mA) POWER(mW)");
for (int i = 0; i < 5; i++)
{
bv = INA.getBusVoltage();
float sv = INA.getShuntVoltage_mV();
cu = INA.getCurrent_mA();
float po = (bv - sv / 1000) * cu;
Serial.print(bv, 3);
Serial.print("\t");
Serial.print(sv, 3);
Serial.print("\t\t");
Serial.print(cu, 1);
Serial.print("\t");
Serial.print(po, 1);
Serial.println();
delay(1000);
}
}
void loop()
{
}
// -- END OF FILE --