Moving from Arduino to MicroPython - 10 Things you need to know.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey robot makers here's 10 things you need to  know if you're moving from arduino to micropython   arduino requires that you compile the  code before you upload it to the board whereas on micropython you simply  just run the script from the ide   you can also use the repl  to type commands in directly   on the arduino you need to use the  library manager to bring in libraries   that you want to use in your code so here  we've got dht which is a temperature sensor   so we can simply bring that in install  it and that will work on our arduino   on micropython we need to make sure the file  is actually installed on the device itself so   here i've got an ssd 1306 display and if i want  to use that with my rotary encoder pro and that   file needs to be installed on the raspberry pi  pico as you can see i've got it there installed so micropython and the arduino have  completely different development environments   so for python or micro python we can use a whole  host of different ones we can use the python ide   or the idle the integrated development and  learning environment we have visual studio code   we have funny microsoft make code we have move we  have pycharm which is a commercial one there's a   community version of this but there is a  paid version as well and then there is atom   io then on the arduino side we have eclipse  which is a java based arduino platform then   we have patamayo again which is interchangeable  and we have visual studio there are some plugins   for arduino visual studio and then we have  obviously the arduino ide as well that comes   in a few different flavors there's the  traditional ide which you can download   onto windows mac raspberry pi linux there is a  cloud-based version which has a small plug-in for   chrome and that can be used on chromebox as  well as any supported browser and then there's   the beta version of the ide version  2 which is just coming out the moment   i've tried that and i couldn't get that to  work it's probably something to do my setup the next thing is voltages so there's usually a  difference between the voltages between arduino   boards and micropython boards generally speaking  the arduino uno and mega are 5 volts this is known   as ttl in the industry and it's quite standard for  components micro python on the other hand most of   the development boards for this run on 3.3 volts  so it's much lower power meaning that your battery   will last longer on your your projects however  it doesn't mean that we have to be considerate of   that when we're plugging in components because if  we plug in a component with a 5 volt supply to a   3.3 volt pin it will probably blow that pin up and  damage your board so just something to consider the next thing is void versus death so on  the arduino if we want to create a function   i'll create a little blink function here just  to demonstrate we use the void and void means   that it doesn't return any values uh if we were  to return a value we'd have to say what type of   value that's going to be is it an integer is it  a string is it a floating point number is it a   boolean and so on so if we want to return true  or false for example we'd set that to a bool   boolean however if not returning the value  we simply say void we then create the little   squiggly brackets little curly braces to define  the code block so in here we're simply going to   cut and paste the code that we've got down here  to make the light blink we just cut that paste up   there and then in our main loop in the void loop  we can simply call that blink function like so   i'm going to create the exact same program but  this time in micro python so what we first need   to do is just bring in from machine the pin  and we're also going to bring in the sleep   and then to create our new function we use the def  so this is to define a function and we're going to   call this one blink and we're simply going to  say we need to define an led first of all so we   just very quickly led equals pin 25 and it's a pin  dot out it's an output and then over here we can   then simply say led.on sleep for a second led off  sleep for another second and then we can create a   main program down here while true blink if i run  that i can see it blinking there on the board so on the arduino if we want to create some  variables so let's create some numbers i'm   going to call this one a i'm going to set that  to zero let's create a floating point number b   that's gonna be one point it's gonna be 3.14  let's have a true or false so this is going to be let's have a string so for each of these variables  i have to define what type the variable is before   we can use it so for the value a set into  zero we have to define that as an integer   which takes up two bytes in memory and the reason  for this is because we're using c c is a strongly   typed language mean but we have to define what  the variables are before we use them and then   throughout the program the compiler can check  that the type of the variable doesn't change and   therefore we're not introducing any bugs in our  program so we can't halfway through the program   if we've defined a as an integer we can't then  start using it as a floating point number in fact   if we try and put a floating point number into the  integer the compiler will tell us that there's a   problem with that so over in python we can define  a variable a equals 1 b equals 1.1 c equals true   d equals hello like so but python doesn't require  that you define what type those variables are   before you use them so we didn't have to say  that a was a integer we didn't have to say b   was a floating point number and so on  we simply just assigned the value to it   and the interpreter it will infer what type  of object that you're going to use there so a   being set to one it knows that one is a whole  number so it will set that to be an integer   whereas b has been set to 1.1 which it knows is  a floating point number so it'll appropriately   put that as a float and we can try that down here  in the repple we say a equals one and then we look   at what type a is we can see there's a class  integer if we say b equals 3.1 and then we do   type b we can see that's a floating point  number we set c to be true and then we do type c   we can see that that's a boolean but we can  change our mind halfway through the code   if we wanted to so i can say that c becomes  equal to kev and i can now look at what type   c is and that's now a class string so this might  introduce bugs in your code if you're not careful   you need to make sure that you don't change what  you're using the variable for throughout your code so if we want to include a library in our arduino  code uh we use the software library manager so if   i go to include library and then let's include  the software serial library for example the very   top of my code that it does include and then with  a little triangular bracket software serial.h and   that tells the compiler to bring in the software  serial library we can use that throughout our   code down here for example we could say do  software serial myself and that will set up a   my serial object whereas over in micropython if we  want to include a library we use import instead so   we can say import machine that'll bring in all  the functions from the machine library typical   one we might bring in is the time function we  might want to bring in the sleep now rather than   bringing in all the functions that are available  in that library because we're trying to cut down   the amount of memory and space used on the device  we can actually just limit the functions that we   want to pull in so instead of doing import machine  and just bringing everything in we might say from   machine import pin for example or uart or whatever  it is that we're bringing in and then instead of   saying import time we might say from time import  and then sleep and then we can use those library   functions now the libraries do need to be present  on the board so machine is a built-in micro python   library so we don't need to have a machine dot  pi file in our pico file system and similarly   time is also a built-in micro python library  so we don't need to bring that in as a separate   external file as well but anything else we  would need to do that so for example on here   i have an ssd 13 or six dot pi and this is a  library which enables us to use a 1306 led display so in this example blink problem that we're  working on here we can see that there's a large   chunk of comments at the top of the code so we  have forward slash and then star and then we have   a huge block of comments it tells us all about  the code where to get extra stuff from an example   um and then we have start and then forward slash  to end that big code block so that's a multi-line   code block and then we have single line comments  that have the double forward slash and they can   be in between the code or they can actually be  at the end of a line of code so for example on   here we've got the end of this line here we've got  double forward slash and then we says turn led on   so in micro python we have a similar kind of thing  so if we want to do a large code block what we   will do is three speech marks and this can be a  multi-line comment block new lines and extra text   we once have a single comment line we use the  hash or pound symbol if you're american and we can   simply have a single line comment because if we go  into the next line it will try and it will treat   that as code that can be at the end of a line of  code so if we just do this here this is a code   comment online so i'm over here now in visual  studio just to give you an idea of what these   docustrings can be used for so up here we've  defined a function that says we'll switch on   the relay for a number of seconds as defined in  sleep seconds which is that parameter just there   so if we come down to where that's actually used  switch on relay and i just hover over that visual   studio understands what docustrings are and it  will actually display that as help underneath the   function name so it says there this will switch on  the relay for a number of seconds in sleep seconds now over here back in the arduino ide you  can see that the naming convention of some   of these variables that we've been using so lie  detector has a lowercase l and then a capital d   to distinguish this as being another word in that  string similar with the software serial you can   see that's capital s capital s again and then we  have lowercase m and then capital s for my serial   pin mode has a lowercase p and a capital m digital  right has a lowercase d and a capital w this is   called campbell case so if you think about the  shape of a camel it has a hump and it might have   two humps the hump refers to the capital letter  to separate the two words and this is very common   with c c plus plus an arduino code however if we  look at micro python we tend to use underscores to   separate words and everything's in lower case we  do have convention of using constants in uppercase   so if we have built in led might all be capitals  but we use the underscore to separate the words   out whereas whereas the little function that  we have there has an underscore to separate the   two words and this is called snake case because  it kind of looks all flat like a snake now the   reason we have camel case or snake case these are  called a house style it's just a way of coding to   be consistent with your your naming conventions so  if you use the same naming convention throughout   your code other people can very easily read this  and you can also read it back easier too so code   that has a mixture of cases in it to different  naming standards it's very hard to follow and   you'll trip up it's it's just a bad programming  practice so pick a naming style snake case is   the preferred one for python whereas camel  case is quite common on cc plus for arduino so in c whenever we want to define a block of  code we use these curly braces and that tells the   compiler the start and the end of that code block  this just helps the compiler it does mean that we   can have indentation however we like within our  code so we could have a load of indentation if we   wanted to or or absolutely none it doesn't matter  to the compiler in fact the compiler will strip   out any what it calls white space any spaces and  it will just look between curly braces to define   the code block for each line of code it will look  to see if the semicolon is at the end of the line   so we can split things up like that  that's a valid line of code in c   but the convention is you keep a line of code on  a single line so over in python we don't use curly   braces to define code blocks we use indentation  so let's create a new file here and let's just   do a piece of code to print a message out we  just run this on the computer so we're just   running this on the local python interpreter  this code doesn't have any structure it's just   a single line so we don't need to indent it  however if we were going to define a function   when i press return it indents it by four spaces  and that's because functions by default the code   block is four spaces indented and then further  down the code we want to run that we've removed   the indentation once more and we can run  that piece of code like so if i run that   it'll say hello and then hello world  because that function is defined   and then called now if i were to do an if  statement in python if i say if a equals true   then do something the colon at the end of the  line there tells it that there's a code block   coming next and therefore it indents it again four  spaces uh now if you have multiple indentations if   you say if b equals false and actually they need  to be double equals if you're doing a comparison   then it will indent it four spaces more again this  actually helps us make the code look more readable   because the indentation helps the eye flow around  the code and it also means that the compiler   knows where the code blocks are because of the  indentation now if we say else we have to come   back in and then the else is a co has a column at  the end that means it's another block of code so   not false so we haven't actually defined what  a or b are so let's just say a equals true   let's run our code and it says hello if b  was actually true then it will say not false so over in the arduino we can see that the main  program loop is defined by this void loop every   arduino program has this it has a setup function  and it also has a void loop which is the main   program loop and that will loop over every single  time the end of the loop has been reached it will   then execute the loop once more in micropython  we don't have that so if we have a program such   as this hello world let's just get rid of if  we have this and we run it it will run through   that script so when it reaches the bottom of the  script it will stop we can see there that with the   repl has stopped and it's no longer running any  code so if we want this to run and run and run   the convention is you say while true now we run  the code it will run and run and run and run so i hope you enjoyed this video and you found  this quite useful if you're moving from arduino   to micropython and if you get stuck on anything  let me know in the comments and maybe i can help   you out as well there is a group over on facebook  called the small robots group um we now have over   10 000 members in that group so if you get stuck  with anything why not hop over to that group and   we'll see if we can help you out so hopefully you  found this video useful and i'll see you next time
Info
Channel: Kevin McAleer
Views: 922
Rating: undefined out of 5
Keywords: micropython, arduino, Moving from Arduino to MicroPython, Kevin McAleer, Python, MicroPython, Arduino, C++, arduino to micropython, python (programming language), python programming for beginners, python programming, python tutorial for beginners, comparison
Id: A7ZJ5TC-zUk
Channel Id: undefined
Length: 15min 49sec (949 seconds)
Published: Thu Jun 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.