I'm working with a Teensy 4.1 and a DFRobot INA219 sensor, and I seem to be running into an issue. The wiring appears to be correct, and my code seems fine as well. However, when I try to initialize the INA219, the serial monitor outputs: INA219 Begin failed
To troubleshoot, I ran a basic I2C scanner sketch, and the serial monitor shows: No I2C Devices Found
I double-checked the connections between the Teensy and the INA219, and they look good. I'm using the standard SDA and SCL pins on the Teensy 4.1. I've also tried running the code on a different Arduino board, and the sensor works fine, so I suspect the issue might be specific to the Teensy setup.
Has anyone else encountered this problem? Any suggestions on how to resolve this? I'd appreciate any help or troubleshooting tips!`
Here's the code:
int relay_status = 0;
const int windowSize = 5;
float circularBuffer[windowSize];
int bufferIndex = 0; // Index to track the position in the buffer
float sum = 0;
int elementCount = 0;
float circularBuffer2[windowSize];
int bufferIndex2 = 0; // Index to track the position in the buffer
float sum2 = 0;
int elementCount2 = 0;
float circularBuffer3[windowSize];
int bufferIndex3 = 0; // Index to track the position in the buffer
float sum3 = 0;
int elementCount3 = 0;
float circularBuffer4[windowSize];
int bufferIndex4 = 0; // Index to track the position in the buffer
float sum4 = 0;
int elementCount4 = 0;
void appendToBuffer(float value) {
int indexToReplace = bufferIndex % windowSize;
// If the buffer is full, subtract the old value from the sum
if (elementCount == windowSize) {
sum -= circularBuffer[indexToReplace];
}
// Add the new value to the buffer and the sum
circularBuffer[indexToReplace] = value;
sum += value;
// Update the buffer index and element count
bufferIndex++;
if (elementCount < windowSize) {
elementCount++;
}
// Ensure the bufferIndex wraps around
if (bufferIndex >= windowSize) {
bufferIndex = 0;
}
}
void appendToBuffer2(float value2) {
int indexToReplace2 = bufferIndex2 % windowSize;
// If the buffer is full, subtract the old value from the sum
if (elementCount2 == windowSize) {
sum2 -= circularBuffer2[indexToReplace2];
}
// Add the new value to the buffer and the sum
circularBuffer2[indexToReplace2] = value2;
sum2 += value2;
// Update the buffer index and element count
bufferIndex2++;
if (elementCount2 < windowSize) {
elementCount2++;
}
// Ensure the bufferIndex wraps around
if (bufferIndex2 >= windowSize) {
bufferIndex2 = 0;
}
}
void appendToBuffer3(float value3) {
int indexToReplace3 = bufferIndex3 % windowSize;
// If the buffer is full, subtract the old value from the sum
if (elementCount3 == windowSize) {
sum3 -= circularBuffer3[indexToReplace3];
}
// Add the new value to the buffer and the sum
circularBuffer3[indexToReplace3] = value3;
sum3 += value3;
// Update the buffer index and element count
bufferIndex3++;
if (elementCount3 < windowSize) {
elementCount3++;
}
// Ensure the bufferIndex wraps around
if (bufferIndex3 >= windowSize) {
bufferIndex3 = 0;
}
}
void appendToBuffer4(float value4) {
int indexToReplace4 = bufferIndex4 % windowSize;
// If the buffer is full, subtract the old value from the sum
if (elementCount4 == windowSize) {
sum4 -= circularBuffer4[indexToReplace4];
}
// Add the new value to the buffer and the sum
circularBuffer4[indexToReplace4] = value4;
sum4 += value4;
// Update the buffer index and element count
bufferIndex4++;
if (elementCount4 < windowSize) {
elementCount4++;
}
// Ensure the bufferIndex wraps around
if (bufferIndex4 >= windowSize) {
bufferIndex4 = 0;
}
}
float AddValue(float value) {
appendToBuffer(value);
// Calculate and return the moving average
float movingAverage = (float)sum / elementCount;
return movingAverage;
}
float AddValue2(float value2) {
appendToBuffer2(value2);
// Calculate and return the moving average
float movingAverage2 = (float)sum2 / elementCount2;
return movingAverage2;
}
float AddValue3(float value3) {
appendToBuffer3(value3);
// Calculate and return the moving average
float movingAverage3 = (float)sum3 / elementCount3;
return movingAverage3;
}
float AddValue4(float value4) {
appendToBuffer4(value4);
// Calculate and return the moving average
float movingAverage4 = (float)sum4 / elementCount4;
return movingAverage4;
}
//const int LED = 8;
// initialize input pin for digital switch
const int digitalSwitchPin = 2; // pin 2
// output pin for the transistor
const int relay = 0; // pin 0
int switchState = 0;
//int volts = 0;
// Code to set up the Digital Wattmeter
/*!
file getVoltageCurrentPower.ino
SEN0291 Gravity: I2C Digital Wattmeter
The module is connected in series between the power supply and the load to read
the voltage, current and power
The module has four I2C, these addresses are:
INA219_I2C_ADDRESS1 0x40 A0 = 0 A1 = 0
INA219_I2C_ADDRESS2 0x41 A0 = 1 A1 = 0
INA219_I2C_ADDRESS3 0x44 A0 = 0 A1 = 1
INA219_I2C_ADDRESS4 0x45 As0 = 1 A1 = 1
Copyright [DFRobot](http://www.dfrobot.com), 2016
Copyright GNU Lesser General Public License
version V0.1
date 2019-2-27
*/
#include <Wire.h>
#include "DFRobot_INA219.h"
DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS4);
DFRobot_INA219_IIC ina219_2(&Wire, INA219_I2C_ADDRESS3);
DFRobot_INA219_IIC ina219_3(&Wire, INA219_I2C_ADDRESS2);
DFRobot_INA219_IIC ina219_4(&Wire, INA219_I2C_ADDRESS1);
// Revise the following two paramters according to actula reading of the INA219 and
// the multimeter
// for linearly calibration
float ina219Reading_mA = 1000;
float ina219_2_Reading_mA = 1000;
float ina219_3_Reading_mA = 1000;
float ina219_4_Reading_mA = 1000;
float extMeterReading_mA = 1000;
float extMeter_2_Reading_mA = 1000;
float extMeter_3_Reading_mA = 1000;
float extMeter_4_Reading_mA = 1000;
float med_1 = 0;
float med_2 = 0;
float med_3 = 0;
float med_4 = 0;
void setup() {
// Initialize input and output pins
pinMode(relay, OUTPUT); // declare transistor as output
pinMode(digitalSwitchPin, INPUT); // declare digitalSwitchPin as input
// Setup Serial communication
Serial.begin(9600);
Serial3.begin(9600);
while (!Serial)
; // Wait for Serial to initialize
// Initialize INA219 sensors
while (!ina219.begin()) {
Serial.println("INA219 begin failed");
delay(2000);
}
while (!ina219_2.begin()) {
Serial.println("INA219_2 begin failed");
delay(2000);
}
while (!ina219_3.begin()) {
Serial.println("INA219_3 begin failed");
delay(2000);
}
while (!ina219_4.begin()) {
Serial.println("INA219_4 begin failed");
delay(2000);
}
// Calibrate INA219 sensors
ina219.linearCalibrate(ina219Reading_mA, extMeterReading_mA);
ina219_2.linearCalibrate(ina219_2_Reading_mA, extMeter_2_Reading_mA);
ina219_3.linearCalibrate(ina219_3_Reading_mA, extMeter_3_Reading_mA);
ina219_4.linearCalibrate(ina219_4_Reading_mA, extMeter_4_Reading_mA);
}
void loop() {
// switchState = digitalRead(digitalSwitchPin); // Read input value from the switch pin
// // Control transistor based on switch state
// if (switchState == HIGH) {
// digitalWrite(transistor, HIGH);
// } else {
// digitalWrite(transistor, LOW);
// }
// Read voltage from INA219 sensors
float busVoltage1 = ina219.getBusVoltage_V();
float busVoltage2 = ina219_2.getBusVoltage_V();
float busVoltage3 = ina219_3.getBusVoltage_V();
float busVoltage4 = ina219_4.getBusVoltage_V();
// Update moving average
float med_1 = AddValue(busVoltage1);
float med_2 = AddValue2(busVoltage2);
float med_3 = AddValue3(busVoltage3);
float med_4 = AddValue4(busVoltage4);
if (elementCount == 5) {
if (relay_status == 0) {
if (med_1 + med_2 - (med_3 + med_4) > 4) {
digitalWrite(relay, HIGH);
relay_status = 1;
}
}
if (relay_status == 1) {
if (med_1 + med_2 - (med_3 + med_4) < 2) {
digitalWrite(relay, LOW);
relay_status = 0;
}
}
// Print values to Serial
Serial.print("Battery1 V: ");
Serial.print(med_1, 2);
Serial.print(",");
Serial.print("Battery2 V: ");
Serial.print(med_2, 2);
Serial.print(",");
Serial.print(" SuperCap1 V: ");
Serial.print(med_3, 2);
Serial.print(",");
Serial.print(" SuperCap2 V: ");
Serial.println(med_4, 2);
// Print values to Serial
// Serial.print(med_2, 2);
// Serial.print(" Current Sum2: ");
// Serial.print(sum2);
// Serial.print(",");
// Serial.print(" Element Count2: ");
// Serial.println(elementCount2);
// Serial3.print(med_1, 2);
// Serial3.print(", ");
// Serial3.print(med_2, 2);
// Serial3.print(", ");
// // Determine ON/OFF status
// if (busVoltage1 - busVoltage2 > 2) {
// Serial3.println("ON");
// } else {
// Serial3.println("OFF");
// }
}
delay(1000); // Delay for stability
}


