Hello. I am trying to test out my INA219 current sensor, however it does not seem to work. I am running the default base program Arduino gives me to test it out. I will paste the code and attach my schematic below.
When I use an Arduino UNO, the program hangs at ina219.begin() and does not proceed. However, when I use an Arduino MEGA, the program actually enters the loop, and prints "Failed to find INA219 chip".
I am not sure why this happens. Please let me know your thoughts. Thank you.
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
void setup(void)
{
Serial.begin(115200);
while (!Serial) {
// will pause Zero, Leonardo, etc until serial console opens
delay(1);
}
Serial.println("Hello!");
// Initialize the INA219.
// By default the initialization will use the largest range (32V, 2A). However
// you can call a setCalibration function to change this range (see comments).
if (! ina219.begin()) {
Serial.println("Failed to find INA219 chip");
while (1) { delay(10); }
}
// To use a slightly lower 32V, 1A range (higher precision on amps):
//ina219.setCalibration_32V_1A();
// Or to use a lower 16V, 400mA range (higher precision on volts and amps):
//ina219.setCalibration_16V_400mA();
Serial.println("Measuring voltage and current with INA219 ...");
}
void loop(void)
{
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
shuntvoltage = ina219.getShuntVoltage_mV();
busvoltage = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getPower_mW();
loadvoltage = busvoltage + (shuntvoltage / 1000);
Serial.print("Bus Voltage: "); Serial.print(busvoltage); Serial.println(" V");
Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
Serial.print("Load Voltage: "); Serial.print(loadvoltage); Serial.println(" V");
Serial.print("Current: "); Serial.print(current_mA); Serial.println(" mA");
Serial.print("Power: "); Serial.print(power_mW); Serial.println(" mW");
Serial.println("");
delay(2000);
}
If you have a different I2C module, example an I2C LCD, wire it up correctly and run the test to see what address it shows (this tests the A4 SDA, and A5 SCL, functionality).
I tried that too but no luck. Honestly I think that I'm just wiring incorrectly. The 2 ina219 sensors I've tested (found them in my lab) are probably old and it could be that they're not working either. I'm not sure.
As per @LarryD 's advice, I will try doing this with other wires and then I'll try wiring up an LCD (which I have done before with success).
Lol I tried that so many times before. I just tried switching them again but no luck. When I run the following sketch I found online, it just says "Scanning...No I2C devices found. Scanning..." and then hangs.
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
//
// SPDX-License-Identifier: MIT
// --------------------------------------
// i2c_scanner
//
// Modified from https://playground.arduino.cc/Main/I2cScanner/
// --------------------------------------
#include <Wire.h>
// Set I2C bus to use: Wire, Wire1, etc.
#define WIRE Wire
void setup() {
WIRE.begin();
Serial.begin(9600);
while (!Serial)
delay(10);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
WIRE.beginTransmission(address);
error = WIRE.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Ok, I will try that again. Just for reference though, I am getting the same issue on the Arduino MEGA as well, and the SCL and SDA pins are labeled on this one, so those aren't an issue.
Hm, yes, very odd. Any chance that this sensor was damaged in an earlier experiment? You mentioned you have two; do both of them manage to hang up the I2C scanner on both the UNO and the Mega?