INPUT e INPUT_PULLUP
Capire perché un pin flottante fornisce letture errate e come risolvere il problema con i pull-up
INPUT e INPUT_PULLUP è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Arduino & IoT Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Pins Can Listen Too
So far your pins pushed signals out. Now you'll make a pin listen instead, so your board can sense a button and react to it.
Set a Pin as INPUT
To read a button, you first tell a pin it should receive a signal. You do that by setting its mode to INPUT inside setup().
void setup() {
pinMode(2, INPUT);
}A Floating Pin Lies
An INPUT pin with nothing pulling it is called floating. It picks up stray noise and flickers between HIGH and LOW all on its own.
Why Floating Hurts
A floating pin gives you random, false button readings. Your code thinks the button is pressed when nobody touched it. ⚡
Pull Resistors Fix It
A pull resistor gently ties the pin to a known voltage. That gives it a steady default reading until the button changes it.
Pull-Up vs Pull-Down
A pull-up holds the pin HIGH by default; a pull-down holds it LOW. The Arduino has handy pull-ups built right into the chip.
Use INPUT_PULLUP
Skip the extra resistor: set the mode to INPUT_PULLUP and the board enables its internal pull-up for you automatically.
void setup() {
pinMode(2, INPUT_PULLUP);
}The Logic Flips
With INPUT_PULLUP the pin reads HIGH when the button is released, and LOW when it is pressed. The logic is inverted on purpose.
Wire to Ground
Because the internal pull-up holds the pin HIGH, you wire the button between the pin and GND. Pressing it pulls the pin down to LOW.
Less Wiring, Fewer Bugs
INPUT_PULLUP means one less resistor on your breadboard and no floating pin. For most beginner buttons it is simply the easiest choice.
Pick the Right Mode
Reach for plain INPUT only when your circuit already supplies its own pull resistor. Otherwise INPUT_PULLUP keeps things safe and simple.
Quick Check
Think about how INPUT_PULLUP behaves on an idle button.
Recap
You learned that an INPUT pin can float and lie, and that INPUT_PULLUP fixes it: HIGH when released, LOW when pressed. Next you'll actually read that value. 👍
Domande Frequenti
La lezione «INPUT e INPUT_PULLUP» è gratuita?
Sì — il testo completo di «INPUT e INPUT_PULLUP» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Arduino & IoT Academy, passa a CoddyKit PRO. Il corso Arduino & IoT Academy include 4 lezioni in totale.
Cosa imparerò in «INPUT e INPUT_PULLUP»?
Capire perché un pin flottante fornisce letture errate e come risolvere il problema con i pull-up Eserciti Arduino & IoT Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Arduino & IoT Academy?
Non è richiesta alcuna esperienza precedente. Arduino & IoT Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «INPUT e INPUT_PULLUP»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Arduino & IoT Academy?
Sì. Ogni lezione Arduino & IoT Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- INPUT e INPUT_PULLUP
- Leggere lo stato di un pulsante
- if/else: il LED segue il pulsante
- Collegare un pulsante su una breadboard