Skip to content

Project 1.12.12

Bluetooth LED Dimmer

Project 1.12.12: Bluetooth LED Dimmer

Beginner Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython

Overview

Build a Bluetooth LED dimmer that lets your phone control brightness.

This project demonstrates PWM, which changes how bright an LED looks by switching it very quickly.

The final result should let a phone send a brightness value from 0 to 100 and smoothly change the LED brightness.

Required Components

Pico 2 W
Raspberry Pi Pico 2 W
LED
LED
220Ω resistor
220Ω resistor
Breadboard
Breadboard
Jumper Wires
Jumper wires
BLE App
Phone with BLE app

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
LED anode (+) 220Ω resistor then GPIO 0 GPIO 0 / physical pin 1 LED long leg
LED cathode (-) GND Physical pin 38 LED short leg

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2 W

Place the Raspberry Pi Pico 2 W on the breadboard so it sits across the center gap.

Keep the USB port facing outward so you can easily connect it to your computer.

Pico Placement


Step 2: Place the LED

Place the LED on the breadboard with its two legs in different rows.

The long leg is the anode (+).

The short leg is the cathode (-).

LED Placement


Step 3: Add the 220Ω Resistor

Connect the LED long leg to one end of the 220Ω resistor.

The resistor protects the LED and the Pico GPIO pin.

Resistor Connection


Step 4: Connect the LED to GPIO 0 and GND

Connect the free end of the 220Ω resistor to GPIO 0.

Connect the LED short leg to GND.

LED Wiring


Wiring Check

    • Pico 2 W is placed correctly across the breadboard center gap
    • LED long leg connects through a 220Ω resistor to GPIO 0
    • LED short leg connects to GND
    • No loose jumper wires

Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

PWM LED Test

Check that the LED brightness can change before adding Bluetooth code.

from machine import Pin, PWM
import time

led = PWM(Pin(0))
led.freq(1000)
for duty in (0, 20000, 40000, 65535, 0):
    led.duty_u16(duty)
    time.sleep(0.8)

Expected test result: The LED should change from off to dim to brighter to full brightness, then off again.

BLE Advertising Test

Check that the Pico advertises as a BLE device your phone can see.

import bluetooth
import time
from ble_uart import BLEUART

ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name='Pico-Dimmer')
print('Scan for Pico-Dimmer in your BLE app')
while True:
    time.sleep(1)

Expected test result: Your phone BLE app should find a device named Pico-Dimmer.


Full Project Code

Upload and run this code after the individual tests work correctly.

from machine import Pin, PWM
import bluetooth
import time
from ble_uart import BLEUART

led = PWM(Pin(0))
led.freq(1000)

ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name='Pico-Dimmer')

brightness_percent = 0

def set_brightness(percent):
    global brightness_percent
    if percent < 0:
        percent = 0
    if percent > 100:
        percent = 100
    brightness_percent = percent
    duty = int((percent * 65535) / 100)
    led.duty_u16(duty)


def on_rx(data):
    command = data.decode('utf-8').strip().lower()
    print('Received command:', command)

    if command == 'status':
        uart.write(('Brightness: {}%\n'.format(brightness_percent)).encode())
    elif command == 'off':
        set_brightness(0)
        uart.write(b'Brightness: 0%\n')
    elif command == 'full':
        set_brightness(100)
        uart.write(b'Brightness: 100%\n')
    elif command == 'help':
        uart.write(b'Send 0 to 100, or use status, off, full, help\n')
    else:
        try:
            percent = int(command)
            set_brightness(percent)
            uart.write(('Brightness: {}%\n'.format(brightness_percent)).encode())
        except ValueError:
            uart.write(b'Unknown command. Send help.\n')

uart.on_rx(on_rx)
set_brightness(0)

print('Bluetooth LED dimmer ready')
print('Send a number from 0 to 100 for brightness')
print('Other commands: status, off, full, help')

while True:
    time.sleep(0.1)

How the Code Works

Code Section What It Does Why It Matters
PWM LED setup Creates a PWM output on GPIO 0 PWM is what makes brightness control possible
set_brightness() Converts a percentage into a 16-bit duty value Students can think in 0 to 100 instead of 0 to 65535
on_rx() callback Reads Bluetooth text commands and changes brightness This links the phone app to the LED output
status and helper commands Report current brightness and support quick commands Makes the project easier to test and understand

Expected Result

After running the code, your phone BLE app should find Pico-Dimmer. Sending a number from 0 to 100 should change the LED brightness. Sending off should turn the LED fully off, full should make it fully bright, and status should return the current brightness percentage.


Troubleshooting

Problem Possible Cause Solution
LED does not light LED is reversed, the resistor is missing, or wiring is wrong Check LED polarity, the resistor, and GPIO 0 wiring
Brightness does not change smoothly The command is outside 0 to 100 or the LED test failed Send a number between 0 and 100 and rerun the PWM LED test
Phone cannot find Pico-Dimmer BLE helper files are missing or Bluetooth is not active Check that ble_uart.py and ble_advertising.py are saved on the Pico and rerun the advertising test