I2C Examples - Raspberry Pi Pico

The following short Python programs will demonstrate essential operation of the Raspberry Pi Pico board. These assume one or more I2C (or I2C) devices are externally attached. The I2C bus is a two-wire bidirectional serial bus for short-distance low-bandwidth communication between a microcontroller and peripherals.

Each sample can be run by copying the program into code.py on the CIRCUITPY drive offered by the board. The text can be pasted directly from this page, or each file can be downloaded from the CircuitPython sample code folder on this site.

Related Pages

I2C Bus Scan

The following utility script uses the built-in scanning feature to detect devices on the I2C bus. It uses only built-in firmware modules.

Direct download: bus_scan.py.

 1# bus_scan.py 
 2
 3# Raspberry Pi Pico - I2C Bus Scan
 4# Search for devices on an I2C bus.
 5
 6#---- electrical connections ---------------------------------------------------
 7# The Pico has two hardware I2C ports which can each be mapped to a number of
 8# possible pin pairs.  This example uses pins 6 and 7 for I2C0 SDA
 9# and SCL.  For alternate choices please see the official pinout diagram.
10
11# Pico                  description
12# pin 7/GP5/I2C0 SCL    I2C clock from port 0
13# pin 6/GP4/I2C0 SDA    I2C data from port 0
14# pin 38/GND            common ground
15
16#-------------------------------------------------------------------------------
17# related CircuitPython module documentation:
18
19# busio             https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/index.html
20# time              https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html
21# board             https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html
22
23#-------------------------------------------------------------------------------
24# load standard Python modules
25import time
26
27# load the CircuitPython hardware definition module for pin definitions
28import board
29
30# load the CircuitPython I2C support
31import busio
32
33#---------------------------------------------------------------
34# The device is connected to I2C0 on pins 6 and 7.
35i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
36
37#---------------------------------------------------------------
38# Scan for devices.
39if i2c.try_lock():
40    print("Starting I2C scan.")
41    devices = i2c.scan()
42    i2c.unlock()
43    
44    print("Found %d I2C device(s)." % (len(devices)))
45    for dev in devices:
46        print("  I2C address:  %d (0x%x)" % (dev, dev))
47
48else:
49    print("Unable to lock I2C interface.")

Library Installation

The Pico firmware includes core modules for using the onboard I/O, but a number of optional packages are available to support specific hardware. The easiest way to install these is to download a bundle of precompiled libraries and copy individual packages or modules as needed to the CIRCUITPY filesystem.

The bundle version must match the installed version of CircuitPython. Current bundles are available at https://circuitpython.org/libraries. The bundle is zip file containing a large number of .mpy library files and sample code. You’ll be unpacking this on your desktop or laptop and installing just a subset of files.

For example, the seven-segment demo uses the adafruit_ht16k33 package, which is a folder containing several individual modules. To install it, unpack the library zip and locate the lib/adafruit_ht16k33 folder. This should be copied to the lib folder on the CIRCUITPY device. This folder is on the default search path for import.

To check the result, when installed correctly the Pico CIRCUITPY filesystem should include the following: lib/adafruit_ht16k33/segments.mpy

Seven-Segment LED Display

This demo shows numbers on a four-digit LED display connected via the Pico I2C bus. Please note it uses an optional library which must be installed separately.

The display is an Adafruit product: 0.56” 4-Digit 7-Segment Display w/I2C Backpack. Adafruit has a useful tutorial which describes this and several related products.

Direct download: seven_segment_demo.py.

 1# seven_segment_demo.py
 2
 3# Raspberry Pi Pico - Seven Segment I2C Display demo
 4
 5# Display a number on a four-digit seven-segment display using an I2C interface.
 6
 7# This uses a particular product from Adafruit:
 8#   Adafruit 0.56" 4-Digit 7-Segment Display w/I2C Backpack
 9#   shop:     https://www.adafruit.com/products/881 (blue version)
