Raspberry Pi Robotics #1: GPIO Control

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to another video from explaining computers calm and to my first tutorial on Raspberry Pi robotics specifically in this introduction I'm going to show you how to write simple Python scripts how to connect LEDs to the PI's general purpose input output pins and how to use Python programming to turn them on and off to undertake this tutorial you're going to need the Raspberry Pi here I've got mine in the same case I installed it in in my previous Raspberry Pi videos you're also going to need a USB power supply to power the thing and you're going to need an SD card installed with the latest version of raspbian again as I showed you in my first Raspberry Pi video you're also going to need some LEDs and here I'm going to use three standard five millimeter LEDs and to stop is blowing up the Raspberry Pi when we connect LEDs you're also going to need some resistors here I'm going to use two 70 ohm resistors to it protect my Raspberry Pi when I connect the only these ten little things as you can see they're difficult to handle you can tell it's a 270 ohm resistor because the color-code starting from the left will be red violet Brown finally to link things together you're going to need four jumper leads here with a female connector on one end it connects to PI and a male connector utley other all here though it's just actually a bare wire these are not proper jumper leads ice cabinet e2 inside a computer audio cable and I'm also going to use a breadboard which is a neat way of just linking together and different bits of electronics and you don't need to use a breadboard you could solder things you could even twist wires if you really wanted to and but this breadboard as you'll see is a rather yellowed which indicated about 35 years old since I first got that breadboard which perhaps suggests that once you've got a breadboard it can be a rather useful device but before we start plugging things together I think I will need to show you a little bit about how you create Python code on your Raspberry Pi okay here we on the Raspberry Pi raspbian desktop I'm going to show you how to write a bit of passing code using the interactive development environment or idle so I'll run up idle while that's running up I would just stress this is exactly the same desktop you'll get in raspberry and if you've just installed it I just made a few changes of things like font sizes you can see things a bit better on the screen so we're now inside idle which is interactive because you can actually type Python commands directly here and run them so I could type for example print hello press ENTER and it would print and Hello which is not totally useful but proves a point we want to write a longer piece of code we have to pull it a new window in here we are and we could know about something like for example and puts it a little loop in for X in range naught comma 3 this will actually increase the value of X up to 3 and will put a colon in there and when we press ENTER it'll indent the next line because this bit of code will run inside the loop and we can put for example print I don't know motor on and say print motor off let's imagine those commands were controlling a motor they aren't but that's the robotics principle we're going to what we need to save our piece of code so we'll save that we'll save it in my Python code directory and we'll call it say motor print test save that and we can now run this piece of code we go to run run module then you can just press f5 to do this and you will see it will print motor or motor off motor or motor off three times running that piece of code close that down go back to our program now that's all very well but you might want some control of a motor if that was a motor or you might wanted to control how long it was on for so what I'm going to do is to input what's called a library bit of Python code isn't always available you have to tell the computer you want it so we were type import time which is a set of commands which surprisingly enough allow us to use some time command so after we've had our motor on I'm going to say time dot sleep 1 which means sleep or wait for a 1 second value and after we've had the motor off we'll also do a time dot sleep 1 save our program again file and save and if I now run the program I'll press f5 this time it's going to now motor on weights motor off motor on motor off motor on and motor off because it's been waiting for those time commands so that's a very very brief introduction to a little bit of Python coding showing you how we can actually create a loop and showing you how we can use the time command right now you know a bit about how the codes going to work we can turn our attention to hardware so I'll go to our Raspberry Pi and the first thing I'm going to do is you can see is to remove the top cover just so we've got proper access to the GPIO pins make sure you take screws out properly I'm then going to take the Raspberry Pi and with some blue tag added I'm going to secure it to a piece of a plastic we have also already secured as you can see the breadboard you don't have to tie things down like this but it will help you just make things a bit easier to get on with the wiring move this thing around and also to show you on camera so what we're going to do here is to create a circuit between the PI and one of our LEDs initially so to set that up we'll connect our first jumper lead to pin 7 on the pipe and then we'll connect that lead to the one of the first rails on the breadboard case you've got use a breadboard before the basic principle is that we have rows of connections here which are all linked together so all these connections across a links and then down the edges we have two rails which are also linked so electrically this allows us to link multiple things together so we'll take our LED this is the LED and you'll see it's got two different lengths of pin a longer pin and a shorter pin the longer pin is positive so the longer pin will connect into the same rail as we just connected to the pipe so effectively we've connected that straight through we're now going to take one of our resistors which I've actually bent around a bit so we can fix it to the breadboard and that's going to be connected in like so if I just put it in first and get the thing in there you are and what that effectively does is to connect the negative terminal from the LED through to our ground rail on this board finally therefore to make this LED work we need to connect pin 6 on the PI with our negative male connector and that's going to go through to be links to the negative rail on the breadboard it can I think be a little difficult to see exactly what's going on here on the board so here's a diagram which shows you exactly how we've connected pin 7 on the PI through to the led to its long terminal linked it through to the resistor on a short terminal and collected that terminal back to sit pin 6 on the PI which is its ground or negative connection because I want to show you how to program 3 LEDs much more exciting than just one I'm not going to repeat this stage and add in second LED connected through from pin 11 on the PI and then pin 13 on the PI again connected through to a positive terminal LED back through a resistor back to a negative rail and here you can see the set up which is all ready for us to start trying out some programming okay here we are back on the raspbian desktop I'm now going to write a bit of Python code to control our LEDs I've got a camera there on the PI as you can see so you can see what will be going on it's not quite as bright a shot as we normally have that set so we can see the LEDs light up hopefully in a minute now this time we're going to use idle again but I can't run idle here from the icon much we're going to run it from the terminal so I'll run up an X terminal I'm going to type sudo idle and here sudo stands for Super User do so this will run the idle environment as a super user or what's called in route mode it's very important you can run idle in route mode as a super user when you want to control the general purpose input output pins on the PI because if you don't it will work something to load in our recent program called motive test there it is look and I'm going to alter a little bit so we can control the general purpose input output pins are control our LEDs first I'm going to do is to import the library that allows us to control those pins which is our PI dot GP i/o as GP pipe oh so that command will load in the general purpose input output pin control library now it's important to state that this library was only included by default in raspbian on the Raspberry Pi fairly recently so if you installed raspberry and from for example the noobs setup I showed you on my first Raspberry Pi video if you installed it before about april-ish 2014 you might not have this library on your machine so the best thing to do before you start experimenting with this stuff is to install the latest version of moobs installer ice cream and you'll be sure you've got this command available right where our libraries install thousand bit heavy wasn't it but you need to do it first of all we're going to do a few setup things on their type GPIO dot set mode and I'm going to type GPIO dot board what's that all about you cry well this command tells the Raspberry Pi the numbering system we're using for the pins we're going to talk about four bring up a picture of the pins here there's various ways you could number them the board numbering system which we're using here is a simplest one which means the pins are numbered 1 2 3 4 5 6 across as you can see there and there are other numbering systems that more complex use so we'll use a simple one which is board next I'm going to do is to set up a particular pin as an output so go GPIO dot setup and I'm going to go 7 comma GPIO dot out what that command is doing you turn the Raspberry Pi to use pin 7 at an output and there are eight different pins you can use to do that with as you can see on the diagram here but here we just need initially pin and seven will then go back down into our little loop in our loop we were there printing motor on but here I'm going to control the LED by typing GPIO dot output and I'm going to go 7 comma true which will turn on and pin 7 and hopefully light up our LED will then go down here and we'll turn motor off to GPIO dot output you can probably guess this bit 7 comma false that will actually of course turn off the LED and finally down here I'm going to type GP i/o dot cleanup and opening your closing bracket what that final bit of code does is it sets the GPIO pins back to neutral after this piece of code is run that is irrelevant for the first time you run the piece of code with it isn't there the next time you run a piece of code you'll start to see error messages they won't stop things happening but it'll make things not terribly problematic and so it's better to be tidy so there's our piece of code it loads in to libraries it sets the board up in board numbering mode and sets pin 7 as an output and when it runs little which would turn up ad on off three time so I'm going to save that as a new program we'll call it link 3 because it should blink the LED three times and we will then run the piece of code make sure we can see there the M that's your board let me run the module yes everything's white properly it energy is going to blink three times isn't that amazing it certainly is but of course we've got here it's really deed so what I'm going to do is to load in a piece of code I wrote earlier I can get me menu in the right place and it's going to be running light seven you can probably guess what this does similar piece of code again those in the same libraries we've just seen again setup the board numbering here that we're setting up three pins 7 11 and 13 as our outputs we're going to run a loop with seven part to it we then going to turn on pin seven wait half a second turn off pin 7 turn on pin 11 wait half a second turn on pin 11 2 up and 11 turn on 13 wait half a second turn up in 13 and clean things up at the end of the loop and if I run that piece of code module you will see hopefully by magic there we are look we've got a running light turning on three LEDs in a nice little sequence which will run seven times and is that amazingly you might think I've seen a flashing LED before it's not very amazing or you might be thinking we have proved how we can use a piece of code to actually control an external electronic device linked to a computer and that I would suggest is a really fundamental thing to have learned how to do in this video we've started to see how a Raspberry Pi can interface with external electronics in the next video I'll take the techniques we've learned and apply them to control a motorized robot but now that's it for another video and I hope to talk to you again very soon
Info
Channel: ExplainingComputers
Views: 802,367
Rating: undefined out of 5
Keywords: Raspberry Pi (Computer), General-purpose Input/output, Robotics (Industry), LED, GPIO, Python, LEDs, robotics, Christopher, Barnatt, Python (Programming Language), Control, Raspbian, raspberry, pi, computing, programming
Id: 41IO4Qe5Jzw
Channel Id: undefined
Length: 15min 38sec (938 seconds)
Published: Thu Jul 17 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.