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

Software Setup
This is the required code/sketch:
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: