Saturday, 30 January 2021

Pi Pico emulating an Etch-a-sketch


One Christmas, long, long ago, someone gave me an Etch-a-sketch.

I was a bit of a klutz then (indeed, I still am). I never produced great works of art, but the Etch-a-sketch was a lot of fun to play with.

Yesterday I wondered how easy it would be to emulate an Etch-a-sketch using the Raspberry Pi Pico.

I’d just finished yesterday’s article about how to link a host to the Pico using PySerial.

Why not

  • get the Pico to print out how far two potentiometer knobs were turned, 
  • read the output on the Pi using PySerial, 
  • and use the Turtle package on the Pi to create the display?

It sounded too simple to work, so I started coding with some trepidation.

The first job was to connect a couple of potentiometers to the analogue inputs of the Pico.

After that I wrote a short MicroPython program which read the analogue values and printed them out.

Here it is:

import machine
import utime

pot_l = machine.ADC(27)
pot_r = machine.ADC(26)

def run():
    while True:
        print(pot_l.read_u16(), pot_r.read_u16())


I installed the program on the Pico and checked that the output changed when I rotated the knobs.

Next I wrote another short Python program on the Raspberry Pi. It uses, described in yesterday’s article.

Here’s the code:

from sender import Sender
import turtle

s = Sender()
scr = turtle.getscreen()
t = turtle.Turtle()

def convert(text):
    return (int(text)-200)//300

while True:
    text = s.receive()
    x,y = [convert(text) for text in  text.split()]

It worked!

I used a Pimoroni Explorer base for the version in the photo at the top, but you can use a Pico with a standard breadboard. Here's the Fritzing diagram.

Here’s some output, confirming that my coding is better than it was when I was young, but my drawing ability isn’t :)

My next task is to adapt the code to create a very simple audio frequency oscilloscope. I’ll write that up next week.

To keep up to date with this series follow @rareblog on twitter.

Friday, 29 January 2021

Controlling a Raspberry Pi Pico remotely using PySerial

Update: I'm using thie code below in another project, and found that I had not correctly fixed the reported bug. The new version passes automated tests, and I am pretty sure it works OK.

I have changed the name of the class to Talker since it can both send and receive information.

Apologies to all concerned for the bug!


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.

In particular, you can use this technique to connect a Raspberry Pi Pico to a Jetson Nano host or any other member of the Jetson family.

The host needs to have the serial package available, so you need to run 

pip3 install PySerial

on the host.

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 script runs on the Pico, but you should rename it to Here's the code:

Example for remote control from host via Serial link.

This is the code to run on the Pico.
It sets up the onboard LED and allows you to turn it on or off.
from machine import Pin

# use onboard LED which is controlled by Pin 25
# on a Pico W the onboad lLED is accessed differently,
# so commeent out the line below
# and uncomment the line below that
led = Pin(25, Pin.OUT) # veresion for Pico
# led = Pin('LED', Pin.OUT) # version for Pico W

# Turn the LED on
def on():

# Turn the LED off
def off():
Install it on the Pico using the Thonny editor.
  1. Open the gist on GitHub.
  2. Copy the code to your clipboard.
  3. Open Thonny and make sure it has connected to the Pico.
  4. Paste the code into the Thonny editor window.
  5. Save it on the Pico as
  6. 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 it will run on the Pico automatically.

The 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 talker code:

import serial

class Talker:
TERMINATOR = '\r'.encode('UTF8')

def __init__(self, timeout=1):
self.serial = serial.Serial('/dev/ttyACM0', 115200, timeout=timeout)

def send(self, text: str):
line = '%s\r\f' % text
reply = self.receive()
reply = reply.replace('>>> ','') # lines after first will be prefixed by a propmt
if reply != text: # the line should be echoed, so the result should match
raise ValueError('expected %s got %s' % (text, reply))

def receive(self) -> str:
line = self.serial.read_until(self.TERMINATOR)
return line.decode('UTF8').strip()

