Skip to content

Project 1.12.13

Bluetooth Smart Lamp

Project 1.12.13: Bluetooth Smart Lamp

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

Overview

Build a Bluetooth smart lamp that can turn on, turn off, and change brightness from a phone.

This project demonstrates how PWM can be used to make one LED behave like a simple dimmable lamp.

The final result should let a phone send commands such as on, off, low, medium, high, or a number from 0 to 100 to set the 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 2W 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 Lamp Test

Check that the LED brightness changes before adding Bluetooth code.

from machine import Pin, PWM
import time

led = PWM(Pin(0))
led.freq(1000)
for duty in (0, 12000, 32000, 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-Lamp')
print('Scan for Pico-Lamp in your BLE app')
while True:
    time.sleep(1)

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


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-Lamp')

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 == 'on':
        set_brightness(100)
        uart.write(b'Lamp ON at 100%\n')
    elif command == 'off':
        set_brightness(0)
        uart.write(b'Lamp OFF\n')
    elif command == 'low':
        set_brightness(25)
        uart.write(b'Lamp LOW at 25%\n')
    elif command == 'medium':
        set_brightness(60)
        uart.write(b'Lamp MEDIUM at 60%\n')
    elif command == 'high':
        set_brightness(100)
        uart.write(b'Lamp HIGH at 100%\n')
    elif command == 'status':
        uart.write(('Lamp brightness: {}%\n'.format(brightness_percent)).encode())
    elif command == 'help':
        uart.write(b'Commands: on, off, low, medium, high, status, help, or 0-100\n')
    else:
        try:
            percent = int(command)
            set_brightness(percent)
            uart.write(('Lamp 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 smart lamp ready')
print('Send on, off, low, medium, high, status, help, or a number from 0 to 100')

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 makes smooth brightness control possible
set_brightness() Converts a 0 to 100 percentage into a PWM duty value Students can use simple brightness numbers
Named commands Provides easy preset commands such as low, medium, high These are easier for beginners than only raw numbers
Status reply Sends the current brightness back to the phone The student gets confirmation that the command worked

Expected Result

After running the code, your phone BLE app should find Pico-Lamp. Sending on, off, low, medium, high, or a number from 0 to 100 should change the LED brightness. Sending 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 The command text is wrong or PWM test failed Send on, off, low, medium, high, or a number from 0 to 100 and rerun the PWM lamp test
Phone cannot find Pico-Lamp 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