Lesson 3 Controlling An LED by Button
Lesson 3 Controlling An LED by Button
Introduction
In this lesson, you will learn how to use push buttons with digital inputs to turn
an LED on and off. Pressing the button will turn the LED on; pressing the other
button will turn the LED off.
Hardware Required
1 * RexQualis UNO R3
1 * Breadboard
1 * 220ohm Resistor
2 * Buttons
Principle
Button
Buttons are a common component used to control electronic devices. They are
usually used as switches to connect or disconnect circuits. Although buttons
come in a variety of sizes and shapes, the one used here is a6mmmini-button
as shown in the following pictures. Pins pointed out by the arrows of the same
color are meant to be connected.
Code interpretation
byte leds = 0;
void setup()
pinMode(ledPin, OUTPUT);
pinMode(buttonApin, INPUT_PULLUP);
pinMode(buttonBpin, INPUT_PULLUP);
void loop()
if (digitalRead(buttonApin) == LOW)
digitalWrite(ledPin, HIGH);
if (digitalRead(buttonBpin) == LOW)
digitalWrite(ledPin, LOW);
Experimental Procedures
Now, Press the right button, the LED will up, and the press the
left button, the LED will off.
If it isn’t working, make sure you have assembled the circuit
correctly, verified and uploaded the code to your board. For
how to upload the code and install the library, check Lesson 0
Preface.