Início›Módulos›Botão de Pressão
⬤ Iniciante · Grupo 5 — Toque, Inclinação e Pressão

🔘 Botão de Pressão

⏱️ 25 min 🧩 Módulo botão · Digital · pull-up · Debounce 📦 Módulo #25

Nesta página

🔬 Sobre o Módulo Botão de Pressão

O módulo botão contém um micro-interruptor de pressão momentâneo (normalmente aberto, NA). Quando premido, liga os dois terminais. O módulo inclui resistência pull-up — liga directamente a VCC, GND e sinal digital.

Estado do botãoPino de sinaldigitalRead()
Solto (não premido)Ligado a VCC via pull-upHIGH (1)
PremidoLigado a GND (curto)LOW (0)
ℹ️
O módulo tem pull-up incorporado — lê HIGH quando solto e LOW quando premido. Isto é lógica invertida em relação ao que parece intuitivo. Podes também usar INPUT_PULLUP do Arduino sem módulo e sem resistência externa.

🔌 Pinagem — Módulo Botão

VCCAlimentação → 5V do Arduino
GNDMassa → GND do Arduino
SSinal → pino digital D2 (ou qualquer digital)

⬆️ Pull-up Interno do Arduino

ex_botao_pullup.ino
#define BTN_PIN 2
#define LED_PIN 13

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP); // Pull-up interno — sem R externa
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int estado = digitalRead(BTN_PIN);

  // LOW = premido (lógica invertida com pull-up)
  if (estado == LOW) {
    digitalWrite(LED_PIN, HIGH);
    Serial.println("Botão: PREMIDO");
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

🔄 Debounce por Software

⚠️
Os contactos metálicos de um botão vibram mecanicamente ao fechar — produzem dezenas de pulsos em poucos milissegundos (bounce). Sem debounce, um único toque pode ser interpretado como múltiplos eventos.
ex_botao_debounce.ino
#define BTN_PIN        2
#define DEBOUNCE_MS    50

bool          lastState     = HIGH;
bool          stableState   = HIGH;
unsigned long lastChange    = 0;

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  bool reading = digitalRead(BTN_PIN);

  if (reading != lastState) {
    lastChange = millis();       // Reinicia o temporizador
  }

  if ((millis() - lastChange) > DEBOUNCE_MS) {
    if (reading != stableState) {
      stableState = reading;
      if (stableState == LOW) {
        Serial.println("Evento: PRESSÃO detetada");
      }
    }
  }
  lastState = reading;
}

🔁 Toggle — Alternar Estado do LED

ex_botao_toggle.ino
#define BTN_PIN     2
#define LED_PIN     13
#define DEBOUNCE_MS 50

bool ledState    = false;
bool lastReading = HIGH;
unsigned long lastDebounce = 0;

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  bool reading = digitalRead(BTN_PIN);

  if (reading != lastReading) lastDebounce = millis();

  if ((millis() - lastDebounce) > DEBOUNCE_MS) {
    static bool lastStable = HIGH;
    if (reading != lastStable) {
      lastStable = reading;
      if (reading == LOW) {        // Flanco de descida = pressão
        ledState = !ledState;
        digitalWrite(LED_PIN, ledState);
      }
    }
  }
  lastReading = reading;
}

🏆 Para ir mais longe

Kit B 37 · PR.FT6_15 · Arduino UNO · Raspberry Pi · Início · led.dge.medu.pt