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ão | Pino de sinal | digitalRead() |
|---|---|---|
| Solto (não premido) | Ligado a VCC via pull-up | HIGH (1) |
| Premido | Ligado a GND (curto) | LOW (0) |
INPUT_PULLUP do Arduino sem módulo e sem resistência externa.#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);
}
}#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;
}#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;
}