Help with I2C Scanner

I’m trying to figure out how to scan for the active slave address on any given I2C chip.

I’ve looked up and found some example code for this but I haven’t had any success in getting a confirmed slave address in use.

In order to test the code I have an Accelerometer ADXL345 that I have powered hooked up to an Arduino Uno. SDA I hooked up to pin 2. SCL is hooked up to pin 7. My problem might be with hooking it up because I don’t see anywhere in the code where you map the pins.


Here is the code I am using:

/**
 * I2CScanner.ino -- I2C bus scanner for Arduino
 *
 * 2009,2014, Tod E. Kurt, http://todbot.com/blog/
 *
 */

#include "Wire.h"
extern "C" { 
#include "utility/twi.h"  // from Wire library, so we can do bus scanning
}

// Scan the I2C bus between addresses from_addr and to_addr.
// On each address, call the callback function with the address and result.
// If result==0, address was found, otherwise, address wasn't found
// (can use result to potentially get other status on the I2C bus, see twi.c)
// Assumes Wire.begin() has already been called
void scanI2CBus(byte from_addr, byte to_addr, 
                void(*callback)(byte address, byte result) ) 
{
  byte rc;
  byte data = 0; // not used, just an address to feed to twi_writeTo()
  for( byte addr = from_addr; addr <= to_addr; addr++ ) {
    rc = twi_writeTo(addr, &data, 0, 1, 0);
    callback( addr, rc );
  }
}

// Called when address is found in scanI2CBus()
// Feel free to change this as needed
// (like adding I2C comm code to figure out what kind of I2C device is there)
void scanFunc( byte addr, byte result ) {
  Serial.print("addr: ");
  Serial.print(addr,DEC);
  Serial.print( (result==0) ? " found!":"       ");
  Serial.print( (addr%4) ? "\t":"\n");
}


byte start_address = 1;
byte end_address = 100;

// standard Arduino setup()
void setup()
{
    Wire.begin();

    Serial.begin(9600);
    Serial.println("\nI2CScanner ready!");

    Serial.print("starting scanning of I2C bus from ");
    Serial.print(start_address,DEC);
    Serial.print(" to ");
    Serial.print(end_address,DEC);
    Serial.println("...");

    // start the scan, will call "scanFunc()" on result from each address
    scanI2CBus( start_address, end_address, scanFunc );

    Serial.println("\ndone");
}

// standard Arduino loop()
void loop() 
{
    // Nothing to do here, so we'll just blink the built-in LED
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300);
}

After executing the code I get this on the serial monitor regardless of whether Accelerometer is powered or not:

I2CScanner ready!
starting scanning of I2C bus from 1 to 100…
addr: 1 addr: 2 addr: 3 addr: 4
addr: 5 addr: 6 addr: 7 addr: 8
addr: 9 addr: 10 addr: 11 addr: 12
addr: 13 addr: 14 addr: 15 addr: 16
addr: 17 addr: 18 addr: 19 addr: 20
addr: 21 addr: 22 addr: 23 addr: 24
addr: 25 addr: 26 addr: 27 addr: 28
addr: 29 addr: 30 addr: 31 addr: 32
addr: 33 addr: 34 addr: 35 addr: 36
addr: 37 addr: 38 addr: 39 addr: 40
addr: 41 addr: 42 addr: 43 addr: 44
addr: 45 addr: 46 addr: 47 addr: 48
addr: 49 addr: 50 addr: 51 addr: 52
addr: 53 addr: 54 addr: 55 addr: 56
addr: 57 addr: 58 addr: 59 addr: 60
addr: 61 addr: 62 addr: 63 addr: 64
addr: 65 addr: 66 addr: 67 addr: 68
addr: 69 addr: 70 addr: 71 addr: 72
addr: 73 addr: 74 addr: 75 addr: 76
addr: 77 addr: 78 addr: 79 addr: 80
addr: 81 addr: 82 addr: 83 addr: 84
addr: 85 addr: 86 addr: 87 addr: 88
addr: 89 addr: 90 addr: 91 addr: 92
addr: 93 addr: 94 addr: 95 addr: 96
addr: 97 addr: 98 addr: 99 addr: 100

done

If someone could help me solve this, it would be awesome.

Arduino Uno. SDA I hooked up to pin 2. SCL is hooked up to pin 7.

On the UNO, SDA is A4 and SCL is A5.

You should also scan up to 120, as 0x68 (Decimal 104) is the address for most RTC’s incase you are looking to confirm them on the bus.

I’ve not seen your code before, but a common, and often used I2C scanner code is from Nick Gammon

// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011

#include <Wire.h>

void setup() {
  Serial.begin (115200);

  // Leonardo: wait for serial port to connect
  while (!Serial) 
    {
    }

  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;
  
  Wire.begin();
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);
    if (Wire.endTransmission () == 0)
      {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);
      Serial.println (")");
      count++;
      delay (1);  // maybe unneeded?
      } // end of good response
  } // end of for loop
  Serial.println ("Done.");
  Serial.print ("Found ");
  Serial.print (count, DEC);
  Serial.println (" device(s).");
}  // end of setup

void loop() {}

jnkettenring:
I’m trying to figure out how to scan for the active slave address on any given I2C chip.

I’ve looked up and found some example code for this but I haven’t had any success in getting a confirmed slave address in use.

In order to test the code I have an Accelerometer ADXL345 that I have powered hooked up to an Arduino Uno. SDA I hooked up to pin 2. SCL is hooked up to pin 7. My problem might be with hooking it up because I don’t see anywhere in the code where you map the pins.


Here is the code I am using:

If someone could help me solve this, it would be awesome.

/* I2C slave Address Scanner
 * Connect a 4.7k resistor between SDA and Vcc
 * Connect a 4.7k resistor between SCL and Vcc
 */

#include <Wire.h>

void scan(){
Serial.println(" Scanning I2C Addresses in HEX");
uint8_t cnt=0;
for(uint8_t i=0;i<128;i++){
  Wire.beginTransmission(i);
  uint8_t ec=Wire.endTransmission(true);
  if(ec==0){
    if(i<16)Serial.print('0');
    Serial.print(i,HEX);
    cnt++;
  }
  else Serial.print("..");
  Serial.print(' ');
  if ((i&0x0f)==0x0f)Serial.println();
  }
Serial.print("Scan Completed, ");
Serial.print(cnt);
Serial.println(" I2C Devices found.");
}

void setup(){
Serial.begin(9600);
Wire.begin();
scan();
}

void loop(){}

Example program output

 Scanning I2C Addresses in HEX
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
50 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. 68 .. .. .. .. .. .. .. 
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. 
Scan Completed, 2 I2C Devices found.

Minimum sketch to scan the I2C bus for Slaves.

Chuck.

cattledog: On the UNO, SDA is A4 and SCL is A5.

You should also scan up to 120, as 0x68 (Decimal 104) is the address for most RTC's incase you are looking to confirm them on the bus.

I did what you said, and then tried my code again but still had the same results.

Then I uploaded your code and it worked flawlessly.

chucktodd: Minimum sketch to scan the I2C bus for Slaves.

Chuck.

Chuck,

I also decide to give your code a whirl and I hooked up those extra resistors.

When I uploaded the code it started the scan in the serial monitor but go hung up at that spot. Either I was too impatient after 5 min of no new data or it wasn't going to work.

Thank you both for your replies!