MicroPython knob dimmer on a Raspberry Pi Pico
MicroPython on a Raspberry Pi Pico: a knob on GP26 sets the brightness of an LED on GP15 through PWM, and print() shows the knob in the Serial Monitor. 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 Runs in your browser. Free, and no account needed.
The parts retail for about $4.65. On Mokxi they're free.
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.
# MicroPython on a Raspberry Pi Pico: a knob dims an LED.
# The potentiometer's wiper is on GP26 (ADC0), the LED on GP15 is driven by PWM.
from machine import ADC, Pin, PWM
import time
knob = ADC(26) # read_u16(): 0 at 0 V, 65535 at 3.3 V
led = PWM(Pin(15))
led.freq(1000) # 1 kHz, far too fast to see it flicker
last = -1
while True:
value = knob.read_u16()
led.duty_u16(value) # the knob's position is the LED's duty cycle
percent = value * 100 // 65535
if percent != last:
print("knob", percent, "%")
last = percent
time.sleep_ms(50)
Parts list
4 parts, plus the jumper wires. They retail for about $4.65; on Mokxi they're free.
- 1 × Raspberry Pi Pico
- 1 × Potentiometer
- 1 × Resistor, 220 Ω
- 1 × LED, red
How it is wired
6 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Raspberry Pi Pico pin GND; LED, red pin C
- Raspberry Pi Pico pin GP15; Resistor, 220 Ω pin 2
- Raspberry Pi Pico pin 3V3; Potentiometer pin 1
- Raspberry Pi Pico pin AGND; Potentiometer pin 3
- Raspberry Pi Pico pin GP26; Potentiometer pin 2
- Resistor, 220 Ω pin 1; LED, red pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.