0Pricing
Arduino & IoT Academy · Lesson

Reading a Button State

Use digitalRead to know if a button is pressed.

Reading a Button State is a free Arduino & IoT Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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. ✅

Frequently asked questions

Is the “Reading a Button State” lesson free?

Yes — the full text of “Reading a Button State” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading a Button State”?

Use digitalRead to know if a button is pressed. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Arduino & IoT Academy?

No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading a Button State” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Arduino & IoT Academy lesson?

Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. INPUT vs INPUT_PULLUP
  2. Reading a Button State
  3. if/else: LED Follows the Button
  4. Wire a Button on a Breadboard
← Back to Arduino & IoT Academy