Fiveable

💾Embedded Systems Design Unit 4 Review

QR code for Embedded Systems Design practice questions

4.2 Interfacing with LEDs, switches, and keypads

4.2 Interfacing with LEDs, switches, and keypads

Written by the Fiveable Content Team • Last updated August 2025
Written by the Fiveable Content Team • Last updated August 2025
💾Embedded Systems Design
Unit & Topic Study Guides

Interfacing with LEDs, switches, and keypads is how you build interactive embedded systems. These components let users send commands and receive visual feedback, forming the foundation of most device interfaces.

Getting the interfacing right matters for reliability and user experience. Current limiting for LEDs, debouncing for switches, and efficient scanning for keypads are all techniques you'll use repeatedly when designing systems that respond accurately to user input.

LED Interfacing

Current Limiting and Brightness Control

LEDs will burn out almost immediately if you connect them directly to a power supply without limiting the current. A current limiting resistor in series with the LED prevents this by capping how much current flows through the circuit.

To calculate the resistor value, use:

R=VCCVfIfR = \frac{V_{CC} - V_f}{I_f}

where VCCV_{CC} is the supply voltage, VfV_f is the LED's forward voltage (typically 1.8–3.3V depending on color), and IfI_f is the desired forward current (often around 10–20mA for standard LEDs). For example, with a 5V supply, a red LED (Vf=2.0VV_f = 2.0V), and a target current of 15mA:

R=5.02.00.015=200ΩR = \frac{5.0 - 2.0}{0.015} = 200\,\Omega

You'd round up to the nearest standard value, like 220Ω.

For brightness control, Pulse Width Modulation (PWM) is the standard approach. PWM rapidly switches the LED on and off at a fixed frequency, and you vary the duty cycle to adjust perceived brightness:

  • 0% duty cycle = LED fully off
  • 50% duty cycle = LED appears roughly half brightness
  • 100% duty cycle = LED fully on

The switching happens fast enough (typically hundreds of Hz or more) that your eye perceives a steady light rather than flickering.

LED Driving Techniques

  • Direct drive connects the LED to a microcontroller GPIO pin through a current limiting resistor. This works for low-current LEDs (under ~20mA) and simple on/off control, since most microcontroller pins can source or sink that much current.
  • Transistor switching places an NPN BJT or N-channel MOSFET between the LED and ground. The microcontroller drives the transistor's base or gate with a small signal, and the transistor switches the larger LED current. This is necessary for high-power LEDs that draw more current than a GPIO pin can handle.
  • LED driver ICs (such as the TLC5940) provide constant-current outputs across multiple channels. They handle brightness control internally and simplify wiring when you need to drive many LEDs with precise, uniform brightness.

Switch Interfacing

Current Limiting and Brightness Control, How to design the PWM circuitry

Switch Types and Characteristics

Momentary switches (pushbuttons) make contact only while you're pressing them and spring back to their default state on release. These are used for triggering events or toggling states in software.

Toggle switches stay in whatever position you move them to. They're used for selecting between fixed states like on/off or mode selection.

Both types come in two configurations:

  • Normally Open (NO): The circuit is open (disconnected) by default. Pressing or flipping the switch closes it.
  • Normally Closed (NC): The circuit is closed (connected) by default. Activating the switch opens it.

When connecting a switch to a microcontroller input, you need a pull-up or pull-down resistor to define the pin's voltage when the switch is open. Without one, the pin floats at an undefined voltage and gives unreliable readings. Many microcontrollers have internal pull-ups you can enable in software.

Switch Debouncing

Mechanical switches don't make clean contact. When you press a button, the metal contacts physically bounce against each other for a few milliseconds, producing a rapid series of on-off transitions instead of a single clean edge. This bouncing can cause the microcontroller to register dozens of false presses from a single button push.

Two main approaches fix this:

  • Hardware debouncing uses an RC low-pass filter (a resistor and capacitor) on the switch output to smooth the voltage transitions. A Schmitt trigger IC can be added after the RC filter to produce a clean digital edge.
  • Software debouncing reads the switch state, waits a short delay (typically 10–50ms), then reads it again. The state is only accepted as valid if it remains consistent across multiple samples. This is the more common approach since it costs nothing in hardware.

Here's a simple software debounce process:

  1. Detect an initial state change on the input pin.
  2. Wait a debounce delay (e.g., 20ms).
  3. Read the pin again.
  4. If the reading matches the initial change, accept it as a valid press. If not, discard it.

Keypad Interfacing

Current Limiting and Brightness Control, Analog Devices' LT3950 1.5 A multi-topology LED driver offers 20,000:1 range dimming of the LED ...

Keypad Matrix Configuration

A keypad arranges its keys in a matrix of rows and columns. Each key sits at the intersection of one row and one column. A standard 4×4 keypad has 16 keys but only needs 8 connections (4 rows + 4 columns) instead of 16 individual wires.

When a key is pressed, it electrically connects its row to its column. The microcontroller detects which key was pressed by driving one row at a time and checking which column line changes state.

Pull-up or pull-down resistors on the column lines ensure they read a known default value when no key is pressed.

Keypad Scanning Techniques

Polling (row scanning) works step by step:

  1. Set all row lines high (or low, depending on your active level).
  2. Drive the first row low while keeping the others high.
  3. Read all column lines. If any column reads low, the key at that row-column intersection is pressed.
  4. Drive the next row low and repeat.
  5. Cycle through all rows continuously.

Interrupt-based scanning reduces CPU overhead by letting the processor do other work until a key is actually pressed:

  1. Configure all row lines as outputs driven low.
  2. Configure column lines as inputs with interrupts enabled (triggered on a falling edge, for example).
  3. When any key is pressed, the corresponding column line goes low and fires an interrupt.
  4. Inside the interrupt handler, perform a row scan to identify the specific key.

Interrupt-based scanning is more efficient because the microcontroller doesn't waste cycles checking the keypad when nobody is pressing anything.

Multiplexing in Keypad Interfacing

The matrix arrangement itself is a form of multiplexing, since you're sharing pins across multiple keys rather than dedicating one pin per key. A 4×4 keypad uses 8 pins for 16 keys; a 4×3 phone-style keypad uses 7 pins for 12 keys.

The scanning process multiplexes in time: the microcontroller enables one row at a time and reads the columns, cycling through all rows fast enough that any key press is caught.

This pin-saving approach has trade-offs:

  • Ghosting can occur when multiple keys are pressed simultaneously. If three keys forming an "L" shape in the matrix are pressed, the fourth corner key appears to be pressed too, even though it isn't.
  • Masking happens when pressing one key prevents the detection of another.
  • Adding diodes in series with each key eliminates ghosting by preventing current from flowing backward through the matrix. This is called an anti-ghosting or blocking diode configuration.