def close(self):

On the host

  1. Copy from this github gist into an editor and save it in a directory of your choice.
  2. In that directory, run python3 to start an interactive session.
  3. Type from talker import Talker
  4. Type t = Talker(). If you are running on Windows, you will need to type t = Talker('COM6') replacing COM6 by whatever port the Pico is visible on.
  5. Type t.send('2 + 2')
  6. Type t.receive(). If all is well, this will print the result 4.
  7. Type t.send('on()')     #  The on-board LED on the Pico should turn on.
  8. Type t.send('off()')    #  The on-board LED on the Pico should turn off.
  9. When you have finished, type t.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.

To keep up to date with this series follow @rareblog on twitter.

Wednesday, 27 January 2021

Which Python should you install on your Raspberry Pi Pico?

 The new Raspberry Pi Pico sold out soon after launch. One of the reasons it's so popular is that you can program it in Python.

There are two versions of Python to consider: MicroPython and CircuitPython. Fortunately they are very easy to install or replace and they are very similar. Let's compare them so you can decide which to use.

The Official guide to the Pico (reviewed here)recommends that you install the official version of MicroPython. MicroPython has been around since 2014, and it's been ported to quite a few boards. It was  created by Damien George, and he is responsible for the port to the Pico.

If you're starting with MicroPython and  want to work your way through the Official Guide, use the official version.

If you've been using MicroPython for a while you've probably heard of CircuitPython. It's based on MicroPython but  adapted by the inventive folk at Adafruit. 

CircuitPython lets you run the same program on any supported board without having to change it. If you use the Adafruit add-ons (or any of the other compatible products) you can use the same code to drive them on any CircuitPython-enabled board. 

Adafruit even have software called Blinka which lets you run the same code on a Raspberry Pi or  Jetson Nano!

CircuitPython makes it easy to move your code from one environment to another, and it supports a huge range of add-on devices from Adafruit and other vendors. At present, though, the Pico version is in beta-test and some features have not been implemented. I'm experimenting with it but I'd advise most Pico users to stick with the official version for now.

In the Pico posts I have planned, most of my code will run on both versions without change. I may occasionally write code that relies on some of the CircuitPython extensions. I'll make it clear when I do!

The next blog post will cover using PySerial to drive a Pico form a Host Computer. The Pico can be running MicroPython or CircuitPython.

To keep up to date with this series follow @rareblog on twitter.

Tuesday, 26 January 2021

Five ways to connect Raspberry Pi and Pico

The Raspberry Pi and Pico are each very capable, but sometimes you'll want to connect them together.

For example, if you're building a robot you may want to use the Pi for computer vision, while relying on the Pico for predictable response times. 

How can you get the Pi and the Pico to talk to each other?

There are several possibilities, each with their own characteristics.

  1. You can connect them with a USB lead and use Thonny to talk to the Pico. That's very easy; it just uses the MicroPython REPL to control the Pico, bit it's very limited. If you've used the REPL to turn the Pico's on-board LED on and off, you've use this option already.
  2. You can connect the Pi to the Pico via USB and use a library called PySerial to send and receive data using a Python program that you write. That's a bit harder but much more flexible.
  3. You can connect the Pi and the Pico using their TTL serial interfaces - possible in theory, though I can't see much reason to do things that way.
  4. You can connect them using a programs that use a protocol called SPI. That's likely to be useful if the Pi needs to talk to a couple of Picos.
  5. You can connect them using a protocol called I2C. That would allow the Pi to talk to lots of Picos, but it's probably the trickiest option, for reasons I'll explain.
In the next post I'll focus on option two. I'll cover SPI and I2C in later posts.

Monday, 25 January 2021

Review: Get started with MicroPython on Raspberry Pi Pico

If you want to explore the Raspberry Pi Pico you'll find it really easy to get started.

