Create a new project

As you follow these steps for the first time after install, Visual Studio may prompt you to do various setup things – when it does just click the default option it is asking you.

Click top left “Explorer” button > Open Folder

Create a folder somewhere you want to store your project and open it

Create main.c

Menu > File > New File

Menu > File > Save As > “main.c”

Paste this code into the file:

#include "pico/stdlib.h"


#ifndef PICO_DEFAULT_LED_PIN
	#warning This example requires a board with a default LED
#endif

//***********************************
//***********************************
//********** MAIN FUNCTION **********
//***********************************
//***********************************
int main()
{
	//----- SETUP PINS -----
	const uint IO_LED = PICO_DEFAULT_LED_PIN;
	gpio_init(IO_LED);
	gpio_set_dir(IO_LED, GPIO_OUT);


	//---------------------
	//---------------------
	//----- MAIN LOOP -----
	//---------------------
	//---------------------
	while (true)
	{
		gpio_put(IO_LED, 1);
		sleep_ms(200);
		gpio_put(IO_LED, 0);
		sleep_ms(200);
	}

}
Create CMakeLists.txt

The C/C++ SDK of Raspberry Pi Pico uses the CMake build automation tool which needs us to create “CMakeLists.txt”, containing the instructions describing the project sources files and targets.

Menu > File > New File

Menu > File > Save As > “CMakeLists.txt”

Paste this code into the file:

# Set minimum required version of CMake
cmake_minimum_required(VERSION 3.12)
#include build functions from Pico SDK
include($ENV{PICO_SDK_PATH}/external/pico_sdk_import.cmake)
# Set name of project (as PROJECT_NAME) and C/C++ Standards
project(blink-led C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
# Creates a pico-sdk subdirectory in our project for the libraries
pico_sdk_init()
# point out the CMake, where to find the executable source file
add_executable(${PROJECT_NAME}
        main.c
)
# create map/bin/hex/uf2 files.
pico_add_extra_outputs(${PROJECT_NAME})
# Pull in our pico_stdlib which pulls in commonly used features (gpio, timer-delay etc)
target_link_libraries(${PROJECT_NAME}
            pico_stdlib
)

Close and re-open Visual Studio, Explorer > Open Folder > Open your project folder

Build the project

Press the “Build” button in the bottom blue toolbar
If prompted, select the “GCC x.x.x arm-none-eabi” build toolchain (you should then see this in the bottom blue toolbar – if another build tool is selected click the wrench+screwdriver button in the blue toolbar and select it instead).

The project should build successfully

Running the project

The Raspberry Pi Pico does not need a programmer, you instead drag and drop the binary file onto the Pico in Windows explorer.

Plug the pico USB into your PC while holding down the BOOTSEL button on it (until the Windows system detects it). Then release the BOOTSEL button.

This will have put the Pico into its Bootloader Mass-storage system mode and it should have appeared as a new drive in Windows Explorer named RPI-RP2.

To run your program, drag the “.uf2” file from your projects “\build” directory into the “RPI-RP2” folder. You should then see the LED start flashing.

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *