Add sensors and output to your PC using the Pico 2040 with custom firmware Desktop Python and Blinka

Extend your PC into the microcontroller sensor and control realm with Desktop Python Adafruit Blinka and an RP2040 Pico running U2IF firmware. This setup gives you access to the microcontroller component libraries without writing code on the microcontroller itself. No code-download-run cycle is required!

Normally, when you want to run some motors, read some sensors or talk to some small displays, attach those elements to a microcontroller running your custom software. Adafruit has made it so that their libraries work with a couple non programmable port expanders.  You run standard Python on your PC using their libraries.  The Adafruit Blinka software has embedded drivers for boards that present the correct USB HID interfaces.  Two examples are the MCP2221A and FTDI breakout expander boards. There is also some open-source U2IF Pico 2040 firmware that turns an RP2040 into an appliance that can be talked to as a HID device.  You can use the same Adafruit Blinka software to talk with these RP2040 effectively giving you a whole slew of ports and pins that can be directly controlled from your PC without writing microcontroller code.

Video

Writing data to an attached LCD display from inside the Python REPL


Controlling Servos from a Jupyter Notebook

Steps

There are several sites that provide good instructions so I'll just provide the basics.

Device Prep

  1. Buy a supported Pico board.  Adafruit has some variants. I used the original Raspberry Pi Pico in the video.
  2. Find and download a Raspberry Pi Pico that is supported by the firmware, UF2 file.  There are a couple different variants of the Pico boards.  most will work but the custom firmware versions may bake in special component support of some of the boards. 
    1. The original firmware is at https://github.com/execuc/u2if 
    2. Adafruit firmware fork https://github.com/adafruit/u2if
    3. Adafruit learning describes the process https://learn.adafruit.com/circuitpython-libraries-on-any-computer-with-raspberry-pi-pico/setup
  3. Hold down the reset button on the Pico board while plugging it into your PC/Mac.  You should see a mounted device labeled RPI-RP2
  4. Drag the firmware onto the RPI-RP2 device.
  5. The device file system. should disappear and new HID devices should appear.

Python Prep

  1. Install Python 3 if you don't already have it.
  2. Set The BLINKA_U2IF environment variable  to 1
    1. #   Linux set BLINKA_U2IF=1
      #   Powershell $env:BLINKA_U2IF=1
  3. Use pip3 to install the Adafruit libraries on your PC using PIP.  You need the HID and the BLINKA libraries plus any device-specific libraries such as your gyroscope, LCD, or servo libraries.
  4. Open a REPL window where we know the BLINKA_U2IF variable is set and verify the board with the following. You should see a list of board capabilities
    1. import os

      # verify "BLINKA_U2IF" set. Should = 1
      try:
          os.environ["BLINKA_U2IF"]
          print("BLINKA_U2IF set correctly.  Well Done!")
      except KeyError:
          print("**** ABORT! BLINKA_U2IF not set")
          exit
      except ValueError:
          print("**** ABORT! BLINKA_U2IF not set")
          exit

      import board
      dir(board)

Sample Program 

This source code comes from my CircuitPython playground repository which is very much a work in progress. 

My sample program assumes you have an I2C 16x2 LCD attached to the default I2C bus, Pins 5 and 6 on the full-sized Pico. 
Your pins may be different if you are using a different device. This just displays a few messages and changes the backlight intensity or color.

import time
import adafruit_character_lcd.character_lcd_rgb_i2c as character_lcd
import board
dir(board)

start_connect = time.perf_counter()
lcd_columns = 16
lcd_rows = 2
i2c = board.I2C()

start_connect = time.perf_counter()
lcd = character_lcd.Character_LCD_RGB_I2C(i2c, lcd_columns, lcd_rows)
end_connect = time.perf_counter()
print("conect time: " + str(end_connect - start_connect))
# I have the RGB backlit Adafruit device

lcd.clear()
lcd.color = [100, 0, 0]

# Show text
start_hello = time.perf_counter()
lcd.message = "Hello\nCircuitPython"
end_hello = time.perf_counter()
print("draw hello time: " + str(end_hello - start_hello))

# blink
lcd.blink = True
lcd.clear()
lcd.blink = False

# final message
lcd.message = "Goodbye"
lcd.cursor_position(8, 1)
lcd.message = "over here"
lcd.home()
lcd.clear()
lcd.color = [0, 0, 0]


Related Posts

https://joe.blog.freemansoft.com/2023/01/using-circuitpython-and-adafruit.html

Revision History

Created 2025 01

Comments

Popular posts from this blog

Installing the RNDIS driver on Windows 11 to use USB Raspberry Pi as network attached

Understanding your WSL2 RAM and swap - Changing the default 50%-25%

Almost PaaS Document Parsing with Tika and AWS Elastic Beanstalk