Raspberry Pi - How to Begin Coding Python on Raspberry Pi

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
please remember that the complete information for the class that you are about to view is at Eli the computer guy comm not only do we have our videos there but we have parts lists diagrams pictures and even complete code examples so if you are watching this video and you want more information please go to Eli the computer guy comm welcome back as you know I am Eli the computer guy and in today's class we're going to be doing a brief overview of how to start programming in a Python for your Raspberry Pi so it is important to understand when you're dealing with a Raspberry Pi that this is a full-fledged computer with a CPU with RAM with storage with a full network stack the whole nine yards and so you can use multiple different programming languages to code for your Raspberry Pi you could code in PHP you can code in nodejs just about just about any coding language that you would use for a normal PC or a normal server you can use for a Raspberry Pi but it is important to understand that kind of like the default standard for the Raspberry Pi as a programming language is Python so if you're going to be interacting with the GPIO pins those are the pins that allow you to get information in from sensors or send information out to other devices or to other things like servo motors if you want to be able to interact with those types of things Python is probably going to be the best way to go at least to start with so what we're going to be doing is I'm going to give you a brief introduction on how to start using Python on your Raspberry Pi today so that we can start getting in to doing cooler projects going forward so a big warning a warning for today is don't be a dummy what do you buy your dummies books so I personally love dummies books if somebody that's been in the technology field literally since 1996 dealt with everything from electronics to Active Directory to voice over IP system surveillance systems Arduino Raspberry Pi so on and so forth one of the things that I really love about dummies books is for 20 bucks I can order one of these off of Amazon get this on my doorstep the next day and basically in an afternoon be able to read through and get a little better understanding of the particular technology that I'm dealing with so with these Raspberry Pi projects that we're going to be doing going forward one of the things that I thought is hey why don't I grab this rad very PI projects for dummies books and I can use this as a blueprint or at least I can use this as a place to get some ideas for some of the projects that I will show you folks going forward but one of the things that I wasn't thinking a lot of things that I wasn't thinking is that there have been two major Python versions and those two major Python versions are not compatible with each other are not very compatible with each other so there is Python 2 Python 2 was very popular for a long time and then the folks decided hey why don't we create a new version of Python we're kind of gonna build on a lot of the stuff that we had on Python 2 but we're going to reconfigure something so Python 3 is not backwards compatible to Python 2 and then when we use Python 3 to go forward and so one of the issues you run into whenever you're dealing with Python you're you start looking at snippets of code is one of the questions you have to ask is is the snippet of code Python 2 or Python 3 because if you start writing a whole bunch of code in Python 2 that is not going to be compatible if you're gonna be using Python 3 on your raspberry pi or with your projects and so when I I went out and I got these dummies book I was a dummy I just wasn't thinking about it when I bought this book and this book is entirely about Python version 2 so everything in here shows you how to do things in Python version 2 which is a problem because again going forward everything should be for Python version 3 so one of the big warnings I will give you is if you're gonna be going out there you're gonna be getting dummies books or any other kind of like kind of material to start learning about Python do make sure you get Python version 3 you will still see a lot of material out in the real world for Python version 2 the reason is is because Python version 2 was such a popular programming language that met many like legacy applications were built in Python too so there's still a lot of material out there prefer for Python 2 but going forward going into the future everybody should be using Python version 3 so that's just one of those don't be a dummy don't be a dummy when by your dummies books make sure you get the dummy with python version 3 or you'll have a pretty yellow paperweight the other thing that I'll say is again when I do these classes I try to try to go down to the lowest common denominator I try to make this as easy as possible for people to understand so today and in many classes we're going to be using an IDE called Sawney so this is a Python IDE it allows you to code and in Python I'll be honest with you it's not a great ID it's not it's not the prettiest IDE in the world but it comes default standard with the Raspberry Pi so it's already built into the Raspberry Pi it is relatively easy to use and frankly it gets the job done so do be clear with this class or any other classes going forward if I'm showing you something you have a an IDE that you prefer to use use that IDE instead again python is a default standard it's built into the Raspberry Pi and that's why I will be showing it to you not necessarily because it's what I would definitely want to be going to if I was going to be building something for a production environment so anyways with that let's go over the Raspberry Pi I will show you the Thani the IDE I'll show you a few other things so you can start to feel comfortable about starting to code projects in Python on your Raspberry Pi so here we are at my Raspberry Pi the first thing look that we need to do is just check on the version of python that we're going to be using now for the most part the version of python that you have probably will not matter especially for the introductory classes when you're learning how to use GPIO pins and that type of thing but it is important to understand once you start doing more advanced projects the version of python that you have running may affect how your project works and whether or not it will actually work so the first thing that we're gonna do is we're going to go up we're gonna click on the terminal and we're going to see what version of python we have so first thing we're gonna do is we're simply want to type out Python space base version and we're going to see what version it is and then this is going to be something that may throw you a bit for a loop so again as I've just talked about we want to be coding for Python version 3 and so this is a brand new installation of the raspberry pi OS and when I simply put in Python space - - we can see that this is Python version two point seven point one six well the important thing with raspberry pi to understand is from the from the default raspberry pi OS installation as you actually install both python version 2 and python version 3 so to see the version of python 3 you're using as you simply type in Python 3 space - hyphen version and then here now we can see that for the version 3 we're using a version at three point seven point three so if you simply type in Python space - hyphen version and you see two point seven point one six you may think that you have to install version three when in reality just do Python three space - hyphen version and then you will see what version of Python 3 that you're running so that's just the first kind of thing and again if you're using the default standard you know a relatively new raspberry pi OS installation there a diversion of Python that you're using should be fine but that's how you go see these two things so we're gonna then close this out and then we're going to go up here to little raspberry pi little start menu type thing click on this and go over to programming so for programming as default they have jeany is installed and they have thawne e python ide so genie actually allows you to code for multiple different coding languages as an IDE for multiple languages fawni is only for Python we're going to go for Thani as because again when I'd when I took a look at Jamie there's a little a little bit more - Jamie is a little bit more complicated so when I'm teaching you folks how to do some of this basic Python work I want to focus on just the Python not the additional features of the IDE so we will be using the thani Python IDE so we're gonna do is we're gonna simply click on this and now it's gonna open up and so this is a little test that I was running before so I just have a Bob py so dot py is the extension for a python script so such as dot PHP is for PHP dot HTM M or dot HTML is for for HTML web pages dot CSS is first cascading style sheets in the Python world it is dot py so basically what I've done here is I've simply written just a crappy a little crampy little script he creating a variable for Bob Bob is going to equal one su is going to equal two tim is going to equal Bob a plus su then su we're going to update the value for su su is then going to equal Tim a plus Bob and then we're going to simply print Tim out if you don't really understand this again go into the future I'll explain more about how Python works for you but this is just a very very very very basic little script here just so I can give you an idea of how this works so if I go up here this is the run current script so a script that I have here that's that's that's up front if I run this we can then see that down here it spits out three so basically when we say print Tim so Tim is Bob plus su and so Bob equals one C equals two so Tim within equal three Bob plus su and that's what gets printed out here one of the things I have a window open here that shows us the variables so it shows us the Bob variable has a value of one this two variable at the end has a value of four because of what we do here and then the tim variable has a value of three now on the interesting things here is we can go through the debug process so one of the things that we need to do is stop so basically whenever we run the script we're also going to need to stop the script so if we want to rerun it again so this run current script this simply runs the script without really the debug process this here is deep the debug process so basically where you can try to step through the code and understand what's going on with your code a little bit better so if I click on this debug script the first thing it's going to do is it's going to start with line 1 and then what I can do is I can I can use these little options here step over step into or step out so basically as we get and do more complicated scripts these these two little boxes on the side will matter more but basically with a step over this is how we can kind of step through this code so we go from Bob equals 1 then it goes down to su equals 2 and what you'll notice here is the Bob variable now equals 1 then we can step down here we can see equals bond plus su so we can now see Bob equals 1 and su equals 2 then we step down one more time now we see over here Tim equals 3 so Tim is Bob plus su and then when we go down to the final thing where it prints out Tim we can now see that Sue's value has been modified now up to 4 and so this is a way that you can try to step through the code especially if you're dealing with variables you're updating the values of variables by having this little variable window open on the side as you step through your code it allows you to see how the different variables values get modified as you go through so it's su right here is 2 and then as we go down su now goes up to 4 so these are some of the things to be thinking about here basically we can get out of this if you want to hit the new button so this this creates a new pane for you if you want to do that or if you want to be able to open a script we're gonna open a script here I'm going to go to PIR test not py so this is going to be something that we're gonna be dealing with more this looks more like what we're going to be dealing with with the projects we'll be doing with Raspberry Pi and this actually allows us to interact with those view Pio pins like I was explaining it to you so basically with this we have a PIR sensor and we have an LED so we have the most motion sensor connected to GPIO pin 4 and we the LED connected to GPIO pins 16 and so basically what we have here is when the PIR sees motion we're going to turn the LED on and when the PIR sees no motion we're going to turn the LED off and then there's a pause for the code and basically it's just going to continuously be redoing this so it's looking for whether there is motion or whether there is not motion let me show you basically how I've created this project so this is a very ugly build for this project but it does it does what we needed to do so basically here this is where I have my PIR sensor my PIR sensor is connected for the the out down to GPIO pin 4 and then I have the LED and the LED is being turned on beat by GPI Oh pin 16 and basically this is what I built out here so the PIR sensor is connected to the GPIO pin and the LED is connected to the GPIO pen so when I run in this script and the PIR sensor sees motion then the LED will actually turn on and it will be green so let's go back to actually take a look at the script again I will run this and we'll do a little double pane thing and you'll see how this works so here we have Thawne II it's open we have this script open and I have the project built out now the important thing to understand when you're dealing with the Raspberry Pi is that you actually have to run your Python script in order for your Python script to work so you'll notice I'm waving my fingers in front of the PIR sensor right now and absolutely nothing is happening with the LED if your Python script is not running then your script literally isn't running so this isn't like the Arduino where you can simply upload the code to the Arduino and as soon as you turn the Arduino on it will automatically fire this you actually have to turn on the Python script you can either turn it on from the terminal or you turn it on from thani or whatever ide you're using so obviously right now this isn't working what I can do is I clicked and click on the run script now we can see that this is running down here and we can see that the LED is now lighting up so basically when it sees movement it lights up green and when it doesn't see movement then it turns off so it's very important to remember from a default troubleshooting standpoint is if you do not have the script on so if I click this off here right so it's now off and as you can see it's now I'm not running the script and so nothing is happening in order to actually make this all work I have to run the script and so I just click on the Run button and then now you can see that the LED turns on so this is gonna be a very important thing whenever you're dealing with Python scripts especially when you're dealing with these of you Pio pens it's very easy to do something stupid and literally just not turn on your script basically not run your script and then you're gonna be sitting there ripping your hair out trying to figure out you know what you screwed up with the variables when it's not the variables it's just you forgot to turn on the script so this is again this is just another exam what you can do with Python with your Raspberry Pi and a little bit of how this works so now let's take a look at a debug process and the errors that you get if you fat-finger something so let's say for this PIR sensors sensors or PI R equals motion sensor for what happens if I forget a parenthesis there one of the nice things if you forget the close of parentheses you'll see this big gray box open up and so you'll know okay if you see something like that that basically runs all the way to where you made the mistake of forgetting to close a parenthesis so that's that's a useful thing there on the other hand let's say here so we have the PIR dot when motion equals LED on so let's just say I forgot to put on or maybe I put palm maybe we put um instead of on that's the type of thing that can happen and so one of the things I can do is if I hit the run current script when I go to run this current script it is going to give me an error here so if I have this assistant open so attribute error led to object has no um so basically what this does it says okay on line eight so I can go to line eight and then I can read through I don't really get any good highlighting or anything but it does give me a little bit of something to work with I go down here maybe I did off I didn't only did a 1f instead of two we can like restart the script again again when it goes through it'll go PIR on line nine so line nine has no attribute of of right you can go down here you can also see it gives you a little bit of information down here when you so you can try to figure out what's going on and basically you can go back here and you can say oh okay so basically off that's the problem what's going on so that's a little bit of what you've got with the debug process again it's not to be honest with you it's not a great debug process it's not as it's not as pretty as some LED as some IDs have but it does give you something so that you can try to figure out what's going on so as I can step through here I get the attribute error LED object has no attribute so again if I go through the step through a process then it will like and then I can try to figure out how to fix that particular problem but to do so I'd have to stop and I have to do the on again then I have to go back to the debug process and then we can step through so that's something that we've got with the debug process well the other nice things we have for just making sure what's what's going on with our code is within the options I'll show you the tools in a few seconds but basically I can highlight wherever a variable is used so if I'm trying to figure out what's going on with the 10 variable if I simply click on the tim variable this this highlights every other time that Tim was used I'm trying to figure out what's going on with a su variable I can click on su and this highlights whenever L su was used this also highlights whenever else Bob was used so these are some of the kind of debugging tools that you have not not great not great this thing in the world but you know they they they they do they do something for you so past that we can go over here to the files menu so a the file menu this is the normal new open recent you know rename that whole type of thing then we go to the edit you know undo redo autocomplete we have a whole bunch of things there then we can go to view so you gives you a lot of options here so it has the assistant so this is the assistant over here that was basically showing us where the problems were events exceptions files so we if we want to have a little file manager here we can have that to the side heap help notes so notes is interesting so basically if you guys need a place to to write something you know you can just write you know some notes there or maybe use this as like a little clipboard basically copy and paste something there you can do that object inspector outline so you're creating a lot a larger project you can show your outline programmed tree shell stack variables so on and so forth so you may want to just go through here and play with these different views see what you actually want see what what you don't need and go from there past that you can come down here to run so you can do select interpreter and so again so right now the details so we're using the interpreter the Python 3 interpreter here so for some reason you wanted to modify the interpreter that's being used so the interpreter is what actually runs the Python 3 or the Python script when you hit the play button or the debug so if you wanted to modify what that was you can modify that there I would I would argue unless you really know what the hell you're doing not to then debug current script they have a couple of versions of the different ways to debug the script I would just simply say debug her a script nicer they were gonna go down you can run current script in terminal so if you just wanted to run this current script in the terminal you can click on this and then it'll open up and over run in the terminal so again so basically this is printing out the value of Tim so it's printing out the 3 right there and it gives you the command line if that's what you want to do if you go through here it's got some other options that you may need in the future but I probably wouldn't work mess with right now past that device device doesn't matter at this point then we can go to tools from tools we have some interesting things here so we have manage packages so do remember in the Python world a lot of the functions that you're going to use come from packages so basically you install a package in here to your Python environment and then you can use functions and resources from that package so an important one here as we go down one package we're going to be using a lot is the GPIO 0 so the GPIO 0 is a package that comes with a Raspberry Pi operating system so when we want to interact with those GPIO pins this this particular package is already here but this shows us the packages that are currently built-in to the Raspberry Pi OS so if we want to add additional packages we can simply plug something in here and there's there actually is a package named Bob you click it Bob you can find the package and if you wanted to install the Bob a package I'm not really sure what the hell the Bob package does but if you want to install the Bob package you would be able to do that so this might be important so again if you're trying to build on somebody else's project so you saw somebody else's project you want to take it you you want to you want tweak it a little bit turn it into your own one of the problems you may have is that the package that you're looking for may not be here so if you go over here you look at this PIR test so from the GPIO zero package import motion sensor an LED so that's coming from the GPIO zero package and then from signal so the signal package is actually built into Python and port pause so if you had a few other things that you're importing it might you may have the issue of you have not installed the particular package that you need so that's something there open system shell if you wanted to then we come down here to options so options gives you a lot of different things you can play with so allow only a single instance of Tony so if you wanted multiple windows of thani to be able to pop up you may want to uncheck that log program usage some information like that the interpreter again I would not modify this unless you really know what the hell you're doing the editor so highlight match me names so highlight local variables highlight parentheses highlight syntax elements highlight current line so this basically gives you some options so when I was doing the the highlighting the variables I actually checked off these boxes here so that's something that might be useful for you theme and fonts so basically you know what theme you're using fonts so on and so forth run and debug so it gives you some information here if you want to modify how things are debugged terminal basically how you want to deal with the terminal shell how do you want to deal with a shell if you want to modify that and then the assistance or the assistant that's this window that opens up over here so what I have is basically open assistant automatically when program crashes when there's an exception and open assistant automatically when it has warnings for your code so basically if this is unpacked and then you may have errors in your code but that assistant window won't open up so I've simply selected this so even if it's closed right so let me just do this so like if this is closed and I go here and let's say I know I just get rid of that right and then I go to run the script what we're gonna see here is that the assistant automatically up and it gives you a little bit more information about what the problem is with your particular Python code so that's that's something that I think is actually pretty useful you know generally it's again we can hit stop and then if the code runs properly you won't get that window opening up so that only opens up if there's actually some issues with your code and so that's a basic understanding that's a basic overview of Thani I think it is useful to use an IDE like this to start coding in Python for the raspberry pi world again is this the IDE that we will be using for bigger projects at the end of the day doubtful doubtful but but it's simple again one of the things have to be thinking about when you're learning is focus on what you actually want to learn right I want to teach you Python on the Raspberry Pi I don't want to have to teach you an entire frickin IDE suite right so this way we can that we can hyper focus just on Python on the Raspberry Pi and then once you feel more comfortable with that then we can use it a better idea so this is just a little overview for you again a big thing to remember a big thing to remember is that if you're actually using the GPIO pins right if the code is not running right so if I have the code stopped if I have the code stopped then the code won't run oops yeah no just logged up no just logged up right so okay so they're gonna run the current script so we run the current script see now now the motion sensor actually works I can see that being a problem with new people where you go and you write the fancy code and then you simply forget to actually run the script and then you're so you're sitting there trying to figure out what the issue is and the issue is simply that you don't have the script running so that's just something to keep in mind so there you go now you have a brief overview of the environment that we're going to be using at least initially in order to code Python scripts for our raspberry pi again do remember that with the raspberry pi you can use a lot of different programming languages and there was different programming languages can interact with the GPIO pins too but the default standard basically for iceberg is Python and so you know me I like to standardize if the standard is Python we will start with Python and then go from there again we will be using thani not because it's the greatest idea in the world but is it relatively simple easy to understand iate so we can focus on the Python code itself then once you understand how the Python code works you can use vs code or whatever idea that you prefer again do remember that when you're dealing with code it doesn't matter what IDE you develop the code in write code is code you can simply so basically what I'm showing you if you have an IDE that you prefer you can use that idea instead you do not have to use --though me if you don't want to use thought I just kind of keep that in mind one of the big things if you are going to be developing Python scripts for your raspberry pi is do remember you should be coding and python 3 now 3 not python 2 where this may run you into trouble again if you're like me and you buy if you buy a little book on Amazon you're not thinking about it one of the issues that you may run into is that all the projects you know in the book that you get may be coded in Python 2 now to be clear the code that you write will most likely run on your raspberry pi but again python 2 is not that not the new standard it is trying to be deprecated and especially if you're learning you know you might as well be learning on the new technology the Python 3 instead of learning on Python 2 and then having to relearn things on Python 3 you know that's that's not necessarily a great way to go for your educational curriculum so anyways as always I enjoy doing this class look forward to seeing the next one if you like the content that I create please think about going to e line the computer guy comm and becoming a member or donating please understand that all the educational videos are in front of the paywall that includes the videos that includes the notes the diagrams and the code example all of that is freely available and in front of the paywall but if you want to watch opinion videos or if you want to be able to comment you do need to become a member membership is $5 a month or $60 a year gives you access to those opinion videos and the ability to comment if you don't want to become a member you just want to give a one-time donation there is also a donate button where you can do that please understand in order to provide the education that I am it does cost money the server's cost money equipment cost money travel cost money all of these things cost a reasonable amount of money and the fact of the matter is is YouTube's advertising program no longer supports creators the way that it used to so if you want to these classes to continue to stick around and you find them to be valuable please think about either becoming a monthly member or donating a few dollars for this project
Info
Channel: Eli the Computer Guy
Views: 143,190
Rating: undefined out of 5
Keywords: Eli, the, Computer, Guy, Repair, Networking, Tech, IT, Startup, Arduino, iot
Id: xTIBG_KD8Bk
Channel Id: undefined
Length: 29min 15sec (1755 seconds)
Published: Wed Jun 24 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.