Project 3.01 Blink an LED with a button

In this project, you'll learn how to control an LED with a button!
In the last project we learned how to read the state of a button. In this project we will learn how to make a decision based on the buttons state that we read. The goal of this project is to turn on an LED when the button is pressed!

Project Code:

				
					///////////////////////////////////////////////////////
// 3.01 - Blink an LED with button press

byte SW1 = 1;
byte LED1 = 13;
bool pressed = 0; 

void setup() {
  pinMode(LED1, OUTPUT);
  pinMode(SW1, INPUT);
}

void loop() {
  bool buttonState = digitalRead(SW1);

  //digitalWrite(LED1, !buttonState); 

  if (buttonState == pressed) {
    digitalWrite(LED1, HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
  }
}
///////////////////////////////////////////////////////
				
			

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

The pressed variable makes the code easier to read. A good programmer makes their code easier for others and themselves to read. By adding the pressed variable, it is easy to see when is going on in the code.

				
					bool pressed = 0; 
				
			

Here we are using an if-statement to make a decision. If the button is pressed, turn the LED on. Else, turn the LED off. The “==” is used to make a comparison to see if something is equal. Using the “=” operator will assign a value to the variable. This is a very common mistake in programming!

				
					if (buttonState == pressed) {
   digitalWrite(LED1, HIGH);
 }
else {
   digitalWrite(LED1, LOW);
}
				
			

Like most things in programming, there is more than one way to do it! Below is a “one liner” that can be used to do the same thing. It digitalWrites the opposite of what was read from the digitalRead function. “!” is the NOT operator. When applied, this operator will turn a 1 into a 0 and a 0 into a 1. This inversion is because the button reads a 0 when pressed and a 1 when not pressed. So, when the button is pressed LED1 is written a 1, and when the button is not pressed LED1 is written a 0 with the inversion.

				
					digitalWrite(LED1, !buttonState);
				
			

Try to make a different LED turn on with SW2!

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

Project 1.00 Blink

In this project, you’ll learn how to blink an LED!

Project 1.01 Blink x2

In this project, you’ll learn how to blink more than one LED!

Bri Wagner

Education Solution Specialist

Bri Wagner brings a bachelor’s degree in Elementary Education and nine years of classroom teaching experience to her role at 1st Maker Space. A former Teacher of the Year Award recipient, Bri is passionate about creating engaging, hands-on learning experiences that inspire student success. She understands the impact that innovative instruction and meaningful classroom resources can have on student achievement and is dedicated to partnering with educators to bring high-quality STEM solutions into classrooms. Bri is committed to empowering teachers and helping students develop the creativity, confidence, and problem-solving skills they need to thrive.