Raspberry Pi: Using GPIO Inputs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to another video from explaining computers comm this time I'm going to show you how to configure the GPIO pins or the Raspberry Pi as inputs and to trigger them in python using physical switches read switches and light sensors in this video I'm going to be using this Raspberry Pi 3 but you can do what I'm doing here on any of us berry pie so you could use a PI 3 or a PI 2 or something like a model B+ or even original Raspberry Pi or a PI 0 I'm also going to be using this little breadboard which allows me to connect different components together and given this is a video about input I'll be using various switches specifically this momentary push switch which I found lying around in my electronics bits box and we should already got some nice wires soldered onto it but I'll also be experimenting connecting the PI's inputs to this Reed switch and to this IR reflectance sensor from Adafruit I'll say a bit more about these two as we get to the relevant point in the video also required to make things actually work will be some jumper leads to connect to our Raspberry Pi and a few other wires for making connections on the breadboard just for fun at some point I'll also be including this led and then finally to make everything work ok I'm going to need to include various resistors now if you're thinking why do I need resistors to use the input on a Raspberry Pi well to explain that I've got to go through a little bit of electronics theory the most obvious way to connect a switch to a GPIO input of the Raspberry Pi will be a circuit something like this so that when we press the switch we connected 3.3 volts via logical level 1 to the GPIO pin however if your wire things like this when the switch is not pressed when it's open the input is what's called floating it doesn't know whether it took a logical value of 1 or 0 so to prevent this what we can do is to connect the GPIO pin to the ground mail to 0 volts so that when it's not pressed it's at a logical level of 0 now as you might have noticed there will be some problems doing this in exactly this way we start when we press the switch the GPIO pin is connected to both these zero Grove volts rail the ground rail and two 3.3 volts and more worryingly we have here created a short between 3.3 volts and the ground rail which is clearly not a good idea so to make this work in practice we include what's called a pulldown resistor here a 10k resistor on the ground rail so the pin is held down to the ground level it's not pressed but rise to 3.3 volts when the switch is pressed but we don't draw a lot of current between the ground rail and 3.3 volts now this arrangement is what's called pull down but you could do it the other way you could use what they call pull up and put our 10k resistor our pull-up resistor here between 3.3 volts and the pin and then when we press the switch it would drop down to the ground level but here I'm going to use a pulldown arrangement so we have a pulldown resistor on our ground line to the switch now in theory this will work just fine but let's imagine what would happen if you accidentally turned your GPIO pin from an input to an output while making error in your code what that would mean is you might connect for example an output which was at say zero volts through two 3.3 volts if you press the switch this would put a lot of current through the PI and almost certainly damage the GPIO pin and maybe even the pie itself so to prevent this we're going to include a current limiting resistor a 1k resistor out GPIO pin like this now you might hear some people saying you don't need this pulldown resistor you don't need this current limiting resistor things will work without them and they might in some circumstances and you might get away with it but for good practice and not damage your pyre you best to include these resistors you might also hear what the PI is got some internal pulldown and pull-up resistors which you can turn on off and that is true you can turn them on and off in software but they aren't that powerful to be really certainly don't do any damage and to learn good electronics practice you should use the sort of configuration you're looking at right here right here we are back in the real world with our Raspberry Pi which I'll get mounted on a little board as I showed you in a fairly recent video and I've also got our breadboard with some components fitted and what I basically done here is to set up the circuit you just see don't screen but in obviously the real world so what we have here is a wire from pin 1 on the PI that's plus 3.3 volts that is going down to a form a positive rail down here I'm using one of the negative rail to ground melt on the PI pin 20 which is going around from this wire to give us a negative rail down here and then chosen to use pin 16 as our GPIO what will be output pin here with the green wire there are lots of different pins you can use as inputs or outputs on the PI general GPIO pins you can see them all here but here are chosen pin 16 which connects across to this rail on the board that then connects through a 1k current limiting resistor that's a brown black red resistor and that links to the row above it whereas he's going to link it through to our switch which will link it through to the positive rail so here we just got the switch which basically means when we press that positive will go through the positive rail through the current limiting resistor to our GPIO input we've also got link through there after the current limiting resistor or pulldown resistor or 10k resistor brown black orange and that this links through to our negative rail if you can't tell you that - exactly clearly on screen in this format maybe it's clearer for you this format as a diagram but either way I hope you can see what I've set up so we'll now go over to raspbian I'm going to roll up the idle environment to actually do a little bit of coding I'll show you some careful runlets so I'll run the terminal either type sudo idle you don't have to use idle to write your Python code but if you do you must use sudo before it to run it as a super user if you want to control the GPIO pins and there we are Ivers run up and as usual I'll just try to just decide so it doesn't go off the edges of anybody's screens so here I've got some code already written I go to recent files I've got a input test bit of code written vary this look hopefully it'll be a lovely piece of code I'll just show basically what I've done so we start off by importing the library to control the GPIO pins our Pi GPIO with the command input our PI GPO as GPIO above that I'd put some commenting in I don't comment my code people moan at me so basically everything here it starts with a hash and when appears in red isn't part of the code it's simply some comments that tell people what's going on after that I need to set up how the GPIO pins are going to work to do that first of all I have to tell the PI what's numbering system to use for the pins here I'm using board numbering which is the simplest one I find that the easiest to use although not everybody agrees this simply means the pins are numbered one two three four five six across in a logical fashion according rack numbering I'm then going to set pin 16 as our input so GPIO setup 16 GPIO in and then go to run a small loop and I'm going to run it in a try finally set up so basically it's going to try to run this and it could have then run a while loop now a while loop basically execute as long as the condition you given it is true and here I've written while true which means this loop will run forever because that true command oh is that true statement that will always be true so it'll run first of all to see if GPIO input 16 equals 0 now you have to have a double equal sign to do a comparison here in Python if GPIO 16 is 0 it'll print open because of course the switch is open a new tab logical state 0 else if it's not open it's closed into one logical value 1 it'll print closed now that loop will go on forever because there's no way out of it but if we press ctrl C to actually actually the program it will still here execute the finally command that's the whole purpose of using try and finally whatever happens with the program it'll always actually what you put under the finally statement and so here what it will do is to clean up the output of our GPIO pins which means if we run a program again they'll be all set that back to a clean state so it seemed runs okay so if we bring the PI back up on screen I'll run this piece of code you can run by pressing f5 but here I'll use the menu so you can see what I've done and as you can see it prints the word open until I press the switch your brush is closed open closed open and you can play with that for hours we're communicating with our PI my GPIO by pressing a switch dun-dun-dun don't die and there we are and if we get really bored we press ctrl C and to finish the thing off right now let's try something a little bit more potentially useful I've got another piece of code here again it starts the same way and we're going to load in the GPIO library a live assets the abort numbering and set pin 16 as an input we're going to do here there is a situation where you might want to press the button to bed start something off in other words the PI will wait your press a button and something will happen in your code so to simulate that first of all we're going to print the statement press the button and then we're going to go into another while loop but here we're given the while loop a condition based upon the GPIO pin which uses GPIO pin 16 is 0 that means it's true the while loop will continue to operate so if a switch isn't pressed of it logical level of the switch is 0 it'll execute what's called the pass command now the pass command is simply another operation it does nothing at all but it allows you to have a while loop that keeps going around and around and around so once the switch is press that condition there is no longer true because the switch is at level 1 it'll imprint thanks and then finally it'll clean things up and exit the program so once again let's see if that works we will run the module bring the PI back up on screen and I think I'll just pull that over here we don't miss the excitement so it press print the button it's waiting for a button press I will go in press the button there we are with prints tanks and the thing comes to an end right we're now going to try to link up our Raspberry Pi to one of these which is a reed switch now this is quite a large reed switch but like all the reed switches it's basically got two contacts inside a hermetically sealed little glass tube you can maybe see that there and the way a reed switch operates why it's so useful because it operates with a magnet if I bring a magnet in here I've got an old speaker magnet here and hopefully if I get it right yeah you can see the contacts move there slightly and they close and open in the presence of a magnetic field that makes it very easy to count things or things like that when something happens these are often used for example in them counting systems where maybe counting a gate opening or closing as it comes close or maybe you use them in a alarm system to lock the security system so that's why this into our PI and here we are I've got the reed switch linked in here exactly the same way as the previous switch on our second row in the breadboard with its two resistors linked in here to a GPIO pin 18 I've also linked in an LED with a current limiting resistor a 470 ohm resistor yellow violet brown that's linked in to a pin 12 so we look at the code this is some slightly more complicated code it again imports libraries it again sets up the GPIO numbering and the pins here setting 18 also to an imported 12 to an output I then set up a counter because this is a counting program I set the counter value to zero and I set up a state variable from a real switch to tell us that it's currently not unclosed in fact of the program print now counting press button to stop now it's not going to perform a while loop as long as the button hasn't been priced as we did previously and it'll first check if the read has been newly activated in other words if the read switch is input this is a1 and the state is currently zero if that's the case it will turn on the LED set the reads take two one codes has been turned on and add one to the counter you set that a pause for a small period of time just for 0.1 seconds because of an issue called debouncing when you close a switch it doesn't actually close cleanly most switches bounce around a bit you might cut several counts if you don't have a little bit of it of a gap it then checks to see if the read switch has been released see if it's now on a zero state but the state is still recorded as one is so it turns off the LED and resets to state and then finally which is when Weaver press the button it will print the counter and clean things up so that's the code we've got to just mention because I've used the time variable here I should have told you that back at the top here I've also imported a library at the time anyway let's now see if this all works rather exciting bit of code will run the module and it'll tell us it's some Counting press button to stop so if I now take our magnet and go yes so you can see every time we bring the magnet in live led registers account so imagine mrs. Sayer a gate with a magnet in registering it's been closed like that so I've been counting like that and when we get really bored we can go back and press the button and it'll tell us it counted in that case eight things so there we're getting a bit more practical using our read switch and the switch and an AED all linked up to our good old Raspberry Pi right as a final experiment I thought we'd wire in one of these to our PI which as you can see is a reflective sensor from Adafruit and in fact I bought two of these so here's one outside of the package you'll see why I bought two bit later on and what you basically got here if you can see is you've got two devices in one package what you have here is a photo transistor and an infrared LED and of course on the bottom we've got the wires that go next to that so what you do with this is your wire it up so with the LED emits infrared and then you want the photo transistor as a switch with appropriate pulldown resistors etc and then the idea is when the infrared is beamed out of here if you put a piece in say white card or something white over the top it will reflect it back to the photo transistor and you can register an input and if it can't say anything to reflect it it doesn't so this is a means of reading things optically by an input on your Raspberry Pi so here we are everything's all wired up lighting is a little bit dark in this shop because I'm having to make it work so it doesn't actually wreck function of this ir sensor but basically we've got the reflectance sensor wide inside the photo transistor is a switch into our pin 16 GPIO input with the usual current limiting and pulldown resistors we've also got five volts through a 470 ohm resistor powering the IR LED on this unit and we've got another led here widened appeared 11 was a typical covered limiting resistor as usual you'll probably see things a lot clearer if we show you on the actual circuit diagram this is what I wired up basically what we've got before except our switch is now a photo transistor and we've wired up an LED and the LED on the sensor so if we now look at some code it's a very simple bit of code pretty much of what we've seen already I've actually imported with gpac I've set up the board numbering set pin 16 to an input in 11 to an output Y values put 11 as the output this time I've no idea I just have and we've got exactly the same type of loop what I showed you in a finally statement not very first example except here when the input is one at a high level it'll turn on LED else it'll turn it off so let's run its exciting piece of code nothing immediately happens but if I actually move my finger over the sensor or it'll turn the LED on there's enough reflectance from my skin to bounce the IR light back onto the sensor I've also got here a piece of cardboard if I move this piece of cardboard at about the right height over the sensor turn OD on off on off it's turning it on and off how is it doing that well on the other side of this card there is a black line so effectively this is bouncing off the white not off the black off the white not of the black exception if you're thinking you could use two of these sensors to make a line following robot indeed you could and in fact I'll be doing that in a future raspberry pi robotics video once you've learned how to configure GPIO pins as inputs and as outputs you're all set to use a Raspberry Pi in all kinds of exciting projects but now that's it from nother video and I hope to talk to you again very soon
Info
Channel: ExplainingComputers
Views: 581,255
Rating: undefined out of 5
Keywords: Raspberry Pi, Raspberry, Pi, GPIO, input, inputs, Python, switch, switches, reed switch, Adafruit, reflectance, sensor, IR, phototransistor, photo, light, resistor, pull-up, pull-down, pull up, pull down, current, limiting, LED
Id: NAl-ULEattw
Channel Id: undefined
Length: 18min 41sec (1121 seconds)
Published: Sun Jun 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.