Auto-start scripts on MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey robot makers do you want to know how to automatically start up a script in micropython when you power up the device then keep watching so one of the challenges that we have is that there are different versions of micropython out there the circuit python from adafruit is the original original official micropython um and then there is different variants such as the make code micropython the micro bit micro python and so on so we need to understand what these different variations mean when it comes to auto starting scripts there are essentially two differences there is code.pi and that's on the circuit python devices such as the adafruit blue fruit and then on the raspberry pi pico it's simply main dot pi and that's the same as well on the micro bit over here i've got a raspberry pi peak on my desk and i've got funny and i've got some very simple code here that's just gonna flash pin 25 which is the the led pin that you can just see flashing just here and it's going to flash that approximately every half second so i've currently called the led dot pi now the issue is if i remove the power from the raspberry pi and then i plug it back in let's see what happens so i've removed the power i'm going to plug it back in and it doesn't continue running that program so if we want to continue running that program we need to load up funny connect it back up to the raspberry pi pico and then we need to hit the run command again so now it's running again so there is an alternative to doing this and it's how you name the file so if we save that file as main.pi instead so let's create a new file we're going to copy the exact same program that we've got there i'm going to paste it in and then i'm going to save it to the raspberry pi pico and i'm going to call it main.pi let's see what happens now when i remove the power and then i put the power back in it starts running the program automatically we can see the led flashing there and that's because we named it main.pi now on the official micropython code there is another file name that you can use that will run a piece of code just once and then the main.pi and that's called boot.pi what i'm going to do now is i'm going to create a new file i'm just going to paste in some code that i've written very similar to what we've done before with the pin but this time we're going to make the led flash 10 times so there's a little loop n equals 1 and while n is less than 10 then flash it and so it goes high and low and the this time the sleep is going to be 10 to the second and we have n plus equals one just to increment the loop so let's just run that and see what happens we should see in a second so save that as boot.pi should see that it flashes very quickly about 10 times so what i'm going to do now now that i've named that boot.kind of saved it to the raspberry pi pico i'm going to remove the power i'm now going to reapply the power and we should see that it flashes 10 times very quickly and then it starts running the main.pi where it's flashing much slower and that's the two different files that are being used so boot.pi runs once very similar to the arduino where you have the setup function that runs the setup code just once and then you have the main loop which is where your main program resides so this is a way of separating those two things out boot.pi and main.pi so now i've got a raspberry pi pico with circuit python installed you can see that there next to the official micropython device and what i'm going to do is i'm going to paste in some code that i've written here which is just going to flash the led on and off so if we just run this we should see the led run i'm just going to save this as led dot pi and then we can see there that the led is flashing on and off about one time a second uh the code's slightly different as well on circuit python we have to import board which is analogous to machine on the official micropython then there is digital i o which again is another adafruit specific library time is the same and then we have the onboard led which is led equals digital i o dot digital in out board dot led so that one is automatically set to pin 25 and then we say led direction and the it's an output so that's what that code there is and then we have a simple loop that just says led value is true waits for a second led value is false and then waits for another second so that loop will carry on forever so now i save that as led dot pi when i remove the power and plug this back in it isn't going to run that code so we need to name it code.pi let's copy that first and create a new file paste that in i'm going to save that to the circuit python device i'm going to call it code.pi click ok now if i actually remove the power and i plug the power back in we'll see that it starts to run the the code and it's flashing the led again one time a second so main.pi and boot.pi on the official micropython and on the adafruit circuit python it's code.pi so now if we look at the microbit that has a slightly different version yet again so i've gone over to makecode.microbit.org and i'm going to create a new project so when we create a new project we'll just give it a name call it led blink and we get two blocks to begin with on the block editor we get onstart and we have forever so onstart is like our boot.pi and forever is like our main.pi so this will run just once and then this will run forever but if we actually look behind the scenes what's going on in the python code but on forever is just a function so anything that we put into this if i just grab a block and show a number for example and then have a look in the python code on forever is just a function and anything that we put into the start so if we just do um show some leds into the start and then have a look at the python it just runs that code first in the script and when we download that to the micro bit it will load that code automatically so if i remove the power now to the micro bit and when i plug the power back in it will run the last probe that we downloaded to it which was that code that shows the number zero on the leds there so that's how we do it on the micro bit so if you found this useful um this can help you disconnect from your computer and have your code running on your different devices all the different devices running micro python and all the different favorites of micropythons i hope you found this useful and i'll see you for the next one
Info
Channel: Kevin McAleer
Views: 603
Rating: undefined out of 5
Keywords: auto-start scripts on micropython, micropython tutorial, auto-start, boot, startup, circuitpython, micropython, MicroPython, CitcuitPython, Kevin McAleer, Raspberry Pi Pico, Pico, RPI 2040, Tiny2040, micro:bit, BBC micro:bit, microbit, Microbit, How to, Start up, Boot Up, on-start, raspberry pi pico projects, raspberry pi pico programming, micropython tutorial for beginners, micropython tutorial pico, nodemcu micropython tutorial, micropython pyboard tutorial, micropython tutorial esp32
Id: 9YvWT8bNllU
Channel Id: undefined
Length: 6min 34sec (394 seconds)
Published: Wed May 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.