Pico Blink LED Code

CMakeLists.txt

cmake_minimum_required(VERSION 3.12)

include(pico_sdk_import.cmake)

project(pico-projects)

pico_sdk_init()

add_executable(blink_led blink_led.c)

target_link_libraries(blink_led pico_stdlib)

pico_add_extra_outputs(blink_led)

blink_led.c

#include "pico/stdlib.h"

int main(){
    // initialise GPIO (Green LED connected to pin 25)
    gpio_init(25);
    gpio_set_dir(25, GPIO_OUT);

    //Main Loop 
    while(1){
        gpio_put(25, 1); // Set pin 25 to high
        sleep_ms(500); // 0.5s delay
        gpio_put(25, 0); // Set pin 25 to low
        sleep_ms(500); // 0.5s delay
    }
}

12 comments

  1. Thanks for your very helpful videos. You created the blink project right under the pico-projects directory. What if i wanted to organize my projects better so that every project had a unique sub-folder under pic-projects, so for example pico-project/blinkfolder and other projects in their own sub-folders. What would change in the Cmakefile.txt, and in the environment variables if any. Also please illustrate with a project where there are multiple files, like main.c, blink.c that have dependencies etc, (ie: something beyond the little toy examples)

    thanks
    bammi

    1. Hi Bammi, Thank you for your comment, hopefully I can answer your questions!

      Firstly, regarding folder structure, it is totally possible to organise your folder structure like this, looking back I should have covered this in my videos! It will require no modification to the environment variables and little modification to the CMakeLists files. Each sub-folder (i.e Pico-Projects/projectX) will need its own CMakeLists.txt file however it is a little shorter and can only needs the add_executable, target_link_libraries, pico_add_extra_outputs and any other project specific instructions. The parent folder (i.e Pico-Projects) will need the pico_sdk_import.cmake file and a CMakeLists.txt file which specifies the cmake_minimum_required, include(pico_sdk_import.cmake), pico_sdk_init(). Also in this CMakeLists.txt file are all the sub-folders (individual projects) listed with the add_subdirectory(projectX).

      This structure can be seen with the Pico-Examples folder (https://github.com/raspberrypi/pico-examples) and you can see how the ‘Parent’ CMakeLists.txt file is structured compared to the sub-folder ones. When in VS Code (assuming you are using VS Code) you open the parent folder “Pico-Projects) and can then select which sub-folder to build by selecting the [all] button near the build icon.

      Let me know if you have any further questions regarding this, I might add this to a queue of upcoming videos.

      Secondly, in terms of adding extra dependencies, in the CMakeLists.txt file you can add multiple files to the add_executable function such as:

      add_executable(hello_world
      main.c
      interrrupts.c
      )

      If you want to add other library dependencies you can use the function add_libraries(dependency1.c …. Dependency2.c …) and if you want to add full directories you use the target_include_directories. Documentation available here: https://cmake.org/cmake/help/latest/command/add_library.html, https://cmake.org/cmake/help/latest/command/target_include_directories.html.

      Hopefully this answered your questions, let me know!

      Kind regards and have a nice day,
      Mark

  2. Hi,

    When I want to configure the task like you do at 3:39, it doesn’t do anything. It only prompts me to create a tasks.json. Do you know the solution?

  3. Fantastic tutorial. I especially like the suggestion to include multiple folders/files under pico-projects.

    For me, I would like to see a tutorial for a Pico I2C scanner with output over USB-uart, and how to invoke the terminal in VS.

  4. Hi,
    Thanks for your efforts!
    Like for Sem above nothing happens when I go to Terminal -> Configure Tasks…
    Do you have a suggestion as to why that could happen?
    I have followed your previous video “How to Set Up Visual Studio Code to Program the Pi Pico (Windows)”. And everything else has worked sofar.

    1. Hi,

      If you try pressing ctrl + shift + P, and in the search that comes up type configure, do you see the “CMake: configure” option?

    2. The above is not totally correct, sorry.
      At Terminal -> Configure Tasks… I get a drop down menu. The editable field says; “Select a task to configure” with four options;
      “Create tasks.json file from template”
      * “Grunt task detection is turned off. Enable grunt task detection…”
      * “Gulp task detection is turned off. Enable gulp task detection…”
      * “Jake task detection is turned off. Enable jake task detection…”

      (“*” is supposed to mimic a cogwheel like the one on my monitor)
      That is as far as I have been.

Leave a comment

Your email address will not be published.