• 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 Increment Decrement 3 LEDs With 2 Pushbuttons

By Abhishek Ghosh June 22, 2024 10:30 am Updated on June 22, 2024

Arduino Increment Decrement 3 LEDs With 2 Pushbuttons

Advertisement

This setup allows you to incrementally and decrementally control which LED is lit using the two push buttons connected to your Arduino. Also, there is a buzzer. Adjust pin numbers and delays as per your actual circuit configuration and desired performance.

This circuit is designed for children. Debounce is done using millis(), no button interrupt has been used. If you are adult, and relatively new to these terminologies then you can read the below detailed articles and modify the code. In this case, using Smiddit Trigger to debounce in circuit and using interrupts for pushbuttons probably best idea.

  • What is Interrupt?
  • How Interrupt Works
  • Understanding Arduino Interrupts
  • Read a Pushbutton with Arduino with Interrupts and Debounce

 

Hardware Setup

 

Components Needed:

Advertisement

---

Arduino board (e.g., Arduino Uno)
3 LEDs (any color)
3 current-limiting resistors (typically 220 ohms)
2 push buttons
1 Buzzer
Breadboard
Jumper wires
Pull-down resistors (optional but recommended, typically 10k ohms)

Circuit Diagram:

Connect the anode (longer leg, positive) of each LED to Arduino digital pins. For example:

LED 1: Digital Pin 8
LED 2: Digital Pin 10
LED 3: Digital Pin 12

Connect the cathode (shorter leg, negative) of each LED through a current-limiting resistor (220 ohms) to the ground (GND) rail on the breadboard.

Connect one terminal of each push button to separate digital input pins on the Arduino. For example:

Push Button 1: Digital Pin 6
Push Button 2: Digital Pin 5

Connect the other terminal of each push button to the ground (GND) rail on the breadboard. It is recommended to use pull-down resistors (10k ohms) between each push button pin and ground to ensure reliable readings when the buttons are not pressed.

Add the buzzer. Connect one terminal of buzzer to separate digital input pins on the Arduino, for example Digital Pin 4, add one current-limiting resistor between the connection. Connect another pole to the ground (GND) rail on the breadboard.

This will be the connection:

Arduino Increment Decrement 3 LEDs With 2 Push Button

 

Software Setup

 

This is the required code/sketch:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// constants won't change. They're used here to
// set pin numbers:
const byte buttonPin1  = 6;     // the number of the pushbutton pin
const byte buttonPin2  = 5;
const byte ResetPin    = 3;
 
const byte blockingLED = 13;
 
const byte ledRED      = 12;
const byte ledBLUE     = 10;
const byte ledGREEN    = 8 ;   // the number of the LED pin
 
const byte buzzer      = 4;
 
int index  = 2;                // to indicate which led should be turned on
int index2 = 0;
 
// variables will change:
// variable for reading the pushbutton status
 
byte buttonState1     = 0;
byte lastButtonState1 = 0;
 
byte buttonState2     = 0;
byte lastButtonState2 = 0;
 
byte buttonStateReset = 0;
byte lastResetState   = 0;
 
unsigned long switchMillis;
unsigned long blockingMillis;
 
//*********************************************************************
void setup()
{
  pinMode(blockingLED, OUTPUT);
 
  pinMode(ledRED,   OUTPUT);  // initialize the LEDRED pin as an output
  digitalWrite(ledRED, HIGH); // On startup led 1, on
  
  pinMode(ledBLUE,  OUTPUT);
  digitalWrite(ledBLUE, LOW);
  
  pinMode(ledGREEN, OUTPUT);
  digitalWrite(ledGREEN, LOW);
 
  pinMode(buzzer,   OUTPUT);  // Set buzzer - pin 4 as an output
 
  pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
  lastButtonState1 = digitalRead(buttonPin1);
 
  pinMode(buttonPin2, INPUT);
  lastButtonState2 = digitalRead(buttonPin2);
 
  pinMode(ResetPin,   INPUT);
  lastResetState = digitalRead(ResetPin);
 
  noTone(buzzer);             // Stop sound...
 
}//closing the void setup()
 
//*********************************************************************
void loop()
{
  //***********************************
  //Check to see is there is blocking code, blockingLED will normally flash a 1Hz
  if (millis() - blockingMillis >= 500)
  {
    blockingMillis = millis();
 
    digitalWrite(blockingLED, !digitalRead(blockingLED));
  }
 
  //***********************************
  //for debounce, check switches every 50 milli seconds
  if (millis() - switchMillis >= 50)
  {
    switchMillis = millis(); // reset timing
 
    //check the switches
    checkSwitches();
  }
 
  //***********************************
  //non blocking code goes here
  //***********************************
 
}//closing the void loop()
 
