How to use Arduino to query IIC address information

How to use Arduino to query IIC address information

Today we learn how to search for information on the device address connected to the IIC. And display it to the PC side.
The connection of our UNO R3 IIC bus and the device is as shown below:

IIC bus connection diagram:

Arduino program code:

/***********************************************************
File name:  IICAdressFind.ino
Description:  Use Arduino to query IIC address information,
You can scan the device address information on the IIC line.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/06/30 
***********************************************************/

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

byte start_address = 1;
byte end_address = 127;

// 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,1);
    if(rc==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);
  addr = addr<<1;
  Serial.print("\t HEX: 0x");
  Serial.print(addr,HEX);
  Serial.println( (result==0) ? "\t found!":"   ");
//  Serial.print( (addr%4) ? "\t":"\n");
}
void setup()
{
    pinMode(13,OUTPUT);
    Wire.begin();

    Serial.begin(9600);
    Serial.println("--- I2C Bus Scanner Test ---");
    Serial.print("starting scanning of I2C bus from ");
    Serial.print(start_address,DEC);
    Serial.print(" to ");
    Serial.print(end_address,DEC);
    Serial.println("...");
    Serial.println();

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

    Serial.print("\n");
    Serial.println("--- I2C Bus Scanner Complete ---");

    digitalWrite(13,HIGH); delay(500); digitalWrite(13,LOW); delay(500);
    digitalWrite(13,HIGH); delay(500); digitalWrite(13,LOW); delay(500);
    digitalWrite(13,HIGH); delay(500); digitalWrite(13,LOW); delay(500);
}
void loop() 
{
}

The device information obtained after scanning:

Scan a total of three address information:0x40; 0x41; 0x70.

One Reply to “How to use Arduino to query IIC address information”

  1. Here at Leslie’s Pugsland Breeder, Our beautiful fawn pug has given birth to 4 healthy Pug puppies.
    She is our beloved family dog and this is her 1st litter.
    Both mum and dad are AKC registered with 5 gen pedigree certificate. Both from Top Champion bloodlines.Mum and Dad can be seen. Puppies have been wormed weeks 2,4,6,8 and will have had a flea preventative treatment before leaving us.
    They will have their vet check, 1st vaccinations and microchip.
    To view all available puppies and recent pictures, Search us in google as LESLIE’S PUGSLAND BREEDER or click on our website link bellow : https://pugslandbreeder.company.com/

    Pug Puppies for sale | pug for sale near me | pug puppy for sale | pug puppy | pug for sale | pug puppies for sale in va | black pug puppies for sale

Leave a Reply