Build it step by step
0 of 6 doneWire the touch module
VCC to 3V3(OUT), GND to GND, I/O to GP15.
Wire the LED
Long leg through 330 ohms to GP16, short leg to GND.
Run the program
The LED is off.
Tap the pad
The LED fades to dim.
Tap again
Medium, then bright, then off.
Save as main.py
So it runs when you plug in the Pico.
How do you build a pico touch dimmer lamp?
A Pico reads a TTP223 touch pad and cycles an LED lamp through off, dim, medium and bright with each tap, using PWM and a smooth fade. You need Raspberry Pi Pico H, Touch sensor module (TTP223), LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points. It takes 6 steps and about 110 minutes.
- Level
- Beginner
- Time
- About 110 min
- Parts
- 5
Parts list
- Development BoardsBuy
- SensorsBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 330 Ω × 1
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| TTP223 VCC | Pico 3V3(OUT) pin 36 | Power at 3.3 V |
| TTP223 GND | Pico GND pin 38 | Common ground |
| TTP223 I/O | Pico GP15 pin 20 | High while touched |
| LED long leg | 330 ohm resistor to Pico GP16 pin 21 | Dimmed by PWM |
| LED short leg | Pico GND pin 23 | Ground |
Code
The TTP223 is a touch switch chip. A metal pad on the board forms one plate of a tiny capacitor with your finger as the other plate, and the chip notices the small change and sets its output pin high. Because it measures capacitance there are no moving parts. The Pico watches for the moment the output goes from low to high, which is a new touch, and steps to the next brightness level. To make the LED seem to change smoothly the program does not jump to the new level but moves toward it a little each ten milliseconds, so a change takes about a quarter of a second. The LED is driven by PWM, which switches it on and off a thousand times a second with a changing share of on time. The four levels are spaced unevenly on purpose, because our eyes judge brightness on a curve, so equal steps would look as if the lowest levels did nearly nothing.
import time
from machine import Pin, PWM
touch = Pin(15, Pin.IN)
led = PWM(Pin(16))
led.freq(1000)
LEVELS = [0, 4096, 16384, 65535] # off, dim, medium, bright (spaced for the eye)
index = 0
current = 0
prev = 0
while True:
t = touch.value()
if t == 1 and prev == 0: # a new touch
index = (index + 1) % len(LEVELS)
time.sleep_ms(40)
prev = t
target = LEVELS[index]
if current < target:
current = min(target, current + 2500) # fade up
elif current > target:
current = max(target, current - 2500) # fade down
led.duty_u16(current)
time.sleep_ms(10)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start | LED off |
| First tap | Fades to dim |
| Second tap | Medium |
| Third tap | Bright |
| Fourth tap | Fades off |
If it does not work
LED does not light
Check the LED direction, the long leg goes to the resistor.
Skips levels
A tap is read twice, raise the 40 millisecond pause.
Changes by itself
Keep your hand and wires away from the pad, and keep the module on a stable surface.
Brightness steps look uneven
Change the numbers in the LEVELS list.
Pad does nothing
Check the I/O wire is on GP15 and the module has 3.3 V.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico touch dimmer lamp?
Raspberry Pi Pico H, Touch sensor module (TTP223), LED 5 mm red, Resistor 330 Ω and Breadboard 830 Tie Points.
How long does the pico touch dimmer lamp take to build?
About 110 minutes for a beginner maker, in 6 steps.
How do I know the pico touch dimmer lamp works?
Start: LED off. First tap: Fades to dim. Second tap: Medium.
LED does not light: what should I check?
Check the LED direction, the long leg goes to the resistor.
Skips levels: what should I check?
A tap is read twice, raise the 40 millisecond pause.
Changes by itself: what should I check?
Keep your hand and wires away from the pad, and keep the module on a stable surface.
Brightness steps look uneven: what should I check?
Change the numbers in the LEVELS list.