On the day of the announcement the Raspberry Pi  Foundation released excellent documentation.The Raspberry Pi press also published a great starter guide: Get started with MicroPython on Raspberry Pi Pico.

It's a super book. The authors are professional journalists and experienced Pi enthusiasts, and it shows!

The book covers everything a beginner needs to know to start exploring Physical Computing with the Pico.

A guided tour of the Pico

Chapter 1 starts with a guided tour of the board.

Next it explains how to solder the headers you'll need if you want to use the Pico with a breadboard. The soldering instructions are clear enough for a novice to follow. The book wisely warns younger users to make sure they are supervised by an adult. It's easy to burn yourself badly with a hot iron while you're learning.

The chapter finishes with clear instructions that tell you how to install MicroPython on the Pico.

Chapter 2: Hello Pico World!

Chapter 2 gets you running your first programs.

The book recommends using the Thonny IDE. As I mentioned in yesterday's blog post, I hit a minor snag with Thonny; it claims to run under Python 3.5, but it won't, as it uses language features that version 3.5 won't support. That's not a problem if you are using a Raspberry Pi with current software.

You will need to make sure that the version of Thonny is up to date (3.3.3 or later). I had to upgrade the software on my Pi to get access to the MicroPython (Rapsberry Pi Pico) option.

You'll probably find the first couple of program rather familiar.

The first is a minimalist version of Hello World.

Since Thonny includes a REPL, you can enter print("Hello, World!") and see an immediate response.

After introducing the idea of looping in Python, and exploring conditional statements, the book moves on to Physical computing: connecting your Pico to the surrounding world using electronic components.

Chapter 3: A whirlwind tour of electronics

You'll find out about all the Pico's GPIO pins and what you can do with them. Old hands will be delighted to discover that the Pico can read analogue signals. Three of its pins are connected to 12-bit ADC channels, so you can measure up to three voltages. Chapter 3 also explains how to use a breadboard with jump wires, and introduces a range of electronic components:

  • push buttons
  • LEDs
  • Resistors
  • Buzzers
  • Potentiometers
  • PIR sensors and
  • I2C LCD Screens.

Chapter 4: blinkenlights at last!

Chapter 4 ups the pace. You'll learn about connecting components to the Pico and controlling them using MicroPython. The very first program needs no extra components, since the Pico has an onboard LED. 

There's lots more to come, but you'll need some extra components for the remaining experiments. The book has a shopping list and all of the components should be easy to find. First you'll control an external LED with its load resistor. After that you'll add a push-button and use that to control your LED.

Chapters 5-9

Chapters 5 to 9 walk you through a series of projects that will introduce you to many of the capabilities of the Pico:

  • Traffic Light Controller
  • Reaction Game
  • Burglar Alarm
  • Temperature Gauge
  • Data logger

Chapter 10: I2C and SPI

Image courtesy of Pimoroni
Chapter 10 covers two of my favourite topics: I2C and SPI. The I2C and SPI protocols make it possible for the Pico to drive LCD displays, as well as controlling servos, motors and other useful components.

Pimoroni offer an Explorer base for the Pico, and it has an on-board LCD. The image  shows a display driven by the Pico which has enough processing power to show animated images!

Pico PIO - an end to bit-banging!

The book concludes with three appendices. Appendix A covers the Pico specification. Appendix B covers the Pico pinout. Appendix C - the last one - covers one of the most exciting features of the Pico - PIO.

As well as its dual-core Arm CPU the Pico has eight tiny PIO computers than you can program and use to control GPIO pins. Not everyone will need them, but they have lots of interesting potential. If you need to use a protocol that's not already supported you have two options.

You can write code to do it, using the main CPU. That's known as bit-banging, and it suffers from a couple of problems; it needs quite a bit of processing power, and it's prone to timing errors.

Your second option uses those extra PIO processors.

Appendix C gives you a quick guided tour with examples to try and then adapt. I'm hoping to use PIOs to implement a couple of useful protocols: Dallas Semiconductor's one-wire protocol, and the DMX protocol used in theatres and discos to control lighting systems. I'll report in due course!


