• 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 » Arduino Blink LED With Pushbutton Control to Turn ON and Off

By Abhishek Ghosh July 18, 2018 2:43 pm Updated on July 18, 2018

Arduino Blink LED With Pushbutton Control to Turn ON and Off

Advertisement

We already know that we can blink one LED with Arduino and also we can blink one LED with 555 IC. We can also blink 2 LEDs alternatively with Arduino and also blink 2 LEDs alternatively with 555 IC. From our older examples, we can start LED to be on with one pushbutton press and turn-off with another pushbutton keypress. 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. It is not exactly easy to a beginner but mandatory next step to learn.

 

Arduino Blink LED With Pushbutton Control to Turn ON and Off

 

Wiring/circuit diagram of this project is very easy. One digital pin of Arduino will be connected to LED, LED’s another leg will be connected to GND of Arduino. Alternatively, the onboard LED on Arduino board can be used. Connection for pushbutton is easy too – one digital pin of Arduino will be connected to pushbutton, pushbutton’s another leg will be connected to GND of Arduino with a resistor with value like 1K Ohm to ensure not much current going back to Arduino board.

Arduino Blink LED With Pushbutton Control to Turn ON and Off

Very basic code will be :

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
const int kPinBtn = 2;
const int kPinLed = 13;
 
boolean kPress = false;
 
void setup()
{
  pinMode(kPinBtn, INPUT_PULLUP);
  pinMode(kPinLed, OUTPUT);
}
 
void loop()
{
  if(digitalRead(kPinBtn) == LOW && kPress == false)
  {
    digitalWrite(kPinLed, HIGH);
    kPress = true;
  }
  if(digitalRead(kPinBtn) == LOW && kPress == true)
  {
    digitalWrite(kPinLed, LOW);
    kPress = false;
  }  
}

You will notice the odd with a delay. If we want to achieve is to have a button alternate between blink and off smoothly, that can be done in this way:

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
enum { LED=13, ButtonPin=2, BounceMS=50, BlinkMS=256 };
unsigned long buttonAt;
bool blinky, bouncy, buttonState;
 
void setup() {
  pinMode(LED, OUTPUT);
  pinMode(ButtonPin, INPUT_PULLUP);
  blinky = bouncy = buttonState = false;
  buttonAt = millis();
}
 
void loop() {
  // Detect button changes
  if (bouncy) {
    if (millis() - buttonAt > BounceMS)
      bouncy = false;       // End of debounce period
  } else {
    if (digitalRead(ButtonPin) != buttonState) {
      buttonState = digitalRead(ButtonPin);
      buttonAt = millis();
      bouncy = true;        // Start debounce period
      if (buttonState) {    // Was button just pressed?
        blinky = !blinky;   // Toggle blink-state
      }
    }
  }
  // Control light-blinking
  if (blinky) {
    // Turn LED on for BlinkMS ms, then off for same
    digitalWrite(LED, (millis() - buttonAt)%(2*BlinkMS) < BlinkMS);
  } else {
    digitalWrite(LED, LOW);
  }
}

We can not directly use easy logic to start and stop. That will leads to a very long time between successive checks of the button as there is a delay between cycles! That invites some other functions to use, like using the millis function. Notice some changes in connection in this diagram :

Arduino Blink LED With Pushbutton Control

Another code with millis :

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* Pushbutton-controlled blinking LED
* David A. Mellis
* http://arduino.berlios.de
*/
int ledPin = 13;                // LED connected to digital pin 13
int ledValue = LOW;             // previous value of the LED
long ledStartTime = 0;          // will store last time LED was updated
long ledBlinkInterval = 1000;   // interval at which to blink (milliseconds)
 
int buttonPin = 2;              // an active-high momentary pushbutton on pin 2
int buttonValue = HIGH;         // the current state of the button
long buttonPressTime = 0;       // will store the time that the button was pressed
 
 
void setup()
{
pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}
 
void loop()
{
// Check to see if the button is pressed.  If so, mark the time.  When the
// button is released, calculate how long it was pressed, and make the led
// blink at the same rate.
buttonValue = digitalRead(buttonPin);
 
// button press
if (buttonValue==LOW && buttonPressTime==0) {
   buttonPressTime = millis();  // capture the time of the press
}
 
// button release
if (buttonValue==HIGH && buttonPressTime!=0) {
   ledBlinkInterval = millis() - buttonPressTime;  // set the new flash interval
   buttonPressTime = 0;                            // clear the button press time
}
 
// check to see if it's time to blink the LED; that is, is the difference
// between the current time and last time we blinked the LED bigger than
// the interval at which we want to blink the LED.
if (millis() - ledStartTime > ledBlinkInterval) {
   ledStartTime = millis();   // remember the last time we blinked the LED
 
   // if the LED is off turn it on and vice-versa.
   if (ledValue == LOW)
     ledValue = HIGH;
   else
     ledValue = LOW;
 
   digitalWrite(ledPin, ledValue);
}
}

We can use timer library :

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Timer.h"
 
// Pin 13 has a LED connected on most Arduino boards.
// give it a name:
const int led = 13;
const int buttonPin = 2;
 
// declare state variables as global
bool buttonState = LOW;
bool buttonState_prev = LOW;
bool toggleBlinking;
bool blinkState;
 
// declare a Timer object so blinking can be done without loosing button presses
Timer timer_b;
int blink_id;
 
// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP);
 
  // initial state: toggle led each 1000 ms
  blink_id = timer_b.oscillate(led, 1000, HIGH);
  blinkState = true;
  toggleBlinking = false;
}
 
// the loop routine runs over and over again forever:
void loop() {
  // Update the timer (required)
  timer_b.update();
 
  // check if button has been pressed (HIGH to LOW),
  // then debounce it and raise toggle flag
  buttonState = digitalRead(buttonPin);
  if ((buttonState != buttonState_prev) && (buttonState_prev == HIGH)) {
    // simple button debounce (confirm press after some ms)
    delay (50);
    buttonState = digitalRead(buttonPin);
    if (buttonState != buttonState_prev) toggleBlinking = true;
  }
 
  // keep current LED state unless the button has been pushed
  switch (blinkState) {
    case true:
      // if button has been pushed, stop blinking and change LED state
      if (toggleBlinking == true) {
        timer_b.stop (blink_id);
        blinkState = false;
      }
      break;
    case false:
      digitalWrite(led, LOW);
      // if button has been pushed, start blinking and change LED state
      if (toggleBlinking == true) {
        blink_id = timer_b.oscillate(led, 1000, HIGH);
        blinkState = true;
      }
      break;
  }
 
  buttonState_prev = buttonState;
  toggleBlinking = false;
}

This ends this tutorial.

Tagged With arduino led on off button code , arduino blink , arduino led blink with button , arduino led blink , arduino with LED and push button , arduino blink with button , arduino keypress , arduino control led with pushbutton , arduino pushbutton led , светодиод управляемый кнопкой arduino
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 Arduino Blink LED With Pushbutton Control to Turn ON and Off

  • Arduino and LED Bar Display : Circuit Diagram, Code

    Here is a Guide Explaining the Basics, Circuit Diagram, Code on Arduino and LED Bar Display. LED Bar Display is Actually Like Multiple LED.

  • Arduino 3 LED and One Push Button

    We can control more than 2 LEDs with one push button. Here is Arduino Project on How to Control 3 LED With One Push Button.

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

  • Control Multiple AC Appliances With One ESP32 Arduino

    Here is how to use ESP32 and IBM Watson IoT to control multiple relays (i.e. multiple AC appliances) by pushbutton and over the internet.

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…

 

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

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

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

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