Odczytywanie stanu przycisku
Używanie digitalRead do sprawdzania, czy przycisk jest wciśnięty
Odczytywanie stanu przycisku to bezpłatna lekcja Arduino & IoT Academy na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Arduino & IoT Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. ✅
Często zadawane pytania
Czy lekcja „Odczytywanie stanu przycisku” jest bezpłatna?
Tak — pełny tekst „Odczytywanie stanu przycisku” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Arduino & IoT Academy, przejdź na CoddyKit PRO. Kurs Arduino & IoT Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Odczytywanie stanu przycisku”?
Używanie digitalRead do sprawdzania, czy przycisk jest wciśnięty Ćwiczysz Arduino & IoT Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Arduino & IoT Academy?
Nie wymagamy żadnego doświadczenia. Arduino & IoT Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.
Ile czasu zajmuje lekcja „Odczytywanie stanu przycisku”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Arduino & IoT Academy?
Tak. Każda lekcja Arduino & IoT Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- INPUT kontra INPUT_PULLUP
- Odczytywanie stanu przycisku
- if/else: dioda LED podąża za przyciskiem
- Podłączanie przycisku na płytce stykowej