Blinking an LED is one of the most basic projects for Arduino beginners. Previously, we have demonstrated how to Blink LED and Beep Every X Seconds Upon Push Button Press. However, adding a condition to blink the LED only a certain number of times after a button press introduces a new level of complexity. In this tutorial, we’ll explore how to achieve this functionality using an Arduino board, an LED, and a push button.
Materials Needed for This Project
- Arduino board (e.g., Arduino Uno)
- LED
- 220-ohm resistor
- Push button
- Breadboard
- Jumper wires
Circuit Diagram

Here is the simulation on TinkerCAD:
Required Code
Here is the required code:
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | // Define constants for LED pin, button pin, and number of blinks const int ledPin = 13; const int buttonPin = 2; const int numBlinks = 2; // Variable to track the number of blinks int blinkCount = 0; void setup() { // Initialize LED pin as an output pinMode(ledPin, OUTPUT); // Initialize button pin as an input with pull-up resistor pinMode(buttonPin, INPUT_PULLUP); } void loop() { // Check if the button is pressed if (digitalRead(buttonPin) == LOW) { // Blink the LED the specified number of times for (int i = 0; i < numBlinks; i++) { digitalWrite(ledPin, HIGH); // Turn on the LED delay(500); // Wait for 500 milliseconds digitalWrite(ledPin, LOW); // Turn off the LED delay(500); // Wait for 500 milliseconds } // Increment the blink count blinkCount++; // Check if the desired number of blinks has been reached if (blinkCount >= numBlinks) { // Reset the blink count blinkCount = 0; // Add additional logic here if needed } } } |
In the above code, this snippet is the main loop of an Arduino sketch, which continuously runs as long as the Arduino board is powered on. Let’s break down each part of the loop function:
1 | void loop() { |
The loop function is a special function in Arduino sketches that runs repeatedly after the setup function has completed. Any code inside this function will execute in a continuous loop until the Arduino is powered off or reset.
1 2 | // Check if the button is pressed if (digitalRead(buttonPin) == LOW) { |
This line of code checks the state of a push button connected to a specific pin (buttonPin) configured as an input. The digitalRead function reads the state of the pin, and if it returns LOW, it means the button is pressed. This condition is used to determine when to execute the code inside the if statement:
1 2 | // Blink the LED the specified number of times for (int i = 0; i < numBlinks; i++) { |
Inside the if statement, there is a for loop that iterates a certain number of times, specified by the variable numBlinks. This loop controls the blinking of an LED connected to another pin (ledPin). During each iteration of the loop, the LED is turned on for 500 milliseconds (using digitalWrite(ledPin, HIGH)) and then turned off for another 500 milliseconds (using digitalWrite(ledPin, LOW)). This creates the effect of blinking the LED.
1 2 | // Increment the blink count blinkCount++; |
After the LED blinks the specified number of times, the blinkCount variable is incremented. This variable keeps track of the number of times the LED has blinked.
1 2 | // Check if the desired number of blinks has been reached if (blinkCount >= numBlinks) { |
This if statement checks if the number of blinks (stored in blinkCount) has reached or exceeded the desired number of blinks (stored in numBlinks). If so, it means that the LED has blinked the desired number of times.
1 2 | // Reset the blink count blinkCount = 0; |
This comment indicates that additional logic can be added here if needed. For example, you could add code to perform other actions or tasks after the LED has finished blinking:
1 2 | // Add additional logic here if needed } |
Overall, this loop function checks if a button is pressed, blinks an LED a specified number of times when the button is pressed, and resets the blink count after the desired number of blinks.