Monday, 15 March 2021

MicroPlot on the PyPortal - progress and frustration

Update: I got a fix within minutes of posing the problem on the Adafruit discord channel!

Here's the correct bitmap:

The problem is now solved,

MicroPlot now runs well on the Adafruit PyPortal and  Clue as well as the Pimoroni Pico Explorer base, but I've been tearing my hair out trying to solve a problem saving bitmaps.

The bitmap problem

Here's a screenshot of a display on the PyPortal together with the bitmap file which should show what's on the screen. I could not work out what's going wrong.

The code creates the plot and then uses the screenshot code that Adafruit provides.

import math from plotter import Plotter from plots import LinePlot import board import digitalio import busio import adafruit_sdcard import storage from adafruit_bitmapsaver import save_pixels def plot(): sines = list(math.sin(math.radians(x)) for x in range(0, 361, 4)) lineplot = LinePlot([sines],'MicroPlot line') plotter = Plotter() lineplot.plot(plotter) def save(): spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) cs = digitalio.DigitalInOut(board.SD_CS) sdcard = adafruit_sdcard.SDCard(spi, cs) vfs = storage.VfsFat(sdcard) storage.mount(vfs, "/sd") save_pixels("/sd/screenshot.bmp") plot() save() print('done')

What works, what doesn't?

When I first wrote about MicroPlot it ran on the Pico. The code worked but I knew it would need refactoring as I extended it to cover other plot types and other devices.

Past experience has taught me the value of automated tests for embedded projects. I wondered if I could capture MicroPlot's output in bitmap files and use them in tests. I didn't know much about the format of Bitmap files so I started to look for code I could borrow and adapt.

I soon discovered that bitmap files could use different ways of encoding colour, and the simplest, smallest format created monochrome bitmaps.  I soon got a Bitmap file-saver working reliably on the Pico. As you can see, it works, and you can find the code in the MicroPlot project on GitHub.

Soon after that I discovered some Adafruit colour bitmap saver code and adapted it to run on the Pico. As you can see, it too worked well.


When I ran the unmodified Adafruit code on the PyPortal, it scrambled the image, as shown earlier.

I've checked that I am using the latest production versions of the Adafruit code. Can anyone suggest what I'm doing wrong? The code (mine and Adafruit's) looks sensible, but it seems to corrupt every screenshot  bitmap that I try to take.

If you can spot the problem, let me know in the comments, tweet to @rareblog or respond to my cry for help on Discord.

Friday, 26 February 2021

More Raspberry Pi Pico experiments

If you're exploring the Raspberry Pi pico, here are some more resources


I've started a new project called MicroPlot. It's been developed on the Pico though it will eventually work on other  micro-controllers and computers. It's a minimal plotting package and it already has enough functionality to be useful.

It will plot simple line plots; the first example below is a series if sine waves and the second shows the voltage across a capacitor as it charges and discharges.

Plotting Sine waves

Capacitor charging and discharging

MicroPlot has its own project on GitHub. You can read about it and download the code at and I will be soon be using it for some more electronics experiments.

Using the UART

The current MicroPython documenation for the Raspberry Pi Pico is a bit thin on detail about using the UART. I've added a short example in my pico-code project on GitHub.

The Tiny2040 is available from Pimoroni!

Pimoroni Tiny2040

I was lucky enough to get an early pre-production version of the Pimoroni Tiny2040.

It's now available in the Pimoroni shop.

You'll find a couple of simple examples in my pico-code project. They are in the


Over the years I've blogged about a number of implementations of resistomatic, a home-made resistance meter. Most of them use an 8-bit ADC which complicates the design because 8 bits give a low resolution. The pico's ADCs are 12-bit. My crude design just needs a bridge that compares the voltages across the reustor to be measured and a know 1K ohm resistor. It's accurate enough (5%) for my purposes across a range of resistance. I've checked it with resistors ranging from 56R to 100K ohms.

You'll find the code and little more information at

More on the way

I've several more Pico projects in the pipeline. To stay up-to-date as I release them, follow @rareblog on twitter.

set up a new GitHub

Saturday, 13 February 2021

MicroPython development on the Raspberry Pi Pico

Get Started with MicroPython on Raspberry Pi Pico suggests that you use the Thonny editor for development.

Thonny will get you off to a quick start, but you may have an alternative editor you'd prefer to use.

Lots of my friends now use VS Code; some are vim or emacs experts; many, like me, use PyCharm as their normal Python development environment.

I find it more comfortable to use my normal editor for MicroPython development, but I have more compelling reasons.

The first is refactoring support. I'm learning as I go, and I often want to improve the design of my code libraries as I come to understand things better. PyCharm does a very good job of refactoring Python code.

There's another issue to do with version control.

I'm currently sharing my Pico code on GitHub. That means I need to keep code on the Pico in sync with the code on my workstation. I haven't found an easy way to do that with Thonny, so I am using another tool to move and test code.

rshell was my first choice, but I've had some problems with it, as have others.  Les Pounder (@biglesp) came to my rescue. He has a really useful series of blog posts called Tooling Tuesday.

A while ago Les recommended Adafruit's ampy for use with MicroPython boards. Ampy works really well with the Pico. I now have it on my workstation, and on a Raspberry Pi which I also use for Pico development.

Les also recommends tio for serial communications. tio is a serial terminal program. Its great advantage over others is that it will try to reconnect automatically if the serial link goes down. tio is available on the Pi but I couldn't find it in the official repositories for my very backdated Linux Mint installation. Fortunately it took just three minutes to download tio and build it from source, following the instructions on its GitHub page.

I'll soon be publishing more Pico and Tiny2040 code. If you want to stay up to date, follow me (@rareblog) on twitter.