Pico W knob on GP28 dims an LED on GP15
MicroPython on a Pico W: a potentiometer on GP28, read with ADC(28), sets the PWM duty of an LED on GP15, and the reading is printed. The whole build, a Raspberry Pi Pico W 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 $6.65. On Mokxi they're free.
What you'll learn
- Reading a knob with the ADC
- Sixteen-bit values from 0 to 65535
- PWM duty as LED brightness
- Printing only larger changes
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico W
- Parts: 5
How it works
The potentiometer sits between ground and 3.3 V, and its middle pin goes to GP28, which is ADC2. The knob changes the voltage there, and read_u16 turns it into a number from 0 to 65535.
That same number is the PWM duty for the LED on GP15, running at 1000 Hz. A higher number means a brighter LED. The program prints the value only when it changes by more than 600.
Parts list
5 parts, plus the jumper wires. They retail for about $6.65; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Power, 3.3 V
Kits that can build it
These 2 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 pin | Connects to |
|---|---|
| Resistor, 220 Ω pin 2 | Raspberry Pi Pico W pin GP15 |
| Potentiometer pin 2 | Raspberry Pi Pico W pin GP28 |
| LED, red pin A | Resistor, 220 Ω pin 1 |
| LED, red pin C | Ground |
| Potentiometer pin 1 | Ground |
| Potentiometer pin 3 | 3.3 V |
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.
# Turn the knob: the potentiometer's wiper is on GP28, which is ADC2.
# read_u16() gives 0 to 65535, and that number is the LED's PWM duty
# on GP15, so the knob sets the brightness.
from machine import ADC, Pin, PWM
import time
knob = ADC(28)
led = PWM(Pin(15))
led.freq(1000)
last = -1
while True:
value = knob.read_u16()
led.duty_u16(value)
if abs(value - last) > 600:
print("knob", value)
last = value
time.sleep_ms(50)
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 middle pin of the potentiometer goes to GP28, not an outer pin. - The LED stays dark. This project
Check its direction. The anode (A) connects to the resistor from GP15, and the cathode (C) to ground. - 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.