- 1 Push Button Momentary Switch
- 1 10K ohm Resistor
- 1 330 ohm Resistor
- 1 red LED
Last active
February 17, 2017 00:21
-
-
Save plttn/c6edd3f935f0b28486eeb35387bec3ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
PUSH BUTTONS | |
Use pushbuttons for digital input | |
Previously we've used the analog pins for input, now we'll use | |
the digital pins for input as well. Because digital pins only | |
know about HIGH and LOW signals, they're perfect for interfacing | |
to pushbuttons and switches that also only have "on" and "off" | |
states. | |
We'll connect one side of the pushbutton to GND, and the other | |
side to a digital pin. When we press down on the pushbutton, | |
the pin will be connected to GND, and therefore will be read | |
as "LOW" by the Arduino. | |
But wait - what happens when you're not pushing the button? | |
In this state, the pin is disconnected from everything, which | |
we call "floating". What will the pin read as then, HIGH or LOW? | |
It's hard to say, because there's no solid connection to either | |
5 Volts or GND. The pin could read as either one. | |
To deal with this issue, we'll connect a small (10K, or 10,000 Ohm) | |
resistance between the pin and 5 Volts. This "pullup" resistor | |
will ensure that when you're NOT pushing the button, the pin will | |
still have a weak connection to 5 Volts, and therefore read as | |
HIGH. | |
(Advanced: when you get used to pullup resistors and know when | |
they're required, you can activate internal pullup resistors | |
on the ATmega processor in the Arduino. See | |
http://arduino.cc/en/Tutorial/DigitalPins for information.) | |
Hardware connections: | |
Pushbuttons: | |
Pushbuttons have two contacts that are connected if you're | |
pushing the button, and disconnected if you're not. | |
The pushbuttons we're using have four pins, but two pairs | |
of these are connected together. The easiest way to hook up | |
the pushbutton is to connect two wires to any opposite corners. | |
Connect any pin on pushbutton 1 to ground (GND). | |
Connect the opposite diagonal pin of the pushbutton to | |
digital pin 2. | |
Connect any pin on pushbutton 2 to ground (GND). | |
Connect the opposite diagonal pin of the pushbutton to | |
digital pin 3. | |
Also connect 10K resistors (brown/black/red) between | |
digital pins 2 and 3 and GND. These are called "pullup" | |
resistors. They ensure that the input pin will be either | |
5V (unpushed) or GND (pushed), and not somewhere in between. | |
(Remember that unlike analog inputs, digital inputs are only | |
HIGH or LOW.) | |
LED: | |
Most Arduinos, including the Uno, already have an LED | |
and resistor connected to pin 13, so you don't need any | |
additional circuitry. | |
But if you'd like to connect a second LED to pin 13, | |
Connect the positive side of your LED to Arduino digital pin 13 | |
Connect the negative side of your LED to a 330 Ohm resistor | |
Connect the other side of the resistor to ground | |
This sketch was written by SparkFun Electronics, | |
with lots of help from the Arduino community. | |
This code is completely free for any use. | |
Visit http://www.arduino.cc to learn about the Arduino. | |
Version 2.0 6/2012 MDG | |
*/ | |
// First we'll set up constants for the pin numbers. | |
// This will make it easier to follow the code below. | |
const int button1Pin = 2; // pushbutton 1 pin | |
const int ledPin = 13; // LED pin | |
void setup() | |
{ | |
// Set up the pushbutton pins to be an input: | |
pinMode(button1Pin, INPUT); | |
pinMode(button2Pin, INPUT); | |
// Set up the LED pin to be an output: | |
pinMode(ledPin, OUTPUT); | |
} | |
void loop() | |
{ | |
int button1State; // variables to hold the pushbutton states | |
// Since a pushbutton has only two states (pushed or not pushed), | |
// we've run them into digital inputs. To read an input, we'll | |
// use the digitalRead() function. This function takes one | |
// parameter, the pin number, and returns either HIGH (5V) | |
// or LOW (GND). | |
// Here we'll read the current pushbutton states into | |
// two variables: | |
button1State = digitalRead(button1Pin); | |
// Remember that if the button is being pressed, it will be | |
// connected to GND. If the button is not being pressed, | |
// the pullup resistor will connect it to 5 Volts. | |
// So the state will be LOW when it is being pressed, | |
// and HIGH when it is not being pressed. | |
// digitalWrite() takes two parameters, a digital pin to control, and a | |
// state of either HIGH or LOW. | |
// Using the knowledge you have of the button state and the LED pin | |
// use logical statements to make the LED turn on when you hold the button | |
//your code here! | |
// hint: you'll need to write a function call that both turns the LED on | |
// and turns the led off based on whether or not the button is being | |
// pushed | |
} |
Author
plttn
commented
Feb 16, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment