Project 3.2.3: Automatic Trash Lid
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico opens a servo-driven trash lid when an IR proximity sensor detects a hand near the bin opening.
Touch-free bins reduce contamination and make waste disposal easier in shared spaces.
A Pico 2 W prototype with an IR proximity sensor, a servo motor for the lid, and a timed open-close sequence.
Proximity-based actuation, servo timing, and safe external-power use for small motors.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Breadboard |
![]() Jumper wires |
![]() IR Sensor |
![]() Servo motor |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| IR sensor VCC | 3.3V output | Physical pin 36 | Check that the module supports 3.3V |
| IR sensor GND | GND | Physical pin 38 | Common ground |
| IR sensor OUT | GPIO 14 | GPIO 14 / physical pin 19 | Active-low proximity input |
| Servo signal | GPIO 15 | GPIO 15 / physical pin 20 | PWM servo control |
| Servo VCC | External 5V supply | Not a Pico GPIO pin | Use a regulated servo supply |
| Servo GND | GND | Physical pin 38 | Common ground with Pico |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ 3V3 ───────────────┼── IR sensor VCC
│ GND ───────────────┼── IR sensor GND
│ GPIO 14 ────────────┼── IR sensor OUT
│ GPIO 15 ────────────┼── Servo signal
│ GND ───────────────┼── Servo GND / external supply GND
│ │
└─────────────────────┘
External 5V supply + ───── Servo VCC
External 5V supply - ───── Common GND
Step-by-Step Assembly
- Connect the IR sensor to Pico 3.3V, GND, and GPIO 14.
- Connect the servo signal wire to GPIO 15.
- Power the servo from an external 5V supply and connect the servo ground to Pico ground.
- Test the servo angles without the lid attached so you do not jam the mechanism during calibration.
- Mount the sensor so a hand near the bin mouth is detected reliably but the surrounding room is ignored.
- Attach the lid only after the servo open and closed positions are safe.
Testing Individual Components
Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.
- Hardware setup: Assemble the Pico, sensor, indicator, and actuator wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Print the raw IR sensor state and move a hand into the target area so you know the detection distance before attaching the lid.
- Test the output device: Test the servo open and close angles with no mechanical load first.
- Test the decision logic: Confirm that one hand event creates one full open-close cycle without repeated jitter.
- Run the full system: Attach the lid and compare the printed states with the real mechanical motion.
- Validate the prototype: Check whether different lid weights or hinge friction require angle or timing adjustments.
- Save the project: Save the validated program on the Pico as
main.pyand keep a copy on the computer for future edits.
Full Project Code
After completing and checking the circuit connections, open Thonny IDE, copy and paste this code 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, PWM
import time
IR_PIN = 14
SERVO_PIN = 15
OPEN_DUTY = 5200
CLOSED_DUTY = 2500
HOLD_OPEN_MS = 2500
CYCLE_GAP_MS = 3000
ir = Pin(IR_PIN, Pin.IN, Pin.PULL_UP)
servo = PWM(Pin(SERVO_PIN))
servo.freq(50)
last_state = 1
last_cycle_ms = -CYCLE_GAP_MS
def set_servo(duty):
servo.duty_u16(duty)
time.sleep_ms(500)
servo.duty_u16(0)
set_servo(CLOSED_DUTY)
print("Automatic trash lid ready")
while True:
now = time.ticks_ms()
current = ir.value()
new_detection = last_state == 1 and current == 0
if new_detection and time.ticks_diff(now, last_cycle_ms) >= CYCLE_GAP_MS:
print("State: OPENING")
set_servo(OPEN_DUTY)
print("State: HOLDING")
time.sleep_ms(HOLD_OPEN_MS)
print("State: CLOSING")
set_servo(CLOSED_DUTY)
last_cycle_ms = time.ticks_ms()
print("State: IDLE")
last_state = current
time.sleep_ms(50)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Servo calibration constants | Defines the duty values for open and closed positions | Servo mechanics vary, so the lid must be calibrated to the actual hardware | Adjust OPEN_DUTY and CLOSED_DUTY with the lid disconnected first |
set_servo() |
Moves the servo and then releases PWM holding torque | This reduces continuous buzzing on small servos during classroom tests | Increase the move delay only if the servo needs more time to reach position |
| Edge detection | Starts a cycle only on a new proximity event | This prevents the lid from retriggering constantly while a hand stays in view | If false triggers happen, improve sensor placement before changing the code |
| Open-hold-close sequence | Runs the full lid motion and then enforces a short gap before the next cycle | This keeps the user interaction predictable and reduces motor stress | Adjust HOLD_OPEN_MS and CYCLE_GAP_MS after observing real use |
Expected Result
The serial monitor reports the current state clearly. When a hand moves near the IR proximity sensor, the servo opens the lid, holds it open briefly, then closes it after the delay. The system ignores repeated triggering during the cycle gap.
Validation Checks
- Normal condition test: Confirm the lid stays closed when no hand is near the sensor.
- Detection test: Move a hand into the sensor range and confirm the lid opens once.
- Cycle test: Keep a hand near the sensor and confirm the lid does not jitter open and closed repeatedly.
- Calibration test: Compare the open and closed angles with the real lid mechanism.
- Limitation test: Try awkward hand positions and note where the sensor fails or triggers too easily.
Deployment and Limitations
- This prototype could be used in school or sanitation demonstrations with safe low-voltage parts.
- Before deployment, the lid mechanism, enclosure, wiring, and power supply need careful review.
- Actuator systems need maintenance planning because hinges, servo gears, and sensor alignment can drift over time.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| Servo twitches but does not move the lid | Angles or power supply are wrong | Calibrate the servo with the lid disconnected and confirm the external 5V supply is stable |
| Lid opens repeatedly | Sensor sees a constant detection or no cycle gap is applied | Narrow the sensor view and keep the cycle gap in place |
| Pico resets when the servo moves | Servo is drawing too much current from the wrong supply | Move the servo to a separate regulated 5V supply and keep grounds common |
| Lid closes too soon | Hold time is too short for the user | Increase HOLD_OPEN_MS after observing the disposal action |




