Pages

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!

Summary

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!