Lesson plan

Buttons, encoders, keypads and touch lesson plan

Read people properly: button bounce on a scope and debouncing, clicks, double clicks and long presses, rotary encoders, matrix keypads, MPR121 touch and joysticks, then a retro game menu with a high score entry screen.

4 chapters, 7 lessons, about 6 hours, on the Arduino Uno R3. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Bounce, debounce, and gestures from one button.
  • Rotary encoders and matrix keypads.
  • Capacitive pads and analog joysticks.
  • A retro game menu with a high score entry screen.

Materials for the whole unit

  • Arduino Uno R3
  • Capacitive touch, MPR121
  • Character LCD with I2C backpack
  • Full-size breadboard
  • Half breadboard
  • Keypad, 4x4
  • LED x 8
  • Oscilloscope
  • Piezo speaker
  • Pushbutton
  • Relay module
  • Resistor x 8
  • RGB LED
  • Rotary encoder, KY-040
  • Thumb stick

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 40 min

Buttons

Bounce, debounce, and gestures from one button.

LessonTimeWhat students do
Bounce on the scope, and debounceOne press, many edges: a scope on a bouncing pushbutton, an interrupt that counts every edge, and a debounce that waits for the contacts to settle before believing them.50 min
  1. Run it: count the edges
  2. See it on the scope
  3. Worse buttons
  4. Debounce in code
  5. Turn the debounce off
  6. Choosing a debounce
Long press and double clickThree gestures from one button: a state machine that waits a moment to tell a click from a double click, and reports a long press while the button is still held.50 min
  1. Run it: one button, three gestures
  2. Why the click is late
  3. Hold it
  4. A state machine
  5. A quicker hold
  6. Gestures in real devices

Materials

  • Arduino Uno R3
  • Half breadboard
  • Oscilloscope
  • Pushbutton

Check for understanding

Each student finishes "Long press and double click" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Gestures in real devices".

Chapter 2. About 1 h 40 min

Knobs and keys

Rotary encoders and matrix keypads.

LessonTimeWhat students do
Rotary encodersA knob that never ends: two contacts a quarter cycle apart, a state table that turns them into steps and a direction, interrupts so no step is missed, and bounce that the table shrugs off.50 min
  1. Run it: turn the knob
  2. Two square waves
  3. Bounce, shrugged off
  4. Interrupts catch every step
  5. The push switch
  6. Coarser steps
Matrix keypadsSixteen keys on eight pins: scanning rows and columns, pressed, held and released events from the Keypad library, two keys at once, and a door code typed into a buffer.50 min
  1. Run it: a door keypad
  2. Scanning
  3. Events: pressed, held, released
  4. Two keys at once
  5. Your own code
  6. Keypads in real devices

Materials

  • Arduino Uno R3
  • Full-size breadboard
  • Half breadboard
  • Keypad, 4x4
  • LED
  • Relay module
  • Resistor x 4
  • RGB LED
  • Rotary encoder, KY-040

Check for understanding

Each student finishes "Matrix keypads" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Keypads in real devices".

Chapter 3. About 1 h 40 min

Touch and sticks

Capacitive pads and analog joysticks.

LessonTimeWhat students do
MPR121 capacitive touchTwelve touch pads on one I2C chip: capacitance that drops when a finger comes near, a baseline that tracks it, thresholds that decide a touch, and a note for each pad.50 min
  1. Run it: touch pads
  2. A finger changes the capacitance
  3. A bitmask
  4. Released
  5. Less sensitive
  6. Touch in real devices
JoysticksTwo potentiometers and a button that should feel right: centering at start-up, a dead zone, -100 to 100 per axis, direction events that repeat while held, and a fire button.50 min
  1. Run it: calibrate the middle
  2. A dead zone
  3. Push it right
  4. Fire
  5. A bigger dead zone
  6. Sticks in real devices

Materials

  • Arduino Uno R3
  • Capacitive touch, MPR121
  • Full-size breadboard
  • Half breadboard
  • LED x 8
  • Piezo speaker
  • Resistor x 8
  • Thumb stick

Check for understanding

Each student finishes "Joysticks" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Sticks in real devices".

Chapter 4. About 1 h

Project

A retro game menu with a high score entry screen.

LessonTimeWhat students do
Project: a retro game menuA Star Blaster arcade menu on a 16x2 LCD and a rotary encoder: scroll and press, a sound toggle, a high score table in EEPROM, and initials dialed in letter by letter.60 min
  1. Run it: Star Blaster
  2. Scroll
  3. The high score table
  4. Play
  5. Dial your initials
  6. A better game
  7. Make it your menu

Materials

  • Arduino Uno R3
  • Character LCD with I2C backpack
  • Half breadboard
  • LED
  • Resistor
  • Rotary encoder, KY-040

Check for understanding

Each student finishes "Project: a retro game menu" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Make it your menu".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.