Paul McWhorter arduino lesson 29
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 gon na 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.
Paul McWhorter arduino lesson 29 Video
Paul McWhorter arduino lesson 29 news
Posted on Friday August 19, 2016Books You Should Read: Basic Electronics Hackaday … Continue Reading »
Paul McWhorter arduino lesson 29 Social