Built-in project · Raspberry Pi Pico

MicroPython dimmer on a Pico: a knob sets an LED

MicroPython on a Raspberry Pi Pico: a potentiometer on GP26 sets the PWM duty, and so the brightness, of the LED on GP15. The whole build, a Raspberry Pi Pico and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $4.65. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a knob with the ADC
  • PWM duty as LED brightness
  • Turning a 16-bit value into a percent
  • Printing only larger changes
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Raspberry Pi Pico
  • Parts: 5

How it works

The potentiometer sits between ground and 3.3 V, and its middle pin goes to GP26. Turning the knob changes the voltage there, and the ADC reads it as a number from 0 to 65535.

That number is the PWM duty for the red LED on GP15, at 1000 Hz, so the brightness rises with the knob. The program prints the brightness as a percent, only when it changes by more than 1.

Parts list

5 parts, plus the jumper wires. They retail for about $4.65; on Mokxi they're free.

Kits that can build it

These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 220 Ω pin 1Raspberry Pi Pico pin GP15
Potentiometer pin 2Raspberry Pi Pico pin GP26
Potentiometer pin 1Ground
Potentiometer pin 33.3 V
LED, red pin AResistor, 220 Ω pin 2
LED, red pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico
# A dimmer: the knob on GP26 sets the LED's brightness. The ADC reading
# (0 to 65535) is the PWM duty cycle, so they go up together.
from machine import ADC, Pin, PWM
import time

knob = ADC(26)
led = PWM(Pin(15))
led.freq(1000)
last = -1

while True:
    value = knob.read_u16()
    led.duty_u16(value)
    percent = value * 100 // 65535
    if abs(percent - last) > 1:
        print("brightness", percent, "%")
        last = percent
    time.sleep_ms(20)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LED does not change when the knob turns. This project
    Check that the potentiometer's middle pin goes to GP26, and the outer pins go to ground and 3.3 V.
  • The knob works backward. This project
    Swap the two outer wires on the potentiometer, ground and 3.3 V.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.