Raspberry Pi Pico W Bluetooth LE Sensor: Wiring, Code and Simulator

To build this Raspberry Pi Pico W Bluetooth LE Sensor project you need a Raspberry Pi Pico W and a potentiometer. MicroPython bluetooth on the Pico W: scan for nearby (simulated) devices, then advertise an Environmental Sensing service and notify a temperature set by a knob. Make the 3 connections in the wiring table, upload the MicroPython code, and open the Network tab to see the scan, the advertisement and each notification. Turn the knob to change the temperature.

Open this circuit in the free Raspberry Pi Pico W simulator

Parts list

QtyPartWhat it does
1Raspberry Pi Pico Wthe microcontroller board
1PotentiometerVCC to 5V, GND to GND, SIG to an analog pin. Drag the knob; analogRead gives 0–1023.

Wiring

#FromTo
1Raspberry Pi Pico W 3V3Potentiometer VCC
2Raspberry Pi Pico W AGNDPotentiometer GND
3Raspberry Pi Pico W GP26Potentiometer SIG

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Raspberry Pi Pico W. No install or account is needed.
  2. Add the parts. Add a potentiometer from the parts palette.
  3. Wire 1. Connect Raspberry Pi Pico W 3V3 to Potentiometer VCC.
  4. Wire 2. Connect Raspberry Pi Pico W AGND to Potentiometer GND.
  5. Wire 3. Connect Raspberry Pi Pico W GP26 to Potentiometer SIG.
  6. Paste the MicroPython code. Paste the MicroPython code below into the code editor.
  7. Run it. Press Run. Open the Network tab to see the scan, the advertisement and each notification. Turn the knob to change the temperature.

MicroPython code

# Pico W: Bluetooth LE with MicroPython's `bluetooth` module.
# The radio is virtual: a scan finds simulated devices, and what this board
# advertises and notifies shows up in the Network tab. (Connections between
# devices are not simulated.)
import bluetooth, struct, time
from machine import Pin, ADC

ble = bluetooth.BLE()
ble.active(True)

# 1. Scan: the IRQ handler gets one event per device, then "scan done".
seen = {}

def on_event(event, data):
    if event == 5:                      # _IRQ_SCAN_RESULT
        addr_type, addr, adv_type, rssi, adv = data
        name = None
        i = 0
        while i + 1 < len(adv):         # walk the advertising fields
            ln, kind = adv[i], adv[i + 1]
            if kind == 0x09:
                name = bytes(adv[i + 2:i + 1 + ln]).decode()
            i += ln + 1
        seen[bytes(addr)] = (name, rssi)
    elif event == 6:                    # _IRQ_SCAN_DONE
        print("scan done:", len(seen), "devices")

ble.irq(on_event)
ble.gap_scan(2000)
time.sleep_ms(2100)                     # results arrive through the IRQ while scanning
for addr, (name, rssi) in seen.items():
    print("  %s  %-14s %d dBm" % (addr.hex(), name, rssi))

# 2. Be a sensor: an Environmental Sensing service with a temperature value.
ENV = bluetooth.UUID(0x181A)
TEMP = (bluetooth.UUID(0x2A6E), bluetooth.FLAG_READ | bluetooth.FLAG_NOTIFY)
((temp_handle,),) = ble.gatts_register_services(((ENV, (TEMP,)),))

name = b"BFB-PicoW-Sensor"
adv = bytes([2, 0x01, 0x06, len(name) + 1, 0x09]) + name + bytes([3, 0x03, 0x1A, 0x18])
ble.gap_advertise(250_000, adv)

knob = ADC(26)                          # GP26 = ADC0
while True:
    # Pretend the knob is a thermometer: 0..3.3 V -> 0..40 C, in 0.01 C units.
    centi = knob.read_u16() * 4000 // 65535
    ble.gatts_write(temp_handle, struct.pack("<h", centi))
    ble.gatts_notify(0, temp_handle)
    print("temperature %.2f C" % (centi / 100))
    time.sleep(2)

What to try

Open the Network tab to see the scan, the advertisement and each notification. Turn the knob to change the temperature.

Frequently asked questions

How do I wire the Bluetooth LE Sensor project on a Raspberry Pi Pico W?

Make 3 connections: Raspberry Pi Pico W 3V3 to Potentiometer VCC; Raspberry Pi Pico W AGND to Potentiometer GND; Raspberry Pi Pico W GP26 to Potentiometer SIG.

What parts do I need for this Raspberry Pi Pico W project?

A Raspberry Pi Pico W and a potentiometer.

Can I simulate this Raspberry Pi Pico W project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and MicroPython code in your browser for free, with no install and no hardware. The Pico W’s RP2040 is emulated and runs the official MicroPython firmware; its Wi-Fi and Bluetooth chip is stood in for by a virtual network that reaches the real internet.

What does the code do?

MicroPython bluetooth on the Pico W: scan for nearby (simulated) devices, then advertise an Environmental Sensing service and notify a temperature set by a knob.

More Raspberry Pi Pico W projects