You can use a Raspberry Pi Pico as a powerful peripheral to a host - a Raspberry Pi, a Jetson Nano, a laptop or workstation. In this article you'll see how to interact with a Pico running MicroPython or CircuitPython by writing some Python code that runs on the host.
The software is easy to use. It enables you to send a Python statement to the Pico and read the results. The statement can be any valid MicroPython code.Setting up the host and the Pico
For this article I've used a Raspberry Pi as the host, but any computer running Windows, Linux or Mac OS will do so long as it has Python 3.5 or later installed.
You can use this technique to connect a Raspberry Pi Pico to a Jetson Nano or any other member of the Jetson family.
You'll need to install MicroPython on the Pico. You'll find instructions for MicroPython installation in the official Getting Started Guide which I reviewed recently.
The £10 Guide is worth buying, but if you can't wait for your copy to arrive you can download a free pdf.
The guide will also tell you how to use the Thonny editor to install the necessary code on your Pico.
Connect the host and the Pico
First, connect the host to the Pico using a USB data lead. Some USB leads only supply power. They will not work.
Install software on the Pico
Next, install the blinker
script.
The blinker.py
script runs on the Pico, but you should rename it to main.py. Here's the code:
from machine import Pin
#use onboard LED which is controlled by Pin 25
led = Pin(25, Pin.OUT)
# Turn the LED on
def on():
led.value(1)
# Turn the LED off
def off():
led.value(0)
- Install it on the Pico using the
Thonny
editor.- Open the gist on GitHub.
- Copy the code to your clipboard.
- Open
Thonny
and make sure it has connected to the Pico. - Paste the code into the
Thonny
editor window. - Save it on the Pico as
main.py
. - Close the Thonny editor.
If you leave the Thonny editor open it will keep the serial port open on the host, and the serial program below will not work!
Since you saved the program as main.py
it will run on the Pico automatically.
The sender.py
script runs on the host. It uses PySerial to send commands from the host to the Raspberry Pi Pico and read the result.
Here's the sender code:
import serial
class Sender:
TERMINATOR = '\r'.encode('
UTF8')
def __init__(self, device='/dev/ttyACM0', baud=9600, timeout=1):
self.serial = serial.Serial(device, baud, timeout=timeout)
def receive(self) -> str:
line = self.serial.read_until(self.TERMINATOR)
return line.decode('UTF8').strip()
def send(self, text: str) -> bool:
line = '%s\r\f' % text
self.serial.write(line.encode('
UTF8'))
# the line should be echoed.
# If it isn't, something is wrong.
return text == self.receive()
def close(self):
self.serial.close()
On the host
- Run
pip3 install pyserial
. - Copy
sender.py
from this github gist into an editor and save it in a directory of your choice. - In that directory, run
python3
to start an interactive session. - Type
from sended import Sender
- Type
s = Sender()
. If you are running on Windows, you will need to types = Sender('COM6')
replacingCOM6
by whatever port the Pico is visible on.
- Type
s.send('2 + 2')
- Type
s.receive()
. If all is well, this will print the result 4. - Type
s.send('on()'
. The on-board LED on the Pico should turn on. - Type
s.send('off()'
. The on-board LED on the Pico should turn off. - When you have finished, type
s.close()
.
Of course in a real application you'd normally create the sender, send and receive data using a Python script. In a future post I'll show how to do that to create a simple weather station with the Pico and plot the temperature and light level on the host. Before that I'll explore other ways of connecting and controlling the Pico from a Host.
This isn't working for me. First, I had to modify the line that sends the text data:
ReplyDeleteline = r'%s\r\f' % text
to treat it as a raw string, otherwise I'd get '♀n()' being sent instead of 'on()'.
However, I'm still not seeing the LED turn on or off and receiving 'False' after the timeout.
Does the pico code (main.py) need to be in a loop?
You shouldn't use a raw string. What the program does is to send your string, followed by a carriage return '\r' and a tab '\t'.
DeleteThe pico code does not need to be put in a loop. You're sending Python commands from the Pi or Nano to the Pico; the Python REPL will execute them as they are received.
What if I didn't wanted the blinker script to run constantly as mail.py, but rather, have the user decides when to toggle the LED by sending a command to the PICO over the serial connection?
ReplyDeleteThen just use a terminal program like screen on the Pi, and type on() to turn the LED on and off() to turn it off. You don't need the sender program at all.
Delete