UAT 204 Microcontrollers and Embedded Systems

Worksheet 3 Driving a servo and reading a button with interrupts

Lab 6 hAssesses CLO2

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

  1. Wire a button with the internal pull-up; poll it and count presses without debouncing
  2. Switch to an interrupt setting a volatile flag; debounce in the main loop
  3. Compare the counts from both methods
  4. Generate 1100, 1500 and 1900 µs pulses at the servo's documented frequency and measure them
  5. Connect the servo and command all three pulses
  6. Calculate PSC and ARR for the Pico 2 clock as in Example 1
  7. Repeat Example 3 with a different stall time and queue size; record missed, overflow and age

Record of results

Table 1 Press counts: polling against interrupt with debouncing
RunReal pressesCount by polling, no debounceCount by interrupt with debounce
Table 2 PWM pulses commanded against measured
Commanded pulse (µs)CCRMeasured width (µs)Measured period (ms)Servo horn position seen
Box 3 PSC and ARR calculation for the Pico 2 clock (step by step)
Table 4 Example 3 repeated with a new stall time and queue size
Main loop run times (ms)Queue sizemissed (latest value)overflow (queue)Largest age delivered (ms)

Questions

  1. 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?

  2. 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?

  3. 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?

  4. 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

CLO2 Program GPIO, timers and interrupts. (CLOs are a web-team draft; the course owner sets the final ones.)

Instructors: answers and teaching notes for this worksheet are in the teaching pack