0:00
Tactile switch, sometimes called push button switch, is a type of switch that focuses on producing a tactile bump and a relatively quiet audible click when pressed
0:13
These small-sized switches are placed on PCBs and are used to close an electrical circuit when the button is pressed by a person
0:23
When the button is pressed, the switch has turned on and when the button is released, the switches turn off
0:37
The click response of the button lets the user feel the response of the operation from the switch
0:44
From vending machines to measuring devices, tactile switches are ideal for enabling users to have the functions they use
0:53
need. Additional applications for tactile switches are control panels on printers and copiers, TV remote
1:01
controls, and computer keyboards. Until now, we have only used the Arduino to control other things
1:09
It is time for us to start sensing the real world. After we do this, then our Arduino will be able to
1:16
make decisions of what to do based on input from the outside world. What's necessary
1:23
missing to complete this picture is a sensor. In this case we are going to use the simplest
1:29
form of sensor available, a push button switch. Pushing a button causes wires under the button
1:37
to be connected, allowing current to flow. This is called the closed state. When the button
1:44
isn't pressed, no current can flow because the wires aren't touching. This is called the
1:50
open state. In this video we're going to make projects 8 and 9 of the Arduino intro app
1:58
We'll just have to modify our breadboard diagram to add a 10K arm resistor for the push
2:05
button to make it more stable. A 10,000 om or 10K om resistor is colored brown, black, orange
2:14
We connect three wires to the board. The first two will be connected to the power rails
2:20
on the side of the breadboard to provide access to the 5-volt supply and ground
2:26
The third wire goes from Digital PIN 2 to one leg of the push button
2:32
That same leg of the button connects through a pull-down resistor, which is in this case the 10K
2:39
arm resistor. This will be connected to the ground. The other leg of the button connects to the 5-volt supply
2:46
When the push button is open, meaning not pressed, there is no connection between the two legs of the push button
2:55
So the pin is connected to the ground through the pull-down resistor, and then we read a low
3:03
When the button is closed or pressed, it makes a connection between its two legs, connecting the pin to 5 volts so that we read a high
3:12
If you disconnect the digital input output pin from everything, the LED may blink erratically
3:20
This is because the input is floating. That is, it will randomly return either high or low
3:27
That's why you need a pull-up or pull-down resistor in the circuit
3:32
To monitor the state of the switch there a new Arduino instruction that you going to learn This is the Digital Read function Digital Read checks to see whether there is any voltage applied to the pin that you specify between the parentheses
3:49
and returns a value of high or low, depending on its findings
3:55
The other instructions that you've used so far, such as the Digital Right, have it returned any information
4:02
They just executed what we asked them to do. do. With digital read, you can ask a question from the Arduino and receive an answer that can be
4:13
stored in memory somewhere in the Arduino and it is used to make decisions immediately or later
4:20
Let's examine the code for Project 8. This project simply tells us the current state of the push
4:27
button, whether it is pressed or not pressed. The first part of the code is the declaration of
4:34
variables. Digital PIN 2 is assigned to PIN button 1 variable. We also declared another variable
4:41
called VAL. This variable is where we will save the current state of the push button, which we will
4:49
then display on the screen. In the setup function, you'll notice that we have set up Digital
4:55
PIN 2 as an input pin. This is because we are going to use the push button, which is attached
5:02
the pin 2 as an input device. Since we already added a pull-up resistor in our circuit
5:08
there is really no need to enable the internal pull resistor of our Arduino board
5:14
Lastly, if we want to display characters on the screen, then we will initialize the serial function
5:20
using the serial that begin 9,600 statement. 9,600 is called the bod rate
5:28
The bod rate is the rate at which information is transferred in a communication channel
5:34
Bod rate is commonly used when discussing electronics that use serial communication
5:40
In the serial port context, 9,600 bod means that the serial port is capable of transferring a maximum of 9,600 bits per second
5:52
There are other bod rates and it really depends on the device you are using
5:56
So far, 9,600 works perfectly on my computer. We will use the serial monitor feature of the Arduino IDEE to display the current state of the push button on the computer monitor
6:10
The first statement in the loop function reads Val is equal to digital read pin button 1
6:19
This means we are now asking Arduino for the current state of the push button
6:24
Is it pressed or is it not? pressed, Arduino will give us an answer. One for pressed and zero for not pressed. Well, this is actually
6:34
depending on how the resistor was used, whether it's a pull-up or a pull-down resistor
6:41
Now that we have the answer, what's left for us to do is to display the answer on the screen
6:47
To do this, we type in serial that print LN vowel. When this is executed, the word vowel is not
6:55
displayed but the value of the valve variable will be displayed instead, which is either 1 or 0
7:04
We then write a delay statement to tell Arduino when the next reading will take place
7:10
In this case the push button is read every 100 milliseconds In addition the value of the variable val will be displayed every 100 milliseconds on the screen To view the push button readings on the screen you have to upload the code first
7:29
Once a code is uploaded successfully, we then go to the Tools menu, then click on Serial Monitor
7:35
A new window will open showing the values being read from the push buttons
7:41
Now that we know how to read the value of the push button, we'll use this for our next project, which is Project 9, in the Arduino Intro app
7:51
This is also called the switch-on switch-off project. What you want to do here is to turn on the LED when the push button is pressed and then turn it off when it is released
8:03
For the circuit, we'll just need to add an LED to our breadboard, which should be fairly automatic by now
8:41
In this circuit, we connect the Anode to Digital PIN 10
8:53
On the code, we declare the variables again for the push button and the LED
8:59
We also need to declare a variable where the value of the push button will be saved for later use
9:06
This is the variable name Val. Here is the complete code. In the setup function, we set the pin button 1 variable as input while the pin lead variable is set to output
9:19
Again, since we already added the 10K arm resistor, there's no need to type in the digital right pin button 1 comma high statement as seen on the Arduino intro app
9:29
Let's go now to the main loop function. I have made a slight modification from the Arduino intro app code to make it more clear
9:38
In this code, you will need to know about the if statement
9:43
The if statement is possibly the most important instruction in a programming language
9:48
because it allows a computer or in this case the Arduino to make decisions
9:53
After the if keyword, you have to write a conditional statement inside parentheses
10:00
Think of the conditional statement as a question you want to ask from Arduino
10:04
And if the answer or result is true, the first block of code will be executed
10:12
Otherwise, the block of code after the else will be executed. Okay, so the first statement here is to read the value of our push button
10:23
So we type Val is equal to og read pin button one
10:28
We should get a one or zero. In Arduino, a one value is the same as high and a zero value is
10:34
is the same as low. So if you get a 1, that means the push button is pressed
10:40
If you get a 0, it means that the push button is released
10:44
After getting the value of our push button, we then write the if statement
10:49
What we want to do here is if the button is pressed, turn on the LED
10:54
Else turn off the LED Converting this statement into an if statement is easy Just type in if val is equal to high then digital right pin lead comma high
11:07
Else, digital right, pin lead, comma, low. Notice that the double equal symbol is very different from the single equal symbol
11:17
The double equal sign is used when two expressions are compared and returns true
11:24
or false. The single equal sign assigns a value to a constant or a variable. Make sure that you
11:32
use the correct one because it is very easy to make that mistake and use just a single equal
11:39
sign instead of a double, in which case your program will never work. This project is already
11:46
good as it is, however we can still improve this one. Right now, holding your finger on the button
11:52
for as long as you need light is not really that practical. We need a way to let the button
11:58
stick to its current state. Like pushing the button once, the LED turns on and pushing it again
12:06
turns the LED off. For these two happen, we'll need the same breadboard circuit, but we need
12:12
to tweak our code a little bit. It's really a simple code but very effective. What we need is another
12:19
variable to save the value of high or low. Every time we push the button we change the value
12:26
of this variable. For example, if the current value of this variable is high, then we'll
12:31
change it to low. If it is low, then we'll change it to high. Finally, when we type a
12:38
digital write statement, we can then use this variable to turn the LED on or off. So how is
12:45
this translated into code? Here's the final code. for our sticky push button
12:53
First, we add this variable that will hold the high or low value
12:58
Let's name it as lead state and give it an initial value of low
13:04
Then in the condition where the button is pressed, which is if val is equal to 1
13:10
we add the line lead state is equal to exclamation point lead state
13:15
You may want to let that thought sink in for a second. the exclamation mark in Arduino means not
13:23
This is really where the magic comes in. We can also interpret it as let the new value of the lead state variable be not the current value of the lead state variable
13:38
Therefore, if the current value of lead state is low, then the new value of the lead state variable is high, which is not low or the opposite of low
13:47
Sounds confusing at first but logically it makes sense. Finally in the digital write function we simply say digital write pin lead comma lead state
13:59
When the program is run the variable lead state will be replaced by the actual value of lead state which is a high or a low
14:08
A delay of one second is added in preparation for the next reading
14:14
reading. Now in this video we talk about getting input from a push button to turn on and turn
14:21
off an LED. This is really what makes Arduino so exciting, being able to read what's happening
14:27
in the physical world and having the ability to react. If this is all new to you, you may want to try