本站改版新增arduino频道
arduino INA226
#include <Wire.h>
#include <INA226_WE.h>
#define I2C_ADDRESS 0x40
INA226_WE ina226 = INA226_WE(I2C_ADDRESS);
void setup()
{
Serial.begin(9600);
while (!Serial); // wait until serial comes up on Arduino Leonardo or MKR WiFi 1010
Wire.begin();
ina226.init();
/* Set Number of measurements for shunt and bus voltage which shall be averaged
Mode * * Number of samples
AVERAGE_1 1 (default)
AVERAGE_4 4
AVERAGE_16 16
AVERAGE_64 64
AVERAGE_128 128
AVERAGE_256 256
AVERAGE_512 512
AVERAGE_1024 1024*/
//ina226.setAverage(AVERAGE_16); // choose mode and uncomment for change of default
/* Set conversion time in microseconds
One set of shunt and bus voltage conversion will take:
number of samples to be averaged x conversion time x 2
Mode * * conversion time
CONV_TIME_140 140 µs
CONV_TIME_204 204 µs
CONV_TIME_332 332 µs
CONV_TIME_588 588 µs
CONV_TIME_1100 1.1 ms (default)
CONV_TIME_2116 2.116 ms
CONV_TIME_4156 4.156 ms
CONV_TIME_8244 8.244 ms */
//ina226.setConversionTime(CONV_TIME_1100); //choose conversion time and uncomment for change of default
/* Set measure mode
POWER_DOWN - INA226 switched off
TRIGGERED - measurement on demand
CONTINUOUS - continuous measurements (default)*/
//ina226.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
/* Set Resistor and Current Range
if resistor is 5.0 mOhm, current range is up to 10.0 A
default is 100 mOhm and about 1.3 A*/
ina226.setResistorRange(0.1, 1.3); // choose resistor 0.1 Ohm and gain range up to 1.3A
/* If the current values delivered by the INA226 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 INA226*/
ina226.setCorrectionFactor(0.93);
Serial.println("INA226 Current Sensor Example Sketch - Continuous");
ina226.waitUntilConversionCompleted(); //if you comment this line the first data might be zero
}
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;
ina226.readAndClearFlags();
shuntVoltage_mV = ina226.getShuntVoltage_mV();
busVoltage_V = ina226.getBusVoltage_V();
current_mA = ina226.getCurrent_mA();
power_mW = ina226.getBusPower();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV / 1000);
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 (!ina226.overflow)
{
Serial.println("Values OK - no overflow");
}
else
{
Serial.println("Overflow! Choose higher current range");
}
Serial.println();
delay(3000);
}
Copyright © 2014 ESP56.com All Rights Reserved
晋ICP备14006235号-22 晋公网安备14108102001165号
执行时间: 0.0094091892242432 seconds