Raspberry Pi Pico-W Bluetooth Demos

     I have many times professed my love for the outstanding Raspberry Pi Pico, but only recently did I start working with the Pico-W. The regular Pico is still my go-to solution for most embedded projects, but I've already done a few projects that need wireless connectivity and I feel silly using NRF24 radios when I could just have two Picos talk directly or connect one of them to my phone. Of course, for the first several months after the Pico-W was released, it didn't have functional Bluetooth. The hardware was there, but the SDK didn't yet support it. Apparently that was due to the complexity of having wifi and Bluetooth connect to the RP2040 over a single SPI bus. But that has been figured out and as of a few months ago we do have Bluetooth functionality, so I decided to try it out.

    As of 2023-June, there is not a lot of support for the new Bluetooth functionality. There is the SDK itself (with examples) and Raspberry Pi Foundation's documentation, but the new BT examples are significantly more opaque and difficult to build than previous examples. As far as community support, I've found several StackOverflow and Reddit pages that contain incomplete conversations or questions without answers, and a few blogs that contain incomplete or clearly broken demos. (There are more blog examples for MicroPython, but no functional examples for C++ that I've found) After a bit of trial and error, I did finally figure out how to build the SDK examples and also how to create new standalone projects that use Bluetooth. Since I expect I'll forget what I've learned in the future, I'm writing down an outline here, and putting all of the demos I create into a git repo.

    The confusing thing for me was that the BT example code isn't contained in the pico-examples repo. Instead they're located back in the pico-sdk library and the pico-examples library links to them in an obfuscated way. And then the BT examples are set up in such a way that they're not as easily built through VSCode. (This might be fully explained in the RPi Documentation, but it contains some errors that led me astray at first so I ended up finding it on my own) So my repo contains a blink demo and also a BT-pair demo where one board reads its ADC and transmits its value to the other board, which writes it to PWM. Hopefully those should work as decent starter code for a variety of BT projects. I haven't yet figured out how to do duplex communication. (though it seems the receiver is sending receipts to the transmitter?)

Part 1: Setting up your build environment

I believe the following terminal commands should completely set up all of the dependencies required to build Pico-W applications on an M2 Mac. (M1 and Intel Macs should be quite similar, but I don't have any Intel or M1 machines to test on.)

# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew update
 

# Use Homebrew to install dependencies

brew install cmake

# IF YOU HAVE AN M2 MAC, YOU'll NEED A NEW COMPILER VERSION
brew install --cask gcc-arm-embedded

# IF YOU HAVE AN EARLIER MAC, YOU NEED THE REGULAR VERSION
brew tap ArmMbed/homebrew-formulae
brew install arm-none-eabi-gcc

# Download and build the Pico SDK
mkdir ~/rpi-pico
cd ~/rpi-pico
git clone https://github.com/raspberrypi/pico-sdk.git
cd pico-sdk
git submodule update --init
cd ..

# Download and build the Pico Examples
git clone https://github.com/raspberrypi/pico-examples.git
cd pico-examples
mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk/
cmake -DPICO_BOARD=pico_w ..
cd pico_w/bt/standalone
make

Part 2: Build and flash the demos

Download demos from gitlab (git clone https://gitlab.com/tllado/pico-project)

I really like VSCode. After installing the CMake and CMake Tools extensions, I can select the correct compiler option and build the demos. (GCC 13.2.1 arm-none-eabi for M2 Macs)

<I want to add a video here but might never get around to it>