Using Sublime Text 3 to code with your Raspberry Pi Pico

There are, of course, many options when it comes to editing files. Raspberry Pi’s suggested way of coding for the Raspberry Pi Pico is to use Visual Studio Code. I must admit, I struggled to get on with it. In order to make my life easier, I wanted to use Sublime Text 3, which is what I use for work. Familiarity with your IDE/editor helps a lot when you’re doing something new, so I looked at how I could use it. I’m on Windows, so expect those kind of file paths 🙂 I’m also using CircuitPython which means the Pico presents itself as a drive on your PC (like a Removable Disk).

First steps – direct editing

To start with, I created a Project and added the CIRCUITPY drive to the project. That way, I could see the files on the drive and just edit them.

I experimented for a while and tried to use the serial_monitor plugin to see the device and access the REPL. This plugin, though, doesn’t allow interaction at that level, so I’ve stuck with Putty for now. I have Sublime Text open on one screen and Putty on the other, but if you’ve only got one screen, just split it using Windows’ in-built facility.

Version control using Git / File synchronisation with the Pico

Of course, eventually you’re going to want to keep your code version controlled and backed up on GitHub… right? I mean, you wouldn’t want to just keep it on your Pico and then accidentally lose it? And version control is good, correct? Say “Yes, Mike”. 🙂

Having your repository keyed to be on the Pico itself is pretty bad form, in my opinion. Instead, you need a place for your code off of the Pico. This makes sense – that way if you want to do Git commits and pushes, you can do it from your hard drive, rather than direct from the Pico. But how to keep that “local” code in step with the Pico? That’s where file synchronisation comes in.

I looked at a few options, but what I really wanted was for the file system to be monitored for changes and then the code file(s) that had been changed to be automatically copied onto the Pico. I came across the FSync plugin. It is pretty bare bones, but it does what I want. You’ll need Package Control installed (see here for instructions). First of all, add a repository to Package Control and enter https://github.com/weverss/FSync into the text box. This links your ST3 installation to the specific GitHub for the plugin. Then, install the package by searching for FSync. Restart Sublime Text 3 and the package will be installed and ready.

In your menu, go to Preferences->Package Settings->FSync->Settings – User.

This will open up a blank file.

Now, go to Preferences->Package Settings->FSync->Settings – Default. Copy the whole thing and paste it into your User file and save it.

Now, edit the local_workspace and remote_workspace settings. Set them according to the location of your local folder and your Pico drive. Use / as the path delimited and do not add a trailing /.

Here’s mine:

{
    // File extensions list to ignore during sync
    "ignore_extensions": [".FSync", ".svn-base", "wc.db"] ,

    // WARNING: In both settings below, don't finish directories with '/'

    // Local directory to work...
    "local_workspace": "D:/Projects/Raspberry Pi/Pico/Pico Picorder/PicoPicorder (code)",

    // Remote directory to sync files from local directory ('local_workspace' setting above)
    "remote_workspace": "E:" // Don't finish it with '/'
}

Save it.

Now, what you should find is that each time you save a file in your LOCAL folder, it copies it to the CIRCUITPY drive.

And, because you have now moved your code to a local folder, you can now add it in the usual way to a GitHub repository.

Indentation

Don’t forget to change your Sublime Text 3 indents to “Spaces: 4”. This is at the bottom right of the screen in the grey strip. This is important as all the Adafruit libraries (and, indeed your code!) should use 4 spaces, not a tab character, otherwise you get indentation errors when it runs on the Pico.

Create a low-res thermal camera with a Raspberry Pi Pico, an AMG8833 sensor and an ILI9341 screen with CircuitPython

As part of an ongoing Raspberry Pi Pico-based project, I wanted to create a thermal camera. To do this, I first of all went shopping to find what I needed. Here are the two components for this mini project:

They’re not cheap components, but they’re what you need. You can get a better thermal sensor. This one is only 8×8, so quite low-resolution, but you get some nice results out of it (see the video below).

I chose CircuitPython for the project, because I’ve learned by now that CP has an enormous number libraries.

As always, Adafruit have provided great guides to both products. The screen tutorial is available here and deals with both wiring up and programming it. The tutorial for the AMG8833 thermal camera (there are two types of breakout boards for the sensor) is available here.

