Arduino Buttons and LEDs - Push Button Tutorial - Circuit Geeks
Arduino Buttons and LEDs - Push Button Tutorial - Circuit Geeks
Popular Posts
Push-button is a key component in most electronics projects. It is a very simple and useful
component to add user interaction to your project. I am sure all of you used a push button before in MAX7219 LED
day-to-day life. For example, a tv remote has push buttons to change channels and volume. Matrix Display
with Arduino
In this tutorial, I will show you how to use a push button with Arduino. I have used some LEDs to
show the output of the buttons. I will use the button to change the brightness of an LED and the Control WS2812B
frequency of a blinking LED. RGB LEDs using
Arduino
Push buttons come in different shapes and sizes. But the internal structure and working principle are
Interfacing 7
the same for most push buttons.
Segment Display
with Arduino
Push buttons usually have four pins. But these pins are connected internally in pairs. So actually you
have two connections. When you press the button these two terminals will connect together. And
Arduino LCD
when you release the button these terminals will disconnect.
Complete Tutorial
for Beginners
Recent Posts
Arduino RGB LED Tutorial
Make an Arduino Temperature
Sensor using Thermistor
Arduino Light Sensor Using LDR
Arduino millis() and micros()
Arduino Potentiometer Tutorial
Arduino Buttons and LEDs | Push
Button Tutorial
Arduino I2C LCD Tutorial
DHT11 & DHT22 Humidity and
Temperature Sensor with Arduino
In the above image, you can see the schematic of a push button. It has two terminals marked 1 and 2.
MAX7219 LED Matrix Display with
When you press the button terminal 1 and 2 will connect together.
Arduino Tutorial
How to Control WS2812B
How to Connect a Push Button with Arduino Addressable RGB LEDs using Arduino
Connecting a push button with an Arduino is very simple. Connect one terminal of the push button
to the ground pin and another terminal to any Arduino digital pins. Here you have to use a pull-up
resistor (10k Ω) to keep the voltage HIGH when you are not pressing the button. Must Read
The pullup resistor is nothing but a high-value resistor connecting to the Arduino digital pin you are How to Control WS2812B
using with the HIGH (5v) voltage. Know more about the pullup resistor here – Arduino pullup Resistor Addressable RGB LEDs using Arduino
MAX7219 LED Matrix Display with
Arduino Tutorial
I2C OLED Display with Arduino
Tutorial
Raspberry Pi Pico Getting Started
with Micropython
Arduino LCD Complete tutorial for
Beginners
Note that – If you use a pull-up configuration the button input pin gets logic HIGH input when you
are not pressing the button. And when you press the button it gets logic LOW input.
If you want a LED to be ON when the button is pressed and OFF when the button is not pressed then
you need to detect the button state only.
If you want to toggle the LED between ON and OFF with each button press then you need to detect
the button state change.
And if you want to increase or decrease the brightness of an led or if you want to control multiple led
using one button you will need to count the button press.
In the below sections, I will use all three cases with different examples.
For this example, you need to make a small circuit like below. You can see that I connect an LED and
a button with the Arduino. LED’s +ve pin is connected to the Arduino pin number 9 and the ground
pin is connected to the ground through a 220 Ω current limiting resistor. The button is connected to
Arduino pin number 13.
Now upload the below code and press the button. See how it works.
Arduino Code
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
currentState = digitalRead(buttonPin);
if (currentState == LOW) {
digitalWrite(ledPin, HIGH);
}
else if (currentState == HIGH) {
digitalWrite(ledPin, LOW);
}
delay(50);
}
This example will work on the previous circuit. So you don’t need to change the circuit. Just upload
the below code, press the button multiple times and see how it works.
The below code will toggle an LED every time the button is pressed.
Arduino Code
void loop() {
currentState = digitalRead(buttonPin);
In this section, I will show you how to count the button presses. The below code will count the button
press from 0-10 and then reset the counter. You can see the output on the Arduino serial monitor. In
the below sections you can see the practical uses of the push-button counter.
Arduino Code
void setup() {
Serial.begin(9600);
Serial.println("Push Button Counter");
Serial.println(" ");
pinMode(buttonPin, INPUT);
}
void loop() {
currentState = digitalRead(buttonPin);
if (currentState != lastState) {
if (currentState == LOW) {
pushCounter++;
Serial.print("Button Press Count = ");
Serial.println(pushCounter);
}
}
lastState = currentState;
if (pushCounter >= 10) {
Serial.println(" ");
Serial.println("Resettng the Counter...");
pushCounter = 0;
}
delay(50);
}
Upload the above code to the Arduino board and open the serial monitor. You will see the button
press count.
For this example, you will need a small circuit like below. You have to connect four LEDs and a button
with the Arduino board. You can follow the below circuit –
Take four LEDs and connect their +ve pin to the Arduino pin numbers 8, 9, 10, and 11. Connect their -
ve pin to the ground through a current limiting resistor (220 Ω). Use a separate resistor for each LED.
Now connect a button to Arduino pin number 13.
Upload the below code and press the button to see the output.
Arduino Code
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
for (int i = 0; i < numberOfLED; i++) {
if (pushCounter % numberOfLED == i) {
digitalWrite(ledPin[i], HIGH);
}
else {
digitalWrite(ledPin[i], LOW);
}
}
pushCounter++;
delay(400);
}
}
Here you need to make a small circuit like below. Connect the two buttons input to the Arduino pin
12 and 13. Connect the LED +ve pin to Arduino pin 9 and the -ve pin to the ground using a current
limiting resistor (220 Ω).
int lastState1 = HIGH; // the previous state from the input pin
int lastState2 = HIGH;
int pushCounter = 0; // counter for the number of button presses
int currentState1, currentState2; // the current reading from the input pin
int fadeValue = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
currentState1 = digitalRead(buttonPin1);
currentState2 = digitalRead(buttonPin2);
if (currentState1 != lastState1) {
if (currentState1 == LOW) {
pushCounter++;
fadeValue = pushCounter * 32;
}
}
lastState1 = currentState1;
if (currentState2 != lastState2) {
if (currentState2 == LOW) {
pushCounter--;
fadeValue = pushCounter * 32;
}
}
lastState2 = currentState2;
analogWrite(ledPin, fadeValue);
if (pushCounter >= 8) {
pushCounter = 0;
}
delay(50);
}
The circuit for this example is the same as the above circuit. So just upload the below code and press
the buttons to see the output.
int lastState1 = HIGH; // the previous state from the input pin
int lastState2 = HIGH;
int currentState1, currentState2; // the current reading from the input pin
int pushCounter = 0; // counter for the number of button presses
int delay_t = 100;
int ledState = LOW;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
currentState1 = digitalRead(buttonPin1);
currentState2 = digitalRead(buttonPin2);
if (currentState1 != lastState1) {
if (currentState1 == LOW) {
pushCounter++;
}
}
lastState1 = currentState1;
if (currentState2 != lastState2) {
if (currentState2 == LOW) {
pushCounter--;
}
}
lastState2 = currentState2;
if (pushCounter >= 10) {
pushCounter = 0;
}
else if (pushCounter < 0) {
pushCounter = 0;
}
delay(50);
}
unsigned long previousMillis = 0; // will store last time LED was updated
void blink_without_delay(long interval) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
previousMillis = currentMillis;
}
}
circuitgeeks is the fastest growing and most trusted community site for any kind of
electronics projects, Guides and Books on the web. Thousands of people visit circuitgeeks! to
search or browse the hundreds of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of
appreciation.
Related Posts:
I2C OLED Display with MAX7219 LED Matrix Display Arduino LCD Complete
Arduino Tutorial with Arduino Tutorial Tutorial for Beginners
DHT11 & DHT22 Humidity Arduino I2C LCD Tutorial Raspberry Pi Pico – Getting
and Temperature Sensor with Started with Micropython
Leave a Comment
Name *
Email *
Save my name, email, and website in this browser for the next time I comment.
Post Comment