 Again, this video is part of a series mainly on home automation. Be sure to check out the link in the description or hopefully an annotation on the screen to the full playlist. And I suggest watching the previous videos because we're building on stuff that we've learned already. Now, today we're working with the Raspberry Pi, which is what we've been messing with lately. And it's great. So far, we've been controlling things with it. We've been turning things on and off with it. Basically, we've gone over lights, air conditioners, stuff of that nature. But you can also get inputs from your house. And one of the simplest inputs is a button press. Now a button press is basically like we have two wires here connected to GPIO pins on the Raspberry Pi. And if I touch them like this, that's the same thing a button does. It's just completing the circuit there. So what we're going to do today, we're going to work with a little button, momentary button here. And there are different types of button, but for the most part you can use any type of button. You can also use a switch. The difference is this is on when I'm pressing it down. And as soon as I let go, it turns off. There are other buttons that once you press down, it turns it on, even after you let go. And if you press it again, it turns off. And then there's also switches, but they all just complete a circuit. Now I'm going to show you this here on the camera, but we're also going to be looking at a diagram momentarily. But you can see there's four pins here. And they're kind of wider, angled out one way. And this button is designed to go into a board like this or a circuit board, a bread board or a circuit board. And it can go into either of those very nicely. And what we're going to do is you kind of connect one wire to the pin here. And again, I'm going to show you on a diagram here better in a moment. And one to here. And by default it's not connected. And when you press it, it's connecting the two wires. So I'm going to put that in the board here. You know which way it goes in because if it's one of these buttons, it won't fit in the other way unless you bend the pins. Okay. So that's in there. Now I have one wire, this orange wire, coming from the top pin here. I believe that's pin one. And again, we're looking at a diagram momentarily. That's a three volt pin. And I'm going to plug that in to either side of this connection. It doesn't matter. Now this green wire is going to pin 23, which on the outside of the board, that's the sixth pin up. Now this is important the way we're hooking this up. And I'm not going to go into too much detail, but we're going to look at a diagram momentarily. But different pins do act different. And depending on what pin you're using, you may need a resistor. Or instead of going to voltage, you're going to want to go to a ground. But right now I have it hooked up this way. So from pin one, which is a three volt, and then the sixth pin up from the outside of the board, that's GPIO 23. And now when I click this button, the Raspberry Pi is detecting that it's being pressed. But we're going to have to look at some log into the Raspberry Pi to see what it's doing right now. It's not doing anything. It's just going, oh, button's being pressed. Okay. So let me move over to my computer now and we'll access it. Okay. So I'm SSH'd into my Raspberry Pi through the network. And if you've watched previous tutorials with GPIO pins, we have to enable the GPIO pins. So what we're going to do here on Unix and Unix-like systems, such as Linux, all hardware can be accessed as if it was a file, whether it's a mouse, keyboard, hard drive, all of it can be read and written to like any other text file. And what we're going to want to do here is we're going to echo 23. It doesn't matter in this particular case where the F quotations are not, normally I put quotations in. I just want to tell you, don't worry whether those quotations are there or not, don't let it confuse you in this particular instance. But we're going to say, we're going to put that into a file. So sys, class, GPIO, and then we're going to say export. So this is saying export. So basically it's turning on pin 23. I'm going to hit enter. I'm going to get an error because I've already done this. It's saying device is busy. It means it's already been activated. You won't get that the first time you do it. But once you do it, if you do it again, no harm, no foul, it's just going to give you a warning that it's already been done. Next in previous videos, we told it that we want the direction to be out because we were sending a signal to something. Well, this time we're receiving a signal, so we're going to set it to in. So we're going to echo the word in into the file, sys, class, GPIO, GPIO, in this case we said 23, and then direction. So we're saying the direction to in because we're receiving a signal. Okay, so we've turned it on. We've activated the GPIO pin, and we're saying that we're going to be looking for information coming into that. Let's real quick switch over here to a diagram. This is a diagram, and this is a two button diagram. We did this first button here. As you can see, it's going from a three volt up here, which if we look at this, we can see that's GPIO pin one, or not GPIO pin, but pin one, and then we are connecting the other side of it to GPIO 23, which is pin 16, which again is coming up this way, and also depends on what RAS should probably mention this. There's different types of Raspberry Pis, newer versions have more pins, so look at a diagram that goes with your Raspberry Pi, but we're hooking to GPIO pin 23, so that's what we're doing here. So this top button is what we're doing here. Now you notice there's a second button here, and it's connected to pin 24, but the other side of it isn't going to three volt, it's going to a ground. And without going into too much detail, basically it depends on whether it's a pull down or pull up, whether it's basically sending voltage one way or the other base kind of. So I'm not going to go into it because I'm not that versed on it and I don't want to steer you in the wrong direction, but this is two types of pin setups, whether you're using pin 24 or 23, the code is the same, it's just how you wire it up. And if you need more than two buttons, look up stuff online, because you'll probably need a resistor for other pins and might need to set it up different, but I just wanted to point that out. Here's at least two and we got one pin going here. Okay, so we've enabled it, we set it as an input. Now just like before, we can read that file. So I can cat out sys class GPIO pin 23 and we can get its value, and it's going to be one or zero, zero if it's not being pressed, and one if it's going to be pressed. Now the button's on the other side of the room, so I can't click this and go over there. I guess I can go, here let me do a sleep command. I'll say sleep for three seconds and then cat it out, and I'll run over here and press the button, there you can see it's a one, but now that I let go of it, it's back to being zero. Okay, so let's put this into a loop so that we can constantly check it. So I'm just going to say while one, and I'm going to go clear the screen, sleep for .1. Now in our actual final code, we're not going to sleep because we want it to go as fast as it can, but in this particular case, if I don't put that sleep command, the screen's going to flicker because it's going to be reading faster than it can write to the screen kind of. So I'm going to say clear the screen, sleep for a tenth of a second, and then we are going to cat out that same file. And then we're going to say done, and I also forgot to put the do here. So we're saying, this is a very basic shell script here, we're saying while it's a loop, it's going to loop as long as whatever is in these brackets is true. We're saying one, make sure you have the spaces on both sides of the one, that just means always loop until I tell you to stop. What are we going to do? We're going to do all these commands and then loop, what are we doing? We're going to clear the screen, sleep for a tenth of a second, and then print out the value of that button. I'm going to hit enter, and we should be seeing a zero on the screen right now. Let me see if I typed everything right. Oh, yes. The order you put stuff in is important because what's here, it's clearing the screen before we have a chance to read it. So I'm going to say sleep point one, and then clear the screen and output it again. So now we see zero, and if I go over to the button, we can press it, and it says one, and I let go, it says zero, one, zero, one, zero, click, click, click, click, and you notice that if I click it real fast, it's missing some of those clicks because we're delaying that tenth of a second. Again, we're going to run our script here without that delay. So let's go ahead and control C to kill that. I've already written out the script, we'll look at it in a moment, but let's go ahead and just run it first. So it's just called button, it's just this basic bash shell script. I'll hit enter and it says not pressed. Now if I press the button, you can see it says pressed, not pressed, pressed, not pressed. If I click it real fast, you can see that it's responding quickly. Okay, so let's go ahead and look at that. Control C to kill that. I'm going to use Vim as my text editor, and I'm going to go into button, and we've got, sorry I don't have color coding set up on the Raspberry Pi, it'd be a little easier to read. We've got bin bash, this is just saying this is a bash script. Now this first script is, this first section here is just checking if you're root because we want to, we're writing to stuff in these, at least these first two commands that you need to have root permissions unless you change permissions on those, those pins, you need to be root or pseudo to access. So this is just saying check if the user is root. If they are not root or have pseudo permissions, put out this message saying that this must be run as root and then say we're trying to restart this with a pseudo command and then it will pseudo and run the same command again. That's what that's doing there. And then exit the original script. Okay, next we're saying a variable pin 23. So if you want to use a different pin, I set this as a variable so it's very easy to change even though my comments still say pin 23, but if you want to use pin 24, you can just change this variable to pin 24 and it changes everything else. That's one of the benefits of using variables rather than writing out the pin in each one of these commands because there's at least, at least two, but if it was a bigger script, it could be more places where that's written. The script does try to enable and set it as an input in case you haven't already, so we've already gone over that. Okay, then it's going to clear the screen and create a variable called B. You'll see why momentarily. And then we're going to echo out not pressed and then we're going to do a while loop just like we did in the command line while do. Okay, so now we're going to create another variable. This variable says check the value of the pin, it can be zero or one. Now it's going to check, is the value of the pin equal to B? Which B by default is zero. That's why we're already printing out the message not pressed. But if it's not equal to, so it's constantly checking the value of that comparing it to B. The reason I'm doing this rather than just putting the output is so we don't have to clear and refresh the screen as much. It's only going to clear the screen if something changes. So basically we're set to zero, mean the button's not pressed. If the button is pressed, that's what it's saying here. If the button is pressed, then clear the screen. Now reset B to whatever the value of the pin is and then we're going to say, well, if B does not equal zero, then we're going to echo, I'm sorry, if B does equal zero. So whatever the pin being pressed is, we're going to echo pressed or not pressed. And if it is pressed, and if it is pressed, echo out pressed. So I'm just going to loop this over and over again as fast as the Raspberry Pi can do it, or at least as fast as it can do it in a shell script. If you write this in another language, it might be a little bit faster, but this is more than fast enough for what we're doing. So that's the whole script. Again, we already saw it running. And the thing is, right here, all we're doing is saying the button is pressed or the button is not pressed, depending on whether the button is pressed or not. But you can put lots of commands in here and this is where it gets fun. Right now, this script is just saying, checking for it and letting you know whether it's pressing that. But anything you can do with a computer, you can program it to do. You can have it send a signal out to another GPIO pin. So if you have an LED on the board, you can have it turn the LED on or off. You can have it when you press it, it starts blinking at different speeds. You can have it since we have a network connection, you can have it send a signal to another computer. You can have it check something online. If you have a server somewhere, when you press the button, it can check a server and get a value from there and then do something else. You can have it text you, email you, turn something on or off, open your garage door. And like I said, since it's connected to the network, which is connected to the internet, it can send signals or check things from all around the world. So you press a button on the board and, like I said, you're the programmer, so it will do whatever you tell it to do, which is great. Which is why being a programmer is so great, because you can do anything that you can think of. You can imagine it. If it can be done, you can do it with a button press. So this code right here will be in a link in the description. As always, I hope that you enjoyed this tutorial. I hope you visit my website, filmsbychris.com, that's Chris with a K. There should be a link in the description. And as always, also, I hope that you check out my Patreon page, patreon.com. There should be a link to that in the description as well. If you like my videos, you can support me there. Also give me your input there and get videos early and that sort of stuff. And also, again, if you like it, like, share, subscribe, comment. All that stuff helps me and I just hope that you have a great day. Okay, this is an introduction to filmsbychris.com. I'm Chris, that's Chris the K. That's me right there. My daughter, Amber. And my wife, Jennifer. We pretty much live in the swamps of Florida. I'm a firefighter by day, as well as by night. We work long hours. But that's not why you're here. You're here about the videos I put up on YouTube. These videos are mainly about computers and programming. Which means most of my videos look something like this. And if that's what you're interested in, great. If not, that's alright. I do videos on other topics too, such as video editing, special effects, photo editing, 3D design, and music creation. If you are one of my viewers and you enjoy my videos, my Patreon page is a place where you can go to help support my videos. So I ask that you take the time to go to my Patreon page and look at different levels of rewards you can receive for different levels of backing. There should be a link in the description of this video if you are watching it on YouTube. Otherwise, you can visit patreon.com forward slash metalx1000. And I thank you for your time and your support. Have a great day.