The I2CScanner library implements a scanner to locate I2C devices, and determine if a device is connected.
User Manual
The I2CScanner library is instantiated through its constructor. The object contains variables for the address range for the scan, Low_Address and High_Address. The object is initialized through the Init() method.
There are three families of methods. The Scan methods work like the traditional I2CScanner Sketch, displaying the results on the screen.
The Check methods check for the existence of the devices, but do not display any output. Finally, the Execute functions take a Callback function as a parameter, and only execute it if the device is connected.
The 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 that address. If they receive a vector of addresses, they act on those addresses.
Constructor
The I2CScanner class is instantiated as an object through its constructor.
I2CScanner();
Using I2CScanner
// Devices found in the last scan
uint8_t Devices_Count;
// Range for the scan without parameters
uint8_t Low_Address = 1;
uint8_t High_Address = 127;
// Initialization
void Init();
// Scanner functions, display result via serial
bool Scan();
bool Scan(byte address);
bool Scan(byte addreses[], uint8_t length);
// Check functions, do not display result via serial
bool Check();
bool Check(byte address);
bool Check(byte addreses[], uint8_t length);
// Execute functions, execute the callback function if the device exists
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 that shows the use of I2CScanner displaying results via 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 that shows 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);
}
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy it to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE