Raspberry Pi LESSON 29: Using GPIO pins as Inputs and Reading them in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is palm reporter with top tech boy comm and we are here today with lesson number 29 on learning how to use your Raspberry Pi if you've been through our earlier lessons you already know how to boot up the system you've learned Linux like your way around the Raspberry Pi using the Linux command line and then in the last few lessons we've really started working on programming in Python and interacting with these GPIO pins kind of what we're trying to do is we're trying it and learn how to do things on the Raspberry Pi that we already know how to do on the Arduino on the arduino interacting with these pins is very straightforward the whole arduino is really sort of designed around using those pins as inputs and outputs but in the Raspberry Pi we've just got to kind of be a little bit more methodical in the use of the pins in the last couple of lessons we learned how to use the Pens as outputs either as digital outputs or trying to simulate analog outputs using PWM we did several examples like dimming an LED and then turning an LED on and off blinking it and then position servo so at this point if you've been through the lessons and I have you should be fairly comfortable in using all of these GPIO pins as outputs the point that we're at now is how to use them as inputs how to read from them well to sort of introduce this the simplest thing to read is a button whether the button is pressed or whether the button is not pressed that my friend is what we are going to do in this lesson so you need to get your gear out you need to hook up this hook up this circuit you can see that we have two buttons on the Left button one leg is hooked to pin 16 the other leg of that button is hooked to the ground rail and then we have the left leg of the right button hooked to pin 12 these are physical pins and the right leg hooked to ground and then we have the ground rail hooked to pin 6 physical pin 6 which is one of the grounds on the Raspberry Pi so now we want to go in get this hooked up then we want to go in and sort of see how we're going to read from these pins as you know in these lessons we're using Python and so we will want to call up a Python editor and we will want to start writing some code so let's come to our terminal window our raspberry pi terminal window and if you have been following along you know that I created a folder called my pond and so if you go to your home directory okay and if you don't have one of those you can make it with mkdir and then my Python since I already have one I'm not going to remake one I like to go down in there because I get tired of having to top the path names and if I go down into the folder then I don't have to keep checked out topping path name so I'm going to CD my Python okay now I am down in my Python now let's create the program and we are going to call the program buttons because there's two plural buttons dot PI down here one two I've already got my circuit hooked up okay so we are going to do a nano and then we are going to do a but cones dot pi and as I've mentioned in the earlier lessons I always like to give my Python programs I like to give my Python programs the dot pi extension you don't have to but it just helps you to keep things organized okay now what are we going to do well one thing is is that as we're reading from the buttons we need to not just read real real real fast we need to kind of slow it down a little bit so we're going to want to put some delays in there so I'm going to say from time import sleep and I like to import it this way because then to do a delay you know if I just said import time then I would have to do the function time dot sleep and then how long if I say from time import sleep that I can just give the sleep command which we like sleep point one would be point one of a second just the little Python tip there right now we need to get our GPIO library so we're going to say import GPIO I mean import RP little eye capitalization matters big R big P little eye GPI oh okay and import that as GPIO now we need to tell Python or tell the Raspberry Pi but pin numbering scheme that we want to use I want to use just the physical pins because I can just look down here at the pins and I can figure out what's what using the physical pin configuration and I've set the physical pin configuration by doing GPIO dot set mode okay and then what mode do I want to set GPIO to get the physical pin numbering system what command do I put here GPIO dot do you remember Bo AR D board okay and that will use the physical pin numbering system now I always like to name my buttons or I like to name my pins with something descriptive so I'm going to say button one is going to be equal to 16 okay so that's the button on the left it's hooked to ten sixteen and then I'm going to say button to is equal to 12 all right that just makes it easier as we're doing the next commands now remember in Arduino we did pin modes we would set things as inputs or outputs remember in our earlier lessons on the Raspberry Pi we were setting these GPIO pins as outputs now we're going to set them as inputs okay so I'm going to go GPIO dot set up just like before but now I'm going to do which button button one and then I want it to be at what GPIO before we always said GPIO dot out this time can you guess yes GPIO yen note the capitalization okay now for these things to work right you need pull-up resistors you could come in and you could put pull-up resistors on the circuit or you could just activate pull-up resistors that are already on the board I'm going to choose to activate the pull-up resistor that's already on the board alright if you have trouble and if you do an application that that isn't working exactly right or you get little noise in your signal back and do the external pull-up resistors but I'm going to just activate an intern one so you do that lower case pull-up down and what is that going to be set at it's going to be set as GPIO dot PUD underscore up so I guess that's like pull-up device is up all right I think that looks pretty good so understand what does that mean that means that what I'm doing with this command is if you look at that pin inside the board it is tying that 10 to 3.3 volts through the resistor so if I'm just sitting there without pressing the resistor that pin is going to see what it's going to see 3.3 volts because there's no current flowing and so it's just going to see through that resistor it's going to see 3.3 volts if I press the button down that brings the pin down to ground yeah current is going to flow through that resistor for that 3.3 volts is going to drop across that pull-up resistor and so all that 10 C's is ground so as you're just sitting there it should see what it should see hi you can see one it should see 3.3 volts it should see true when you press the button that's a direct short to ground the 3.3 drops across the resistor when the button is down it should read zero it should read false it should read ah it should read low okay let's do the other pin GPIO dot set up but come to GPIO dots going to be an input and then we go pull up down equal GPIO dot PUD underscore up like that okay so now I have instigated I have put in my pull-up resistors I have define my two kids as input pins and so now what am i ready to do well I'm ready to just sit and read this thing so what am I going to do I'm going to create a while loop so I'm going to site while this is just going to be sort of an infinite loop while true I don't know if true is capitalized or not so I'm just going to say while one because one is always one this will loop forever in a Python we specify the clause with an indent so I'm going to indent here and now I'm going to read the pin if GPIO dot input which button button one equal equal zero all right what does this mean GPIO input means read from the GPIO button once so we're reading so when we do this read what's it going to return a 0 or a 1 or you could also think of it equivalently as a true or a false if it's just sitting there and the button is not pressed remember you've got that pull-up resistor it's going to be seeing one so if it sees zero that means I want that means I press the button ok so what do I want to do if I press the button well I want to print aa button one was press ok ok then I need to put a delay in here so I'm going to lose sleep point 1 then that just slows it down a little bit so it's not just flying by you can try to read those things too quick it's good to put a delay in there so I'm gonna see if GPIO dot and put button to equal equals 0 you know one of the biggest mistakes I see is I have students they try to do an if statement or a conditional with one equal and man that is a hard problem to find remember when you're doing conditionals it's always two equals okay and then we're going to go print 10 2 was pressed ok and then sleep 0.1 alright how does that look for a program I think that looks pretty good how do we save it we do a ctrl o to write out it says file name to write button stop PI I like that so I just click enter and then I ctrl X to exit out and now I should be able to run it when you're running a Python program that is going to play with these GPIO pins you have to run it as sudo as Pi is PI here you do not have access to these pins only super user has access to the pen so you have to run the program sudo is super user Python we want to run a Python program what is the program but tons dot pui that look good ok what does it do absolutely nothing why the buttons aren't pressed all eyes on button 1 so I come over here and I'm going to press it button 1 was pressed I let up and it stops I press it again button 2 I press button to button 2 was pressed button 1 was pressed button 2 was pressed boom we are reading from these GPIO pins and now we've got something where we can tell the state of the buttons well what would that be used for well maybe you want to turn on a light on LED when you press the button you want the LED to go on when you press it again you want the LED to go off maybe you would have this one over here as you pressed it you could sort of dim up or brighten up or brighten up or dimmed down an LED you could have all types of different things happen when you press the button but you have to know if the button is pressed or not and so that's what this simple example has been how to do a simple read from the GPIO pins on the Raspberry Pi ok guys this has been lesson number 29 I'll probably come back today with another lesson showing you a little bit or you know kind of kind of a little bit more playing around with these with these buttons and these input statements but this is just a quick introduction will do another lesson where we'll go into a little more depth this is palma quarter with top tech boy comm if you like my lessons give me a thumbs up come on man just give me a thumbs up or share it maybe leave me a comment you know give me some feedback on these lessons let me know that there's someone out there actually listening palma quarter top tech boy calm we will talk to you guys later
Info
Channel: Paul McWhorter
Views: 104,323
Rating: undefined out of 5
Keywords: Raspberry Pi (Computer), Python (Programming Language), Tutorial, STEM, GPIO, Input
Id: lOb4G8Le9fQ
Channel Id: undefined
Length: 13min 39sec (819 seconds)
Published: Wed Jun 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.