กลับไปหน้ารวมไฟล์
multiple-button-switches-easy-effective-polling-e827af-en.md

Hardware Hacking: Resistor Ladder Polling

An Arduino Uno only has 14 digital pins. If you want to build a specialized USB macro keyboard with 20 buttons, you are mathematically out of pins immediately. The Multiple Button Polling via Analog project uses a brilliant feat of electrical engineering—the Resistor Ladder—to compress dozens of physical inputs into a singular, analog mathematical decimal.

invisible_mess_glasses_relay_schema_1772681179521.png

The Physics of the Voltage Divider

Instead of giving every button its own digital wire, you daisy-chain them.

  1. You wire 5 buttons in a row. Between each button, you solder a 1k Ohm resistor.
  2. One end of the chain is connected to 5V, the other to Analog Pin A0.
  3. The Mechanical Trap:
    • If you press Button 1, the 5V power goes through exactly zero resistors. A0 reads 1023.
    • If you press Button 2, the 5V power must flow through one 1K resistor. The voltage drops. A0 reads 800.
    • If you press Button 5, the power flows through four 1K resistors. A0 reads 200.

The C++ Threshold Parsing

The Arduino is no longer checking HIGH or LOW. It acts as a digital voltmeter.

int btnValue = analogRead(A0);

if (btnValue > 1000) {
  Serial.println("Button 1 Pressed!");
} else if (btnValue > 750 && btnValue < 850) {
  Serial.println("Button 2 Pressed!");
} else if (btnValue > 150 && btnValue < 250) {
  Serial.println("Button 5 Pressed!");
}

Note: We never use btnValue == 800. Analog electrical resistance fluctuates wildly based on the temperature of the room or the tiny oil on your fingers. You must define generous "Threshold Windows" (between 750 and 850) to prevent ghost-presses!

Component Architecture

  • Arduino Uno/Nano/Mega.
  • Mass quantities of Tactile Push Buttons.
  • 10 to 20 identical Resistors (e.g., all 1k Ohms, or all 10k Ohms) to create uniform mathematical drops in the ladder tree.
  • A multi-meter (Critical for debugging why Button 4 is outputting 3.8 Volts instead of 4.1 Volts!).

ข้อมูล Frontmatter ดั้งเดิม

title: "Multiple Button Switches - Easy & Effective Polling"
description: "Input expansion! Evade hardware pin limitations by utilizing complex resistor ladder networks and analog-to-digital (ADC) mathematical voltage dividers to read 10 distinct buttons using only a single analog pin."
category: "Tools & Equipment"
difficulty: "Advanced"