Learn MicroPython - Part 1 Controlling the flow

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey robot makers do you want to learn how to use micropython right from the very beginning then this is the show for you come with me as we build robots bring them to life with code and have a whole load of fun along the way okay let's go over to our Keynotes and we'll have a look what today's show is all about so yeah we're gonna have a look at micro python right from the very beginning so if you're a learner you're looking to get started with micro python then this is exactly what this show is all about so we're going to look at what is micropython why is it even called micro python where'd you get it from how to install it why would you use this instead of something like C plus plus the things that you need to write the code IDs and we'll look at what IDs are and then we'll write our first program as well hello world so let's get over to it shall we so right over my desk over here I've got a number of different devices which are all micropython devices so pimroni tiny2040 we've got the M5 stack there we have the Adafruit blue fruit which is a circuit playground device we've got the feather as well which is also another Adafruit device but the micro bit over there that can also run micro python we have the Raspberry Pi Pico that's the one with the Pico W with the Wi-Fi got an esp32 camera there is an esp32 wemos D1 mini there's any this is an esp32s and we've also got a couple of other devices as well on the end here so we've got the Pico lipo from pimeroni which is an RP 2040 micropython device and finally we have the Arduino it's got a very long name this one Nano rp20 connect that one is also an rp2040 based board similar to the Raspberry Pi Pico so there's a whole load of devices that you can actually get to run micro python okay so so what is micropython and why is it called micropython so micropython is a small version of the programming language Python and it's been designed specifically for these really small micro controllers they're all 32-bit so that's one of the restrictions we can't run micropython for example on an Arduino Uno the name comes from the fact that it's a very small version specifically designed for microcontrollers so that's where we get the microbit from python because it's based on the Python language and python gets its name from a TV show so there was a TV show back in the 70s that was called Monty Python's Flying Circus that was a BBC comedy series and you can see in the quote that we have there when we began implementing python Greedo van rossen was also reading the published scripts from Monty Python's Flying Circus a BB comedy series from the 1970s van Rossum thought he needed a name that was short unique and slightly mysterious so he decided to call the language Python and it stuck very quick background of micropython so this was actually created by Damien George with the initial release on 3rd of May 2014 so it's about eight years old as a recording of this show it's actually a program itself that runs on the microcontroller and is itself written in the C language and the logo for micropython is an M but it's also a snake it's not the snake's eye just on the end over here so where do we get micro python from so this can be installed on many different 32-bit microcontrollers as we've just seen on our overhead over here and you'll see a small thumbnail when you visit the micropython.org website so you've got to micropython.org downloads you'll be able to see all the different boards that are supported there's a thumbnail image for each of them if you click on that you'll then open up a page specifically with all the different versions on so let's go and do that now and have a look what that actually looks like so I'm over here in my browser I've gone to micropython.org and we're going to click on the downloads button and then if we scroll down a little bit you'll see all these different thumbnail images of the boards that we're interested in so I'm going to look at the Raspberry Pi Pico today so I'm just going to keep scrolling down until I get to the picot which is near here now there we go Raspberry Pi Pico I'm going to click on that and we can see there that there's the firmware and the releases so the very latest release of as of recording of the show is 1.19.1 and we can see that was made on the 18th of June 2022 so if we click on that image there it has a uf2 file extension so that's now downloaded to my my local computer I can now use that to flash the firmware on the Raspberry Pi Pico that we're going to flash in a second so how do we install micropython there's a couple of different ways of doing this we could do this from the command line if you're familiar with the terminal that seems a bit over the top from my skills point of view the easiest way is to get to thorny so thorny is another program and this is available for Windows Macs and Linux computers and all we need to do is go to thorny.org download thorny open up thony and then we can start to install micropython so I'm not going to show you how to download it I'm sure you'll be able to do that part but what I will do is I will show you how to install from funny so installing micropython using the thorny is very very straightforward it's designed to be as easy as possible so we're going to show you this in a second what we're going to do is we're going to click on the very right bottom corner of the funny window we're going to click on that that's an open and put a dialog box which enables us to install micropython and then we're just going to click the install button and hey Presto we've got micropyton installed on our device so let's go over to Sonic and do that now okay so here I'm in sunny and the very bottom right hand corner over here there's a little button if we press this it might say something slightly different than yours we can install micro python so what I'm going to do I'm going to get my my board and I'm going to put it into the the mode that we need which if I just show this overhead camera over here so here is my Raspberry Pi picot I'm going to hold down the boot select button and I'm going to push the cable in like so and what that'll do that will tell the device to go into its boot select mode and be ready to have micropython installed on it so if I now go back over to this button here I can now see that there is an install micropython option that's available we can also install circuit python but for this tutorial we're going to look at micropython only okay so let's go full screen on this one now so you can see here it says the target volume so I'm on an Apple Mac so I get this slash volumes if you're on a Windows PC it will look slightly differently but not too different and the name of the volume it will be RPI Dash rp2 and what we're going to do is we're going to choose which micropython variant we want and we're going to go with the most common one so I'm using a Raspberry Pi Pico it's not the Pico W so I'm just going to select that option there and I'm going to click the install button and you'll see that it says copying and it'll just take a couple of seconds to finish that copying and then we have Micro python installed in our device there we go so we can click close And if I now click this stop button up here we can now see micro python version 1.19.1 which is the release we've we downloaded is now available on our device so why use micropython why not use something like C C plus plus instead so micropython has been designed to be easier to write cleaner and clearer to read and it's faster to get the results that you want than other languages such as C plus plus when I say faster to get the results I don't mean that the code is going to run faster I mean it's easier to write the code run the code and get the results that you're after C is actually faster by an order of magnitude than python depending on what you're doing in many cases you're not even notice the difference particularly for robotics it's more than good enough to use micropython so compare the two examples on the right hand side of the screen here very top we've got some C plus plus code so if you're writing this as a beginner there's a lot of stuff in here that looks quite esoteric looks mysterious so there's a hash include speech marks stdio.h what does that mean if you're not familiar with C plus plus that might look quite mysterious then we've got the word void what does void mean is it a black hole then we have hello world and we've got lowercase hello with the capital on W for the world some brackets some squiggly brackets print F and then some brackets and hello world then a semicolon at the end of the line we've got the closing of those brackets and then we've got this void main brackets squiggly brackets hello world brackets colon close with squiggly brackets it's a lot of brackets going on lots of things like semicolons at the end of lines and things that we don't understand that we need to include compare that to micropython now we don't understand what def is yet but it says def hello underscore world there's some brackets and a colon then it says print hello world and it looks like there's actually a typo there this should be a an extra speech Max at the end of that otherwise we'll get a narrow we'll try this code in a minute and see what happens and then we've simply got hello world so what's going on here is that we're defining a function called hell world the function prints hello world to the console and then we're simply just running it that's the exact same call that's going on up here in C plus plus but there's a lot more stuff that we need to do there and a bit more knowledge required just to get those basic results the only thing we need to know in here is to to use Def and the brackets to define a function we need to know the print command to print things to the console and then we know how to run the function which is just by using the same function name on its own we've also got a couple of characters indented there on the print and that's to it tell micropython that this is a block of code related to that hello world program so to write our code we need an integrated development environment we could just write this in notepad or using something like idle which is the integrated development learning environment for python but it's much better to use a dedicated editor that's been specifically designed to help you write code so there's quite a few available there's one that's called Microsoft Visual Studio or vs code that's my personal favorite I use that to write code I tend to use thorny if I'm interacting with the the micropython device and I need to tweak things because it's actually easier to do that in thony than Visual Studio code at this point in time there's also atom which is similar to vs code that works on all the different platforms Mac Linux and windows we have pycharm we have moo we have thoni which we're going to be using quite a bit in this tutorial and we have Microsoft make code as well so that's a web-based IDE and finally there's the Arduino IDE now arduinos tend to be a form of C C plus plus that's called processing and that compiles code onto like a machine code that that you then flash onto whichever board you're using but they've recently updated this so it can now actually code in micro python too so I'm going to be checking out later on so the development environment will enable you to create faster more accurate code with features such as autocomplete so you start typing something it'll complete the word for you it's got things like code hinting which can help you understand what the parameters are for your functions and so on I also has like line numbering as well which can just really help most development environments also have code highlighting so as you type the words they change color depending on what their meaning is and it's quite a nice useful extra thing to have compared to just a plain text okay so let's make make our first example shall we so the tradition is when you're writing a program the very first program that you'll learn to write in any programming language is hello world so we're going to do that right now so I've got funny I've got my device Plugged In which is just over here let me just get that just in the screen there at the moment it doesn't look like it's doing anything you can't really see anything running on it but it is there and we will be able to write a program that does something more interesting a bit later on so the first thing we need to do is we need to print we need to have a bracket we need to have some speech marks I also write my speech marks and close the brackets and then sort of back arrow into the between them it's just easier then remember to close the speech marks out and then we're going to type the uh the phrase hello world exclamation mark that's all we're going to do we're then going to run this code so if I just move this down there's a green button over here that says run script so if I click on that run script we can see down here in this shell it says hello world now that hello world program has actually run on this micropython device it has run from that device and then presented the results back to us we can actually type things down here this is called the rep hole the read evaluate print Loop area and we can type the program in here if we wanted to so we could do hello hello world and it will run that one line one line at a time so it's much easier to write your programs I find in this top area here you can actually save everything that you've done have many lines of code on there whereas down here once you're written it it's essentially forgotten but that you've written that so that's our very first program so let's get back to our tutorial and see what we're going to do next so the next thing we want to learn about is variables so variables are like little boxes I always think of these pigeon halls I used to work in in education we had this whole wall full of these little pigeon holes and back in the day people used to keep pigeons in these little things and have a little door that they close it up and then they would open the door put a little message on the pigeon and let it fly away or whatever so this is where these pigeon holes get the name it's quite an Antiquated way of storing things but we used to have a whole wall full of these and you'd have like mail in there essentially just have notes and things packages that kind of thing and your name would be underneath the Shelf from a variable point of view what are variables well what we want to store a value we want somewhere to store it first of all so we need some kind of area of memory in our micropython device and we want to give it a nice friendly name so that we can find it again and not really understand what it is without having to look inside it so in this example that we're going to look at we're going to store the value 1 in the variable that's simply called a and we're going to assign it using the equal sign so let's go ahead and do that now so let's just remove that first program and we're simply going to do a equals one so let's run that code and nothing happens so what what's happened in the background is we've assigned that value but we can actually see what that value is doing so if I just do a I can see what the value of a is let's do another one let's do it down here in the Ripple b equals two if I now type B and enter I can see B is two if I say C equals a plus b guess what's going to happen going to be what value three so that's just gone off screen slightly there there we go so assigning values to variables is really useful if we want to do things such as let's say instead of storing one in a variable called a that's a bit abstract let's store an age so let's store an age of 47 I don't know what that could relate to and Well on our little program we'll just print out age like so so we can use the variable name in our print statement and it will print out what the value of that variable is currently holding so in this case we've assigned 47 so when we print out age 47 is what we get back and we can do other things with variables we can store more than just numbers we can store things like name so let's say name equals let's say Kevin and then let's print out name and let's run this little program so we can see there that we've printed out 47 which is the value of age and we printed out Kevin which is the value of the variable that's called name that's what we can do with variables we can store values in them and then we can call them by a particular name so when we think about variables we can think of them as little boxes that store things and we can give those little boxes names so it makes it easier to read our code and understand what's in these these variables we can put things into boxes and we can also compare what's in them so we can say compare a with B let's have a quick look at what that would look like let's go back over here so we previously said a equals 1 b equals two we can say a equals B and that's not true a doesn't equal B because a is one and B is two if we make b equal 1 as well oops b equals one and then we do a equals B that's true because they are both the same a is one and B is also one so we can do kind of comparative things with them as well we don't even have to look what's in them we just want to compare are they the same if variables store things that change constants store things that don't change so why on Earth would we want to store something as a constant well say we've got a value that never changes so the value of pi for example never changes that's always going to be 3.14159265 and so on that's never going to change if we're doing things with triangles with circles with angles syn costs and tan that kind of thing then we might want to store pi as a constant and then be able to refer back to it so it works very similar to storing variables but the convention is that constants are always stored in uppercase so we have like a capitalized Pi so let's go and do that on on funny because let's clear out our program let's just clear the uh the messages there and then they say Pi in uppercase equals 3.141592654 and the recommended way of doing this is actually to say const and brackets and then the value so we run that now and we type pi we can see that Pi is 3.141593 it's rounded up a little bit just because it's a very long floating Point number it does actually have the full value stored in pi but just printing out to screen um it's going to do this that's how we can store constant values we can store them using the constant type which is uh this thing in Brackets here let's have a look at another example now so we're going to head over to thony again and we're going to write our second program okay so we're back over in thoni and we're going to write our second program now this one is going to use variables so what I'm going to do now is I'm going to do the hash symbol or pound if in the US and this is how we can write comments and comments are quite useful in your code just because it can help you remind things you can keep notes and so on what I always like to do is just type the name of the programs this is like variables I also like to type the date that I brought this on so I'm going to do 20th of November 2022 and this is going to be a little problem that does something with names so what we're going to do is we're going to say name equals and then input and then we're going to say please type your name like so so input is a new function that we're going to use and that will get input from the console and it will store it in a variable so we're going to assign it to the variable name so let's just have a little space and an arrow after that and then we're going to print out a message which just says hello and then comment and then name which is the variable there so let's run that let's save this first of all so I'm going to save this one I'm going to save it to the computer first and I'm going to call this one example two not pi and we can see there there it is and then we're simply going to run this so let's just click run so it says please type your name so I'm going to go down here and just type Kevin and it says hello Kevin so let's have a look again at that now interestingly print has done some nice things for us here as well as printing out the word hello it's also printed a space after the word hello before the variable name so it just makes it a little bit easier to read for us so that's the first thing we want to do now let's extend this a little bit further what we want to do next is bring an age in so let's do a second line instead of printing out straight where we'll say age oops age equals input and then please oops please import your age and then we'll do a space and I'll put a space after them as well and then on here we can then extend this print statement out so we do comma we'll say how does and then age and then comma and then feel like so so let's run this one so type your name so I'm going to click there and type in Kevin tap your age it says hello Kevin how does 47 feel so we've done quite a few things there we've assigned some variables one we've assigned some some what we call a string of text a string the second one we've assigned an integer the whole number we can see it's an integer because it hasn't got a point zero or Point decimal point after the the number and we then just printed them out to the screen so let's run this again but this time we're going to type in nurse I'll do Kev on here and for the age I'm going to say 47.5 oops 47.5 and it says hello Kevin how does 47.5 feel so it still knows this is a number but it's treating it as a floating Point number or a float as we call it in micro python python can store values in different ways it can store them as different types so we've got things like strings of text we've got integers which are whole numbers and we've got floating Point numbers which are numbers with a decimal point okay let's get back over to our tutorial and continue on so if you're enjoying this tutorial please give me a like on the video give me a comment let me know if you're following along with this what what your think your skill level is are you a beginner an intermediate or an advanced python user have you used any other programming languages before such as Arduino C C plus plus lower let me know in the comments and if you've not ticked the Bell already tick the Bell click the Bell if you're not click the Bell already give the Bell a click and that'll also help the the channel grow a little bit more I do go live every single Sunday at seven o'clock GMT today's actually an exception I would normally go live on a Sunday but today I'm actually traveling down to Raspberry Pi headquarters tomorrow so I want to uh get this one out and I'll actually be traveling during the the normal slot that I go live so this is a bit of an exception today okay and if you haven't already joined our Discord server you might want to head over to casualbox.com Discord join up there it's completely free and you'll join a growing community of people just like you who are interested in electronics programming and Robotics and if you want to follow me on social media you can head over to Instagram I'm at Kevin McAleer on Instagram I'm at kev's Mac on Twitter if it's still around when this the video goes live and I'm also now on Tick Tock I'm Kevin mcclure6 on Tick Tock so uh follow me there if you can and if you want to help support the show there's quite a few different ways you can do that you can do a super thanks you can do a super chat if you're watching live and if you go over to casualbox.com you can also buy me a coffee there as well and uh you'll get your names upon the the credits as well if you do that and if you also want to join the YouTube membership program um you can do that too just by clicking the join button at the bottom of the screen I think you have to be subscribed before you can see the join button but uh give that a hit if you're already subscribed and you can join the membership program too so support us yes thank you so much to everybody who's supported the channel so far so we've got uh three different groups of people represented over here I always get the way around uh so we've got somebody uh bought me a coffee this morning we've got um uh maker Schultz there we've got Frank we have uh Dana Huff members we've got chemi we have Steve Phillips Thomas wiser and then we've also got new YouTube members so we've got uh chair lights and we've got Michael we have uh Fraser Bill Hoy Josie we have Jeff Johann John Paul and we have Tom as well so if you want to see your name over here get that the right way around head over to kennisrobots.com credits and you can get your name in the credits as well okay so I hope you enjoyed this short video and I shall see you next time bye for now
Info
Channel: Kevin McAleer
Views: 11,269
Rating: undefined out of 5
Keywords: Kevin McAleer, Small Robots, MicroPython, learn micropython, MicroPython for beginners, micropython, circuitpython, beginner, how to learn micropython, how to use micropython, micropython from scratch, raspberry pi, raspberry pi projects, raspberry pi pico, small robots, robot, python, robotics, pico, python programming, learn python programming, python crash course, python programming for beginners, python programming full course
Id: TkuDQ-SzXwU
Channel Id: undefined
Length: 22min 35sec (1355 seconds)
Published: Sun Nov 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.