
CircuitPython Libraries on any Computer with MCP2221
Created by Carter Nelson
Last updated on 2021-04-26 06:05:02 PM EDT

2
4
4
4
6
6
7
7
7
8
8
8
10
10
10
10
11
11
13
13
13
14
14
14
15
15
16
16
16
17
18
18
18
18
18
19
19
19
20
20
21
21
22
23
25
27
27
29
Guide Contents
Guide Contents
Overview
CircuitPython and CircuitPython Libraries
CircuitPython Libraries on Personal Computers
Setup
Additional Information
Windows
Have Python 3 Installed
Install hidapi
Install Blinka
Set Environment Variable
Check Platform was detected
Mac OSX
Python 3 Check
Install hidapi
Install Blinka
Set Environment Variable
Check that Platform was detected
Linux
Install libusb and libudev
Setup udev rules
Install hidapi
Remove Native MCP2221 Driver
Install Blinka
Set environment variable
Run the sanity check.
Post Install Checks
Check that hidapi is installed correctly
Check that MCP2221 can be found
Check environment variable within Python
Pinout
Power Pins
GPIO Pins
I2C Pins
UART Pins
ADC Pins
DAC Pins
Logic Level
Examples
Installing Libraries for Breakouts
GPIO
Digital Output
Digital Input
Digtal Input and Output
I2C
Install MSA301 Library
Example Code
ADC
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 2 of 43

31
32
32
32
32
33
35
35
35
35
35
36
36
37
37
37
37
42
42
DAC
UART
Install pySerial
Find COM port in Linux
Find COM port in Windows
UART Example with GPS
FAQ & Troubleshooting
Update Blinka/Platform Libraries
Getting an error message about "board" not found or "board" has no attribute
Mixed SPI mode devices
Why am I getting AttributeError: 'SpiDev' object has no attribute 'writebytes2'?
No Pullup/Pulldown support on some linux boards or MCP2221
Getting OSError: read error with MCP2221
Using FT232H with other FTDI devices.
I can't get neopixel, analogio, audioio, rotaryio, displayio or pulseio to work!
Help, I'm getting the message "error while loading shared libraries: libgpiod.so.2: cannot open shared object file:
No such file or directory"
When running the libgpiod script, I see the message: configure: error: "libgpiod needs linux headers version
>= v5.5.0"
Downloads
Schematic and Fab Print
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 3 of 43

Overview
This guide will show you how to use an MCP2221(A) to connect to I2C sensors and breakouts from your
desktop PC running Windows, Mac OSX, or Linux. The MCP2221 also allows for general purpose digital
input and output (GPIO) for things like buttons and LEDs, analog to digital conversion (ADC), and digital to
analog (DAC).
The cool part about this is that you can then use any of the CircuitPython Libraries that have been written
for the numerous I2C sensors and breakouts. You can bring that data directly into your PC for any kind of
powerful analysis or presentation.
Our breakout uses the MCP2221A chip, but we may refer to it as MCP2221 as the difference are not
relevant to using Blinka/CircuitPython libraries and use can use either version of the chip!
CircuitPython and CircuitPython Libraries
As you are going through this guide, keep in mind the difference between CircuitPython and CircuitPython
Libraries:
CircuitPython - a microprocessor specific implementation of Python written in C. Here's the source
code (https://adafru.it/tB7). And here's the main CircuitPython guide (https://adafru.it/cpy-welcome).
CircuitPython Libraries - sensor and breakout specific code written in Python using the CircuitPython
hardware API. There are a lot of these - check out the bundle (https://adafru.it/zdx).
There are various hardware combinations that allow for running CircuitPython and CircuitPython Libraries.
In this guide we will not be using the actual CircuitPython firmware. But we will be using CircuitPython
Libraries.
CircuitPython Libraries on Personal Computers
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 4 of 43

This is essentially the same idea as discussed in the FT232H Guide (https://adafru.it/GNe). How can we
directly connect common hardware items like buttons and I2C breakouts to a PC?
The MCP2221 provides another way to do this by utlizing the USB bus. The MCP2221 just makes different
trade offs relative to the FT232H. The biggest being no hardware SPI support . But you do gain ADC and
DAC support. Also, it's much cheaper than the FT232H.
So you end up with something like this:
Great! Let's get everything setup so we can actually do some fun stuff.
Your browser does not support the video tag.
Adafruit MCP2221A Breakout - General Purpose USB to GPIO ADC I2C
Wouldn't it be cool to drive a tiny OLED display, read a
Out of Stock
Out of
Stock
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 5 of 43

Setup
The support for the MCP2221 in Blinka utilizes the hidapi library (https://adafru.it/HEI). This in turn relies on
a few other things which vary for different OS's. So before we can actually use the MCP2221, 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's the README from the hidapi source code repo, which has some install
information:
https://adafru.it/HEJ
But first try the install instructions on the pages that follow for your OS.
https://adafru.it/HEJ
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 6 of 43

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
Install hidapi
From the command line, manually install hidapi with
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://adafru.it/JBp)
then you will need to also install the Microsoft Visual C++ Build Tools. Thanks to @jklem for pointing this
out in the forums (https://adafru.it/doW).
Download it from here (same link as in text):
https://adafru.it/JBp
NOTE: You do not need the full Visual Studio IDE. Just the Build Tools.
https://adafru.it/JBp
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 7 of 43

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.
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:
pip3 install adafruit-blinka
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_MCP2221=1
If you are using Windows Powershell, the syntax is a little different. In that case do:
$env:BLINKA_MCP2221=1
Check Platform was detected
In the same command window you set BLINKA_MCP2221=1 env var, run python and run
import board
dir(board)
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 8 of 43

at the Python REPL. If you get no errors, and you see a list of all the pins available - you're good to go!
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 9 of 43

Mac OSX
Python 3 Check
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 python3 --version
Install hidapi
From the command line, manually install hidapi (https://adafru.it/HIA) with:
Install Blinka
To install Blinka (https://adafru.it/BJX) and its dependencies, run:
pip3 install adafruit-blinka
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 10 of 43

Set Environment Variable
You'll need to set this variable every time before running CircuitPython code. To do this, we set the
environment variable to BLINKA_MCP2221.,
You can set the variable by running:
export BLINKA_MCP2221="1"
Then, verify that the variable is set by running:
Don't forget this step. Things won't work unless BLINKA_MCP2221 is set.
Check that Platform was detected
In the same terminal window you ran export BLINKA_MCP2221="1" , run python3 .
At the REPL, run:
import board
dir(board)
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 11 of 43

If you get no errors and see a list of all the pins available, you're good to go!
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 12 of 43

Linux
The following shows a typical run through installing and setting things up on Linux.
Install libusb and libudev
Run the following:
sudo apt-get install libusb-1.0 libudev-dev
and answer Y to the prompt. This should install libusb and libudev.
Setup udev rules
Use a text editor to create and edit the file /etc/udev/rules.d/99-mcp2221.rules and add the following
contents.
SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTR{idProduct}=="00dd", MODE="0666"
Here we use nano, so run:
like this:
and add the contents from above:
and then press CTRL-X and Y to save and exit.
© Adafruit Industries https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-mcp2221 Page 13 of 43