UAT 204 Microcontrollers and Embedded Systems
Worksheet 3 Driving a servo and reading a button with interrupts
Objective
Compare polling with interrupts, generate accurate PWM pulses with a timer and drive a servo safely
Equipment and materials
- Pico 2 board
- Push button and breadboard
- Servo and a separate servo power supply
- Oscilloscope or logic analyzer
- Documentation for the servo used
Safety
- Power the servo from a separate supply, never the board's 3.3 V pin, with a common ground
- Clamp the servo and keep fingers clear of the moving horn
Steps
- Wire a button with the internal pull-up; poll it and count presses without debouncing
- Switch to an interrupt setting a volatile flag; debounce in the main loop
- Compare the counts from both methods
- Generate 1100, 1500 and 1900 µs pulses at the servo's documented frequency and measure them
- Connect the servo and command all three pulses
- Calculate PSC and ARR for the Pico 2 clock as in Example 1
- Repeat Example 3 with a different stall time and queue size; record missed, overflow and age
Record of results
| Run | Real presses | Count by polling, no debounce | Count by interrupt with debounce |
|---|---|---|---|
| Commanded pulse (µs) | CCR | Measured width (µs) | Measured period (ms) | Servo horn position seen |
|---|---|---|---|---|
| Main loop run times (ms) | Queue size | missed (latest value) | overflow (queue) | Largest age delivered (ms) |
|---|---|---|---|---|
Questions
In Table 1, why does polling without debouncing count more presses than were made, and is the 5 ms debounce of Example 2 right for your button? What result shows it?
How far did the measured pulse widths in Table 2 differ from the commanded values, and with the timer counting in 1 µs steps, why does CCR equal the pulse width in microseconds?
Why must the servo have its own supply with a common ground? What would happen if it drew power from the board's 3.3 V pin?
From Table 4, does a bigger queue fix a stalled main loop? Which policy suits a controller and which suits a data logger?
Check before you hand in
What to hand in
- Press counts for polling and interrupts
- Captures of the three pulses
- PSC and ARR calculation
- Source code
- Table of the repeated Example 3 results