🛠 Bluetooth LE : serveur GATT avec la radio 5.2
Objectives
The Pico 2 W embeds the same CYW43439 as the Pico W: Bluetooth 5.2 confirmed, with BLE central and peripheral roles and even Bluetooth Classic. This tutorial sets up a GATT server: the board broadcasts an advertisement, a smartphone connects to it and reads a characteristic that counts seconds. BLE coexists with Wi-Fi on the same chip, but you must choose the btstack architecture instead of lwIP at compile time.
Hardware
- Pico 2 W + pico-sdk (feature btstack)
- Smartphone with any BLE scanner (nRF Connect)
- USB cable and serial console
Complete code
// main.c - squelette inspire de pico-examples pico_w/btstack
// lien : pico_btstack_cyw43 pico_stdlib
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include "btstack.h"
static const uint8_t adv_data[] = {
0x02, 0x01, 0x06, // flags LE general
0x06, 0x09, 'P', 'i', 'c', 'o', '2' // nom d'annonce
};
int main(void) {
stdio_init_all;
if (cyw43_arch_init) { return 1; } // meme puce que le Wi-Fi
cyw43_arch_enable_bt_mode; // mode Bluetooth du CYW43
// 1) hci_setup + btstack_run_loop
// 2) att_server : service 16 bits 0xA0FB + caracteristique compteur (READ)
// 3) gap_advertise(adv_data) ; dans la boucle, compteur++ chaque seconde
while (true) { sleep_ms(1000); }
}
Expected verification
The smartphone's BLE scanner sees the Pico2 advertisement, connects, and the characteristic increases by one unit per second. The radio LED may blink during advertising.
Common errors
- Compiling with the lwIP architecture (pico_cyw43_arch_lwip_*) and then calling btstack APIs: you need pico_btstack_cyw43.
- Confusing it with the standard Pico 2: without the CYW43 chip, no BLE is possible.
- Advertisement name too long: the BLE envelope is limited, and the advertisement is rejected.
- No BLE scanner on Windows without an external adapter: test from the phone.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython