# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/101/968/medium800/sensors_pico_u2if_banner2.png?1619722234)

This guide will show you how to use a Raspberry Pi Pico RP2040 to connect various sensors and breakouts to your PC running Windows, Mac OSX, or Linux. Special firmware gets loaded onto the Pico and turns it into a sort of Swiss army knife providing:

- General Purpose digital Input and Output ( **GPIO** ) for things like buttons and LEDs
- Analog to Digital Conversion ( **ADC** ) for reading analog signals
- Pulse Width Modulation ( **PWM** ) for servos or LED dimming
- **I2C** and **SPI** for connecting \*lots\* of external sensors, displays, etc.
- **NeoPixels** (WS2812B) for happy rainbow blinky fun!

This is very similar to what the [FT232H](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h) and [MCP2221](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221) already provide.

Warning: 

The approach in this guide is useful if you want to run "regular" Python code on your main computer and have it communicate with external devices connected through the Pico (or other RP2040 board). If you are instead trying to run MicroPython code directly on the Pico and use CircuitPython libraries, then see this other guide: [CircuitPython Libraries on MicroPython using the Raspberry Pi Pico](https://learn.adafruit.com/circuitpython-libraries-on-micropython-using-the-raspberry-pi-pico).

## The Magical u2if Firmware

The key element to enabling this capability on the Raspberry Pi Pico is thanks to the excellent [**u2if firmware**](https://github.com/execuc/u2if) written by [**execuc**](https://github.com/execuc). The main repo not only contains the firmware that goes on the Pico itself, but micropython compliant Python code for interfacing to the Pico from your PC. So if you're more used to the micropython interface, then checkout the u2if repo. It has everything you need.

[u2if project](https://github.com/execuc/u2if)
In this guide, we will use firmware from the Adafruit fork of the original u2if project:

[Adafruit u2if fork](https://github.com/adafruit/u2if)
Then, on the host PC, we will use the [Adafruit Blinka library](https://github.com/adafruit/Adafruit_Blinka) which has support for interfacing with a Pico, or other RP2040 based boards, running the u2if firmware.

## CircuitPython Libraries on Personal Computers

This is essentially the same idea as discussed&nbsp; in the [FT232H Guide](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h/overiew) and the [MCP2221 Guide](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221/overview). How can we directly connect common hardware items like buttons and LEDS (GPIO) or sensor breakouts (I2C/SPI) to a PC?

![](https://cdn-learn.adafruit.com/assets/assets/000/101/849/medium800/sensors_board_pc.png?1619472142)

By loading the [u2if firmware](https://github.com/execuc/u2if) onto the Pico, it turns it into sort of a bridge using USB on the main PC. So you end up with something like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/848/medium800/sensors_interfaces.png?1619472107)

On the computer, we install Blinka which provides a CircuitPython compliant interface to the Pico with u2if. That way, all the CircuitPython libraries can then be used - on your PC!

## Required Hardware

The main requirement is a supported RP2040 based board. The main guide and examples are based on the original Raspberry Pi Pico RP2040:

### Raspberry Pi Pico RP2040 with Loose Unsoldered Headers

[Raspberry Pi Pico RP2040 with Loose Unsoldered Headers](https://www.adafruit.com/product/4883)
The Raspberry Pi foundation changed single-board computing&nbsp;[when they released the Raspberry Pi computer](https://www.raspberrypi.org/archives/723), now they're ready to do the same for microcontrollers with the release of the brand new&nbsp; **Raspberry Pi...**

In Stock
[Buy Now](https://www.adafruit.com/product/4883)
[Related Guides to the Product](https://learn.adafruit.com/products/4883/guides)
![Top view of Raspberry Pi Pico with two 20-pin male headers.](https://cdn-shop.adafruit.com/640x480/4883-06.jpg)

However, there are many other RP2040 based boards that are supported. See the **Other RP2040 Boards** section for details.

You'll need a USB cable for programming and interacting with the Pico - but you probably have one of these laying around. Just make sure it's **not a charge only cable**.

Beyond that, it all depends on what you want to do. There are examples provided later in this guide that show some typical use cases.

## Other Hardware

If you're using STEMMA QT breakout boards, these cables can be helpful.

### STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable

[STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable](https://www.adafruit.com/product/4209)
This 4-wire cable is a little over 150mm / 6" long and fitted with JST-SH female 4-pin connectors on one end and premium Dupont male headers on the other. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert...

In Stock
[Buy Now](https://www.adafruit.com/product/4209)
[Related Guides to the Product](https://learn.adafruit.com/products/4209/guides)
![Angled Shot of the STEMMA QT / Qwiic JST SH 4-pin to Premium Male Headers Cable.](https://cdn-shop.adafruit.com/640x480/4209-05.jpg)

### STEMMA QT / Qwiic JST SH 4-pin Cable with Premium Female Sockets

[STEMMA QT / Qwiic JST SH 4-pin Cable with Premium Female Sockets](https://www.adafruit.com/product/4397)
This 4-wire cable is a little over 150mm / 6" long and fitted with JST-SH female 4-pin connectors on one end and premium female headers on the other. Compared with the chunkier JST-PH these are 1mm pitch instead of 2mm, but still have a nice latching feel, while being easy to insert and...

In Stock
[Buy Now](https://www.adafruit.com/product/4397)
[Related Guides to the Product](https://learn.adafruit.com/products/4397/guides)
![Angled shot of JST SH 4-pin Cable with Premium Female Sockets.](https://cdn-shop.adafruit.com/640x480/4397-02.jpg)

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Running CircuitPython Code without CircuitPython

There are two parts to the CircuitPython ecosystem:

- **CircuitPython firmware** , written in C and built to run on various microcontroller boards (not PCs). The firmware includes the CircuitPython interpreter, which reads and executes CircuitPython programs, and chip-specific code that controls the hardware peripherals on the microcontroller, including things like USB, I2C, SPI, GPIO pins, and all the rest of the hardware features the chip provides.
- **CircuitPython libraries,** written in Python to use the native (built into the firmware) modules provided by CircuitPython to control the microcontroller peripherals and interact with various breakout boards.

But suppose you'd like to use CircuitPython **libraries** on a board or computer that does not have a native CircuitPython **firmware** build. For example, on a PC running Windows or macOS. Can that be done? The answer is yes, via a separate piece of software called **Blinka**. Details about Blinka follow, however it is important to realize that the **CircuitPython firmware is never used**.

Info: 

# Adafruit Blinka: a CircuitPython Compatibility Library

Enter **Adafruit**  **Blinka**. Blinka is a software library that emulates the parts of CircuitPython that control hardware. Blinka provides non-CircuitPython implementations for `board`, `busio`, `digitalio`, and other native CircuitPython modules. You can then write Python code that looks like CircuitPython and uses CircuitPython libraries, without having CircuitPython underneath.

There are multiple ways to use Blinka:&nbsp;

- Linux based Single Board Computers, for example a Raspberry Pi
- Desktop Computers + specialized USB adapters
- Boards running MicroPython

More details on these options follow.

## Raspberry Pi and Other Single-Board Linux Computers

On a Raspberry Pi or other single-board Linux computer, you can use Blinka with the regular version of Python supplied with the Linux distribution. Blinka can control the hardware pins these boards provide.

## Desktop Computers&nbsp;

On Windows, macOS, or Linux desktop or laptop ("host") computers, you can use special USB adapter boards that that provide hardware pins you can control. These boards include [MCP221A](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221)&nbsp;and [FT232H](https://learn.adafruit.com/adafruit-ft232h-breakout) breakout boards, and [Raspberry Pi Pico boards running the **u2if** software](https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-raspberry-pi-pico). These boards connect via regular USB to your host computer, and let you do GPIO, I2C, SPI, and other hardware operations.

## MicroPython

You can also use Blinka with MicroPython, on [MicroPython-supported boards](https://learn.adafruit.com/circuitpython-libraries-on-micropython-using-the-raspberry-pi-pico). Blinka will allow you to import and use CircuitPython libraries in your MicroPython program, so you don't have to rewrite libraries into native MicroPython code. Fun fact - this is actually the original use case for Blinka.

# Installing Blinka

Installing Blinka on your particular platform is covered elsewhere in this guide. The process is different for each platform. Follow the guide section specific to your platform and make sure Blinka is properly installed before attempting to install any libraries.

Info: 

# Installing CircuitPython Libraries

Once Blinka is installed the next step is to install the CircuitPython libraries of interest. How this is down is different for each platform. Here are the details.

## Linux Single-Board Computers

On Linux single-board computers, such as Raspberry Pi, you'll use the Python `pip3`&nbsp;program (sometimes named just `pip`) to install a library. The library will be downloaded from [pypi.org](https://pypi.org) automatically by `pip3`.

How to install a particular library using `pip3` is covered in the guide page for that library. For example, [here is the `pip3` installation information](https://learn.adafruit.com/adafruit-lis3dh-triple-axis-accelerometer-breakout/python-circuitpython#python-installation-of-lis3dh-library-2997958) for the library for the LIS3DH accelerometer.

The library name you give to `pip3` is usually of the form `adafruit-circuitpython-libraryname`. This is not the name you use with `import`. For example, the LIS3DH sensor library is known by several names:

- The GitHub library repository is [Adafruit\_CircuitPython\_LIS3DH](https://github.com/adafruit/Adafruit_CircuitPython_LIS3DH).
- When you import the library, you write `import adafruit_lis3dh`.
- The name you use with `pip3` is `adafruit-circuitpython-lis3dh`. This the name used on [pypi.org](https://pypi.org).

Libraries often depend on other libraries. When you install a library with `pip3`, it will automatically install other needed libraries.

## Desktop Computers using a USB Adapter

When you use a desktop computer with a USB adapter, like the MCP2221A, FT232H, or u2if firmware on an RP2040, you will also use pip3. However, **do _not_ install the library with `sudo pip3`** , as mentioned in some guides. Instead, just install with `pip3`.&nbsp;

## MicroPython

For MicroPython, you will not use `pip3`. Instead you can get the library from the CircuitPython bundles. See [this guide page](https://learn.adafruit.com/welcome-to-circuitpython/circuitpython-libraries) for more information about the bundles, and also see the [Libraries page on circuitPython.org](https://circuitpython.org/libraries).

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Setup for Pico

![](https://cdn-learn.adafruit.com/assets/assets/000/101/947/medium800/sensors_firmware_install.png?1619642868)

The first step is to install the [u2if firmware from the Adafruit fork](https://github.com/adafruit/u2if) onto the Raspberry Pi Pico. This is super easy:

1. Download the latest release of **u2if\_pico.uf2** from the repo: [https://github.com/adafruit/u2if/releases](https://github.com/adafruit/u2if/releases)
2. Put the Pico in bootloader mode by holding the **BOOTSEL** button while plugging in the board.
3. Drag the downloaded UF2 file to the **RPI-RP2** folder.
4. DONE!

The board will reset after the copy is complete. **Note that no folders will show up.** So it may seem like nothing happened.

Info: 

Now the Pico will show up as two USB devices:

- **USB HID** (Human Interface Device)
- **USB CDC** (Communication Device Class)

The former provides a generic interface for sending 64 byte "reports" back and forth. The later is essentially a serial interface, aka "com port". How these show up on your PC will depend on OS.

## linux dmesg

On linux, the **dmesg** output will look something like this when connecting the Pico:

```none
[Mon Apr 26 13:07:36 2021] usb 2-1.5: new full-speed USB device number 12 using ehci-pci
[Mon Apr 26 13:07:36 2021] usb 2-1.5: New USB device found, idVendor=cafe, idProduct=4005, bcdDevice= 1.00
[Mon Apr 26 13:07:36 2021] usb 2-1.5: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[Mon Apr 26 13:07:36 2021] usb 2-1.5: Product: U2IF
[Mon Apr 26 13:07:36 2021] usb 2-1.5: Manufacturer: Pico
[Mon Apr 26 13:07:36 2021] usb 2-1.5: SerialNumber: 0xE6604430433F5326
[Mon Apr 26 13:07:36 2021] cdc_acm 2-1.5:1.0: ttyACM0: USB ACM device
[Mon Apr 26 13:07:36 2021] hid-generic 0003:CAFE:4005.000C: hiddev1,hidraw7: USB HID v1.11 Device [Pico U2IF] on usb-0000:00:1d.0-1.5/input2
```

## Windows Device Manager

On Windows, several new entries should show up in **Device Manager** :

![](https://cdn-learn.adafruit.com/assets/assets/000/101/847/medium800/sensors_dm.png?1619470463)

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Setup on PC

Danger: 

The main support for the Pico running the u2if firmware in Blinka utilizes the [hidapi library](https://pypi.org/project/hidapi/). Some of the features rely on sending data via a serial connection. For that, we use the [pyserial library](https://pypi.org/project/pyserial/). And to allow use of CircuitPython Libraries, we need the [Blinka](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/overview) interface layer.

All of these in turn rely on a several other things which vary for different OS's. So before we can actually use the Pico, we need to get everything setup. See the OS specific sections for what we went through to get things working for each.

## Additional Information

Just for reference, here are links to more information about the main Python libraries being used. Here's the README from the hidapi source code repo, which has some install information:

[hidapi README](https://github.com/trezor/cython-hidapi/blob/master/README.rst)
Here's the main documentation for pySerial:

[pySerial documentation](https://pythonhosted.org/pyserial/)
The pySerial source code [repo is here](https://github.com/pyserial/pyserial).

 **But first try the install instructions on the pages that follow for your OS.**

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Windows

## Have Python 3 Installed

We assume you already have Python 3 installed on your computer. **Note we do not support Python 2** - it's deprecated and no longer supported!

At your command line prompt of choice, check your Python version with `python --version`

![](https://cdn-learn.adafruit.com/assets/assets/000/101/957/medium800/sensors_image.png?1619646595)

## Install hidapi

From the command line, manually install **hidapi** with

```python
pip3 install hidapi
```

![](https://cdn-learn.adafruit.com/assets/assets/000/101/958/medium800/sensors_image.png?1619646647)

Danger: 

If the install fails with text that ends with something like:

> **distutils.errors.DistutilsError: Setup script exited with error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": [https://visualstudio.microsoft.com/downloads/](https://visualstudio.microsoft.com/downloads/)**

then you will need to also install the Microsoft Visual C++ Build Tools. Thanks to **@jklem** for pointing this out [in the forums](https://forums.adafruit.com/viewtopic.php?f=19&t=161344#p793969).

Download it from here (same link as in text):

[Microsoft Visual Studio Downloads](https://visualstudio.microsoft.com/downloads/)
 **NOTE: You do not need the full Visual Studio IDE. Just the Build Tools.**

- Scroll down to where it says **Tools for Visual Studio 2019**.
- Expand the list to show the sub options.
- Click the Download button for **Build Tools for Visual Studio 2019**.

![sensors_build_tools.png](https://cdn-learn.adafruit.com/assets/assets/000/101/959/medium640/sensors_build_tools.png?1619646745)

This downloads a .exe file with a name like **vs\_BuildTools.exe**. Run that to install the build tools and then try the pip install again.

## Install Blinka

To install Blinka and its dependencies, run:

```python
pip3 install adafruit-blinka
```

![](https://cdn-learn.adafruit.com/assets/assets/000/101/960/medium800/sensors_image.png?1619646895)

## Set Environment Variable

You must do this every time before running circuitpython code, you can set it permanently in windows if you like, for now just type into the same cmd window you're using with Python

`set BLINKA_U2IF=1`

If you are using Windows Powershell, the syntax is a little different. In that case do:

`$env:BLINKA_U2IF=1`

## Run the sanity checks.

Now move on to the Post Install Checks section and run the commands there to make sure everything is installed correctly.

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Mac OSX

We assume you already have Python 3 and brew available on your Mac. Thankfully, setup on MacOS X is not so bad!

Note: If you are running VMWare Fusion on MacOS, then you can also try the [Windows install](https://learn.adafruit.com/circuitpython-on-any-computer-with-ft232h/windows) process.

## Install libusb

Start by installing libusb with

`brew install libusb`

![](https://cdn-learn.adafruit.com/assets/assets/000/101/963/medium800/sensors_screenshot_03.png?1619647537)

## Install PySerial

Type `pip3 install pyserial`

## Install hidapi

Type `pip3 install hidapi`

Danger: 

## Install Blinka

Then `pip3 install adafruit-blinka`

![](https://cdn-learn.adafruit.com/assets/assets/000/101/964/medium800/sensors_screenshot_05.png?1619647595)

## Set Environment Variable&nbsp;

You'll need to set this variable every time before running CircuitPython code. To do this, we set the environment variable `BLINKA_U2IF`.

You can set the variable by running:

```python
export BLINKA_U2IF="1"
```

## Run the sanity checks.

Now move on to the Post Install Checks section and run the commands there to make sure everything is installed correctly.

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Linux

The following shows a typical run through installing and setting things up on Linux.

## Install libusb and libudev

Run the following:

```python
sudo apt-get install libusb-1.0 libudev-dev
```

and answer **Y** to the prompt. This should install libusb and libudev.

## Setup udev rules

For this, we just follow [recommended setup](https://github.com/execuc/u2if/blob/main/firmware/README.md#linux-udev-rule) from the firmware. Use a text editor to create and edit a file named **/etc/udev/rules.d/55-u2if.rules** and add the following contents:

```python
SUBSYSTEM=="usb", ATTR{idVendor}=="cafe", ATTR{idProduct}=="4005", MODE="0666"
```

## Install hidapi

To install hidapi, run:

```python
pip3 install hidapi
```

![](https://cdn-learn.adafruit.com/assets/assets/000/101/850/medium800/sensors_hidapi.jpg?1619473519)

Danger: 

## Install pySerial

To install pySerial, run:

```python
pip3 install pyserial
```

## Install Blinka

To install Blinka and its dependencies, run:

```python
pip3 install adafruit-blinka
```

![](https://cdn-learn.adafruit.com/assets/assets/000/101/851/medium800/sensors_pip.png?1619473605)

## Set environment variable

We need to manually signal to Blinka that we have a Pico running the u2if firmware. To do this we set the environment variable **BLINKA\_U2IF**. The value doesn't matter, just use 1:

```python
export BLINKA_U2IF=1
```

Danger: 

## Run the sanity checks.

Now move on to the Post Install Checks section and run the commands there to make sure everything is installed correctly.

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Post Install Checks

After going through all the install steps for your OS, run these checks as simple tests to make sure everything is installed correctly. **Go ahead and plug in your Pico to a USB port on your PC.**

Most of these tests are done via the Python REPL, at the `>>>` prompt. To get there, simply launch Python:

```python
$ python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt;
```

Danger: 

## Check that hidapi is installed correctly

At the Python REPL, type:

```python
import hid
hid.enumerate()
'device' in dir(hid)
```

The `enumerate()` command should dump a listing of everything attached to your USB ports. The last command is a test of the actual hid module imported and should return `True`.

![](https://cdn-learn.adafruit.com/assets/assets/000/121/306/medium800/sensors_Screenshot_from_2023-05-30_11-09-52.png?1685470397)

Danger: 

## Check that pySerial is installed correctly

At the Python REPL, type:

```python
import serial.tools.list_ports as lp
lp.comports()
```

You should get a list of available COM ports.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/949/medium800/sensors_Screenshot_from_2021-04-28_14-14-07.png?1619644461)

## Check that Pico can be found

At the Python REPL, type:

```python
import hid
device = hid.device()
device.open(0xCAFE, 0x4005)
```

Danger: 

It should run without any errors:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/950/medium800/sensors_Screenshot_from_2021-04-28_14-15-00.png?1619644508)

If for some reason the Pico can not be found, you might see something like this:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/951/medium800/sensors_Screenshot_from_2021-04-28_14-15-50.png?1619644559)

Check your USB cable connection and double check that the u2if firmware is loaded.

If you want to continue testing in the same Python session, then make a quick call to `close()` to free up the device.

```python
device.close()
```

Or, just exit the Python session.

## Check environment variable within Python

At the Python REPL, type:

```python
import os
os.environ["BLINKA_U2IF"]
```

If you get a KeyError it means you did not set the environment variable right:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/952/medium800/sensors_Screenshot_from_2021-04-28_14-16-33.png?1619644602)

If you have set it correctly, you'll get a value back:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/953/medium800/sensors_Screenshot_from_2021-04-28_14-17-33.png?1619644668)

## Check Blinka is setup correctly

**If all of the above checks pass** , go ahead and try this as a quick sanity check that basic Blinka functionality is in place. At the Python REPL, type:

```python
import board
dir(board)
```

You should not get any errors and the various pins available on the Pico should be shown.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/954/medium800/sensors_Screenshot_from_2021-04-28_14-22-14.png?1619644941)

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Pinout

![](https://cdn-learn.adafruit.com/assets/assets/000/102/148/medium800/sensors_pico_u2if_pinout.png?1621003908)

While the Raspberry Pi Pico allows the I2C and SPI pins to appear in multiple locations, the u2if firmware fixes these locations to specific pins.

## Power Pins

- **VBUS** - micro-USB input voltage
- **VSYS** - main system input voltage
- **3V3** - regulated 3.3V output, **300mA max**
- **GND** - main ground reference
- **AGND** - ground reference for GP26-29 and **ADC0** and **ADC1**

## GPIO Pins

- **GP0** to **GP28** - General Purpose Input Output ( **GPIO** ) as well as Pulse Width Modulation ( **PWM** )

## I2C Pins

- **SCL0** - I2C port 0 clock
- **SDA0** - I2C port 0 data
- **SCL1** - I2C port 1 clock
- **SDA1** - IC2 port 1 data

## SPI Pins

- **SCLK0** - SPI port 0 clock
- **MOSI0** - SPI port 0 data out
- **MISO0** - SPI port 0 data in
- **SCLK1** - SPI port 1 clock
- **MOSI1** - SPI port 1 data out
- **MISO1** - SPI port 1 data in

## ADC Pins

- **ADC0** - Analog to Digital Converter (ADC) 0
- **ADC1** - Analog to Digital Converter (ADC) 1

Warning: 

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Examples

All right, now that all that annoying install stuff is done, let's have some fun.

The following sections will provide some basic examples for the main use cases - GPIO, ADC, PWM, I2C, SPI, and NeoPixel.

Danger: 

## Installing Libraries for Breakouts

The general process for installing the CircuitPython library you are interested in will be the same as shown in the Python section of the Learn guide for your sensor. Just use pip3.

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## GPIO

## Digital Output

Let's blink a LED!

Here's the bread board layout. The resistor can be something around 1kOhm. We don't need to make the LED super bright.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/861/medium800/sensors_gpio_led_bb.jpg?1619552950)

And here's a complete blink program you can run to make the LED blink forever.

```python
import time
import board
import digitalio

led = digitalio.DigitalInOut(board.GP17)
led.direction = digitalio.Direction.OUTPUT

while True:
    led.value = True
    time.sleep(0.5)
    led.value = False
    time.sleep(0.5)
```

## Digital Input

Let's read a button!

The cool thing here is that the Pico has internal pull up resistors. Therefore we don't need to add any additional external resistors, which you might see in some other wiring diagrams. The equivalent resistor is inside the Pico!

Here's the breadboard layout.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/862/medium800/sensors_gpio_button_bb.jpg?1619553441)

Here's the code to run. It will continuously print the button state.

- `True` = **not** pressed
- `False` = pressed

```python
import board
import digitalio

button = digitalio.DigitalInOut(board.GP16)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

while True:
    print(button.value)
```

## Digital Input and Output

Ok, let's put those two together and make the button turn on the LED. So we'll use two digital pins - one will be an input (button) and one will be an output (LED).

Here's the bread board layout.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/863/medium800/sensors_gpio_combo_bb.jpg?1619553982)

And here's the code. Note how the code uses `not` to invert the button logic.

```python
import board
import digitalio

led = digitalio.DigitalInOut(board.GP17)
led.direction = digitalio.Direction.OUTPUT

button = digitalio.DigitalInOut(board.GP16)
button.direction = digitalio.Direction.INPUT
button.pull = digitalio.Pull.UP

while True:
    led.value = not button.value
```

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## ADC

Let's read an analog signal!

For this, we'll use a small [10k trim pot](https://www.adafruit.com/product/356) to set up a voltage divider. Here's the wiring diagram:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/864/medium800/sensors_adc_knob_bb.jpg?1619554747)

And here's the code:

```python
import time
import board
import analogio

knob = analogio.AnalogIn(board.ADC0)

def get_voltage(raw):
    return (raw * 3.3) / 65536

while True:
    raw = knob.value
    volts = get_voltage(raw)
    print("raw = {:5d} volts = {:5.2f}".format(raw, volts))
    time.sleep(0.5)
```

Spin the knob and the values should change.

Note that even though the Pico's ADC is 12 bits, the value is scaled to 16 bits to comply with the CircuitPython API.

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## PWM

Let's dim an LED!

To do this we will use Pulse Width Modulation (PWM) output. The `duty_cycle` of the PWM output will control the LED brightness. We'll combine this with the previous ADC example so we can use the knob to control the LED brightness. Here's the breadboard layout:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/865/medium800/sensors_adc_pwm_bb.jpg?1619555608)

And here's the code to run:

```python
import board
import pwmio
import analogio

knob = analogio.AnalogIn(board.ADC0)

led = pwmio.PWMOut(board.GP15, frequency=1000)

while True:
    led.duty_cycle = knob.value
```

Turn the knob and the LED should get dimmer and brighter.

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## I2C

Let's talk to an I2C sensor!

The Pico has two I2C ports. Remember that you can attach multiple sensors to a single port as long as each has a unique I2C address. So you don't need to use two just because you have two sensors.

We'll use the [MSA301 sensor](https://www.adafruit.com/product/4344) which can read acceleration. Here we show wiring via the header pins. But if you wanted to use the STEMMA QT connector, you could by using one of the pigtail breakout cables.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/867/medium800/sensors_i2c_msa301_bb.jpg?1619556816)

Danger: 

Info: 

## Install MSA301 Library

To install the MSA301 library, run the following:

```python
sudo pip3 install adafruit-circuitpython-msa301
```

Note that this step is the same as shown in the [main MSA301 guide](https://learn.adafruit.com/msa301-triple-axis-accelerometer/python-circuitpython#circuitpython-installation-of-msa301-library-6-7). You would do the same general process for any other sensor with a CircuitPython library.

## Example Code

And then we can run the example from the library. Download it from here:

[MSA301 Simple Test Example](https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_MSA301/master/examples/msa301_simpletest.py)
save it as **msa301\_simpletest.py** and run it with:

```python
python3 msa301_simpletest.py
```

Pick up the board and spin it around. You should see the values change:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/868/medium800/sensors_msa301_test.png?1619557005)

## Live Plot Example

This one is a little fancier and requires [matplotlib](https://matplotlib.org/) to be installed on the host PC as well. This is the example shown running in the guide thumbnail image.

Here's the code:

```python
import board
import busio
import adafruit_msa301

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from collections import deque
import time

i2c = busio.I2C(board.SCL1, board.SDA1)

msa = adafruit_msa301.MSA301(i2c)

REFRESH_RATE = 50
HIST_SIZE = 61

x_time = [x * REFRESH_RATE for x in range(HIST_SIZE)]
x_time.reverse()

y_data = [deque([None] * HIST_SIZE, maxlen=HIST_SIZE) for _ in range(3)]

fig, ax = plt.subplots(1, 1)
fig.canvas.manager.set_window_title("MSA301 Acceleration")
fig.set_figwidth(9)
fig.set_figheight(3)

ax.grid(True, linestyle=':')
ax.set_facecolor('#303030')
ax.set_xlim(min(x_time), max(x_time))
ax.set_ylim(-15, 15)
ax.invert_xaxis()

lines = []
for data in y_data:
    line, = ax.plot(x_time, data)
    lines.append(line)

lines[0].set_color('#d1ff7a'); lines[0].set_linewidth(3)
lines[1].set_color('#7af6ff'); lines[1].set_linewidth(3)
lines[2].set_color('#ff36fc'); lines[2].set_linewidth(3)

def animate(foo):
    for i, a in enumerate(msa.acceleration):
        y_data[i].append(a)
        lines[i].set_ydata(y_data[i])
    fig.canvas.draw()

ani = animation.FuncAnimation(fig, animate, interval=REFRESH_RATE)
plt.show()
```

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## SPI

Let's talk to a SPI sensor.

The Pico has two SPI ports. Remember that you can attach multiple sensors to a single port as long as each has a separate chip select (CS) pin.

Here we use a BME280 sensor on the **secondary** SPI port.

![](https://cdn-learn.adafruit.com/assets/assets/000/101/983/medium800/sensors_spi_bme280_bb.png?1620055232)

Info: 

## Install the BME280 Library

To install the BME280 library, run the following:

```python
sudo pip3 install adafruit-circuitpython-bme280
```

Note that this step is the same as shown in the [main BME280 guide](https://learn.adafruit.com/adafruit-bme280-humidity-barometric-pressure-temperature-sensor-breakout/python-circuitpython-test#python-installation-of-bme280-library-2995297-10). You would do the same thing for any other sensor.

## Run Example

Here's is the example code to run:

```python
import time
import board
import busio
import digitalio
import adafruit_bme280

spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
 
cs = digitalio.DigitalInOut(board.GP13)

bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)

while True:
    print("\nTemperature: %0.1f C" % bme280.temperature)
    print("Humidity: %0.1f %%" % bme280.relative_humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)
    time.sleep(2)
```

Save this as something like **bme280\_test.py** and run it with:

```python
python3 bme280_test.py
```

and you should see it print out sensor readings over and over:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/984/medium800/sensors_Screenshot_from_2021-05-03_08-17-25.png?1620055376)

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## NeoPixel

Let's light up some NeoPixels!

We could use the SPI port to do this, using the hack provided by the [neopixel\_spi library](https://learn.adafruit.com/circuitpython-neopixels-using-spi/overview). But the u2if firmware supports the real deal. No need for the hack. Just wire NeoPixels to any available GP pin and use the normal neopixel library.

Here's an example wiring:

![](https://cdn-learn.adafruit.com/assets/assets/000/101/881/medium800/sensors_neopixel_ring_bb.jpg?1619566113)

This example uses a [12 ring RGB NeoPixel](https://www.adafruit.com/product/1643). For any other setup, just change the number of pixels and possibly the pixel order.

Info: 

Info: 

## Install NeoPixel Library

To install the NeoPixel library, run the following:

```python
sudo pip3 install adafruit-circuitpython-neopixel
```

These are the same install instructions as found in the [main NeoPixel guide](https://learn.adafruit.com/adafruit-neopixel-uberguide/python-circuitpython#python-installation-of-neopixel-library-3005638-10).

## Run Example

And here is the example code to drive the 12 NeoPixel ring. To keep things simple, we simply fill the ring with various colors.

```python
import time
import board
import neopixel

COLORS = (
    (255,   0,   0),
    (  0, 255,   0),
    (  0,   0, 255),
    (255, 255,   0),
    (255,   0, 255),
    (  0, 255, 255),
)

pixels = neopixel.NeoPixel(board.GP28, 12)

while True:
    for color in COLORS:
        pixels.fill(color)
        time.sleep(1)
```

Save that as something like **neopixel\_ring.py** and then run with the following:

```python
python3 neopixel_ring.py
```

And the ring should light up!

# CircuitPython Libraries on any Computer with Raspberry Pi Pico

## Other RP2040 Boards

Since the u2if firmware uses standard HID and CDC interfaces for communicating with the host PC, it can potentially run on any Raspberry Pi RP2040 based board, not just the Pico. The main code changes needed are:

- Provide appropriate USB PID and VID.
- Change pin mappings to specific RP2040 based board.

We've done that for several Adafruit RP2040 based boards. Details for each are provided below. For each, install the provided UF2 firmware, set the environment variable:

```terminal
BLINKA_U2IF=1
```

and then launch Python. The board will be auto detected based on USB PID and VID.

Info: 

## Feather RP2040
### Adafruit Feather RP2040

[Adafruit Feather RP2040](https://www.adafruit.com/product/4884)
A new chip means a new Feather, and the Raspberry Pi RP2040 is no exception. When we saw this chip we thought "this chip is going to be awesome when we give it the Feather Treatment" and so we did! This Feather features the&nbsp; **RP2040** , and all niceties you know and...

In Stock
[Buy Now](https://www.adafruit.com/product/4884)
[Related Guides to the Product](https://learn.adafruit.com/products/4884/guides)
![Angled shot of black rectangular microcontroller "Feather RP2040"](https://cdn-shop.adafruit.com/640x480/4884-04.jpg)

Use the firmware file **u2if\_feather.uf2** from the [latest release](https://github.com/adafruit/u2if/releases).

Pico Firmware USB IDs:

- `USB_VID = 0x239A`
- `USB_PID = 0x00F1`

Example check-if-found test code:

```python
import hid
device = hid.device()
device.open(0x239A, 0x00F1)
```

Here is what you should see if you list the `board` pins:

```python
$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import board
&gt;&gt;&gt; dir(board)
['A0', 'A1', 'A2', 'D0', 'D1', 'D10', 'D11', 'D12', 'D13', 'D24', 'D25', 'D4', 'D5', 'D6', 'D9', 'I2C', 'MISO', 'MOSI', 'SCK', 'SCL', 'SCLK', 'SDA', 'SPI', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']
&gt;&gt;&gt;
```

Here is an example that scans for connected I2C devices. Make sure something is actually connected to the **SCL/SDA** pins or the **STEMMA QT** connector.

```python
import board

i2c = board.I2C()

i2c.try_lock()
i2c.scan()
i2c.unlock()
```

## ItsyBitsy RP2040
### Adafruit ItsyBitsy RP2040

[Adafruit ItsyBitsy RP2040](https://www.adafruit.com/product/4888)
A new chip means a new ItsyBitsy, and the Raspberry Pi RP2040 is no exception. When we saw this chip we thought "this chip is going to be awesome when we give it the ItsyBitsy teensy-weensy Treatment" and so we did! This Itsy' features the RP2040, <a...></a...>

In Stock
[Buy Now](https://www.adafruit.com/product/4888)
[Related Guides to the Product](https://learn.adafruit.com/products/4888/guides)
![Video of hand holding an ItsyBitsy PCB. An on-board LED glows rainbow colors.](https://cdn-shop.adafruit.com/product-videos/640x480/4888-05.jpg)

Use the firmware file **u2if\_itsybitsy.uf2** from the [latest release](https://github.com/adafruit/u2if/releases).

Pico Firmware USB IDs:

- `USB_VID = 0x239A`
- `USB_PID = 0x00FD`

Example check-if-found test code:

```python
import hid
device = hid.device()
device.open(0x239A, 0x00FD)
```

Here is what you should see if you list the `board` pins:

```python
$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import board          
&gt;&gt;&gt; dir(board)
['A0', 'A1', 'A2', 'BUTTON', 'D0', 'D1', 'D10', 'D11', 'D12', 'D13', 'D2', 'D24', 'D25', 'D3', 'D4', 'D5', 'D7', 'D9', 'I2C', 'MISO', 'MOSI', 'NEOPIXEL', 'NEOPIXEL_POWER', 'SCK', 'SCL', 'SCLK', 'SDA', 'SPI', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']
&gt;&gt;&gt;
```

Here is a simple example program that reads the state of the **BOOT** button.

```python
import time
import board
import digitalio

button = digitalio.DigitalInOut(board.BUTTON)
button.direction = digitalio.Direction.INPUT

while True:
    # value is False when button is pressed
    if not button.value:
        print("Button pressed!")
        time.sleep(0.1)
```

## QT Py RP2040
### Adafruit QT Py RP2040

[Adafruit QT Py RP2040](https://www.adafruit.com/product/4900)
What a cutie pie! Or is it... a QT Py?&nbsp;This diminutive dev board comes with one of our new favorite chip, the RP2040. It's been made famous in the new [Raspberry Pi Pico](https://www.adafruit.com/pico) _and_ our [Feather...](http://www.adafruit.com/product/4884)

Out of Stock
[Buy Now](https://www.adafruit.com/product/4900)
[Related Guides to the Product](https://learn.adafruit.com/products/4900/guides)
![Video of hand holding a QT Py PCB in their hand. An LED glows rainbow colors.](https://cdn-shop.adafruit.com/product-videos/640x480/4900-06.jpg)

Use the firmware file **u2if\_qtpy.uf2** from the [latest release](https://github.com/adafruit/u2if/releases).

Pico Firmware USB IDs:

- `USB_VID = 0x239A`
- `USB_PID = 0x00F7`

Example check-if-found test code:

```python
import hid
device = hid.device()
device.open(0x239A, 0x00F7)
```

Here is what you should see if you list the `board` pins:

```python
$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import board
&gt;&gt;&gt; dir(board)
['A1', 'A2', 'A3', 'BUTTON', 'D0', 'D1', 'D10', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'I2C', 'MISO', 'MOSI', 'NEOPIXEL', 'NEOPIXEL_POWER', 'SCK', 'SCL', 'SCL1', 'SCLK', 'SDA', 'SDA1', 'SPI', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']
&gt;&gt;&gt;
```

And here is a simple example to light the onboard NeoPixel:

```python
import board
import digitalio
import neopixel

pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)

neopwr = digitalio.DigitalInOut(board.NEOPIXEL_POWER)
neopwr.direction = digitalio.Direction.OUTPUT
neopwr.value = True

pixel.fill(0xADAF00)
```

## Trinkey QT2040
### Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT

[Adafruit Trinkey QT2040 - RP2040 USB Key with Stemma QT](https://www.adafruit.com/product/5056)
It's half USB Key, half Adafruit QT Py, and a lotta&nbsp;RP2040_..._it's&nbsp; **Trinkey QT2040** , the circuit board with an RP2040 heart and Stemma QT legs. Folks are loving the [QT Py 2040](https://www.adafruit.com/product/4900) we made, but maybe you...

In Stock
[Buy Now](https://www.adafruit.com/product/5056)
[Related Guides to the Product](https://learn.adafruit.com/products/5056/guides)
![Video of Trinkey RP2040 plugged into a laptop. An OLED display is connected and shows a graphic keyboard cat animation.](https://cdn-shop.adafruit.com/product-videos/640x480/5056-04.jpg)

Use the firmware file **u2if\_trinkey.uf2** from the [latest release](https://github.com/adafruit/u2if/releases).

Pico Firmware USB IDs:

- `USB_VID = 0x239A`
- `USB_PID = 0x0109`

Example check-if-found test code:

```python
import hid
device = hid.device()
device.open(0x239A, 0x0109)
```

Here is what you should see if you list the `board` pins:

```auto
$ python3
Python 3.8.5 (default, Jan 27 2021, 15:41:15) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import board
&gt;&gt;&gt; dir(board)
['BUTTON', 'I2C', 'NEOPIXEL', 'SCL', 'SDA', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'ap_board', 'board_id', 'detector', 'pin', 'sys']
&gt;&gt;&gt;
```

Here is a simple example program that reads the state of the **BOOT** button.

```auto
import time
import board
import digitalio

button = digitalio.DigitalInOut(board.BUTTON)
button.direction = digitalio.Direction.INPUT

while True:
    # value is False when button is pressed
    if not button.value:
        print("Button pressed!")
        time.sleep(0.1)
```

Info: 


## Featured Products

### Raspberry Pi Pico RP2040 with Loose Unsoldered Headers

[Raspberry Pi Pico RP2040 with Loose Unsoldered Headers](https://www.adafruit.com/product/4883)
The Raspberry Pi foundation changed single-board computing&nbsp;[when they released the Raspberry Pi computer](https://www.raspberrypi.org/archives/723), now they're ready to do the same for microcontrollers with the release of the brand new&nbsp; **Raspberry Pi...**

In Stock
[Buy Now](https://www.adafruit.com/product/4883)
[Related Guides to the Product](https://learn.adafruit.com/products/4883/guides)
### Raspberry Pi Pico RP2040

[Raspberry Pi Pico RP2040](https://www.adafruit.com/product/4864)
The Raspberry Pi foundation changed single-board computing [when they released the Raspberry Pi computer](https://www.raspberrypi.org/archives/723), now they're ready to do the same for microcontrollers with the release of the brand new **Raspberry Pi Pico**. This...

In Stock
[Buy Now](https://www.adafruit.com/product/4864)
[Related Guides to the Product](https://learn.adafruit.com/products/4864/guides)
### Adafruit MSA301 Triple Axis Accelerometer - STEMMA QT / Qwiic

[Adafruit MSA301 Triple Axis Accelerometer - STEMMA QT / Qwiic](https://www.adafruit.com/product/4344)
[Discontinued - **you can grab** Adafruit MSA311 Triple Axis Accelerometer - STEMMA QT / Qwiic **instead!&nbsp;**](https://www.adafruit.com/product/5309)

The MSA301 is a super small and low-cost **triple-axis accelerometer**. It's inexpensive, but has...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/4344)
[Related Guides to the Product](https://learn.adafruit.com/products/4344/guides)
### Adafruit BMP388 - Precision Barometric Pressure and Altimeter

[Adafruit BMP388 - Precision Barometric Pressure and Altimeter](https://www.adafruit.com/product/3966)
Bosch has been a leader in barometric pressure sensors, from the [BMP085](https://www.adafruit.com/product/391). [BMP180](https://www.adafruit.com/product/1603), and [BMP280](https://www.adafruit.com/product/2651)... now we've got the next generation, the...

In Stock
[Buy Now](https://www.adafruit.com/product/3966)
[Related Guides to the Product](https://learn.adafruit.com/products/3966/guides)

## Related Guides

- [Adafruit BMP388 and BMP390 - Precision Barometric Pressure and Altimeter](https://learn.adafruit.com/adafruit-bmp388-bmp390-bmp3xx.md)
- [Adafruit Wii Nunchuck Breakout Adapter](https://learn.adafruit.com/adafruit-wii-nunchuck-breakout-adapter.md)
- [MacroPad Remote Procedure Calls over USB to Control Home Assistant](https://learn.adafruit.com/macropad-remote-procedure-calls-over-usb-to-control-home-assistant.md)
- [NeoTrellis MIDI Feedback Controller](https://learn.adafruit.com/neotrellis-midi-feedback-controller.md)
- [Adafruit PyPortal Titano](https://learn.adafruit.com/adafruit-pyportal-titano.md)
- [¡Bienvenido a CircuitPython!](https://learn.adafruit.com/bienvenido-a-circuitpython-2.md)
- [Adafruit BNO055 Absolute Orientation Sensor](https://learn.adafruit.com/adafruit-bno055-absolute-orientation-sensor.md)
- [CircuitPython Libraries on Linux and Raspberry Pi](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux.md)
- [Generating Text with ChatGPT, Pico W & CircuitPython](https://learn.adafruit.com/generating-text-with-chatgpt-pico-w-circuitpython.md)
- [Clue And MagTag Pep Talk Generator](https://learn.adafruit.com/clue-and-magtag-pep-talk-generator.md)
- [Build a Cloud-Connected ESP8266 Power Meter](https://learn.adafruit.com/build-a-cloud-connected-esp8266-power-meter.md)
- [Adafruit DVI Sock for Pico](https://learn.adafruit.com/adafruit-dvi-sock-for-pico.md)
- [Adafruit TMC2209 Stepper Motor Driver Breakout Board](https://learn.adafruit.com/adafruit-tmc2209-stepper-motor-driver-breakout-board.md)
- [Create a Memory Game on Fruit Jam Metro RP2350](https://learn.adafruit.com/create-a-memory-game-on-metro-rp2350.md)
- [A Sillier Mousetrap: Logging Mouse Data to Adafruit IO with the Raspberry Pi](https://learn.adafruit.com/a-sillier-mousetrap-logging-mouse-data-to-adafruit-io-with-the-raspberry-pi.md)
