PROJECT 1: PUSHBUTTON-CONTROLLED LED

IN THIS PROJECT, YOU’LL ADD A PUSHBUTTON SWITCH TO AN LED CIRCUIT TO CONTROL WHEN THE LED IS LIT.

image
image

PARTS REQUIRED

• Arduino board

• Breadboard

• Jumper wires

• LED

• Momentary tactile four-pin pushbutton

• 10k-ohm resistor

• 220-ohm resistor

This project will take you through the basics of switches, which you’ll be using a lot throughout this book. Almost all electrical items use switches to turn an element on or off. There are many types of switches, and the one you’ll use now is a pushbutton (Figure 1-1).

FIGURE 1-1:
A pushbutton

image

HOW IT WORKS

When pressed, a pushbutton 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 pushbutton 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 pushbutton 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. As Figure 1-2 shows, the pins work in a circuit. Pins A and C are always connected, as are pins B and D. When the button is pressed, the circuit is complete.

FIGURE 1-2:
A pushbutton’s incomplete circuit

image

THE BUILD

  1. Place your pushbutton in a breadboard, as shown in Figure 1-3.

    FIGURE 1-3:
    Placing your pushbutton

    image
  2. 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.

    PUSHBUTTON

    ARDUINO

    Pin A

    GND and pin 2 via 10k-ohm resistor

    Pin B

    +5V

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

    LED

    ARDUINO

    Positive leg

    Pin 13 via 220-ohm resistor

    Negative leg

    GND

  4. Confirm that your setup matches the circuit diagram shown in Figure 1-4, and then upload the code in “The Sketch” on page 27.

    FIGURE 1-4:
    Circuit diagram for the pushbutton-controlled LED

    image

THE SKETCH

In this sketch, you assign a pin for the pushbutton 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.

/* 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
*/

const int buttonPin = 2;      // Pin connected to pushbutton
const int ledPin = 13;        // Pin connected to LED
int buttonState = 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
  }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.7.179