I2C scanner stuck on 'scanning...'

Hi there,

I have been having problems with my MPU-6050 accelerometer and gyroscope (the code hangs on wire.endTransmission())

my wiring.

VCC<<3.3V
GND<<GND
SCL<<A5
SDA<<A4

To try and diagnose the problem I used the I2C scanner to try and see if I got the registers correct.

However the I2C scanner will not return anything and is stuck on “Scanning…” Ive left it for 15 minutes and no change.

anyone have any idea what Im doing wrong? could it just be a fault board? or do i need to update firmware/software? Any and all help is appreciated.

This is the code I have used (taken from arduino website):

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    https://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
 
#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  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
}

it loops endlessly here. no I2C devices at the moment. try it on a bare Arduino.

the one I use:

//sample code we found on google.com

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             
  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
}

I just tried it with your code on a bare arduino uno R3, still wont get past the "scanning..." part.

Could my MPU6050 be broken? or even my arduino?

More than likely you are missing the required pullups.
--- bill

Hi bill,

In the tutorials on the internet no external pickup are needed? I assume they are on the board already?

Should I try adding pullup resistors to the SDA and SCL pins on a breadboard?

it gets past "scanning" here with no I2C connected. the fact that it indicates nothing scanned demonstrates that the code did its thing and determined there was no result.

I just noticed that the scan is starting at i2c address 1. That is not good and is wrong.
Addresses below 8 are reserved for special purposes and should not be used and can cause errors and/or lockups.
It doesn't seem to cause issues on the AVR chips but other chips do have issues with it as they are supporting some of the reserved functions for those low addresses.
Change that starting address for sure.

In terms of further diagnosing, I don't believe in trial an error or guessing as diagnostic methods.
Either the pullups are already there or they aren't.
I would look at the board and the traces and see if they are there or look at the schematic for you board.

If want to use a tool to check for them, my hd44780 library comes with a i2c diagostic that will report if there are pullups.
While the diagnostic tool is intended to be used with an LCD that has an i2c backpack you could run the diagnostic and it will look at the i2c signals and check them for pullups and report status information.
The library can be found in the IDE library manager and the diagnostic sketch is called I2CexpDiag and is under the examples for the hd44780_I2Cexp i/o class.

--- bill

1 Like

thank for the continued responses everyone!

I checked my board and there are pull up resistors! Like you said bill I changed the starting address to 9 (to avoid the first 8). However I am still getting the same error (scanning...).

Ill look into your library bill! Im extremely new to arduino so i will have to learn how to install library's and such so I may have to put that on the back burner!

You MUST have pullup resistors. If they are not on your Arduino, then they MUST be part of your wiring!

dohara96:
thank for the continued responses everyone!

I checked my board and there are pull up resistors! Like you said bill I changed the starting address to 9 (to avoid the first 8). However I am still getting the same error (scanning...).

The first valid address is actually 8. (0 to 7 are the reserved addresses)

Ill look into your library bill! Im extremely new to arduino so i will have to learn how to install library's and such so I may have to put that on the back burner!

It is very easy to install the hd44780 library as it can all be done from the IDE.

Just search for hd44780 and you should see it.
The library will be useful if you decide to add a LCD display with a i2c backpack in the future.

--- bill

What kind of level shifter are you using?

Check the Arduino Uno for solder blobs or pcb shortcuts with a magnifier near the SDA, SCL, A4 and A5 pins.

Hi everyone,

I tried an adafruit 3 axis accelerometer and it works perfectly! I do need the MPU6050 as it has a gyro but does this mean my board MPU6050 has the problem?

Kind regards,

Duncan

What MPU-6050 module do you have?

What about post #10 ?
i.e. how are you handling the 5v vs 3v i2c signals?

SOLVED! The chip was broken, I bought a new one and it works fine! thank you everyone for trying to help, I learnt a lot more about electronics through this pointless exercise! thank you!

I find that with all the changes in code there seems to be a lot of bad code that needs to be rectified.
Using Arduino IDE version 1.8.10, The following code should read all I2C’s connected to your Arduino Uno. If you have a LCD 2004 it works even better. Change the code below to suit your needs. Replace the lcd with Serial.print to view in the monitor.

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
int z=0;
LiquidCrystal_I2C lcd(0x27,20,4);

void(* resetFunc) (void) = 0;
void setup()
{
  Wire.begin();
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  Serial.begin(9600);
  while (!Serial); 
 
  
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
 
  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)
    {
   
      lcd.setCursor(0,z++);
      lcd.print("Found I2C at 0x");
      if (address<16)
       
        lcd.print("0");
        lcd.print(address,HEX);
        
 
      nDevices++;
    }
    else if (error==4)
    {
      lcd.print("Unknown error at address 0x");
      if (address<16)
        lcd.print("0");
      lcd.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    lcd.println("No I2C devices found\n");
  else
    delay(10000);
    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("Resetting....");
    delay(5000);           // wait 5 seconds for next scan
    resetFunc();
}

ZilliQuister:
I find that with all the changes in code there seems to be a lot of bad code that needs to be rectified.

Including the code you provided.
Like I said in post #6 the lower 8 addresses in i2c are reserved for special purposes. Scanning should start at address 8.
Using addresses lower than that can cause the Wire library to hang on certain cores depending on the micro-controller/chipset.

Also, I'm not sure if calling to address 0 works to restart the system on all Arduino boards.
It might be the case, but it isn't a given that all the microcontrollers used on Arduino boards startup at location zero.

IMO, it isn't necessary to toss in the additional code to try to use an LCD with a backpkack and restart the arduino board system after scanning the i2c bus for devices.
After-all, a very common reason people are running the i2c scanner is to find the i2c address of the i2c backpack on their LCD device.

For testing an hd44780 LCD with a PCF8574 based backpack the hd44780 library I2CexpDiag tool will do a much better job testing the i2c bus signals, scanning the i2c bus for devices, and testing any detected LCD devices.
When using the hd44780_I2Cexp i/o class you don't have to know the i2c address of the backpack, as it will auto locate it for you, so there is no need to do a scan for the i2c backpack address.
hd44780_I2Cexp also will auto detect the pin mappings used by the backpack to the LCD so it will work with all the i2c backpack designs vs the single one that LiquidCrystal_I2C library works with.

--- bill