CircuitPython I2C Scan

This quick-start example shows how you can use CircuitPython to scan the I2C bus for all connected devices

Copy and paste the code block into using your favorite text editor, and save the file, to run the demo

# Gemma/Trinket IO demo - I2C scan

import board
import busio
import time

# can also use board.SDA and board.SCL for neater looking code!
i2c = busio.I2C(board.D2, board.D0)

while not i2c.try_lock():

while True:
    print("I2C addresses found:", [hex(i) for i in i2c.scan()])

You can also use the Trinket to chat with I2C sensors and devices. Before you start, we recommend connecting it up and doing an I2C scan so you can tell if it was detected.

You can create the I2C devices on the Trinket M0's D2 (default SCL) and D0 (default SDA) pins. You can use board.D2 or board.SDA (which is more flexible if you're going to run the code on another device).

Then the I2C device must be locked (that means you are reserving access to it)

Then run a scan with i2c.scan() It will return an array of addresses, but since usually they are referred to in hex format, you may want to convert the array to hexadecimals with [hex(i) for i in i2c.scan()])

Don't forget that the Trinket M0 does not have I2C pullup resistors built in, you must add 2.2-10K ohm pullups on both SDA and SCL to 3.3V (our breakouts come with them already)

We wired up a MCP9808 breakout with address 0x18 to test it!

Last updated on 2017-12-24 at 06.57.45 PM Published on 2017-08-23 at 05.57.02 PM