Skip to main content

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.

Select Board
Inkplate10_Qwiic.ino
#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 None

Creates 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

TypeNameDescription
uint8_tmodeDisplay mode: INKPLATE_1BIT (0) or INKPLATE_3BIT (1).
display.begin()returns void

Initializes the library, the peripherals and the frame buffers. Call it once in `setup()`, later calls are ignored.

ReturnsNothing.

display.clearDisplay()returns void

Clears 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 void

Sets 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

TypeNameDescription
uint8_tsText size multiplier.
display.setTextColor(uint16_t c)returns void

Sets the text color with a transparent background. This comes from the Adafruit GFX base class that the library builds on.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tcText color, for example BLACK.
display.setCursor(int16_t x, int16_t y)returns void

Sets 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

TypeNameDescription
int16_txX coordinate in pixels.
int16_tyY coordinate in pixels.
display.println(value, format)returns size_t

Same as `print()`, then moves the cursor to the start of the next line.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text, char or numbervalueWhat to print. Optional: with no value it only moves to the next line.
intformatOptional. Number base for integers, for example DEC.
display.display(bool _leaveOn = 0)returns void

Refreshes the whole panel from the frame buffer (a full update).

ReturnsNothing.

Parameters

TypeNameDescription
bool_leaveOnIf 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 void

Sets 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

TypeNameDescription
uint8_t_pinExpander pin number.
uint8_t_stateHIGH or LOW.
bool_bypassCheckSetting this to true bypasses the library block on pins it reserves for itself. Defaults to false.
Serial.begin(unsigned long baud)returns void

Starts the serial connection to the computer. Set the Serial Monitor to the same baud rate.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longbaudBaud rate. The sketch uses 115200.
Serial.println(value, format)returns size_t

Prints text or a number to the Serial Monitor, followed by a line break.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text or numbervalueWhat to print.
intformatOptional. Number base for integers, for example HEX.
Serial.print(value, format)returns size_t

Prints text or a number to the Serial Monitor without a line break.

ReturnsNumber of bytes printed.

Parameters

TypeNameDescription
text or numbervalueWhat to print.
intformatOptional. Number base for integers, for example HEX.
Wire.beginTransmission(uint8_t address)returns void

Arduino I2C function that starts a transmission to the device at the given address.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_taddress7-bit I2C address to talk to.
Wire.endTransmission()returns uint8_t

Arduino 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_t

Prints 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

TypeNameDescription
text, char or numbervalueWhat to print.
intformatOptional. Number base for integers, for example DEC.
delay(unsigned long ms)returns void

Arduino function that pauses the sketch.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longmsTime 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.

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
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.