How to combine them, though, that’s the trick. I found this tutorial which uses an Adafruit Pygamer board and the Featherwing version of the AMG8833 breakout. This was enough to get me going with translating the array of pixel values returned from the camera denoting sensed temperatures into colour blocks. I had to strip out a lot of functionality relating to the Pygamer board, which was a bit of a pain if I’m honest, but what I was left with should fit nicely into the project I’m working on.

Here’s the wiring that I used:

and you can find my stripped-down code on GitHub. You will need to save the code onto your Pico as code.py to make it run automatically on power-up. You will also need to copy over some libraries to your Pico, so just take a look at the imports at the top of the code for those. You can find the library bundle here.

Here’s the result of the mini-project:

Playing sounds from the Raspberry Pi Pico using CircuitPython – a journey of discovery

As you probably know by now, I’m not a microcontroller expert. However, with the advent of microcontrollers that use MicroPython and CircuitPython, I am a lot more comfortable using them now than I used to be! However, because the Raspberry Pi Pico is new, there aren’t a lot of practical examples of how to do things out there. There is excellent documentation, of course, but when you’re trying to do something specific, or something that hasn’t been thought of, it can be a voyage of discovery.

TL;DR

Scroll down to “Playing a WAV file” for the code that works and “Finding the Hardware” for a wiring diagram.

The project

I am currently working on a new version of my long-running Picorder project, my version of the classic Star Trek prop, the Tricorder. Whereas in the past I’ve used exclusively regular Raspberry Pi (with an occasional foray into Arduino to get analog inputs) as the brains, this time I am choosing to use the Pico. A PicoPicorder, if you will. One thing I’ve never done before is to reproduce the sounds made when the device is opened and scanning.

Finding the sound effect

My first job was to find the sound that I wanted to play. This was easy. TrekCore has a whole host of sound effects from the Star Trek series. I picked the one I wanted (this one) and downloaded it. I knew straight away that the full file was too long (and large), so I converted it, using Audacity, into a WAV file, squashed it into a mono sample and chopped most of it away, leaving me with a self-contained, much shorter, loopable sound effect.

How to play it

Now… how to play it. To start with, I looked at creating my own circuit and using PWM Audio. This post from Greg Chadwick indicated that I could do just that. However, it was, excuse me, a bit beyond me. It also used C/C++ as a programming language. I had already decided that, for this project, I would use Adafruit’s CircuitPython derivative of MicroPython. There were a lot of libraries, it is, basically, Python (which I can already use to a certain extent, thanks to the CamJam EduKits and all the other tutorials that are available), and it is extensively documented.

However, that blog post, and some previously-read Pico documentation, pointed me to needing to use I2S audio, which is how audio (DAC) boards work with the Raspberry Pi. This would give me high enough quality audio to play my sound effect and give me a bit of familiarity (which I always think is key to understanding how to do something).

The Raspberry Pi Pico and I2S

However (again), from my past reading I knew that the Pico does not have I2S pins in the same way that the regular Pi does. Some microcontrollers do have that built-in, but the Pico does not. I also knew that, in order to give functionality to the Pico that it does not have, you use PIO (Programmable Input/Output). (If you want to know about how to understand PIO even more, there’s this excellent article). It’s actually quite difficult to find “how to’s” on the Raspberry Pi website for PIO… in fact, I completely failed, so you will have to root around a bit.

First of all, I discovered this example from the Raspberry Pi GitHub repository that showed what PIO code you needed in order to accomplish that. However, because it’s written in C/C++ and is targeted at the SDK method of programming the Pico, rather than CircuitPython, it rather blew my mind. This is the important part, the PIO code:

.program audio_i2s
.side_set 2

    ; /--- LRCLK
    ; |/-- BCLK
bitloop1: ; ||
    out pins, 1 side 0b10
    jmp x-- bitloop1 side 0b11
    out pins, 1 side 0b00
    set x, 14 side 0b01

bitloop0:
    out pins, 1 side 0b00
    jmp x-- bitloop0 side 0b01
    out pins, 1 side 0b10
