• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Blink an LED Only Twice After a Button Press Using Arduino

By Abhishek Ghosh March 25, 2024 5:27 am Updated on March 25, 2024

How to Blink an LED Only Twice After a Button Press Using Arduino

Advertisement

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

 

  1. Arduino board (e.g., Arduino Uno)
  2. LED
  3. 220-ohm resistor
  4. Push button
  5. Breadboard
  6. Jumper wires

 

Circuit Diagram

 

How to Blink an LED Only Twice After a Button Press Using Arduino

Here is the simulation on TinkerCAD:

 

Required Code

 

Here is the required code:

Advertisement

---

Vim
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:

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

Vim
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:

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

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

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

Vim
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:

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

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Blink an LED Only Twice After a Button Press Using Arduino

  • Arduino Blink LED With Pushbutton Control to Turn ON and Off

    Arduino Blink LED With Pushbutton Control to Turn ON and Off is Few Steps Higher Than Basic Example. There is Matter of Repeat Checking by Microcontroller.

  • Arduino Blink LED Rate Depending On Push Button Press Duration

    We Can Use millis() To Perform Many Things. Here is Arduino Blink LED Rate Depending On Push Button Press Duration Guide With Circuit and Code.

  • Arduino : One Push Button Multiple Functions (Single Press, Double Press, Long-Time Press)

    With Arduino, We Can Have One Push Button Multiple Functions Like Single Press, Double Press, Long-Time Press. There is Nice Library For Faster Prototyping.

  • How to Write Arduino Library of Your Own

    Library of Arduino is Set of Instructions to Avoid Repeated Huge Coding. Here is Guide on How to Write Arduino Library of Your Own With Example.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

vpsdime

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Cloud-Powered Play: How Streaming Tech is Reshaping Online GamesSeptember 3, 2025
  • How to Use Transcribed Texts for MarketingAugust 14, 2025
  • nRF7002 DK vs ESP32 – A Technical Comparison for Wireless IoT DesignJune 18, 2025
  • Principles of Non-Invasive Blood Glucose Measurement By Near Infrared (NIR)June 11, 2025
  • Continuous Non-Invasive Blood Glucose Measurements: Present Situation (May 2025)May 23, 2025
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2026 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy