Raspberry Pi Pico USB Serial Communication with MicroPython (Beginner's Guide)

Raspberry Pi Pico USB Serial Communication with MicroPython (Beginner's Guide)

Communication is one of the most important skills for any robot. Whether you're sending commands, reading sensor data, or debugging your code, your robot needs a reliable way to talk to your computer.

One of the most useful features of the Raspberry Pi Pico is its built-in USB serial communication. Using MicroPython, you can easily send data between your PC (or Mac) and your Pico over a standard USB cable. This makes it simple to control hardware, monitor what's happening inside your programs, and eventually communicate with more advanced robots and devices.

  • In this beginner-friendly tutorial, you'll learn how to:
  • Communicate with a Raspberry Pi Pico over USB serial
  • Send data from your computer to your Pico
  • Process that data using MicroPython
  • Send results back to your computer
  • Control the Pico's built-in LED using serial commands
  • Lay the foundations for controlling a robot from your PC

What You'll Need

The hardware requirements are refreshingly simple:

  • Raspberry Pi Pico
  • USB cable
  • PC or Mac

On the software side we'll use Thonny, which is one of the easiest ways to program a Raspberry Pi Pico with MicroPython. If you've already used a Pico before, you're probably ready to skip ahead to the code.

Otherwise, install Thonny, connect your Pico, and we'll get everything set up before writing our first USB serial communication program.

Once your software is installed we’ll start writing our super simple program to show us how communication works over usb or serial as we’ll call it (USB stands for universal serial bus)

import sys, select

#https://docs.python.org/3/library/sys.html
#https://docs.python.org/3/library/select.html

#say something
print("PICO READY to times your whole number by two")
print("what is your number?")

#set up our input method 
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
buf = ""

while True:
    
    if poll.poll(0):
        ch = sys.stdin.read(1)
        if ch:
            if ch == "\n":
                line = buf.strip()
                buf = ""
                if not line:
                    continue

                try:
                    print("You said: ",line," two times that is ",(int(line)*2))  
                except Exception as e:
                    print("ERROR", e, ". You Wrote: ", line, "which means you didn't give me a whole number")
            else:
                buf += ch
time.sleep(0.005)

This program essentially takes values from our stdin (standard input) which is our USB and passes it to the Pico, where we take whole numbers (int or integers), times them by two and send them back via the print command.

Click to save your code as main.py on the Pico and then run your code. In the "Shell" section below you can see the welcome message we put in the code.  


Lets test our code by typing in the “Shell” section. Put in a whole number and it will return double what you told it. Put in anything else and it should give you an error message. In this example you can see I've given the code the number 569 and it's returned double, and when I've given it text it's shared a helpful error message.


Cool eh? Maybe it just seems like you’re typing on the Pico because you’re using the same programmer. 

Let’s try another program to communicate, one that we know won’t send new code. This is a cool little thing called Putty.

Download and install Putty.

 
Connect over serial by selecting your USB or COM port then type into the console. As before integers will come back doubled.

Still not excited? Ok let’s control some hardware that’s exciting right?

Picos have an inbuilt LED. With just a few lines of code we can toggle the LED every time we do the sum. 

You can either look at where the differences are and copy them into your code or cut and paste over the whole thing

import sys, select, time
from machine import Pin

#https://docs.python.org/3/library/sys.html
#https://docs.python.org/3/library/select.html
#https://docs.python.org/3/library/time.html
#https://docs.micropython.org/en/latest/library/machine.html

#say something
print("PICO READY to times your whole number by two")
print("what is your number?")

#set up our input method 
poll = select.poll()
poll.register(sys.stdin, select.POLLIN)
buf = ""

#set up the LED

led = Pin(25, Pin.OUT)

while True:
    
    if poll.poll(0):
        ch = sys.stdin.read(1)
        if ch:
            if ch == "\n":
                line = buf.strip()
                buf = ""
                if not line:
                    continue

                try:
                    print("You said: ",line," two times that is ",(int(line)*2))
                    led.toggle()
                except Exception as e:
                    print("ERROR", e, ". You Wrote: ", line, "which means you didn't give me a whole number")
            else:
                buf += ch
time.sleep(0.005)

Now update your code and every time you do the sums it will toggle your LED Off and On. That's the end of this mini tutorial. 

In this tutorial you've learned the basics of USB serial communication with the Raspberry Pi Pico using MicroPython. You sent data from your computer to the Pico, processed it, and sent the results back over the same USB connection.

You also used that communication to control the Pico's built-in LED, proving that serial data isn't just useful for printing text—it can be used to control real hardware.

Although doubling a number isn't the most exciting application, the same technique is used in real robotics projects. Instead of sending numbers, you could send commands like:

  • FORWARD
  • BACKWARD
  • LEFT
  • RIGHT
  • STOP

Your Pico can receive those commands, interpret them, and control motors, servos, LEDs, sensors, or anything else connected to it.

This simple USB serial connection is the foundation for debugging your programs, building PC control applications, and creating robots that respond to commands in real time.

In the next tutorial we'll build on what you've learned here by using serial communication to control an actual robot.

 

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.