I love this book. It's clear, beginner-friendly, and beautifully illustrated.

At £10 it's not expensive, and the profit will help the Raspberry Pi foundation in its work. If you really can't afford a printed copy you can download a free pdf version. I have both.

If you're getting a Pico, or wondering whether to, start by getting this book!

I've lots more to share as I explore the Pico. Follow @rareblog on twitter to keep up to date!

Sunday, 24 January 2021

Getting started with the Raspberry Pi pico

I'm always excited when Eben Upton pulls another Raspberry Pi rabbit out of his capacious topper.

Last week he announced the $4 Raspberry Pi pico. It's a unique product for several reasons, and I put an order in as soon as I saw the announcement.

It's easy to get the pico going

The pico's on-line documentation is superb and it's supplemented by an excellent and inexpensive book from Gareth Halfacree and Ben Everard.

I did hit one minor 'gotcha'. I've been working on my main workstation which runs Linux Mint, and discovered the hard way that the Thonny Editor won't install properly if you're using Python 3.5

Python 3.5 has reached end-of-life, but it's still the default on Linux Mint 18.3. If I want to use a later version I have to set up a virtual environment. Once I did that, Thonny installed perfectly and within a couple of minutes I'd created a Hello World script and run it on the pico.

If you're using a Raspberry Pi to program the pico you won't have this problem, as the current version of Raspberry Pi OS has Python 3.7 installed.

Raspberry Pi partners with Pimoroni, Adafruit, sparkfun and Arduino

I mentioned that the pico was unique for several reasons. One I find particularly exciting: the RP2040 chip at the heart of the pico will be used in products from Pimoroni, Adafruit, sparkfun and Arduino as well as the Raspberry Pi Foundation.

It's great to see these movers and shakers cooperating in this way. It makes good commercial sense. This cooperation will create more opportunities for us all, and will expand the market as a result.

I'll be documenting my pico exploration as I go. Follow @rareblog on twitter if you want to stay informed.

Tuesday, 12 January 2021

Tom Gilb on Systems Enterprise Architecture

A few days ago my friend Tom Gilb asked me to comment on his new book on SEA (Systems Enterprise Architecture).

I've followed Tom's work since he wrote an inspirational and iconoclastic column in Computer Weekly back in the 1980s. 

Tom has always been sceptical of claims made in IT without quantified evidence. Early in SEA he tells us 'There comes a threshold of complexity in all disciplines where quantification becomes a necessary tool. The time has come for IT Enterprise Architecture to really make use of quantification, and not just talk about doing it.'

He's right, and many traditional IT departments need to mend their ways, but I think Tom underestimates the extent to which the Smart Kids already practice what he preaches. They know that gives them a competitive edge, and for that reason they tend not to broadcast what they do. I'm lucky enough to know a few of them, so I get to hear a little of what they are up to and how they do it.

The TDD world has always placed a lot of emphasis on verifying functional correctness through automated testing. Tom has always placed additional emphasis on the (non-functional) qualities of software: reliability, usability, performance, security and so forth. These are vital to the delivery of business value but they need much more skill to quantify and test. That's why most requirements documents have paid lip service to the qualities without specifying quantified goals and the means of measurement.

While traditional IT often fails in that respect, most young, successful cloud-based businesses live or die by the efficiency, availability, usability, and reliability of their software. They measure these qualities in real-time and deploy changes in real-time to fix problems when they detect them. I'm sure they could do better, and I'm sure that Tom's book could help them to do that, but they are already way ahead of traditional IT.

Tom's expertise can help those who are already doing the right things; for others, his approach would be a game-changer. Sadly I fear few will take advantage of it. They would have to change from being an expert in the old ways to being a novice in the new, and that's always a difficult thing to do.

If you'd like to take a look at the SEA book, you can download a free PDF here: