Leggere lo stato di un pulsante
Usare digitalRead per sapere se un pulsante è premuto
Leggere lo stato di un pulsante è una lezione Arduino & IoT Academy gratuita su CoddyKit. Questa è la lezione 2 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.
Ask the Pin a Question
Now that a pin is listening, you can ask it what it sees. The function digitalRead checks a pin and tells you its current state.
It Returns HIGH or LOW
digitalRead gives back just two answers: HIGH or LOW. That is perfect for a button, which is only ever pressed or not.
int state = digitalRead(2);Store the Reading
Save the result in a variable so you can use it. Here state holds whatever digitalRead found on pin 2 at that moment.
int buttonPin = 2;
int state = digitalRead(buttonPin);Set the Mode First
Reading only works if you prepared the pin. Call pinMode with INPUT_PULLUP in setup() before you ever read it.
void setup() {
pinMode(2, INPUT_PULLUP);
}Read Inside loop()
Buttons change all the time, so you read inside loop(). Each pass grabs a fresh state, keeping your device up to date.
void loop() {
int state = digitalRead(2);
}Remember the Inversion
With INPUT_PULLUP the meaning flips: LOW means the button is pressed, and HIGH means it is released. Keep that in mind reading state.
See It on Serial
You can't see HIGH or LOW directly, so print it. Sending state to the Serial Monitor lets you watch the value as you press.
Serial.println(state);Open the Channel
Before printing, open the USB channel once in setup with Serial.begin. A common speed is 9600 baud for simple projects.
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
}Watch the Values Change
Upload, open the Serial Monitor, and press the button. You'll see the number jump from 1 to 0 the instant you push it. 🔘
HIGH and LOW Are Numbers
Under the hood HIGH is just 1 and LOW is 0. That is why your printed readings show up as plain 1s and 0s.
You Have Live Input
You now turn a physical press into a value your code can test. That single reading is the seed of every interactive device you'll build.
Quick Check
Recall what digitalRead hands back to your code.
Recap
You used digitalRead in loop() to capture HIGH or LOW, stored it, and printed it over Serial. Next you'll make an LED react to that state. ✅
Domande Frequenti
La lezione «Leggere lo stato di un pulsante» è gratuita?
Sì — il testo completo di «Leggere lo stato di un pulsante» è 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 «Leggere lo stato di un pulsante»?
Usare digitalRead per sapere se un pulsante è premuto 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 2 di 4.
Quanto tempo richiede la lezione «Leggere lo stato di un pulsante»?
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