DEV Community

Cover image for Talking to the Pico over USB
djchadderton
djchadderton

Posted on

Talking to the Pico over USB

The Raspberry Pi Pico can be used to create some useful USB peripherals to plug into a regular computer, Pi or otherwise, if you can communicate between the two over USB.

I bought a Pimoroni RGB Keypad when I visited the Raspberry Pi shop in Cambridge to try to do more with a Pico than just activating LEDs and buzzers. But after toying with the examples that flashed pretty colours across the keyboard and mimicked a numeric keypad, I wanted it to do something more useful—and complicated.

What I really wanted was to turn it into a more affordable version of something like an Elgato Stream Deck. While one of the examples had an interesting way to create different mappings of keys that can be switched between by holding down a control key, I really wanted it to be able to switch automatically to a set of predefined keys for whichever app was currently active on my Mac.

The keypad itself can mimic any keypresses on a regular keyboard if the Pico is installed with CircuitPython, so the Pico-to-Mac communication is already set up. For the Mac to communicate with the Pico, I managed to piece the instructions together from various sources. That’s the bit I’ll be bringing together into this article.

To start, you will need a Raspberry Pi Pico with CircuitPython installed plugged into a USB port on the Mac (Linux is similar but the port names will be a little different; Windows is probably similar except the port names will be COM-something, but I don’t have a machine to test this on).

You will need to find out first of all which port the Pico is on. If you have Thonny running and connected to the Pico, the port name will be in the bottom right corner. Mine is /dev/cu.usbmodem1413301. You could also type into a terminal window on the Mac ls -l /dev/cu.* and look for something with a similar name to this.

That is the port that Thonny and the REPL are currently using to communicate with your Pico, but you can also open a second port for data, which is disabled by default. Create a file on the Pico in the root directory called boot.py, or open it if it exists, and add the following code:

import usb_cdc

usb_cdc.enable(data="true")
Enter fullscreen mode Exit fullscreen mode

There is also a console parameter to enable the REPL port, but this is True by default so there is no need to specify it. You will then need to hard reset the Pico to take on that setting—just unplug it and plug it back in again.

To find the name of the new data channel, run the ls -l /dev/cu.* command again in the Mac console and see which new entry appears in the list. For me, it’s /dev/cu.usbmodem1413303.

CircuitPython has a default code file of code.py. Open that and replace anything in it with the following:

import usb_cdc

while True:
    # Check whether any data has been sent
    if usb_cdc.data.in_waiting > 0:
        # read next line of waiting data ending in line ending ('\n')
        data_received = usb_cdc.data.readline().strip().decode("utf-8")
        # Print data to console
        print(f"Message received: {data_received}")
Enter fullscreen mode Exit fullscreen mode

Run this on the Pico and make sure the shell window is open on Thonny, then open a terminal on the Mac. To send a string to the Pico, simply print to that port with echo "Hello pico!" > /dev/cu.modem1413303.

If all has gone well, you should see the message appear in the console in Thonny.

You can send any data you wish from the Mac and program the Pico to react to it however you wish: for example, display something on a screen, control lights, send MIDI data to music equipment, send an e-mail or, as I did, change the mapping of keys on a miniature keypad.

Top comments (0)