public entry_point:
    set x, 14 side 0b11

Complete gibberish, am I right? It was at this point that I almost gave up. 0b10? jmp? It’s assembly language, I thought, for the particular implementation on the Pico. Fair enough, but I have no idea what it all means. However, did that mean that I couldn’t use it? I had seen an example in MicroPython for using PIO inside Python (thanks Alister!) itself, but not CircuitPython.

I thought I’d step back and approach things a different way.

Finding the hardware

I weighed up the amount of problem-solving skills I had and, rather stupidly, assumed that I would be able to get I2S working, somehow.

If I could get I2S working, then I knew I would need a DAC and an amplifier to output the sound from the Pico. I came across this tutorial over on the Adafruit site for using the MAX98357 I2S mono amp which combined an I2S amp and a DAC on the same board. Ideal. It only outputs to one speaker but, for this project, one small speaker is all I need. I found the breakout on The Pi Hut’s site and they were kind enough to send it to me with some other bits for review purposes.

Looking more at the tutorial on Adafruit, I came across the CircuitPython wiring test. This shows how to hook up the MAX board to a microcontroller with I2S pins. To my dismay, the code example did not cover the PIO part. Okay, I thought, we can smash that bit and the other bit together somehow.

The MAX board arrived the next day and I wired it up. Here’s my wiring (thank you Adafruit and Fritzing for the software and parts library!):

Now that was done, PIO was the next step. Or so I thought.

Using PIO in CircuitPython and the Adafruit Discord server

By this point, I knew I was going to need some extra help. Adafruit has, amazingly, created a Discord channel for discussion of their products and, Hallelujah, CircuitPython. There was even a special channel for PIO. I joined and then realised how active the channels all were – awesome, so much expertise and experience.

From suggestions received there, I came across this tutorial on the Adafruit site for using PIO in CircuitPython, so I finally knew that what I was trying to do was possible. Somebody then pointed this example out to me that uses PIOASM (PIO assembler) to create audio on the Pico using CircuitPython.

Wanting to know how things work

At this point, I lost several hours trying to combine things together before realising that I ought to start from first principles.

Returning to the example from earlier which shows the PIOASM example to create audio in CircuitPython. All the example does is to create the necessary PIO code, assemble it, and then use it to play a sine wave.

To my surprise, once I’d got the pins sorted out, it crashed. I got the following error which others might get, so here it is:

Traceback (most recent call last):
File "code.py", line 40, in <module> File "adafruit_pioasm.py",
line 65, in assemble IndexError: list index out of range

Hmmm. Vexing, that.

I had come across this video, a “Deep Dive” with Scott, who works on CircuitPython for Adafruit:

And I wondered… was the functionality so new that I was using a too-old version of CircuitPython and its libraries? I downloaded the latest stable build of the CircuitPython UF2 from this page which was 6.2.0-beta.4 at the time. I also downloaded the latest version of the Libraries and updated the PIOASM Python file from there onto my Pico. I ran it again and… it worked! It worked! Awesome! That wasn’t the end of the adventure, though… I wanted to play a WAV file.

Playing a WAV file

I returned to the CircuitPython wiring and test page for the MAX board. This contains the following code:

import audiocore
import board
import audiobusio

wave_file = open("StreetChicken.wav", "rb")
wave = audiocore.WaveFile(wave_file)

audio = audiobusio.I2SOut(board.D1, board.D0, board.D9)

while True:
    audio.play(wave)

while audio.playing:
    pass

This simply creates an object containing a sample WAV file (StreetChicken), sets up an I2S device and then plays it until it’s finished.

I changed the D1, D0 and D9 references to my own pins and added the argument names for clarity:

audio = audiobusio.I2SOut(bit_clock=board.GP10, word_select=board.GP11, data=board.GP9)

This matches the wiring diagram above. At this point, I knew I was blundering about in the dark and I knew it wouldn’t work. No PIO stuff evident there, after all. The Pico wouldn’t know what to do with itself. Right?

Wrong.

The sample, which I had copied to the root of the Pico’s CIRCUITPY drive, came out of the speakers. It was distorted and repeated over the top of itself, but it was playing! What the heck?

