Avatar
ARDUINO I2C SCANNER LIBRARY
Aug 17th, 2021 | by: Rick Sanchez | Views 190
Views 190

The I2CScanner library is instantiated through its constructor. The object contains as variables the address range for Low_Address and High_Address scanning. The object is initialized through the Init () method. There are three families of methods. The Scan methods work in the same way as the traditional Sketch I2CScanner, displaying the results on the screen. The Check methods check for the existence of the devices, but do not show any output. Finally, the Execute functions receive a Callback function as a parameter, and execute it only if the device is connected. All three families of functions have overloads. If they do not receive any parameters, they act on the range defined by Low_Address and High_Address. If they receive an address, they act on it. If they receive a vector of directions, they act on them.

BUILDER

The I2CScanner class is instantiated as an object through its constructor.


I2CScanner();

USING I2CSCANNER

   // Devices found with last scan
   uint8_t Devices_Count;
   
   // Range for the scanner
   uint8_t Low_Address = 1;
   uint8_t High_Address = 127;
   
   // Initialize
   void Init();

   // scan functions
   bool Scan();
   bool Scan(byte address);
   bool Scan(byte addreses[], uint8_t length);

   // Check functions
   bool Check();
   bool Check(byte address);
   bool Check(byte addreses[], uint8_t length);

   void Execute(I2C_Callback callback);
   void Execute(byte address, I2C_Callback callback);
   void Execute(byte addreses[], uint8_t length, I2C_Callback callback);

EXAMPLES

The I2CScanner library includes the following examples to illustrate its use.

 

  • Scanner: Example showing the use of I2CScanner showing results by Serial

 #include "I2CScanner.h"

I2CScanner scanner;

void setup() 
{
   Serial.begin(9600);
   while (!Serial) {};

   scanner.Init();
}

void loop() 
{
   scanner.Scan();
   delay(5000);
}

  • Check: Example that shows the use of I2CScanner, storing the check results in an array, which we would then use in the code

#include "I2CScanner.h"

I2CScanner scanner;

const uint8_t num_addresses = 4;
const byte addresses[num_addresses] = { 0x20, 0x21, 0x40, 0x41 };
bool results[num_addresses] = { false, false, false, false};


void setup() 
{
   Serial.begin(9600);
   while (!Serial) {};

   scanner.Init();
}

void loop() 
{
   for (uint8_t index = 0; index < num_addresses; index++)
   {
      results[index] = scanner.Check(addresses[index]);
   }
   
   for (uint8_t index = 0; index < num_addresses; index++)
   {
      if (results[index])
      {
         Serial.print("Found device ");
         Serial.print(index);
         Serial.print(" at address ");
         Serial.println(addresses[index], HEX);
      }
   }
   delay(5000);
}

  • Execute: Example showing the use of I2CScanner executing callback functions

#include "I2CScanner.h"

I2CScanner scanner;

const byte address;

void debug(byte address)
{
   Serial.print("Found at 0x");
   Serial.println(address, HEX);
}

void setup() 
{
   Serial.begin(9600);
   while (!Serial) {};

   scanner.Init();
}

void loop() 
{
   scanner.Execute(debug);
   delay(5000);
}

INSTALL the library
  • Download the latest version from GitHub
  • Unzip the file
  • Copy to your libraries folder (usually My Documents \ Arduino \ libraries)
  • Relaunch the Arduino IDE

 

0 Comments

Login or Sign Up to post comments on this tutorial.