Chapter 12 – Experiment 05 – PUSH BUTTON

Up to this point we have focused entirely on outputs, time to get our microcontroller to listen, watch and feel. We’ll start with a simple pushbutton. Wiring up the pushbutton is simple. There is one component, the pull up resistor, that might seem out of place. This is included because the microcontroller doesn’t sense the same way we do (ie button pressed, button unpressed). Instead it looks at the voltage on the pin and decides whether it is HIGH or LOW. The button is set up to pull the microcontroller pin LOW when it is pressed, however, when the button is unpressed the voltage of the pin will float (causing occasional errors). To get the microcontroller to reliably read the pin as HIGH when the button is unpressed, we add the pull up resistor.

experiment-pushbutton

schema-pushbutton

 

/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.

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 12.

created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button

*/

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  12;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

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

Serial.begin(9600);

digitalWrite(ledPin, HIGH);

}

void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

Serial.println(buttonState);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}

delay(200);
}

 

 

MAKING IT BETTER

Making a morse code generator

Try to use the previous “traffic lights” code to create a morse code generator.

2 comments

  1. [...] Chapter 10 – Experiment 03 – LIGHT SENSOR Chapter 11 – Experiment 04 – TEMPERATURE SENSOR Chapter 12 – Experiment 05 – PUSHBUTTON Chapter 13 – Experiment 06 – INTEGRATED CIRCUIT Chapter 14 – Experiment 07 – MATH IS FUN! [...]

  2. There are so many different styles that are fashionable and in so many different colors that show glitz and glamor.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


istanbul evden eve nakliyat istanbul evden eve nakliyat istanbul evden eve nakliyat sarıyer evden eve nakliyat beylikdüzü evden eve nakliyat şişli evden eve nakliyat