LED Blinking With a Push Button
by Liono Maker in Circuits > Arduino
30884 Views, 3 Favorites, 0 Comments
LED Blinking With a Push Button
LED Blinking with a Push Button using Arduino Uno
In this Project, You’ll add a push button switch to an led circuit to control when the led is lit.
Parts Required:
• Arduino board
• Breadboard
• Jumper wires
• LED
• Momentary tactile four-pin push-button
• 10 k-ohm resistor • 220-ohm resistor
How it Works:
When pressed, a push-button completes a circuit, turning it on. As soon as the button is released, the connection will spring back and break that circuit, turning it off. The push-button switch is also known as a momentary or normally open switch, and is used in, for example, computer keyboards. This is in contrast to a toggle switch, which stays either on or off until you toggle it to the other position, like a light switch.This type of push-button has four pins, but you generally use only two at a time for connection. You’ll use the top connections in this project, although the two unused pins at the bottom would do the same job.
About Schematics:
Place your push-button in a breadboard.Connect pin A to one leg of a 10k-ohm resistor, and connect that same resistor leg to Arduino pin 2. Connect the other resistor leg to the GND rail, and connect the GND rail to the Arduino’s GND. Connect pin B on the switch to the +5V rail, and connect this rail to +5V on the Arduino.Add the LED to your breadboard, connecting the longer, positive leg to Arduino pin 13 via a 220-ohm resistor and the shorter leg to GND Confirm that your setup matches the circuit diagram shown in Figure. Arduino Code: In this sketch, you assign a pin for the push-button and set it as INPUT, and a pin for the LED and set it as OUTPUT. The code tells the Arduino to turn the LED on as long as the button is being pressed (completing the circuit), and to keep the LED off when the button is not being pressed. When the button is released, the circuit breaks and the LED will turn off again.
const intbuttonPin = 2; // Pin connected to pushbutton
const intledPin = 13; // Pin connected to LED
intbuttonState = 0; // Give pushbutton a value
void setup() { pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buttonPin, INPUT); // Set pushbutton pin as input
} void loop() { buttonState = digitalRead(buttonPin); // Read input from pin 2
if(buttonState == HIGH) { // If pushbutton is pressed, set as HIGH
digitalWrite(ledPin, HIGH); // Turn on LED
} else{ digitalWrite(ledPin, LOW); // Otherwise, turn off LED } }