MC2023 Chapter 5: Programming the Pico PIO

Page created: 2023-05-22 , updated: 2023-06-26

Welcome to the fifth "chapter" of my Year of the Microcontroller!

(In retrospect, I should have called these "episodes" since I’ve been mentioning various books…​which have chapters. Oh well.)

2023-05-14

I’m still making my way through "Programming The Raspberry Pi Pico/W In C, Second Edition" by Harry Fairhead. Now I’m into the chapters about using the Pico’s programmable I/O (PIO).

It’s a lot like the PWM controllers for the GPIO pins - you call some SDK functions to load your settings and then they run entirely on their own, freeing up the CPU (and your main program loop) to do more important things.

Except the the PIO is truly programmable. Also, you read from and write data to them via registers. It’s almost as if they were on a separate chip elsewhere in your circuit.

There are four separate PIO "state machines" on the Pico. These state machines have an instruction set with 9 instructions and you program them by loading the instructions as either raw machine code or by assembling them into a form that can be accessed from your C program using some Cmake incantations.

I managed to get the book’s "PIO Blinky" program working and I think I have a decent idea of how this works.

But wow, these things are a whole separate area of study and if I needed to write my own interfaces for one of these from scratch, I’d expect to set aside a boat load of time to get it working.

2023-05-20

I’ve read the rest of the PIO content in the book up 'til the temp/humidity sensor example. I bought the sensor part (it’s cheap!) a while back in preparation for this moment, so I just need to wire it up on my breadboard and give the example code a whirl.

The additional challenge I’ve given myself is to display the current readings on my 128x64px OLED screen featured in the previous "chapter" of my journey.

Time passes…​ 2023-06-26

A month later, I have now utterly failed to make that temperature sensor work (they came in a two-pack, so I’m pretty confident the problems lies with me, not the devices). I have failed to make it work with bit-banging or PIO.

I continued on and tried a second type of temperature sensor from the next project in the book. I failed to make that work as well.

I’ve tried debugging communication with both devices using the oscilloscope, but I’m just not that capable with it…​yet.

The old me would have stuck it out. I have zero doubt I would eventually have gotten both of these devices working…​and I would have burnt myself out and stopped having fun.

The new me knows that getting these devices working is not my goal this year. Learning and building is my goal, and I have certainly learned things from these code examples - even if they didn’t actually give me any temperature readings.

I’m sure it all comes down to some simple mistakes - maybe I hooked something up wrong on my increasingly crowded breadboard. Maybe I typed something wrong. Who knows?

The final chapter of the book covers manually accessing the memory-mapped IO in the Pico, and thankfully that example did work for me - I was able to blink the on-board LED by writing to a pointer directly.

I highly recommend this book if you need some guidance with the Pico using the official C SDK!

image of Programming The Raspberry Pi Pico/W In C

Next

So this ended up being a rather short and disappointing "chapter" of my journey this time around, comparatively speaking, but I’m moving forward according to my plan.

The next project is to build something genuinely useful for the family!

It’s going to be a timer device. We use the kitchen timer a lot and it’s annoying to keep entering the same times (often 30 and 10 minutes) on the oven timer over and over again. A device that has our most frequently used times built-in and that keeps track of which times have been "used up" will be very handy. I think the kids will also get a kick out of using it.