No I2C devices found

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
}

Post the scanner code, if it fails you either have a wiring error (so post CLEAR diagram of wiring, photo of hand drawn is fine) or a possible board failure.

Have you tested another i2c device with the i2c scanner sketch on Teensy (and your other Arduino)?

I personally haven't. But one of my teammates used the a different teensy and there were no problems. Now even his teensy is giving the same problem

I ran this code again now. It says "I2C Scanner" and nothing else on the serial monitor

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  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("Unknow 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

Here's a photo of the board. Ignore the stray wires, sorry.

You might want to read this, especially about the Wire1 pins , and pullups.
I see no Wire port on the 4.1, just Wire1 and Wire2. ??
https://www.pjrc.com/teensy/td_libs_Wire.html

There are wire ports on teensy 4.1. 18 for SDA and 19 for SCL.

Yeah, where are those pullups?

Thanks ledsyn! From that page:

1K to 4.7K resistors are recommended for most applications. The on-chip pullup resistors in Teensy 2.0, Teensy 4.0, Teensy 4.1 are very weak. Usually communication with 1 chip at 100kHz can work, but with poor signal quality. The on-chip resistors are not enough for several chips or higher speeds.

@nobodyisme213 @SurferTim @ledsyn I have run many I2C scans and never used a pullup resistor but maybe the Teensy 4.1 is different.
When running the test, if it fails, reverse the SCL/SDA wires and try again just in case.
I wonder if boards with dedicated I2C pins have builtin pullups and those without need them.
If the above fails, drop in a pull UP/DOWN? maybe try both ways?

Ah, post #10 seems to nail it.

Not both ways. Just UP.

Regarding T4.1 and I2c device detection:

Results of hooking up a Teensy 4.1 with an I2C SSD1306 display hooked up to SCL/SDA on 19/18 and running the I2C scanner.

Connecting to /dev/ttyACM0. Press CTRL-C to exit.

I2C Scanner
Scanning...
I2C device found at address 0x3C  !
done

I then threw together a quick sketch to actually write to the display:

Remembering that the display had pullups built in, I went back to the scanner sketch but this time used a bare 24LC256 I2C EEPROM (i.e. no pullups) and got:

Connecting to /dev/ttyACM0. Press CTRL-C to exit.

I2C Scanner
Scanning...
I2C device found at address 0x50  !
done

Mind you, since the Teensy is a 3.3V board, I did power both of the I2C devices with the 3.3V pin, not the 5V pin from the USB like you're doing... I see the INA219 board you're using does have a 3.3V regulator, but it might still be worth looking into using 3.3V.

1 Like

The schematic of the INA219 shows that it has 10k pullups on SDA and SCL. This is probably not sufficient. Add 3.3k pullups on both SDA and SCL.

Pete

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.