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 main.py 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():
    pass

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

You can also use the Gemma 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 Gemma M0's D2 (SCL) and D0 (SDA) pins.

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 Gemma 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 Flora TSL2561 breakout with address 0x39 to test it!

Last updated on 2017-12-01 at 04.59.09 PM Published on 2017-07-26 at 06.08.30 PM