Blink LED in C/C++ on the Raspberry Pi Pico [Linux SDK Setup]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] if you want to learn how to write c for raspberry pi's new microcontroller the pico stick around we're doing that next on low level learning um so the way we're going to start is we are going to go to google and we are going to search for raspberry pi pico um and that's going to get us to the getting started site so these there are a lot of really good resources here that raspberry pi has put out on how to get started with the raspberry pi pico um specifically we're going to go to the c c plus plus area go away to the bottom and then we're going to go to the raspberry pi pico c plus plus c plus or c c plus plus sdk that'll bring us to what i would argue is one of the most well documented apis for an mcu i've ever seen um this data sheet should be used whenever you're programming the raspberry pi um they fully document almost every api that the raspberry pi pico has so if you need to learn something new or want to play with a new feature for the device you need to go here so once we have this document open we're kind of reading through it we're going to be playing with the hardware gpio functionality of the board you don't have to really read too much of the documentation to understand how this part works it's just good to know that it's here so once you've kind of perused this documentation a little bit you're going to go to github and go to this link i'll put this link in the description in the video right there you're going to clone this link so this link is the code required by the developer to interface with the board without this middle layer of code between us and the raspberry pi we wouldn't know how to do a lot of things that the board is capable of doing raspberry pi produces this code to enable us to do it so if we ls tack l on pico sdk we should have gotten this nice folder of a bunch of different libraries and cmig files and all that other good stuff um so now that we have our sdk in a place that we can use it which is just in the home directory right here um we need to make a folder that is going to contain our project so for me i made this folder called custom right so you can name it whatever you want you can even potato cat doesn't matter make a folder that is where your code is going to live so then what you need to do is into your your project folder you need to copy from the pico sdk external this cmake file you should put it in your folder right so now if we read the contents of my folder we have the cmake file um the c make file that we get from raspberry pi is actually a part of the c make build system so i'm not going to get too deep into this but cmec is this really powerful build system that is platform independent that allows developers to control how their code is compiled built and tested so by making the raspberry pi pico build through cmake you can actually build projects for it on windows linux or otherwise pretty cool so now that we have this file here we need to make this new file that we're going to write it's called cmakelists.txt and i have mine open here so go ahead pause the video and copy this down basically what this does is it sets up our build environment to import that file that we got from raspberry pi to set the name of our project and a couple programming standards it initializes the sdk which means that it sets up the build environment to use that sdk for us we then say that we're going to produce an executable called blink which is the name of the tutorial from the blink.c file we're also going to link the pico standard library which we need to do pico things right kind of like the c standard library and see we're going to link that library into our blink executable and then finally we're going to do this part called pico add extra outputs blink so if you've seen other tutorials um when you use a raspberry pi pico you produce a uf2 file the way you produce that file is by adding this to your cmix list so go ahead copy this down and once you have that uh file written out to cmakeslist.tt we'll keep going okay so now we're going to actually do some programming right um so here we have blink.c which is the actual code that is going to get ran on the raspberry pi pico so pretty straightforward here this is pretty similar to how the arduino works when it comes to gpio so the first line here we are going to include pico standard lib note that these are in quotes and not in hard braces that's because we're actually using the pico sdk and the way that cmake involves include files we were allowed to do quotes like this we're gonna do three pound defines um the first is gpio on which is just one gpio off which is zero and then we're gonna define the led pin on the raspberry pi which as we saw from our schematic here it's pin 25 right and again this picture is really powerful we want to do other stuff with the board but for this tutorial just gp25 is all you need to know so pin 25 all right so we have pin 25 and then just like any other c function we in this initialize our main function um we need to initialize gpio on a pin in this case we're going to initialize it on the led pin and then gpio set direction so just like in the arduino tutorial i've done where you have to set the pin as an output or input pin we have to do the same thing so led pin becomes a gpio out pin because we want to put voltage and source current from that pin as opposed to sync current into that pin and then while true so we're going to serve this code forever put something onto the gpo line on pin 25 turn it on done then we're going to sleep for 2000 milliseconds or two seconds and then we're going to do a gpio put we're gonna on the led pin turn it off and then sleep for two seconds and then we're gonna serve that forever so normally when you're doing this you would either put this code into like the arduino ide or you would run gcc on it now that we're using cmake it's a little more complicated than it's not too bad though you need to first make a build directory and then we're going to go into that build directory and then first of all if you don't have cmake installed you need to make sure you go out and do that and the way you do that is by doing sudo snap install and make and for me i already have it installed but if you didn't it would install it for you after we've installed cmake we need to write export pico sdk path equals the path to your pico sdk so for me it's this directory that exports a global variable to the environment that tells cmake where to look for the pico stk that raspberry pi provided now that we have that taken care of to create our build chain we need to do cmake dot dot and what that does is it tells cmake to build the program build structure that's listed in our cmakelist.txt once we've done that we can run cmake dot dot what that tells cmake to do is to look in the folder above it for this file the cmakelist.txt and build the build chain for this program we'll run that here great and now that that's built we can look in this folder again and before we had nothing in this folder and now we have a make file and now just like we're used to doing normally we can run make on this folder and the build chain will produce all of the code in pico binary format and now that all that is done we have our blink.uf2 file which is the one that we can actually put under the raspberry pi pico and now that we have that file i can copy that file to media user raspberry pi 2. doing that if you heard the little do to do on my computer it actually unplugged the raspberry pi for me and it booted back up now it's running my program and i'll put a picture of the raspberry pi right there anyway so i hope that made sense to you guys the build chain for raspberry pi pico is a little more complicated than it's like arduino and so on um but cmake is really powerful and i cannot emphasize enough how good this documentation is you can take this tutorial and this document and do a lot of really powerful things with the raspberry pi pico anyway guys i hope you learned something today if you did please drop a like hit subscribe and i'll see you guys in the next tutorial in probably about a week from now keep on learning have a good night bye-bye
Info
Channel: Low Level Learning
Views: 41,028
Rating: undefined out of 5
Keywords: Raspberry pi pico blink led, raspberry pi pico, raspberry pi pico blink, raspberry pi pico coding, raspberry pi pico c++, raspberry pico linux
Id: JhajoAyP8e4
Channel Id: undefined
Length: 8min 35sec (515 seconds)
Published: Thu Feb 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.