Lab 3 - Button Control Lab details

Objective:  Part 1: Make an LED turn on and off using one button.  Part 2: Use a second button so one turns it on, and the other turns it off.

Parts List

  Qty Notes
Push button 2  
330 Ohm Resistor (orange-orange-brown)                                                                                                                                                                                              1 This resistor is for the LED so that it does not blow out!
10k Ohm Resistor (brown-black-orange) 2 This resistor is for the buttons.  We need this because it provides a high resistance for the electricity coming out of the button which is going into the Arduino digital pin.
LED 1  
Wire (red) 1  
Wire (black) 1  
Wires (any color) 4  

Remember that the wire colors don't matter and are for aesthetics.  The red and black wires are used just so we know which one is 5V and GND by looking at them but they do not have to be red/black.

A note on push buttons:  There are 2 common types in kits!

2-pin mini push buttons:  these conduct electricity straight across to the two prongs.

4-pin mini push buttons:  these conduct electricity across the TWO CLOSEST prongs!

Button States:  It is possible to buy buttons that are OFF when pressed and ON when not pressed.  If this is the case, just reverse your code for HIGH/LOW (on/off) 


Parts Set Up 

Instructional Parts Set-Up

Part Connection 1 Connection 2
Push Button 1 (if you have a 4-pin push button, make sure the two CLOSEST prongs are Connection 1 and Connection 2.  It does not matter which holes you put the other 2 prongs in.) F5 F8
Push Button 2 C11 F13
LED F21 (longer wire or anode)                                                                                                 F22 (shorter wire or cathode)                                                                                                 
330 Ohm Resistor J22 -ve column
10k Ohm Resistor 1 J8 -ve column
10k Ohm Resistor 2 J13 -ve column
Wire (red) +ve column Arduino 5V
Wire (black) -ve column Arduino GND
Wire (any color) H5 -ve column
Wire (any color) J11 -ve column
Wire (any color) H8 Arduino digital Pin 2
Wire (any color) H13 Arduino digital Pin 3

Visual Parts Set-Up

Note that in the diagram of the pushbutton below, the pushbutton's pins have been EXTENDED so you can visually see what rows they are connecting on!

They are not extended in real life.  This diagram is only for clarification of where they go!

Lab 3 Code

Without Comments


With Comments

Line Code Comments
1 const int buttonPin = 2;                                                                                                                                                                                                           This makes a variable called butonPin which holds a value of 2.  The 2 is the Arduino digital pin we are using.  This could also be typed without the letters "const".  The "const" just makes this a constant and unchanging variable.  For further reading othe use of const, please read this link:
2 const int ledPin = 9; Makes a variable called ledPin which holds the value 9.  The 9 is the Arduino digital pin we are using. 
3 int buttonState = 0; Makes a variable called buttonState and gives it a value of zero.  0 will mean the button is not pressed or LOW.
4 void setup() Declares the setup() function is starting
5  { Bracket shows the start of the setup() function. This bracket is usually in line 4, but is separated to emphasize the start and end of the setup() function.
6 pinMode(ledPin, OUTPUT); Declares ledPin (pin 9) to be an output.  
7 pinMode(buttonPin,INPUT); Declares button(pin 2) to be an input.  
8  } Closes out the setup() function
9 void loop() This is the main part of our program which will loop continuously
10 { This bracket shows us the beginning of the loop() function
11 buttonState = digitalRead(buttonPin); digitalRead makes the Arduino checks buttonPin or Digital Pin 2 and assigns that value to the variable buttonState.  For buttons, buttonState will either be OFF(0 or LOW), or ON(255 or HIGH).  So this is saying to store the value of the button in the variable buttonState
12 if (buttonState == HIGH)

An if statement compares one value to another and it is true, it executes the code within in.  In this case, it is seeing if buttonState is equal to HIGH or 255.

**Note that two = signs are used: ==

This is because one = sign means "assign a value".

Two equal signs mean "is equal to".


if (buttonState == HIGH) means "if button state is equal to 255"


int x = 5;  means "make an integer x and assign a value of 5.

13 { This bracket could have been in line 12 but is here to emphasize the beginning of the program for the if statement
14 digitalWrite(ledPin, HIGH); This puts power to ledPin (pin 9).  So if the button is HIGH or pressed, the LED will have power.
15 }  
16 else An "else" statement always follows an "if" to show what you should do if the "if" statement does not happen.  This is the equivalent of asking you to go to the store to buy a candy bar.  When you get to the store, "IF" the candy bar is present, buy it.  "ELSE" go home without buying anything.
17 { This bracket could have been in line 16 but is here to emphasize the start of the else program
18 digitalWrite(ledPin, LOW);

This turns the LED off.  So the complete IF-ELSE program says:

-IF the button is pressed, turn the LED ON

-ELSE turn the LED OFF

So if the button is not pressed, the LED is OFF.

19 } This bracket indicates the end of the ELSE program
20 } This final bracket indicates the end of the loop() function

 Shorter Version:

The original program is fairly formal, but if you wanted a simplified version that is more intuitive for you, here's another version on the right:


**note that the digitalRead is now inside the IF statement!

Part 2:  On Button/Off Button Program

Save the old program as Lab3Part1.  Start a new program with the following code:

(note, I have had pushbuttons that registered HIGH when UP, not down. Change digitalRead == HIGH if yours are ON when pushed)

Lessons Learned:


 If Statements