0% found this document useful (0 votes)
14 views6 pages

Lesson 3 Controlling An LED by Button

Uploaded by

cejek20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Lesson 3 Controlling An LED by Button

Uploaded by

cejek20
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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 * 5mm Red LED

 1 * 220ohm Resistor

 2 * Buttons

 7 * M-M Jumper Wires

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

int ledPin = 5; // Red Color to pin 5 on the Arduino

int buttonApin = 9;//one button to pin 9 on the Arduino

int buttonBpin = 8;//anther button to pin 8 on the Arduino

byte leds = 0;

//The 'setup' function defines the ledPin as being an OUTPUT


as normal, but now we have the two inputs to deal with. In
this case, we use the set the pinMode to be 'INPUT_PULLUP'

void setup()

pinMode(ledPin, OUTPUT);

pinMode(buttonApin, INPUT_PULLUP);

pinMode(buttonBpin, INPUT_PULLUP);

//The pin mode of INPUT_PULLUP means that the pin is to be


used as an input, but that if nothing else is connected to the
input, it should be 'pulled up' to HIGH. Inother words, the
default value for the input is HIGH, unless it is pulled LOW by
the action of pressing the button.
//This is why the switches are connected to GND. When a
switch is pressed, it connects the input pin to GND, so that it is
no longer HIGH. Since the input is normally HIGH and only
goes LOW when the button is pressed, the logic is a little
upside down. We will handle this in the 'loop' function.

void loop()

if (digitalRead(buttonApin) == LOW)

digitalWrite(ledPin, HIGH);

if (digitalRead(buttonBpin) == LOW)

digitalWrite(ledPin, LOW);

Experimental Procedures

Step 1:Build the circuit


Schematic Diagram
Step 2: Open the code:Controlling_an_LED_by_Button_Code

Step 3: Attach Arduino UNO R3 board to your computer via


USB cable and check that the 'Board Type' and 'Serial Port' are
set correctly.

Step 4: Upload the code to the RexQualis UNO R3 board.

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.

You might also like