Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

PROJECT 3 – Push Button

Using a push button switch, you will be able to turn on and off an LED.

Parts Needed
 (1) Arduino Uno
 (1) USB A-to-B Cable
 (1) Breadboard
 (1) LED 5mm
 (1) 220 Ω Resistor
 (1) 10K Ω Resistor
 (1) Push Button Switch
 (6) Jumper Wires

Project Diagram

Project Code
1. Connect the Arduino board to your computer using the USB cable.
2. Copy/Input the code below to the newly opened Arduino window.
3. Select the board and serial port as outlined in earlier section.
4. Click upload button to send sketch to the Arduino.
 
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board attached to pin 13.
CODE USED
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output:
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
}

void loop() {
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value:
// check if the pushbutton is pressed.
if (buttonState == HIGH) { // if it is, the buttonState is HIGH:

digitalWrite(ledPin, HIGH); // turn LED on:

} else {
digitalWrite(ledPin, LOW); // turn LED off:
}
}

You might also like