Project 3.00 Read Input

In this project, you'll learn how to read the state of a digital input pin on the Arduino, specifically from a button connected to pin 1, and then print that state to the Serial Monitor!

In this project you will be introduced to buttons. By the end of this project, you will know how to read the state of a button (on or off) and how to store that in a bool type variable.

There are two buttons on the MC Trainer we can use, SW1 and SW2. SW1 is on the left side of the board and SW2 is on the right side. The abbreviation “SW” stands for switch.

Project Code:

				
					////////////////////////////////////////////////////
// 3.00 - Read Input
byte SW1 = 1;

void setup() {
  Serial.begin(9600);
  pinMode(SW1, INPUT);
}

void loop() {
  bool buttonState = digitalRead(SW1); 
  Serial.print("The state of the button is: "); Serial.println(buttonState);
  delay(250);
}
////////////////////////////////////////////////////
				
			

*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.

SW1 is connected to pin 1 so we first create a variable to represent our button in code:

for-loops can be a foreign concept. For a video explanation please visit:

				
					byte SW1 = 1;
				
			

Next, in setup(), we initialize Serial so we can get feedback from our microcontroller:

				
					  Serial.begin(9600);
				
			

Then because we are trying to read something from the outside world (a button press), SW1 is an input.

				
					pinMode(SW1, INPUT);
				
			

If you think about a button, it is either pressed or not pressed, there is no in-between. In similar terms, it is HIGH or LOW, ON or OFF, TRUE or FALSE, 1 or 0. A bool type variable is strictly meant to hold values like this, 1 or 0. So to store our buttons current state we will use a bool type variable.

				
					bool buttonState 
				
			

The digitalRead function can be used to read the digital state of a pin. This function will return a 1 if the voltage is HIGH (the button is not pressed) and a 0 if the voltage is LOW (the button is pressed).

				
					bool buttonState = digitalRead(SW1);
				
			

Once read, the state of the button is printed out to the Serial port. Make sure to open it so you can see what is being printed!

				
					Serial.print("The state of the button is: "); Serial.println(buttonState);
				
			

Lastly, there is a delay so thousands of lines don’t print out at once and the loop() is ended.

				
					  delay(250);
}
				
			

Press SW1 and watch the button state change in the Serial monitor!

Now that we can read the button state, we can combine that with programmatic logic to make decisions on what to do when the button is pressed!

Learn More In Our Free Instructional Guidebook

This comprehensive guidebook for the 1st Maker Space Microcontroller Trainer provides a comprehensive introduction to the world of Arduino programming for beginners. It guides users through the foundational concepts of microcontrollers, detailing the unique features of the Arduino Leonardo-compatible MCU Trainer board. The manual offers a step-by-step journey from understanding the hardware components and the Arduino programming language to the vibrant global community of Arduino enthusiasts. It delves into the intricacies of each onboard circuit, explaining their functionalities and applications. With a focus on hands-on learning, the manual includes a series of coding exercises, tutorials in C/C++, and insights into the Arduino IDE.

More Projects

Developmental Project 1.00 Vote

In this project, vote using the OLED and buttons.

Developmental Project 2.00 Mind the Dog

The program demonstrates how embedded systems can track time without stopping the program. Instead of using delay(), which pauses execution, it uses the millis() timestamp technique to calculate elapsed time. This allows multiple tasks—timers, display updates, and LED indicators—to run simultaneously.