Lab 3: Using a Pushbutton for
Digital Control of an LED.
In Lab 1, we saw how to use a pushbutton to turn an
LED on and off by making or breaking an analog circuit.
In this lab, we’ll see how to use the Arduino to read the
pushbutton state and then decide whether to turn the LED
on or off.
Parts required:
1 Arduino
1 Arduino proto shield
1 Pushbutton
1 1 KW resistor (brown/black/red)
1 LED
1 10 KW resistor (brown/black/orange)
5 Jumper wires
Pushbutton LED Circuit
[ASIDE: Figure 18 shows a photo of a physical
realization of the circuit shown in Figures 16 and 17.
Note that the jumper wires are not the same color as
in the diagram, and that they obscure the view of the
board. You’ll need to be extra careful when building
the boards to make sure that you’ve connected the
Use a Pushbutton to Turn an LED On
Check off when complete:
; Make sure the power is off before building the
circuit.
; Build the circuit shown in Figures 16 and 17.
; Plug the USB cable into the Arduino.
; Apply power to the circuit.
; Open the Arduino IDE and load the
C4_Pushbutton_LED program.
; Verify and upload the program to your Arduino.
// C4_Pushbutton_LED // Program turns LED on or off depending on // pushbutton state // Constants used to set pin numbers const int buttonPin = 12; // the number of the pushbutton pin const int ledPin = 11; // the number of the LED pin // variables int buttonState = 0; // variable the pushbutton status void setup() { // set the buttonPin mode to INPUT pinMode(buttonPin, INPUT); // set the ledPin mode to OUTPUT pinMode(ledPin, OUTPUT);
April 2014 63
; FIGURE 16: Breadboard pushbutton LED digital control.
; FIGURE 17: Schematic pushbutton LED digital control.