Rolling with it

I decided to roll with it. I changed the code to simply play the WAV file once and then sleep. This resulted in the following (you might need to turn the volume up):

 

It was quite distorted, as you can hear, but it did match what I could play on my laptop. A piece of electronic hip-hoppy music. But why did it work?

I asked on Twitter, I asked on the Adafruit Discord channel and Peter Onion asked me whether I knew what the I2SOut code did. I did not. I received a pointer to the audiobusio/I2SOut code on the Discord channel and took a look. To my surprise, it had all the PIO code in it! That means… That means that someone else has done all the hard work with the PIOASM stuff… That’s amazing!

Clearing up the sound

I spoke to David Glaude on the Discord channel about the problem I was experiencing with the glitchy sound (see video above). He let me see his test script (which was adapted from the previous examples I noted above). I tried it out. It loops the StreetChicken music 10 times. It showed that the glitching problem was still there and also pointed to a problem with the while i2s.playing part – there might be a bug (which David has reported). David also suggested that there was an issue with the Pico being connected to the laptop – i.e. the reading of the flash memory was interfering with the I2S playback. I tried it with a wall PSU and, indeed, this cleared the issue up.

I then altered the code to play back my sound sample of the tricorder in a loop, based on David’s code. To my delight, it works perfectly (if connected to a non-reading power source). You might need to turn your audio volume up a bit, but this is what it now does!

In conclusion

You can get sound, quite easily, out of a Raspberry Pi Pico using CircuitPython. Use audiocore to open and play your WAV files over I2S with the CircuitPython audiobusio library from Adafruit doing the heavy lifting with regards to PIO. And get on the Adafruit Discord channel – there’s a lot of help available out there! 🙂

Introducing the LumiCube – a Raspberry Pi-powered LED cube on Kickstarter now

London-based start-up AbstractFoundry have launched their first Kickstarter for the Lumicube. It is a 10cm cube with 192 LEDs over three sides and ports etc on the back:

You add your own Raspberry Pi to the cube and then use Python to control it. There are plenty of built-in apps in the software to produce patterns, scroll text etc. There are lots of paid-for add-ons, such as sensors and a screen and, of course, you can connect the LumiCube to the Internet and turn it into an animated Internet of Things device, for example. Below is an example of the LumiCube in action, playing Pong!

The campaign video is below and you can view the campaign here. As of writing, there are still 10% off Earlybird pledges available at £81. They’re expecting to ship in September this year, and they’ve already smashed their target. If you like blinkies, this is a lovely product to get behind.

Finding a Raspberry Pi Zero retro emulator in the smallest of places!

Reddit user JamHamster has done this lovely retro emulator build out of an audio cassette and a Pi Zero W.

The entire underside is a heatsink made out of aluminium (which is a bit of an overkill for a Zero, but never mind).

They’ve routed sound and composite video out to the relevant jacks and even sanded part of the GPIO away to maintain the clean external look.

They’ve also added a tiny USB NanoHub which brings out two USB ports from the Zero’s single port (which they’ve removed). There’s even a reset button set-up attached to the last two GPIO pins. He’s hooked it up to a car reverse composite monitor, as you can see below:

I think it looks lovely. You can see some more pics and read a bit more (in the comments) over on Reddit. You can follow them on Twitter.

Introducing Piper Make, a drag-and-drop visual programming environment for the Raspberry Pi Pico

Screengrab from CNX Software

San Francisco-based company Piper, who up til now have concentrated on wooden, DIY educational Raspberry Pi bundles have branched out into a visual programming environment for developing projects on the Raspberry Pi Pico. Called “Piper Make“, the web-based software allows you to develop programs for the Pico using a drag-and-drop interface similar to the Scratch programming environment. A set of tutorials takes you through building simple stuff and then you can move on to creating your own, unique, projects. They have also launched the Makers Club which provides you with a starter kit (including Pico and breadboarding items) and access to new functionality when it is released. You can use Piper Make for free, but there is a subscription for what I assume to be more advanced features. It looks like an excellent system for introducing kids, especially, to what the Pico has to offer. Piper Make is available here. Why not give it a go and then let me know in the comments what you think of it? 🙂