Raspberry Pi Pico - Getting Started with MicroPython REPL (on Windows)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's take our first steps with raspberry pi pico we'll get it running micropython and we'll send commands to it over usb serial that it will execute interactively using what's called the repl let's get started let's start by putting micropython onto the pico follow this url which is also linked below and you'll see this getting started page if you scroll down there is a getting started with micropython tab and a friendly animation of what we're about to do so download the uf2 file that's going to go to my downloads directory and you'll see it here next hold down the button the boot cell button on the pico and then connect it to your computer and in this pc i can see there's a it looks like a flash drive it's appeared as a device called rpi dash rp2 all we need to do is to drag that file into that directory and as easy as that we now have micropython running on the pico to program it we're going to connect to it over serial using a software called cool term also linked below follow the link and you'll wind up on roger meyer's freeware if you scroll down we have the application cool term this is a free terminal program i'll download that and in downloads i'll just need to right click extract open the directory that was extracted and we can now run cool term i'm just going to use the default preferences to connect cool term to the pico we need to go to options and select the com port for the pico if i open up this com port list i'm going to pick com port 4 because that's probably the pico if you are unsure about what com port to choose you can open your start menu and search for device manager and under the section ports common lpt you can open that up and i can see two entries the one that i'm interested in is the entry that says usb serial device and that's listed as com4 so that'll be fine the rest of these settings are fine just come over to the terminal tab and use the terminal mode line mode that'll make things a little bit easier and finally we can press the connect button and if everything goes well then almost nothing should happen there's just a small timer here saying that we're connected and we've been connected for a few seconds so the pico is ready and waiting for our code so let's write some let's start with a very simple print statement print open bracket quote hello world close the quote close the bracket and there we have it this this is pretty cool what we've just done is sent a command to the pico and this is the reply interface the reply interface is allowing us to execute code live interactively so this code that we have just sent the pico is executed and by executing that it's printed uh what we asked for it's printed hello world so it's executed our command on the spot that's pretty cool okay moving right along let's do something physical on the pico at the moment the only physical periphery on the pico is the led that's on pin 25 so let's blink that in the terminal uh we need to import some packages so i'll import pin from the machine package we'll import pin functionality and we need to define the we need to create our led object so let's say led equals pin 25 and it's an output okay so far so good we've imported a package that allows us to control gpio and we have declared a gpio object led as being connected to pin 25 and we're calling it an output so now we can drive it simply by executing led dot value and we'll set it to a 1 to turn that gpio on and i don't know if you can see it but that led has just come on cool i could also execute led.toggle so it doesn't matter what state the led is in but led toggle will turn that off so now by using the up arrow i can call the last command that i executed so i can press led toggle led toggle and as i keep doing that you'll see that led turning on and off but i'm doing that manually that's that's not very gratifying how about we set up a timer and have that execute automatically this terminal is getting a little bit messy but you can always just hit the clear data to erase all that history so let's create a function that will toggle the led and a timer that will call that function periodically we can all our code is still running so the led is still set up i've just cleared the terminal so that it's a little bit easier to see let's import timer from machine from machine import timer and create a timer object tim i'll call him tim tim equals timer so we have our timer now we just need the function that our timer will call every time it elapses every time it ticks let's say we can we can declare functions in the rapple as well let's let's see how this works i'll define a function and call it i don't know tick pass timer as the argument and enter colon to begin the function definition now our prompt has changed to these ellipses these three dots and that's telling us that there's just going to be a tab so anything we enter here is going to be inside our function declaration now i wish i hadn't cleared the terminal we declared our led gpio object as the variable led so i'm going to tell the timer function to look for that global variable global led and now call led toggle so we have our function defined all it's going to do is to toggle the led but we need to press enter a couple of times to leave the function definition let's let's see how that works we still have the three ellipses so we're inside the function if i hit enter we're still inside it if i hit enter a couple more times you can see the prompt has changed back to the regular python prompt so that means that all of this is inside the function definition and now we're just back to entering live code all we need to do now is to set the timer to run so we have the function that's going to be called we just need to call it with the timer so we'll tim.init to initialize the timer and we'll run it at i don't know three hertz let's say let's say 3.5 you can enter floating point numbers here too we'll set the mode as timer periodic and callback callback is the function that will be called every time the timer ticks so 3.5 hertz this is the function that's going to be called and that's our tick function that we just wrote and once i hit enter this led should begin blinking and indeed it does so we have that led blinking at a constant rate because the timer is calling that callback let's have a bit of an experiment now i'm going to i'm going to create tim2 as a as a timer just a second timer and i wonder what would happen if i'm going to press the up arrow key to go back to my tim init line what if i initialize tim2 at a frequency of let's just say two hertz and use the same callback so now i'll set up a second timer to call that toggle function that's quite interesting so you can see the the led now has a has a little bit of a different character to it it's kind of a a jerky loping frequency that it's that it's flashing at and probably what's happening here is that we have two timers running independently but they're each calling the toggle command so you have like a a bit of a a bit of a poly rhythm happening with this led michael from the future here just to let you know at the moment we're live coding and that means that when you power cycle or reset the pico it's going to drop that code that you've written and you'll have to reprogram it again through the repo stay tuned for a follow-up video on scripting with pico and that means we'll write a script that uploads to the pico and when you remove power and power it again or if you reset the pico it will still run that same program every time stay tuned for more there you have it uploading code to a raspberry pi pico using the rapple if you have any questions head over to the core electronics forums and we'll see you there thanks for watching
Info
Channel: Core Electronics
Views: 149,467
Rating: undefined out of 5
Keywords: electronics, maker, education, tutorial, technology, raspberry pi, raspberry pi pico, microcontroller, dev board, development board, raspberry pi pico micropython repl, microsoft windows, windows
Id: yzsEr2QCGPw
Channel Id: undefined
Length: 10min 28sec (628 seconds)
Published: Thu Jan 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.