10#   tutorial: https://learn.adafruit.com/adafruit-led-backpack/0-dot-56-seven-segment-backpack
11
12# IDeATe stocks a small quantity of these devices in Lending for long-term loan.
13# The description can be found in the Physical Computing lab database under part
14# numbers 2342, 2343, 2345, 2347, and 2349, corresponding to different digit
15# colors.
16
17# The board is based on a HT16K33 I2C LED driver chip.  The default address
18# (with no jumpers installed) is 0x70.
19
20#---- Adafruit library ----------------------------------------------------
21# This sample uses an Adafruit library which is not included in the firmware,
22# but which must be copied to the board.  All of these optional libraries are
23# distributed in a bundle which must match the CircuitPython firmware version.
24
25# To install, unzip the bundle zip and locate the lib/adafruit_ht16k33 folder.
26# This contains several precompiled .mpy files; the folder should be copied to the
27# lib folder on the CIRCUITPY device. 
28# 
29# E.g., the installed files should include the following:
30#   CIRCUITPY/lib/adafruit_ht16k33/segments.mpy
31
32#---- electrical connections ---------------------------------------------------
33# The Pico has two hardware I2C ports which can each be mapped to a number of
34# possible pin pairs.  This example uses pins 6 and 7 for I2C0 SDA
35# and SCL.  For alternate choices please see the official pinout diagram.
36
37# Pico                  display         description
38# pin 7/GP5/I2C0 SCL    SCL             I2C clock from port 0
39# pin 6/GP4/I2C0 SDA    SDA             I2C data from port 0
40# pin 36/3.3VOUT        VCC             3.3V to power display
41# pin 38/GND            GND             common ground
42
43################################################################
44# related CircuitPython module documentation:
45
46# busio             https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/index.html
47# time              https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html
48# board             https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html
49# adafruit_ht16k33  https://circuitpython.readthedocs.io/projects/ht16k33/en/latest/api.html
50
51# Adafruit_CircuitPython_HT16K33 source: https://github.com/adafruit/Adafruit_CircuitPython_HT16K33
52
53################################################################################
54# load standard Python modules
55import time
56
57# load the CircuitPython hardware definition module for pin definitions
58import board
59
60# load the CircuitPython I2C support
61import busio
62
63# load the Adafruit HT16K33 library
64import adafruit_ht16k33.segments
65
66#---------------------------------------------------------------
67# The device is connected to I2C0 on pins 6 and 7.
68i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
69display = adafruit_ht16k33.segments.Seg7x4(i2c)
70
71#---------------------------------------------------------------
72# Run the main loop to generate a counting display.
73
74while True:
75    for count in range(10000):
76        display.print("%04d" % count)
77        print("%04d" % count)
78        time.sleep(1)
79        

LCD 20x4 Character Display

This demo shows text and numbers on a LCD 20x4 display connected via the Pico I2C bus. Please note it uses an optional library which must be installed separately.

Direct download: lcd_display_demo.py.

 1# lcd_display_demo.py
 2
 3# Raspberry Pi Pico - LCD I2C 20x4 Character Display demo
 4
 5# Display text on a 20x4 character display using an I2C interface.
 6# IDeATe stocks a small quantity of these devices in the Hunt A10 Physical
 7# Computing Lab as part 0627.
 8
 9# This is a generic display part which uses a PCF8574 "Remote 8-Bit I/O Expander
10# for I2C Bus" to drive the LCD display parallel port.  The default I2C address
11# is 0x27 as determined by a bus scan.
12
13#---- LCD library ---------------------------------------------------------
14# This sample uses a third-party library which is not included in the firmware,
15# but which must be copied to the board.
16#
17# PCF8574 LCD library: https://github.com/dhalbert/CircuitPython_LCD
18#
19# To install, clone the repo or download it, then copy the lcd/ folder
20# to the lib/ folder on the CIRCUITPY device.
21
22#---- electrical connections ---------------------------------------------------
23# The Pico has two hardware I2C ports which can each be mapped to a number of
24# possible pin pairs.  This example uses pins 6 and 7 for I2C0 SDA
25# and SCL.  For alternate choices please see the official pinout diagram.
26
27# Pico                  display         description
28# pin 7/GP5/I2C0 SCL    SCL             I2C clock from port 0
29# pin 6/GP4/I2C0 SDA    SDA             I2C data from port 0
30# pin 36/3.3VOUT        VCC             3.3V to power display
31# pin 38/GND            GND             common ground
32
33# On the display, install a jumper on the two-pin header labeled LED in order to
34# power the backlight, otherwise the display is almost unreadable.
35
36################################################################
37# related CircuitPython module documentation:
38
39# board  https://circuitpython.readthedocs.io/en/latest/shared-bindings/board/index.html
40# busio  https://circuitpython.readthedocs.io/en/latest/shared-bindings/busio/index.html
41# time   https://circuitpython.readthedocs.io/en/latest/shared-bindings/time/index.html
42
43################################################################################
44# load standard Python modules
45import time
46
47# load the CircuitPython hardware definition module for pin definitions
48import board
49
50# load the CircuitPython I2C support
51import busio
52
53# load the LCD library
54import lcd.lcd
55import lcd.i2c_pcf8574_interface
56
57#---------------------------------------------------------------
58# The device is connected to I2C0 on pins 6 and 7.
59i2c = busio.I2C(scl=board.GP5, sda=board.GP4)
60
61iface = lcd.i2c_pcf8574_interface.I2CPCF8574Interface(i2c, 0x27)
62display = lcd.lcd.LCD(iface, num_rows=4, num_cols=20)
63display.set_backlight(True)
64display.set_display_enabled(True)
65
66display.print("Hello, world.")
67
68#---------------------------------------------------------------
69# Run the main loop to generate a counting display.
70
71while True:
72    for count in range(10000):
73        display.set_cursor_pos(1, 4)
74        display.print("%04d" % count)
75        print("%04d" % count)
76        time.sleep(1)
77