The I2C, or inter-integrated circuit, is a 2-wire protocol for communicating with simple sensors and devices, which means it uses two connections, or wires, for transmitting and receiving data. One connection is a clock, called SCL. The other is the data line, called SDA. Each pair of clock and data pins are referred to as a bus.
Typically, there is a device that acts as a controller and sends requests to the target devices on each bus. In this case, your microcontroller board acts as the controller, and the sensor breakout acts as the target. Historically, the controller is referred to as the master, and the target is referred to as the slave, so you may run into that terminology elsewhere. The official terminology is controller and target.
Multiple I2C devices can be connected to the same clock and data lines. Each I2C device has an address, and as long as the addresses are different, you can connect them at the same time. This means you can have many different sensors and devices all connected to the same two pins.
Both I2C connections require pull-up resistors, and most Adafruit I2C sensors and breakouts have pull-up resistors built in. If you're using one that does not, you'll need to add your own 2.2-10kΩ pull-up resistors from SCL and SDA to 3.3V.
I2C and CircuitPython
CircuitPython supports many I2C devices, and makes it super simple to interact with them. There are libraries available for many I2C devices in the CircuitPython Library Bundle. (If you don't see the sensor you're looking for, keep checking back, more are being written all the time!)
In this section, you'll learn how to scan the I2C bus for all connected devices. Then you'll learn how to interact with an I2C device.
Necessary Hardware
You'll need the following additional hardware to complete the examples on this page.
While the examples here will be using the Adafruit MCP9808, a high accuracy temperature sensor, the overall process is the same for just about any I2C sensor or device.
The first thing you'll want to do is get the sensor connected so your board has I2C to talk to.
Wiring the MCP9808
The MCP9808 comes with a STEMMA QT connector, which makes wiring it up quite simple and solder-free.
Connect the STEMMA QT cable from the STEMMA QT port on your board to the STEMMA QT port on the MCP9808.
Find Your Sensor
The first thing you'll want to do after getting the sensor wired up, is make sure it's wired correctly. You're going to do an I2C scan to see if the board is detected, and if it is, print out its I2C address.
In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, open the directory CircuitPython_Templates/i2c_scan/ and then click on the directory that matches the version of CircuitPython you're using.
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries # SPDX-License-Identifier: MIT """CircuitPython I2C Device Address Scan""" import time import board i2c = board.I2C() # uses board.SCL and board.SDA # i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller # To create I2C bus on specific pins # import busio # i2c = busio.I2C(board.GP1, board.GP0) # Pi Pico RP2040 while not i2c.try_lock(): pass try: while True: print( "I2C addresses found:", [hex(device_address) for device_address in i2c.scan()], ) time.sleep(2) finally: # unlock the i2c bus when ctrl-c'ing out of the loop i2c.unlock()
In the editor window in your browser, click the Open button to view the file dialog. Then, click the Upload button and select Upload Files.
You'll be asked if you want to overwrite the previous code.py with the new code.py file from the Project Bundle. Click OK.
You'll see a new code.py file appear in the file browser. Select it and click Open to view it in the code editor.
The Feather ESP32-C6 comes with 1 I2C sensor built in: the MAX17048. The I2C scan code will show the address from the built in sensor (0x36) and the MCP9808 (0x18).
ESP32-C6 Feather comes with an I2C sensor built in: the MAX17048. The I2C scan code will show the addresses from the built-in sensor and the MCP9808.
If you run this and it seems to hang, try manually unlocking your I2C bus by running the following two commands from the REPL.
import board board.I2C().unlock()
First you create the i2c
object, using board.I2C()
. This convenience routine creates and saves a busio.I2C
object using the default pins board.SCL
and board.SDA
. If the object has already been created, then the existing object is returned. No matter how many times you call board.I2C()
, it will return the same object. This is called a singleton.
To be able to scan it, you need to lock the I2C down so the only thing accessing it is the code. So next you include a loop that waits until I2C is locked and then continues on to the scan function.
Last, you have the loop that runs the actual scan, i2c_scan()
. Because I2C typically refers to addresses in hex form, the example includes this bit of code that formats the results into hex format: [hex(device_address) for device_address in i2c.scan()]
.
Open the serial console to see the results! The code prints out an array of addresses. You've connected the MCP9808 which has a 7-bit I2C address of 0x18. The result for this sensor is I2C addresses found: ['0x18']
. If no addresses are returned, refer back to the wiring diagrams to make sure you've wired up your sensor correctly.
Text editor powered by tinymce.