MicroPython Basics: What is MicroPython? with Tony D! @micropython #LIVE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

I can see excellent uses for micropython when prototyping. I would like to switch from the Arduino IDE. The challenge I have is the vast amount of libraries available for the Arduino IDE that are not available for the micropython environment. For example, drivers like one for the RFM69 - where network protocols/timing are critical. Is there an easy way to port libraries?

👍︎︎ 1 👤︎︎ u/m_k_johnson 📅︎︎ Nov 14 2016 🗫︎ replies
Captions
hey it's Tony from Adafruit and in this video I want to talk about micro Python so normally I do like Raspberry Pi related streams so this stream and maybe some future streams are going to be looking at an really cool project called micro Python and even if you are a raspberry pi users stick around and check this out because I think you'll find this pretty exciting so I want to talk about what is micro Python what it can do show off some demos and maybe help you understand if it's something that's worth checking out and just at a really high level micro Python is a version of the Python programming language that you can use on tiny little embedded devices like for example a little board like this this is the PI board which is like a native micro Python board it's actually a little ARM Cortex I think in m3 or m4 microprocessor so fairly powerful little chip but it runs a version of the Python programming language which is really cool because if you've used like arduino or even just bare metal c c++ programming for embedded systems you've met you probably ran into some challenges you know using those low-level languages you need to manage memory you need to you know use the right syntax you have to compile the code you have to upload it the board somehow compared to something like python which is a high level language or an interpreted language you know python has a much simpler syntax very beginner friendly very nice like error messages even an interactive prompt where you can go in and start running python code and getting results back immediately so really cool things and all of that is possible on some of these microcontrollers and some of these small boards and so that's what I want to show in this video is just some examples of what you can do and it might surprise you it's pretty capable as far as what micro Python can do for you so let's just kind of dive in and I'll show you something first that's kind of the basis of this video so I just published a guide and I'll put a link to this guide in the description below and this goes up on youtube so you can check it out yourself but a little guide where I'm basically gonna talk about the exact same thing here so it's Mike replied on basics what is Mike Rowe Python and it's fairly short it's just one page a bunch of questions kind of FAQ style so you know I just answer questions that you probably have primarily what is Mike Rowe Python so I talked a little bit about that and then go into what are the differences between it and Arduino and so that's what I'm going to do I'm going to talk about you know what it is like I mentioned it's a version of the Python programming language and if you aren't familiar at all with Python so here's a link to just Python org that's the home page for Python and you might already have it on your computer right now so if you're using like a Macintosh or Linux machine very likely you have Python installed just open a terminal and run Python and if you get something like this you've got Python and you know you can uh here's your first program if you've never done anything HelloWorld print that out and hey look at that your your Python programmer now and if you're on Windows it's really easy just run a little exe to install it and you're good to go with it so definitely worth checking out even if you're not interested in vetted systems it's a very nice language like I said meant to be very beginner friendly so they're showing examples of Python code right here so like here's a function to write out the Fibonacci sequence of numbers and so you can see it's very simple there's not a lot of extra junk I would say so if you're looking at a version of this in like C or C++ you're going to see like curly braces and variables declared with types and all kinds of things and not to say that that's bad or that you know using C and C++ is wrong they're definitely valid reasons to use that but as a beginner something like this is a lot more friendly and approachable compared to some of those low-level languages where before you can even get code running you've got to just understand the tooling and the workflow and compiling code and getting through compilation errors and what's a header and what's a CPP file and you know what are objects you've got to link together and just you know you haven't gotten the code on the board yet and you've already learned all these crazy things with Python it's a lot simpler and by design you know it's meant to be more expressive and more powerful so that you can you know write just a few lines of code that do a lot of things and another big important thing with python is that they call it batteries included we're basically Python has a standard library that just comes with every Python installation and that has tons of functions that you can use so you don't have to rewrite all the code like if you're talking to a web service and you need to parse JSON encoded data you know in C and C++ you've got to go find a library to do that you've got to go figure out how to include the library in your code you got to figure out how to compile it you got to keep it up-to-date with Python install Python import JSON you're ready to go that's you know you've got JSON encoding and decoding lots of powerful capabilities and so the standard library Python is vast it includes all kinds of things for manipulating data talking to network services and network protocols even just simple things like mathematics and stuff there's all kinds of great functions and so that makes it really useful because they want to empower you just right out of the box to get started and start using it and microbiota is really similar now I'm not going to say it's exactly the same as Python so you know clear distinction is that micro Python is a separate project from Python it's open source it was created by a couple people you know really awesome job that they've done to bring it to where it is today there's a community around it that's starting to form but it is a separate project so it has a separate code base and it has slightly separate goals so it's meant to run on these little embedded systems which means that you know maybe it doesn't have all of the capabilities of desktop Python but it's got quite a few there you know there's very few differences between normal Python and micro Python and honestly the difference is I'm not even going to get into them because they're so technical it doesn't matter you're not going to run into them in most cases but one difference that you might notice the standard library you know you just can't fit the same breadth of things onto these little embedded chips but micro Python comes pretty close so like I said JSON encoding and decoding they've got a version of that in micro Python if you're doing like regular expressions and string processing something in Arduino that you probably think is like impossible because there's no function for that and good luck getting a library loaded in to do it's very easy in micro Python it's ready to go it's right there file system access you can create files you can read files even if there is no file system or SD card or you know thing on your device you can create files in flash memory potentially and I'll show demos of all of this so you know very powerful very easy to get started with and use you know just like the Python programming language and meant to be that batteries included to have a lot of functionality built in okay so enough kind of talking about it let's just dive in and run some demos real fast so I'm going to show a few things off and you know like I said I'm hoping if you've used Arduino or you know other embedded systems I want to show you that you can do very similar things with Python code running on these little chips using this amazing micro Python project and by the way too I'll put a link to micro Python org is the home page for it so this has some good links but really check out the guide that I wrote because I tried to collect a lot of different resources here I've got links at the bottom that kind of points you towards okay here's where you can learn more about stuff here's where you get some help on things so check out this guide I've kind of tried to distill everything you need to know to just understand what it is and what it can do in here so okay enough talking let's jump to the workbench shot and let's remove that okay so we'll go here well actually let's go back to the main shot because we need a we need to see the code and things like that too so okay so what I'm going to do I'm going to start with this guy right here and so look in the upper right corner this is the pie board and I'll show you real quick link to it I guess the rendering is a little messed up here let's search for the pie board real fast but this is the basically the first micro Python board that was available and it's a really nice little board so it comes to you automatically loaded with the micro Python firmware and so you're just ready to go you plug it into your computer and like I said it's got a pretty powerful arm cortex chip on here so very capable and so let's plug it in and let's see what we can do so I'm just going to plug in it has a little USB port on it and then I've just plugged this cable in my computer here move that over okay so I plugged it in and then what I'm going to do here so we'll clear my terminal so I'm just going to connect because this thing shows up as a USB serial device so if I look at my serial devices on this Mac you know if I search for all the USB devices you can see this USB modem 1 4 3 4 2 2 so let's use the screen tool to connect to this serial port so screen just a real simple terminal if you're on Windows you could use putty or I think mini-com maybe that's not a Windows to anymore it's been a long time since I've done serial stuff on Windows if you're on Linux and Mac the screen tool is a pretty good one to use that's what I'm going to use here anything that can open a serial port doesn't matter you know even I think there's a web serial API now so that eventually web pages will be able to access serial ports but for now we'll use a tool so we'll point it at our serial port here and then the baud rate is 11 v 200 bond so we'll do that and okay this is pretty cool like we've got a Python prompt right here and this is all coming from this chip none of this is running on my computer this is all on this tiny little arm cortex chip here and this is real Python so if I want to do hello world I can just say print hello world and look at that you get a response back and it really is Python so like you can do string formatting for example like if I wanted to print out you know hex number and then we'll use the string formatting syntax and you know let's get fancy or let's put o X in front of it and we'll say you know we're going to put a variable in here and let's Pat it with up to two zeros uppercase hex characters and then you know that so that's the syntax and Python basically for the string format it has this whole language but this is cool because if you ever try to do this in C and C++ you're going to want to use like you know printf but on Arduino you don't have printf and so now you've got to like hack this in yourself so you know watch this so let's just give the format command a hex number will give it a small one you know how about the number nine or something like that just cuz I want to see that it pads it with zeros and so now I print this out X number Oh X Oh nine you know or let's let's go for a higher one like 255 Oh X F F so that's pretty cool like this is exactly how you would use Python in your computer it feels just like normal Python code very expressive you know very simple and straightforward to use like this so this is pretty cool the next thing I wanted to show let's go back to the Python homepage here so they had an example here of defining the Fibonacci sequence so how to print out let's do it let's type it in like you know we're just gonna take normal Python code and throw it in here so let's define a function like this might be blowing your mind already if you're use to Arduino you know you we're doing this all dynamically on the board like there's there's no compilation there's no upload to the board you know I'm gonna define a function and call it just directly on the board like this is pretty powerful and cool here so okay so let's go back and just to make sure that I don't make any embarrassing mistakes on live video you know we'll we'll just type in exactly what we see here so this is actually a pretty nice implementation of the Fibonacci sequence because it doesn't use recursion which is just a different kind of I wouldn't say style of programming but a different way to implement this which has some trade-offs so they're actually just using a loop here so we're going to start with values a and B and you have to seed them with an initial value and then while a or while a a is less than n so basically this is a function where you're going to pass in you know I want the first the Fibonacci sequence up to the value like a thousand for example so we're going to loop through all the numbers less than a thousand and then the way this works you print out your first number and then you increment the number so the Fibonacci sequence they show the example here you start with 0 and 1 and then the next number is the sum of the previous numbers so 0 plus 1 equals 1 and then 2 is the sum of 1 plus 1 so you get this value and you just keep going with that so real interesting mathematic thing but this is a real example of programming you know we're doing a loop we're computing things in here this is exactly what you would do with normal you know code it's just you know it's not necessarily a toy example here so okay so let's finish this up let's print out a and then we'll tell it to end with a space instead of a new line because normally print adds a new line but we're going to keep this all on one line and then they do a fancy little thing here you know you can do tuple unpacking where you've got like two different values here and we can assign them different values with this comma operator so you know if you're used to Python and I'll show you some more examples like get ready to have your mind blown like this is this is still pretty amazing to me how powerful you know I'm going to show some example in a second of defining like a lambda function and doing functional programming on a microcontroller this is pretty amazing okay so let's go back and so we're going to set a to the value B and then B to the value a plus B so this is kind of the magic step that says okay just go in order computing you know the next value is the sum of the previous values right here basically for this and then let's see they end it with a print so this is just going to add a new line at the end and then we're done defining this function so let's call it so let's call fib of a thousand and hey check this out so we get a bunch of numbers here and these look awfully similar to the values that we expected right here so that's pretty cool I just took some Python code typed it into this prompt ran it on this little arm cortex chip and it ran exactly the way we expected you know no drama here this is pretty cool so again you know just trying to show that you can run real Python code on a little microcontroller now this microcontroller this has a little LED built in so if I remember correctly I can use it if I import this PI B library and then the LED I can say equals pi LED zero I think one I have to do and then I can say LED dot on and notice the red LED just turns on and then LED off so it turns off so this is kind of cool and something you can't do with Arduino you know I have this interactive prompt and so if you're learning about hardware you know maybe you connected up a device to this board because it has all these general purpose input and output pins and you just want to start playing around and talking to this device you know just open up this little interactive prompt here this is called a ripple repl a read evaluate print loop because that's what's happening here you know it's reading input that I type in it evaluates that as Python code it prints out the results and then it just loops over and over just waiting for me to print in or type in more python code and this is not the only way that you can use micro Python so in later videos I'm going to show a better workflow where you can load things on to the board you know you have a file that's run that's has your Python code so this is you know more showing off that you can explore with code on the board and again very powerful you can't do this with Arduino you can't just open a console and type Arduino code you've got to upload it you've gotta you know push it out to the board you've got to compile it so this is pretty cool for learning okay so we showed up a few things here let's jump to the next board so I'm going to close this terminal session and let's check out this thing down here so this is the huzzah feather huzzah esp8266 so this is a really popular Wi-Fi microcontroller and basically this microcontroller pretty inexpensive so this board the feather is aa version I think is around $15 if I remember correctly yeah it's about sixteen dollars which for this style of chip you know this is a really good deal now you can program this with the Arduino IDE if you set it up and you install some different tools and things and so you can run code on here just like you're running Arduino but you can also load micro Python on here so I've gone ahead and I've already loaded the micro Python firmware on here and you know go to this guide if you're curious and at the end I link to you know more details on the esp8266 and you can see the documentation for that tells you how to load the firmware and stuff like that but this thing's running micro Python so again it shows up as a serial device so if I look at my serial devices or at least it should show up as a serial device only make sure that I've got it connected here correctly so see I'll put this back in there there we go and oh that's right it doesn't show up as a USB device so there's a different chip on this board it's this sy Labs USB to UART chip so that's what it shows up as for me but let's connect to this thing so we'll do screen and again we'll point at this device T key Y dot SCI Labs and then same thing 11 v 200 bond and okay so you open this one up and it doesn't give you that nice little help but just press ENTER and hey look I'm in a Python prompt here again so you know print hello world and it's it's micro Python you know we're ready to go with this so I want to show a couple things with this board I've got some stuff connected to it so I have this is a dht22 style sensor it's actually this AM 23:02 sensor but it's basically a temperature and humidity sensor and so this is connected to the little esp8266 feather huzzah and then these are a little ring of neopixels so there's 12 neopixels here and i'll show that you can light these up and control these all from micro Python code so this is pretty cool so okay so let's start with maybe the neopixels first so to do this you import the neopixel library and then you also have to import this machine library because this lets you access the pins that are connected or the GPIO pins on that outside of the board and this is just standard Python syntax you know these are modules we're importing these modules if you're really familiar with Python like the derp command can kind of show you what's in a module you can see like this has a neopixel object in here they actually do implements the help function so if you want help on this module I don't think there's much it tells you okay you know there's a class in here called neopixel obviously this is not the same as pythons native help command which looks at the document strings and functions you know again some compromises have to be made to run on these little systems but this is pretty cool like you don't have this kind of help in in arduino ide usually so okay so let's actually create a neopixel object will say pixels equals neopixel and we're going to use that neopixel class now it takes in a couple parameters here so you have to pass in a pin and that's actually a class in the machine module and the pin that i have connected to the neopixels it's this green signal pin is pin number 15 I'm gonna do something this is wrong I'm just I'm going to leave out a parameter so I do this notice I get a nice error back it seated through an exception so there's actually exceptions you can use just like a normal Python type error function takes three positional arguments but two are given now you might be a little confused you actually just see one argument here but in Python because this is a class there is a kind of an implicit self variable that's at the start so you know even though you see one Python actually sees two parameters here but this is telling me that hey you did something wrong you're missing a parameter so this let's go back and I have to tell how many pixels there 12 pixels so I do that okay and now to light these things up so you know if you've lit neopixels with arduino it's pretty easy the neopixel library is excellent it makes it really straightforward but not as easy as this so you've got you know just an array basically you can access any of these twelve pixels so pixel zero equals and you pass in a tuple you know three values the red green blue values so let's say red is 255 green zero blue is zero you know you don't have to mess with like manipulating colors and bytes and things just set the components again very expressive easy to understand you don't have to learn all these details about like you know types of objects and stuff okay so that sets pixel zero now notice none of the pixels changed just because you set this in the buffer and then you called a pixels dot right function and that will actually turn on pixel zero as as red and you know you can control other pixels so will say like pixel one equals about green instead of red and then pixels dot right and most CDs go out so hey we've got a red and green pixel now and you can animate stuff so let's throw a loop in here you know an infinite loop while true and let's actually before I do this let's import the time module because I want to do a little bit of delay you know when I'm animating stuff it might run too fast so I need to slow it down and we'll say while true and then let's say let's just have it light up the red pixel and animate it moving around so we'll say well actually it's not me doing a while loop let's do a for loop for I in range of 12 so this is the standard Python syntax you know going through the values 0 through 11 and then let's say pixels I so this is going to start with pixel 0 then move on to 1 and 2 and so forth equals 255 0 0 and actually before I do this I want to make sure let's actually turn off all the pixels so it has a fill command that can push out the same value to all the pixels and we're just going to send the color 0 so this turns off all the pixels effectively and now let's say pixels I equals red so I'll do that and then let's do pixels dot right so we'll light up the pixels and then how about time dot sleep so this is just the standard Python function for delaying for a small period this takes a value in seconds and it can be floating points we'll say 0.5 you know half a second of sleep time here so let's do that and I'll hit enter and hey watch that so we've got our little pixel moving around here so it's just cycling through all the positions and it stops when it gets to 12 so that's pretty cool in my opinion I mean look at how simple that code is it's I love it because there's really nothing extra here it is expressing exactly the intent of you know what I wanted to do like I clear all the pixels and yet maybe this syntax is a little bit funky you know be nicer there's like a clear function you could write that though there's no problem with that but you know you clear the pixels you set the current pixel while you're looping through you know all these values you push out those color values to the hardware you delay for a small period you know there's no extra junk in here we're not like defining local variables and giving their types and all these complex things so that's pretty cool and you know in my opinion that it's you've got this expressive power okay let's do the DHT sensor real quick so again this is just a module you can import so the DHT module is what they have and you just need to create a DHT object so I'm going to create the DHT dot 22 object here and again this has to take in the pin that it's connected to which is machine class some machine dot pin and it's pin number twelve that I have for this hardware here so I do that and then the way this module works you call this DHT measure function and the first time you call it it fails you know again this is all kind of in development stuff just because it's got to kind of synchronize when it talks to this sensor there's a very specific train of pulses that it needs to read and so I think this initial read it's maybe not in a good state but after you call it once you called DHT dot measure and you know by the way this threw an exception if I wanted I could throw this in a try finally block just like a normal Python and catch this and then maybe realize like oh ignore timed out air you know something like that but anyways you call measure and then you can just call DHT temperature and that gives you back the temperature value in Celsius that we saw previously and same thing for humidity so you call a humidity that gives you the percent humidity and that was so every time you call measure it just fills in those new values for that so let's just do a quick little loop you know while true let's do DHT measure so grab a new reading and then let's print out and again this is I love you got this spring formatting that just works beautifully so we'll say you know here's your humidity and let's print out this value as a floating point value with only two decimal places so it's going to round up and then you have to say after floating point excuse me I'm going to sneeze live internet sneeze I don't have a sneeze button unfortunately okay so there's percent humidity and then let's print out the temperature and again now I want to use value one and let's do the same thing just two decimal places and this is in Celsius so you format and then I have to give the values so I say humidity is the first value and then H T temperature is the second value and then make sure I get all the right parenthesis lined up here and then let's delay so this sensor only updates every two seconds so we'll say sleep for two seconds and then let's let it go so hey check this out like a few lines of code we've got a temperature and humidity sensor and it's printing out these values here and so I can if I blow this we should actually see the humidity increase here and making the temperature a little bit so you see kind of goes up like that so this is pretty cool and it just kind of scratches the surface so you know there's other types of hardware they're supported so all of the different things you can do with this chip so you can control like the general-purpose inputs and outputs like have buttons and switches and relays and things connected you can control those with micro Python you can talk to the analog to digital converter on this chip and you know hook up like a thermistor or a photocell and read you know those analog values you can talk to I squared C and spy devices so other hardware and there are actually libraries that people have created and there's going to be a lot more libraries I've bet over time so that you know hook up like a little OLED display and talk to that and control it with a library but or if you need to you can talk to it directly with I squared C interface so lots of cool options and things you know pretty much think of it as anything you can do with Arduino in most cases you can do with micro Python now like I said though the big caveat is that this is interpreted code and so that means that at runtime this code is not you know the code I'm typing in right here like a print statement this is not something that the esp8266 can natively run on its CPU that print statement has to be converted into an assembly language or like a low-level language which are real low-level instructions of like move this bit into memory here or increment this value by one or jump to this place in the code so all of those low-level instructions you know if you make the right you know set of those instructions you can implement a print function but it's going to be pretty complex so with interpretive code you know you have these high-level functions like print and then something has to run and interpret that code and convert it into code that the CPU can run and so there's a little bit of overhead with that like there's a performance cost there's a memory cost with that so you know just to be aware like compared to Arduino we're using C and C++ which has this compilation step ahead of time which takes that high level code converts it into that machine and then gives you an output file that is the machine code that just runs directly on CPU so there's no extra step there to convert code from the high-level language into the machine code at runtime and you know their trade also that so by using an interpreted language like this you have the advantage in that like you know I'm typing in the code and running it on the fly I don't have to do that compilation step ahead of time the code can be a lot simpler here so you know I don't have to like use this prickly syntax because C and C++ the syntax is evolved a lot from the machine instructions and you know language and so a lot of that complexity creeps into the programming language like all the memory management and things like that that you have to deal with is just because that's how the hardware deals with it and so when your program added a low-level close to that hardware you have to learn to deal with that whereas the higher-level languages like this that are interpreted they can do a lot of that grunt work for you and make it a lot simpler so that's kind of the point you know I'm trying to get across here is that you know sure you can do things in the low-level languages but you can also do them in the high-level languages and you might worry and you might think like oh no like this is slower I can't have slow code but you know it's really not that common that you have performance critical code like things that have to happen in milliseconds or nanoseconds you know in most cases like I'm printing out this temperature and humidity every two seconds so for two seconds the CPU is just sitting there doing nothing so this is a really simple program and it doesn't matter if you know these print statements take 200 milliseconds instead of like 10 milliseconds if they were in C or C++ there's random numbers here but you know if it's an order of magnitude longer it doesn't matter you my programs not running that fast anyways but if you do have those performance critical sections so like reading this DHT sensor like I said there's a very specific set of pulses that it has to read and there's like an 80 nanosecond pulse if I remember I wrote a driver for this once I should know this like a 50 nanosecond pulse it has to read that's pretty fast and so you know maybe you can't get to that level of nanosecond accuracy with micro Python code because you've got that interpreted overhead but there is a really cool thing with micro Python just like the Python programming language or C Python is what it's called you can extend micro Python you can right code in C or C++ write a function in C or C++ that has all the benefits of the low-level like you know you're running directly on the CPU you don't have the overhead of interpreting and you know you can manage memory yourself you can access the hardware peripherals and things like that as much as you want so you write that performance critical code in C or C++ and then it can be exposed to your micro Python code and so that's actually what's happening here when I'm calling DHT dot measure that's actually invoking it goes through a few steps but one of those steps is it's calling some C code that someone wrote that does that performance critical stuff so you know even though micro Python might not be as fast as Arduino and seen C++ code doesn't matter because in most cases you don't need it to be fast and when you do need it to be fast you can do that code and see your PC plus plus so that's a really powerful thing that you know you can write your code write your your business logic or your you know your main program logic in this high-level language you get all this expressive syntax and things to deal with and then when you need to you drop down to that lower level to you know do more things with with lower level languages so that's you know cool example now one last thing I'll show with the esp8266 I'm actually going to turn on Wi-Fi because like I said this is a Wi-Fi microcontroller and by the way there are libraries to use that Wi-Fi microcontroller in micro Python so you can talk to web services you can download data can you know connect to a Wi-Fi network very powerful for Internet of Things stuff and hopefully we're going to look at that in future videos okay so I turned on Wi-Fi on the Mac and you can't see it but this chip is actually advertising an access point a wireless access point and I've connected my Mac to it right now and I'm going to go to something called the web repple so like I said you know we've got the repple right here I'm connected to over the serial port they also have a web Ripple now this doesn't look super pretty because this is all pretty early in development this is like you know the very first versions of it but I'm going to click connect on this thing and watch what happens ask for a password which I set ahead of time it's Python on this board web rebel connected so this is pretty cool and this might be blowing your mind if you're used to Arduino like good luck trying to do this with an Arduino program it over or a webpage so I've connected to you know this web repple it's actually using WebSockets so there is an implementation of WebSockets at least a listener on this little ESP board that's pretty impressive to me and I can run code on here so you know I can say prints hello world and it's actually kind of funny you can see in the background the code is actually it's showing on both of these terminals but I can run this and get the result back so you know everything I could do with the serial connection I could do it also with this web Rempel and so I think this is going to probably be a lot of the future of micro Python is you've got a lot of these little boards you know various ways to connect to them whether it's like Wi-Fi maybe Bluetooth in the future connect them run some code so really powerful stuff that you can do with this and one other thing I wanted to show real quickly too if you know if you're a normal Python I would say normal if you're an advanced Python user you know like I said before and kind of hinted at there is a lot of power with Python in micro Python so for example like if you just wanted to print the even numbers from 0 to 10 so you know an easy way to do it would just be like for I in range of 10 and then if I modulo 2 equals 0 so if I is even if it / - you have no remainder that means or even then how about printout I so this is just a standard implementation so you can see you know this prints out all the numbers from 0 up to 10 the even ones easy thing you probably do this in programming 101 nothing wrong with it Python lets you get a little fancier though so like if I wanted I could say how about a functional style so let's make a list and we'll use the filter function and let's define a lambda function can't do this in Arduino which takes one parameter and if that parameter is even it's going to return true so the filter function takes in a set of values so like the range of you know 0 to 10 and it's going to apply this function to each value and every value that this function returns true for it's going to pass on to the next function so this is kind of like a functional style of programming when I do this look at this I got a list of all the values all in one line like this is again the power of Python that you've got you know this capability and there's nothing wrong with either of these approaches you know it really just comes down to how you want to do this and then hey if you're a more advanced Python programmer you probably thinking why don't you use a list comprehension okay advanced Python programmer let's use a list comprehension here so let's create a list or actually no sorry let's do 4x X in range oh wait no I'm trying to remember what is the syntax for this I think it's yet X for X in range of 10 if X modulo 2 equals 0 so hey look at that you get all the values and then you're using that fancy list comprehension syntax so you know as a Python programmer I'm pretty darn impressed because remember this is running on this tiny little ESP and you've got this really cool expressive syntax that you can do you know your programming in so but hey if you're a beginner you don't have to get tripped up with this stuff it's perfectly fine to use you know loops and basic things like that so I just wanted to show that even though this is micro Python it's still Python and it's still pretty powerful ok so we're running a little bit long and I'm going to just jump to the last demo here so I just wanted to show one more board and actually I'll close out before I get to that so I'm going to show the micro bit and I'm going to close out of the serial connection here so micro pet is this guy here and if you're in America you can't get this right now unfortunately I got this on eBay so check out eBay dot co uk everyone in the UK in like certain grades I think I got a ninth grade or something like that every student got one of these which is pretty cool like it's that's amazing that they want to give them and this is just a small little board you know very similar to all the boards that we have an Adafruit it's got a chip on here you can program and it's got some things built in there's a little grid of LEDs on the back here and it supports micro Python and watch this this is pretty cool so I'm going to plug this thing in and I've gone to their web page here and I'm going to go to create code and they've got a really nice little system here so you pick which language you want to use they have like a visual programming language and stuff let's do Python we'll do a new project here and they kind of start you off with a basic little starting script here so like already your mind might be blown like we're going to program this from a web page here and pretty nice-looking web page I gotta give them credit this is beautiful but you know what I think this is kind of where the future of a lot of this micro Python stuff is going to go so even though it's really fancy and slick on the micro bit you know someday I bet a lot of the other Python board is going to be like that anyway so they start you out and they've got a nice little micro bit module that lets you do things like scroll a message on the the LEDs that are on the back and I've actually done this ahead of time so it's scrolling this hello world on here but let's just see what happens so you know maybe it said hello world will say hello Python just so we can show that this is different and then let's just click this download so the way that the micro bit works one of the ways it works it looks like a USB device USB storage device on your computer and so when you click download here it actually generates a firmware file that you can drag over to the board so I'm going to do that I just have to open up finder to my downloads folder and so if you look here you can kind of see over here here's the micro bit device that's connected here and I guess gives you some little like debug info here but I go to my downloads here's that script that I just downloaded so I drag this over here and then now you can actually see this flashing it's programming and then when I flip it over we should hopefully see in a second year so it's copying the file over and it's going to program the chip and hey look at that it's scrolling across hello python so that's pretty cool it's going to show the heart message there and again you know all just from this web page I didn't have to install anything I just clicked download and dragged it to the you know the machine I'd have to install libraries it's all just there you know as a beginner I can imagine this is much much simpler than maybe the current state of the world with some of the desktop tools like Arduino and stuff but you know I'm not going to say that it's better and you know always better than Arduino because you know again there are very valid reasons why you might want to use Arduino like you know maybe there are performance issues or maybe there are libraries that Arduino has that aren't in micro Python yet so you know there is no best or worst it's just a different option and that's what I really wanted to get across in this video is that if you've used Arduino and you know you've done some basic things to control Hardware check out micro Python you know it's it's a version of the Python programming language that runs on some boards now it doesn't run on all boards so go back to the guy that I mentioned and I talked about exactly which boards it runs on so you know it's it's not like you can take your Arduino Uno and suddenly program it with Python you know unfortunately that chip is just not powerful enough but check out all the chips that do support it and I think you'll find there's a good mix of you know really advanced ones that are maybe a little higher cost and simpler ones that are cheaper and still just pretty cat you know just as capable so definitely check that out but again you know micro Python is a version of Python that runs on these little microcontrollers and it's a very powerful thing very good for beginners but even if you're an advanced person you know and you know Python really well you're gonna feel right at home like I showed before you can do lots of advanced things with Python you know you can create classes you can create modules you can go crazy with it so I'm gonna wrap it up if there are questions maybe throw them into the chat right now and we'll see if we can get to them let me jump back to the main head shot here so let's see an apology's it gets kind of stuttery sometimes oh I just lost one of the camera batteries but no big deal just a little dramatic lighting now sometimes get stuttery in this view so I'm still troubleshooting Wirecast but anyways let's see look if there any questions buh-buh-buh-buh so some folks are mentioning they really like this which is cool yeah I think if you like Python if you've used the raspberry pi and you program that with Python I think you'll be pretty excited about micro Python because you know the Raspberry Pi is a fairly large board and you know you can see those microphone boards are pretty small and nice and and you know very very small systems potentially less power sometimes in some case you know the Raspberry Pi can take fifty two hundred milliamps sometimes where some of these boards can be a little less power-hungry so let's see let's see how some folks are mentioning like you know if your teacher and you can't install like the arduino ide yeah that's that's a problem like sometimes you know you're in an environment where you've got things locked down and you can't put different tools on they're pretty powerful when you can just plug a board in and just open a serial terminal and start talking to it so that's pretty cool opens up a lot of good out scenarios so ah let's see what's your opinion okay so how would you describe micro pythons maybe question so what I'll say is go to this guide so I'll put a link on here to this but it is a version of the Python programming language made to run on small embedded systems so you know I say a version of the Python programmer H because it's not the full Python programming language II know you're not gonna be able to take code that you wrote that runs on your desktop and immediately run it on a tiny little chip because it doesn't have all the same libraries and maybe there are a few little syntax things are different but it's pretty darn close you know I'd say it's at least like 90% of the language is implemented there so it's it's a different language to use to program these little chips and you know Python is a much simpler language but you know simple in the sense of simple to write not simple in the sense of capabilities you know Python is very advanced and a lot of you know like I'd always blows me away if you've ever used Dropbox you know the service to sync your files whole thing is written in Python the back end the clients all of that is Python code you might not even realize it you know that's that's impressive to me that the services that that kind of scale can can use Python so it's a very good language you you know if you can build Dropbox with it you can probably build an Arduino project with it so that's what micropile on it's small version of it open source you know great community so check out this guide you learn a little bit more about it and my opinion of it is that it's awesome and it's worth checking out especially if you're new to hardware new to programming if you're not new you know like I said if you're a Python programmer you're going to love this if you're new to programming you're going to love this too because it's really easy to use it so then someone's asking what's the difference between two versions of the esp8266 firmware that's a good question I would check out the forum's so I link here to the micro Python for rooms just from the two versions you mentioned there's the if so you there's a version 1.3 bin and a 1.3 - weird characters those weird characters are the github commit ID so it's basically just if it's got those weird characters after it that was a commit that happened during that day so you know it's basically grab the latest one is what you want if you're looking for the firmware for that so you get the latest and then someone asked is it Python 2 or Python 3 this is Python 3 syntax on here so if I go back to the well I don't have it connected but you know if I try to use print not as a function it's going to fail and I think it's smart because you know hey it's 2016 python 3 is where we've all kind of moved and evolved too for this so you know it's I think it's a good thing to go on the Python 3 route for this so anyways so I'll wrap it up then that was this stream on what is micro Python and I just wanted to show the basics of it and really do a few demos to show the capabilities because maybe you've heard of micro Python but maybe brushed it aside and thought utz this early little thing you know I just showed off like five or six little demo is talking to hardware I don't think I wrote more than 20 lines of code and you know I talked to a sensor I controlled neopixels you know I did some basic like math functions and things so really cool really expressive and really powerful and like I said this will be kind of the first and what will probably be a series of videos on micro Python and check out the links in the description below you can find this guide links to all the things I mentioned on here and check out youtube.com slash Adafruit you'll find this video all kinds of other videos that we have up there and then twitch.tv slash Adafruit you can watch me stream these things live there I like to string a couple times a week on Mondays and Fridays on Mondays I like to do like a quick look at interesting Raspberry Pi software I might start doing Quick Looks at like micro Python stuff so for the next few weeks it might be a lot of micro Python streams so you know get ready buckle up seat belts we're gonna dive deep into micro Python going forward and then Friday's I would do in depth Raspberry Pi streams but I think for the next few weeks we're gonna do some micro Python stuff so you know get ready I think we have a lot of fun with this and really just explore what can micro Python do you know is it maybe something you can use yourself to start building some projects you know things that you might have used an Arduino for you can potentially use Python and micro Python to do that so really powerful things so anyways if you like this stuff comment subscribe hit the like button you know let us know this is good info and we'll keep turning it out so thanks a lot everybody and I'll see you guys later it's Tony from Adafruit
Info
Channel: Adafruit Industries
Views: 70,847
Rating: undefined out of 5
Keywords: adafruit, electronics, diy, arduino, hardware, opensource, projects, raspberry, computer, raspberrypi, microcontrollers, limor, limorfried, ladyada, STEAM, STEM
Id: 8btQWSu7DdM
Channel Id: undefined
Length: 46min 51sec (2811 seconds)
Published: Fri Aug 12 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.