Skip to content

Project 2.6.30: Smart Timer

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


Overview

This project builds a BLE timer controller that turns an output on for a chosen duration and then turns it off automatically.

The example output in this manual is an LED so the timing logic can be tested safely before replacing the output with a relay or another actuator.

The final system should let the phone set a duration, start the timer, stop it early, and read the remaining time.

Required Components

LED
LED
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
220Ω resistor
220Ω resistor
Breadboard and jumper wires
Breadboard

Phone with BLUETOOTH app

Circuit Connections

Component terminal Connect to Pico physical pin Notes
LED anode (+) 220Ω resistor then GPIO 16 GPIO 16 / physical pin 21 Timed output
LED cathode (-) GND Physical pin 38 Common ground

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│                     │
│  GPIO 16 ───────────┤──── Resistor (220Ω)
│  GND   ─────────────┤──── LED cathode (-)
│                     │
└─────────────────────┘

LED Circuit
LED anode (+) ──[220Ω]──── GPIO 16
LED cathode (-) ────────── GND

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W 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.

Step 2: Place the LED

Place the LED on the breadboard.

Identify the long leg as the anode (+) and the short leg as the cathode (-).

Step 3: Connect the LED Long Leg

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

Step 4: Connect the Resistor to GPIO 16

Connect the other end of the 220Ω resistor to GPIO 16.

Step 5: Connect the LED Short Leg

Connect the LED short leg to GND.

Step 6: Check the LED Polarity

Make sure the long leg goes through the resistor to GPIO 16 and the short leg goes to GND.


Wiring Check

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

Intermediate Note

This project uses wireless UART on the Raspberry Pi Pico 2W. Use a terminal app such as nRF Connect, LightBlue, or another UART terminal to connect to AIDER_SmartTimer and send commands. Timer commands control the LED output, so confirm the LED works before testing longer timed commands.


Testing Individual Components

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

Hardware setup

  • Build the simple LED output circuit
  • Use this safe output before changing the project to a relay or another actuator

Test the output device

  • Run a short script that turns the LED on and off
  • Confirm the resistor and polarity are correct before testing the timer logic

Test timer behaviour

  • Upload the final code and use the Shell output to watch the remaining time
  • Set a short duration first so you can verify the stop behaviour quickly

Test communication

  • Connect to AIDER_SmartTimer from the terminal app
  • Send set 10, start, status, and stop to confirm the command set

Run the full system

  • Set several different durations and confirm the LED stays on for the expected amount of time
  • Stop the timer early and check that the output turns off immediately

Save the project

  • Save the working timer code
  • If you later replace the LED with a relay, update the component table and safety notes in your own engineering log

Quick testing checklist

  • ☐ LED output works before timer testing
  • ☐ Terminal app finds AIDER_SmartTimer
  • ☐ set <seconds> changes the timer duration
  • ☐ start turns the output on
  • ☐ stop turns the output off
  • ☐ Timer ends automatically after the set duration

Full Project Code

After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below into a new file, or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

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

DEVICE_NAME = "AIDER_SmartTimer"

output = Pin(16, Pin.OUT)
ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name=DEVICE_NAME)

duration_seconds = 10
timer_running = False
timer_deadline = 0
command_queue = []

def send_line(message):
    print(message)
    uart.write((message + "\n").encode())

def on_rx(data):
    command = data.decode("utf-8").strip().lower()
    if command:
        command_queue.append(command)

def remaining_seconds(now_ms):
    if not timer_running:
        return 0
    return max(0, time.ticks_diff(timer_deadline, now_ms) // 1000)

uart.on_rx(on_rx)
output.off()
send_line("Bluetooth smart timer ready")

while True:
    now = time.ticks_ms()

    if timer_running and time.ticks_diff(now, timer_deadline) >= 0:
        timer_running = False
        output.off()
        send_line("Timer complete. Output OFF")

    if command_queue:
        command = command_queue.pop(0)
        if command.startswith("set "):
            duration_seconds = int(command.split()[1])
            send_line("Duration set to {} seconds".format(duration_seconds))
        elif command == "start":
            timer_running = True
            timer_deadline = time.ticks_add(now, duration_seconds * 1000)
            output.on()
            send_line("Timer started for {} seconds".format(duration_seconds))
        elif command == "stop":
            timer_running = False
            output.off()
            send_line("Timer stopped")
        elif command == "status":
            send_line("Running:{} Duration:{}s Remaining:{}s".format(
                timer_running, duration_seconds, remaining_seconds(now)))
        elif command == "help":
            send_line("Commands: set <seconds>, start, stop, status, help")
        else:
            send_line("Unknown command. Send help.")

    time.sleep_ms(100)

How the Code Works

Code Section What It Does Why It Matters
duration_seconds Stores the chosen timer length Lets the user tune the project for different tasks
timer_deadline Marks when the timer should end The system compares the current time against this value
start and stop commands Turn the output on or off and control the timer state Provide clear remote control behaviour
remaining_seconds() Calculates how much time is left Makes status reports useful during testing

Expected Result

The phone can set a duration, start the timer, and stop it early. While the timer is running, the LED stays on. When the timer expires, the LED turns off automatically.


Troubleshooting

Problem Possible cause Solution
LED never turns on The LED polarity or resistor path is wrong Check GPIO 16, the resistor, and the LED orientation.
Timer never ends The start command may not have updated the deadline correctly Send status and verify the remaining time changes while the timer is running.
Remaining time looks odd The timer was restarted or the duration was changed unexpectedly Set the duration again and run a fresh timer cycle.
Phone cannot find the BLE device The Pico is not advertising or the BLE helper files are missing Confirm ble_uart.py and ble_advertising.py are saved on the Pico, restart the board, and scan again from nRF Connect or LightBlue.
Commands do not change the system state The phone is connected to the wrong service or the command text is different from the expected command Reconnect to the BLE UART service and send the exact command shown in the test section.