0% found this document useful (0 votes)
90 views8 pages

Lesson 5 CP Robotics Revised

The document provides instructions for building and coding a pushbutton circuit using an Arduino board. It outlines objectives of designing, building, and coding a pushbutton with Arduino's digital input. It then details steps to build the circuit on a breadboard, explains how to add a pull-down resistor, and describes the Arduino code to read the button state and control an LED.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views8 pages

Lesson 5 CP Robotics Revised

The document provides instructions for building and coding a pushbutton circuit using an Arduino board. It outlines objectives of designing, building, and coding a pushbutton with Arduino's digital input. It then details steps to build the circuit on a breadboard, explains how to add a pull-down resistor, and describes the Arduino code to read the button state and control an LED.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lesson 5

Objectives

 Design a pushbutton using


Arduino's digital input
 Build and test pushbutton
using Arduino's digital input
 Code and run the program for
digital input.

Content
 Pushbutton Digital Input with Arduino
Tinkercad and Arduino IDE.

Learning Activities
36
Presentation
Create and Program a Pushbutton
Digital Input with Arduino Tinkercad
and Arduino IDE.
Discussion
Let's learn how to read a pushbutton using Arduino's digital input! We'll
connect up a simple circuit using a solderless breadboard and use some simple
Arduino code to control a single LED.So far you've learned to control LEDs with
code, which is one use for Arduino's outputs. This lesson builds on outputs by adding
an input. Your Arduino board can be programmed to listen to electrical signals and
take actions based on those inputs. A pushbutton is one kind of switch, a mechanical
device that connects or breaks a circuit.

To optionally build the physical circuit, gather up your Arduino Uno board, USB
cable, solderless breadboard, an LED, resistors (one from 100-1K ohms and one
10K ohm), pushbutton, and breadboard wires.

37
Step 1: Build the Circuit

 Identify the pushbutton, LED, two resistors, and wires connected to the
Arduino in the Tinkercad Circuits workplane.
 Drag an Arduino Uno and breadboard from the components panel to the
workplane.
 Connect breadboard power (+) and ground (-) rails to Arduino 5V and
ground (GND), respectively, by clicking to create wires.
 Extend power and ground rails to their respective buses on the opposite
edge of the breadboard by creating a red wire between both power buses
and a black wire between both ground buses.
 Plug the LED into two different breadboard rows so that the cathode
(negative, shorter leg) connects to one leg of a resistor (anywhere from 100-
1K ohms is fine). The resistor can go in either orientation because resistors

38
aren't polarized, unlike LEDs, which must be connected in a certain way to
function.
 Connect other resistor leg to ground.
 Wire up the LED anode (positive, longer leg) to Arduino pin 13.
 Drag a pushbutton from the components panel to the center of your
breadboard, and place it across the center column break so that its legs are
plugged into four different breadboard rows.
 Click to create a wire connecting one button leg to power.
 Connect the diagonally opposite leg to Arduino digital pin 2.
 Create and position a high value resistor (such as 10K) between that same
button leg and ground.

Step 2: Pull-Down Resistor

Click Start Simulation and click to press/hold the pushbutton to observe the digital
signal being picked up by pin 2.

39
Step 3: Pushbutton Arduino Code Explained

int buttonState = 0;

void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}

void loop()
{
// read the state of the pushbutton
buttonState = digitalRead(2);
// check if pushbutton is pressed. if it is, the
// button state is HIGH
if (buttonState == HIGH) {
digitalWrite(13, HIGH);

40
} else {
digitalWrite(13, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}

When the code editor is open, you can click the dropdown menu on the left and
select "Blocks + Text" to reveal the Arduino code generated by the code blocks.
Follow along as we explore the code in more detail.

int buttonState = 0;

Before the setup() , we create a variable to store the current state of the button. It’s
called int because it’s an integer, or any whole number.

void setup()
{
pinMode(2, INPUT);
pinMode(13, OUTPUT);
}

Inside the setup, pins are configured using the pinMode() function. Pin 2 is
configured as an input, so we can "listen" to the electrical state of the pushbutton.
Pin 13 is configured as an output to control the LED.

void loop()
{
// read the state of the pushbutton
buttonState = digitalRead(2);

Anything after a set of slashes // is a comment, just for us humans to read, and is
not included in the program when the Arduino runs it. In the main loop, a function
called digitalRead(); checks the state of pin 2 (which will be either 5V aka HIGH or
ground aka LOW), and stores that state in the buttonState variable we created at
the top.

41
// check if pushbutton is pressed. if it is, the
// button state is HIGH
if (buttonState == HIGH) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}

Below two more comment rows is an if statement that checks to


see if buttonState is HIGH ( == is a comparison operator, not to be
confused with = , which is an assignment operator). If the condition
is met, the built-in LED is set HIGH (on). If not, the code contained
inside the else { is executed instead: the built-in LED is set LOW
(off). If statements can exist alone, or with one or more else
statements.

Step 5: Build a Physical Arduino Circuit with Arduino IDE

 To program your physical Arduino Uno, you'll need to install the free software
(or plugin for the web editor), then open it up.
 Wire up the Arduino Uno circuit by plugging in components and wires to
match the connections shown here in Tinkercad Circuits. For a more in-depth

42
walk-through on working with your physical Arduino Uno board, check out the
free Instructables Arduino class (a similar circuit is described in the third
lesson).
 Copy the code from the Tinkercad Circuits code window and paste it into an
empty sketch in your Arduino software, or click the download button
(downward facing arrow) and open the resulting file using Arduino.You can
also find this example in the Arduino software by navigating to File ->
Examples -> 02.Digital -> Button.
 Plug in your USB cable and select your board and port in the software’s Tools
menu.
 Upload the code and watch your LED light up when you press the button!

43

You might also like