I2C scanner over Qwiic
What this example shows
Scan the I2C bus for connected Qwiic or I2C devices and list their addresses, both on the screen and in the Serial Monitor. I2C is a two-wire bus that lets chips talk to each other, and Qwiic is a plug-in connector for it. Use this sketch to check that a device is wired correctly before you write code for it.
The sketch tries every address from 1 to 126 and repeats the scan every 5 seconds.
Example code
Connect a Qwiic or I2C device to the Inkplate, upload the sketch and open the Serial Monitor at 115200 baud. In the Arduino IDE, choose the Inkplate 10 board under Tools, Board, Inkplate Boards before you upload.
#include "Inkplate.h"
#include "Wire.h"
// Create Inkplate object
Inkplate display(INKPLATE_1BIT);
void setup()
{
// Initialize Inkplate
display.begin();
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(BLACK);
display.setCursor(0, 0);
display.println("Inkplate I2C Scanner");
display.display();
display.expander1.digitalWrite(3,HIGH, true);
display.expander1.digitalWrite(4,HIGH, true);
// Initialize I2C and Serial
//Wire.begin();
Serial.begin(115200);
Serial.println("\nSoldered Inkplate I2C Scanner!");
}
void loop()
{
byte error, address;
int nDevices = 0;
int yCursor = 30; // Vertical position for listing devices
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
display.println("Scanning I2C...");
display.setTextSize(1);
Serial.println("Scanning...");
for (address = 1; address < 127; 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(" !");
display.setCursor(0, yCursor);
display.print("Found: 0x");
if (address < 16)
display.print("0");
display.println(address, HEX);
yCursor += 12;
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");
display.setCursor(0, yCursor);
display.println("No devices found.");
}
else
{
Serial.println("Done.\n");
}
display.display();
delay(5000);
}
Trimmed from Inkplate10_Qwiic.ino: the file header comment is left out. The code is otherwise unchanged. The Wire.begin() line is commented out in the original because display.begin() already starts the I2C bus.
Functions used
Inkplate(uint8_t mode)returns NoneCreates the Inkplate object and chooses the display mode. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` is grayscale and only supports full updates.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | mode | Display mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1). |
display.begin()returns voidInitializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.
ReturnsNothing.
display.clearDisplay()returns voidClears the frame buffer in memory. It does not clear the panel itself, call `display()` afterwards to do that.
ReturnsNothing.
display.setTextSize(uint8_t s)returns voidSets the text magnification. Size 1 is the classic 5x7 pixel font, size 5 is five times bigger. This comes from the Adafruit GFX base class that the library builds on.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | s | Text size multiplier. |
display.setTextColor(uint16_t c)returns voidSets the text color with a transparent background. This comes from the Adafruit GFX base class that the library builds on.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | c | Text color, for example BLACK. |
display.setCursor(int16_t x, int16_t y)returns voidSets where the next printed text starts, in pixels from the top left corner. This comes from the Adafruit GFX base class that the library builds on.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int16_t | x | X coordinate in pixels. |
int16_t | y | Y coordinate in pixels. |
display.println(value, format)returns size_tSame as `print()`, then moves the cursor to the start of the next line.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text, char or number | value | What to print. Optional: with no value it only moves to the next line. |
int | format | Optional. Number base for integers, for example DEC. |
display.display(bool _leaveOn = 0)returns voidRefreshes the whole panel from the frame buffer (a full update).
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | If true, the e-paper supply stays on after the update, which saves time on the next update. Defaults to 0. |
display.expander1.digitalWrite(uint8_t _pin, uint8_t _state, bool _bypassCheck = false)returns voidSets an output pin on the internal IO expander to `HIGH` or `LOW`. The sketch sets pins 3 and 4 to `HIGH` with the check bypassed, before it starts scanning. The example does not say what these two pins do.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | _pin | Expander pin number. |
uint8_t | _state | HIGH or LOW. |
bool | _bypassCheck | Setting this to true bypasses the library block on pins it reserves for itself. Defaults to false. |
Serial.begin(unsigned long baud)returns voidStarts the serial connection to the computer. Set the Serial Monitor to the same baud rate.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | baud | Baud rate. The sketch uses 115200. |
Serial.println(value, format)returns size_tPrints text or a number to the Serial Monitor, followed by a line break.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text or number | value | What to print. |
int | format | Optional. Number base for integers, for example HEX. |
Serial.print(value, format)returns size_tPrints text or a number to the Serial Monitor without a line break.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text or number | value | What to print. |
int | format | Optional. Number base for integers, for example HEX. |
Wire.beginTransmission(uint8_t address)returns voidArduino I2C function that starts a transmission to the device at the given address.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | address | 7-bit I2C address to talk to. |
Wire.endTransmission()returns uint8_tArduino I2C function that ends the transmission. The sketch uses the result to tell whether a device answered at that address.
Returns`0` if a device acknowledged at that address. The sketch reports `4` as an unknown error.
display.print(value, format)returns size_tPrints text, a character or a number at the cursor position, in the frame buffer. The display object builds on the Adafruit GFX base class, which extends the Arduino `Print` class, so it prints like `Serial` does. Call `display()` or `partialUpdate()` to show the result.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text, char or number | value | What to print. |
int | format | Optional. Number base for integers, for example DEC. |
delay(unsigned long ms)returns voidArduino function that pauses the sketch.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | ms | Time to wait, in milliseconds. |
What to expect
The screen shows Scanning I2C... and then one Found: 0x.. line for each device, or No devices found. if none answers. The Serial Monitor shows the same results as they happen.
Soldered Inkplate I2C Scanner!
Scanning...
For each device found, the sketch then prints I2C device found at address 0x followed by the address in hexadecimal and !. If no device answers it prints No I2C devices found, otherwise it ends the scan with Done.. An error code of 4 is reported as Unknown error at address 0x plus the address.