Monday, July 20, 2020

Arduino Tutorial - push buttons

Arduino Tutorial - Push Buttons

Push Buttons


Introduction

  • this is the second of my arduino tutorial. we will be expanding what we learned in part one and expanding upon it to include push buttons.
  • A push button is a circuit component which acts like a switch. when the button is pressed down,it creates a closed circuit between to points. when it is not pressed, it acts as an open circuit between two points.
  • What you will need:
    • arduino
    • breadboard
    • wires
    • push button
    • LED
    • (1) 220 ohm resistor
    • (1) 2K ohm or higher resistor

circuit

here is the circuit diagram for this project

  • the positive leg of the LED is connected to pin 4 on the arduino.
  • the 220 ohm resistor connects to the negative leg of the LED and GND pin on the arduino
  • one leg of the push button is connected to 5 volt pin of the arduino
  • the other leg of the push button is connected to pin 8 of the arduino
  • the 2K ohm resistor is connected as a pull-down resistor o the push button
    • when using inputs it is a good idea to use pull-down/pull-up resistors
    • a pull-down resistor pulls the pin down to LOW when it is not being driven HIGH

picture of the circuit i built for this project.

code

screenshot of the code for this project. the full code can be found here

  • you should recognize #define,setup,loop,pinMode, and digitalWrite from the last post.
  • the new items are:
    • digitalRead
      • this function reads the current value of the given pin.
        • in this case it is reading the value on BTTNPIN
    • int bttn
      • this is a variable declartaion.
        • a variable is a way to store data in your program so that you can use it later on in your program.
        • int this declares the type of data.
          • in this case it is an integer type
          • an integer stores whole numbers(non floating point) from -32,768 to 32,767.
        • bttn
          • this is the name of the variable.
            • you can use almost any name you want for your variables.It helps if you use descriptive names or names which relate to what they are being used for.
        • = this is how you assign data to a variable
          • in this case we are assigning the data from digitalRead to our variable bttn
        • if
          • this statement checks what is in the parenthesis to see if it is true or not
            • if it evaluates to be true then it executes the code within the brackets { }
              • in this case it sets LEDPIN to be logic HIGH when the statement is true.
            • if it evalutes as false then it skips to the next part
      • bttn == HIGH
        • the == is how you check if two values are equal.
          • this checks if bttn is equal to the value of HIGH.
          • if so, the statement is considered true.
          • otherwise it is considered false.
      • else
        • the code within the brackets { } is executed when the if evalutes as false.
          • in this case, when false, it sets LEDPIN to be logic LOW.

congragulations,if everything is working properly you should now have a LED which lights up as long as you are pressing the button.


review

  • we learned that a push button is used as a switch to toggle on/off when it is pressed.
  • you learned that a variable is used to stored data
  • an int is a type of variable to hold whole numbers
  • digitalRead is a function which reads the value of a digital pin
  • you leanred what an if/else statment is

going further

what can you do to keep going with this leasson

  • can you figure out how to make this program work without using an if/else statment?
  • you can try to make this program work without using a variable.

thank you for checking out my tutorial. check back soon for the next part of this tutorial.

No comments:

Post a Comment

Arduinot Tutorial - Serial Monitor

Arduino Tutorial - Serial Monitor Introduction ...