Week 2: Embedded Programming

Created
TagsEmbedded SystemsMicropythonRP2040

Background

I have prior experience with embedded programming, so to start off, I wanted to do something that would push me to use modules that I haven’t used before. Neil mentioned that the ESP32 XIAO came with in-built bluetooth support, so I thought it would be cool to get two ESP32’s to talk to each other over bluetooth.

My experience was also primarily with the ATMEL and MSP32 series of controllers and Arduino/Energia/Texas Instruments CCS Toolchains, so getting to use micropython was exciting!

The SEAS makerspace didn’t have the ESP32 board so I decided to use the Pi PICO W instead, which recently received an OTA that added bluetooth capabilities.

Setting Up

I looked at this reference to setup Micropython & the latest firmware on my Pi Pico W

https://projects.raspberrypi.org/en/projects/get-started-pico-w/2


I hadn’t worked with bluetooth before, so I found an article on how to setup bluetooth with the raspberry pico w. I followed the instructions but the code crashed at

import bluetooth

I figured this possibly could be because I didn’t download the latest version of micropython/the pico W bootloader so I went down a rabbit hole of making sure I had the latest version and then finally ended up installing it using the in-built Thonny bootloader installer but to no avail.

So then eventually I found a github issue where someone faced the same problem as me. Turns out the bluetooth support was not added in the latest STABLE version but in the latest NIGHTLY build. So I had to download and reflash the Pico with the nightly build of the firmware which was v1.20.0-490. God, I hope I don’t regret flashing an unstable firmware version later.

After successfully flashing the Pico with the nightly build of the firmware, I was able to import the bluetooth module without any issues. Now, I could proceed with my plan of getting two Picos to communicate over Bluetooth. I started by experimenting with simple Bluetooth communication protocols and exploring the available functions and methods provided by the bluetooth module.

Looking at the bluetooth spec documentation and module, I realized getting two pico’s to communicate would probably be a pain in the ***. I decided to explore another idea - can I send messages to my pico from my phone and use text to speech to play it out of a speaker instead?

I was able to setup a bluetooth connection with my phone and to start off sent a continuous “hello” ping via bluetooth from the pico. Woohoo!

Soldering

It had been a while since I’ve soldered stuff, so here goes…

Welp. Will do better next time.

Build-build-build

I decided to solder a PAM8302A amplifier and use it with a speaker so I can begin to play audio from a Raspberry Pi Pico. Unfortunately I soon realized that mp3 playback for the PAM8302A is only supported by circuitpython, and not micropython. (damn you adafruit and your noncompetitive practices).

So now figuring out how to play an audio file using micropython off the pico and interfacing it with the mono amp from adafruit would be non-trivial. Not to mention figuring out how to perform text-to-speech in micropython.

Re-evaluating my situation at 7pm on Tuesday night before class, I decided to cut my ambitions short and use the speaker more like a monotone buzzer that is driven via bluetooth from my phone. Lesson learned, start early - SUPPLY SIDE TIME MANAGEMENT!!

I used this textbook (that is seemingly for kids) to understand how to drive the speaker with the Pico’s GPIO pins

Finally, I was able to drive the speaker over bluetooth from my phone! Watch the video below