Digital input pullup

De ArduWiki
Saltar a: navegación, buscar

Este ejemplo demuestra el uso de pinMode() con INPUT_PULLUP. Lee una entrada digital en el pin 2, prende el LED a bordo e imprime los resultados en el monitor de serie.

Circuito

Conecte un pulsador entre el pin digital 2 y GND.

Placa aplicable

Todas

Código

const byte pin = 2;
void setup() {
   Serial.begin(115200);
   pinMode(pin, INPUT_PULLUP);
   pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
   bool pulsador = digitalRead(pin);
   Serial.println(pulsador);
   if (pulsador == HIGH) {
      digitalWrite(LED_BUILTIN, LOW);
      Serial.println(""LED OFF);
   }else{
      digitalWrite(LED_BUILTIN, HIGH);
      Serial.println(""LED ON);
   }
}

Comentarios

  • Notese que gracias a la configuración del pin digital 2 (INPUT_PULLUP) este pin esta normalmente en HIGH y pasa a LOW cuando presionar pulsador.
  • if (pulsador == HIGH) es exactamente lo mismo que if (pulsador) o if (ditalRead(pin)).

Vea también


Referencias