//*********************************************************************
void checkSwitches()
{
  //***********************************
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
 
  if (lastButtonState1 != buttonState1)
  {
    lastButtonState1 = buttonState1; //update to the new state
 
    // if it is, the buttonState is HIGH/pressed:
    if (buttonState1 == HIGH)   // button is 1
    {
      index++;
      if (index > 2)
      {
        index = 0;
      }
      
      switch (index)
      {
        case 0:
          digitalWrite(ledBLUE, HIGH); // turn the LED on (HIGH is the voltage level)
          digitalWrite(ledRED, LOW);
          digitalWrite(ledGREEN, LOW);
 
          digitalWrite(buzzer, 0);  // Switch pressed, buzzer on
          tone(buzzer, 100);        // Send 1KHz sound signal...
          delay(100);               // ...for 1 sec
          noTone(buzzer);           // Stop sound...
 
          break;
 
        case 1:
          digitalWrite(ledBLUE, LOW);
          digitalWrite(ledRED, LOW);
          digitalWrite(ledGREEN, HIGH);
 
          digitalWrite(buzzer, 0);  // Switch pressed, buzzer on
          tone(buzzer, 100);        // Send 1KHz sound signal...
          delay(100);               // ...for 1 sec
          noTone(buzzer);           // Stop sound...
 
          break;
 
        case 2:
          digitalWrite(ledBLUE, LOW);
          digitalWrite(ledRED, HIGH);
          digitalWrite(ledGREEN, LOW);
 
          tone(buzzer, 100);            // Send 1KHz sound signal...
          delay(100);                   // ...for 1 sec
          noTone(buzzer);               // Stop sound...
 
          break;
 
      }//closing switch index
 
    }//closing buttonState1
 
  }//END of if (lastButtonState != buttonState)
 
  //***********************************
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);
 
  if (lastButtonState2 != buttonState2)
  {
    lastButtonState2 = buttonState2; //update to the new state
 
    // if it is, the buttonState2 is HIGH/pressed:
    if (buttonState2 == HIGH)   // button is 2
    {
      index--;
      if (index < 0)
      {
        index = 2;
      }
 
      switch (index)
      {
        case 0:
          digitalWrite(ledBLUE, HIGH); // turn the LED on (HIGH is the voltage level)
          digitalWrite(ledRED, LOW);
          digitalWrite(ledGREEN, LOW);
 
          digitalWrite(buzzer, 0);  // Switch pressed, buzzer on
          tone(buzzer, 100);        // Send 1KHz sound signal...
          delay(100);               // ...for 1 sec
          noTone(buzzer);           // Stop sound...
 
          break;
 
        case 1:
          digitalWrite(ledBLUE, LOW);
          digitalWrite(ledRED, LOW);
          digitalWrite(ledGREEN, HIGH);
 
          digitalWrite(buzzer, 0);  // Switch pressed, buzzer on
          tone(buzzer, 100);        // Send 1KHz sound signal...
          delay(100);               // ...for 1 sec
          noTone(buzzer);           // Stop sound...
 
          break;
 
        case 2:
          digitalWrite(ledBLUE, LOW);
          digitalWrite(ledRED, HIGH);
          digitalWrite(ledGREEN, LOW);
 
          tone(buzzer, 100);            // Send 1KHz sound signal...
          delay(100);                   // ...for 1 sec
          noTone(buzzer);               // Stop sound...
 
          break;
 
      }//closing switch index
 
    }//END of if (buttonState2 == HIGH)
 
  }//END of if (lastButtonState2 != buttonState2)
 
  //***********************************
  // read the state of the pushbutton value:
  buttonStateReset = digitalRead(ResetPin);
 
  if (lastResetState != buttonStateReset)
  {
    lastResetState = buttonStateReset; //update to the new state
 
    if (buttonStateReset == HIGH)
    {
      digitalWrite(buzzer, 0);  // Switch pressed, buzzer on
      tone(buzzer, 100);        // Send 1KHz sound signal...
      delay(200);               // ...for 1 sec
      noTone(buzzer);
 
    }// closing buttonStateReset
 
  }//END of if (lastResetState != buttonStateReset)
 
}// closing void checkSwitches()
 
//*********************************************************************
// credit: https://forum.arduino.cc/t/playing-with-push-button-inputs-and-output-led-s/584213/12
// published for learning purpose

Here is the simulation on Tinkercad:

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 Increment Decrement 3 LEDs With 2 Pushbuttons

  • 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 Interrupt: Blink LED and Beep Every 1 Second, Pauses Upon Button Press

    Though the previous few articles, I have explained some theories required for embedded projects, which include three longer articles on Interrupt – What is Interrupt, How Interrupt Works and Understanding Arduino Interrupts. If you have not read those articles and are not sure what I am talking about, then kindly read the three articles after […]

  • LED Chaser Effect With PWM Using Arduino

    In our earlier article, We have discussed PWM and informed you that this PWD technology is used in particular for light-emitting diodes (LEDs), as they are often used as backlights on mobile phones or brake lights in newer motor vehicles. Our older article on Breathing LED also uses PWM. This was the sketch, LED is […]

  • How to Control Multiple Relays With Single Arduino ESP32?

    Before How to Control Multiple Relays With Single Arduino ESP32 Testing, You Need to Learn How to Create Multiple MQTT Channels & Fetch Data.

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