As simple as it may seem, knowing when something is either on or off can be a great tool to designing something useful. Answers to the following questions are what this lesson plans to tackle:
Is a button being pressed?
Has a switch been turned on?
What is my on/off sensor status?
When you can answer questions like these, you can implement actions based on the current status – if the button is pressed do this – otherwise, do that. If the sensor is HIGH take this action, otherwise do nothing. You get the gist. But before we can implement the actions, we have to be able to track the status and the changes of the digital pins.
You Will Need
A momentary push button – this is a button that is spring loaded, so when you press it, it pops back out
Jumper wires – at least three
A 10,000 Ohm resistor AKA 10K resistor
A solder-less breadboard
A very ripe banana, with one peel removed (not completely useful, but nutritious)
Connect your push button to the breadboard.
Connect one side of the push button to the 5-volt pin on the Arduino board using a jumper wire.
Connect one side of the 10K resistor to the other side of the pushbutton.
Connect the other side of the resistor to the ground pin on your Arduino. You may have to use a jumper wire to make this reach.
The same side you have your resistor connected to the pushbutton, connect a jumper wire and run this to pin 2 on your Arduino board.
Connect your Arduino to your computer with the USB cable.
Go to File, Examples, 01.Basics, digitalReadSerial
Click the Verify button in the top left corner. The verify button will turn orange and then back to blue when it has completed compiling.
Click the Upload Button (to the immediate right of the verify button). This button will also turn orange and then back to blue once you sketch is uploaded to your Arduino board.
Now go to the menu bar at the top and select Tools, Serial Monitor. You could use the short cut key of Shift + Control + M.
The serial monitor window will open and will be spotting off numbers. It should be a bunch of 0’s.
Press and hold the pushbutton – watch the serial monitor window, the numbers should now be 1.
If the numbers are not scrolling, make sure you click scrolling at the bottom left of the serial monitor window.
This sketch opens up with a multi-line comment containing a short description of the program and circuit. The first block of code is where we declare and initialize variables. From the last lesson we are familiar with the int data type.
int pushButton = 2; //This is the pin that our push button is connected to.
Notice how on one line the variable pushButton is declared and initialized. Also notice the descriptive name of the pushButton – this is how it ought to be – variable name with meanings.
So let’s consider what we have done so far – we have made a variable that will store the pin number that our push button is connected to.
The next block of code we come to is the setup(). Inside these wily curly brackets there are two functions – one which we are already familiar, namely pinMode() and another which we will learn to love – Serial.begin().
Serial.begin() is part of a family of functions referred to as a library. The name of the library is the Serial library. A library is just a group of functions that work towards a similar purpose. If you had a Circus library, it might contain the functions juggle(), balance() and flamingCircleOfDeath(). To access the functions in a library you write the name of the library followed by the name of the function in the library, with a period in-between.
Arduino has many built in libraries and many community contributed libraries. You can check them all out here or Google for a specific library if you have a need.