Feedback
Feedback

Click or drag files to this area to upload. You can upload up to 2 files.

Exercise 2

v2.x.x

Build and flash your first nRF Connect SDK application

In this exercise, we will program a simple application based on the blinky template to toggle an LED on our board. Again, any Nordic Semiconductor development board will work (nRF52, nRF53, nRF91 Series). The idea is to make sure that all the tools needed to build and flash a sample are set up correctly. The focus is to learn how to create an application from a template, build the application, and flash it on a Nordic-powered board.

Basically, we will walk you through the three short videos below to:

  • Create a new application based on a template.
  • Build an application.
  • Flash an application to a board.

Either watch the videos or follow the steps below.

Exercise steps

1. Create a folder close to your root directly that will hold all the exercises that we will be working on throughout this course.

We will create that folder on the C drive in C:\nordic\myapps.

2. In VS Code, click on the nRF Connect Extension. In the Welcome View, click on Create a new application.

nRF Connect for VS Code: Create new application

3. In the New Application View, do the following:

nRF Connect for VS Code: New Application View

3.1. Select a Freestanding application. This will link and use a version of the nRF Connect SDK that you have already installed on your machine.

The other type of application is the Workspace application, which will have a dedicated SDK instance for each application. Workspace applications are out of the scope of this course.

3.2. Choose the version of the nRF Connect SDK to compile and build your application with. The nRF Connect Toolchain version should match the version of the SDK.

The list contains versions already installed on your machine. If you want to install a version that is not already present on your machine, click the Install button. This starts the Toolchain Manager, where you can download different versions of the SDK.

3.3. Choose the directory to store your application folder, nordic\myapps.

3.4. Choose the template to base your application on. This will make a copy of the template and store it in the directory you specified in step 3.3 and name it as you will specify in step 3.5.

We will base our first application on the blinky sample. Type blinky in the Application template field and choose the one that says zephyr\samples\basic\blinky . This is the relative path directly within the nRF Connect SDK where the template is stored.

The Browse… button in the Application template field allows you to filter templates by several criteria including Wireless Communication Protocol, Device (SoC or SiP), and more (crypto, drivers, etc.). It also shows the documentation for the template.

Template documentation accessed through the Select button

Note

The SDK contains a rich set of templates. In the context of templates, Samples are simple and showcase a single feature or library (e.g. SHA256 sample, UART sample, PWM sample, etc.), while Applications are complex and include a variety of libraries to implement a specific use case (e.g. Asset tracker application, Keyboard application, Mouse application).

3.5. Name your application fund_less1_exer2 as this is the naming convention that we will use for exercises throughout this course. Note that this will create a folder for your application called fund_less1_exer2.

3.6. Finally, click on the Create Application button.

This will add an unbuilt application to VS Code as shown below:

Note

nRF Connect for VS Code might show some error squiggles of headers files not found. Please ignore these error squiggles at this stage. These errors will automatically be resolved once you add and build a configuration (next step).

4. Add a build configuration.

One of the many advantages of the nRF Connect SDK is the high decoupling between an application source code and the software configuration/hardware description, making it extremely easy to switch the build for a new hardware or software configuration.

In this step, we will specify which development board or custom board (hardware) we want to build the application for. We will also select the software configuration (*.conf and possible overlays) to be used in the build.

Hover over the application name and click on the small Add Build Configuration icon.

nRF Connect for VS Code: Add Build Configuration icon

This will open the Add Build Configuration View shown below:

4.1. Using the board ID, choose the board you want to flash your application to. See the table below for an overview of board ID’s for Nordic devices.

DeviceBoard ID
nRF5340 DKnrf5340dk_nrf5340_cpuapp_ns
nRF52840 DKnrf52840dk_nrf52840
nRF52833 DKnrf52833dk_nrf52833
nRF52 DKnrf52dk_nrf52832
nRF9160 DKnrf9160dk_nrf9160_ns
Thingy:91*thingy91_nrf9160_ns
Thingy:53*thingy53_nrf5340_cpuapp_ns
Board ID for Nordic devices

In the screenshot, we chose the nRF52833DK by specifying its ID, nrf52833dk_nrf52833. You can also create your own board and define your own board ID, however this is beyond the scope of this course.

4.2. The board revision is usually written down on a sticker to the back of the board. However, this field is optional and by setting it to default the build system selects a revision version.

4.3. Depending on the template you chose in step 3, you will be presented with at least one application configuration prj.conf. Some templates contain more than one application configuration file (eg: prj.conf, prj_minimal.conf, prj_cdc.conf). These different configurations, if found in the template, are explained in the template documentation. The blinky template has only one option.

4.4. The Kconfig fragments field will list the Kconfig overlays (aka Kconfig Fragments) that are available in the template or have been added in the application folder. These are modifiers to the application configuration file (covered in Lesson 3). The blinky template has none.

4.5. The Extra CMake arguments field allows you to pass arguments to the build system if needed. We will leave that blank for this demo.

4.6. The Build directory name field gives you the option to manually name the build directory where the final binary files and temporary build files will be stored. We will leave this to be the default name specified by the tool, which is build or build_1 if you have already built one application before.

4.7. Leave the Build after generating configuration option enabled to trigger the build process after clicking on the Build Configuration button.

4.8. Select the Enable debug options to enable debugging of the application.

4.9. Click on the Build Configuration button to create the configuration and initiate the build process.

The build process will take some time to finish. Open the Terminal (View->Terminal) to see the progress of the build.

nRF Connect for VS Code: Terminal

A successful build is indicated by displaying the memory usage of the application as shown in the screenshot above.

Note

A key component in the build process is west, which is a core command-line utility in nRF Connect SDK/Zephyr. West provides several services like repository management, building applications, and flashing, and is internally invoked by nRF Connect for VS Code.

You can also issue these commands manually in the command line prompt if you prefer to work in the terminal.

Type west help in the Terminal Panel if you are interested in learning how to use the command-line interface with the nRF Connect SDK. Alternatively, you could checkout this short video.

If you switch to the Explorer in VS Code or browse to the application directory, you will notice that there is a new subdirectory created from the build process called build. This folder contains the build output including the binary file that we will flash on the board in the next step.

5. Make sure that your development kit is connected to your computer and that it is switched on. It should be listed in the Connected Devices View in the nRF Connect for VS Code extension.

nRF Connect for VS Code: Connected Devices

If you don’t see your board listed, press on the Refresh Connected Devices icon in the Connected Devices View.

6. From the Actions View, click Flash to flash the application to the board. You can open the Terminal Panel to see the progress of the flashing as shown below.

Note

The difference between Flash and Erase And Flash To Board is that the latter erases the whole device, including all data the application has saved.

For example, if the device is added to a mesh network, Erase And Flash To Board will make the device forget this.

LED1 on your board should be blinking now in 1-second intervals.

Important: “Thingy:53” & “Thingy:91”

If you are using a Thingy:53 or a Thingy:91, please read the flash procedure for the Thingy:53 here and for the Thingy:91 here.

7. Just for the sake of this demonstration, let’s change the LED blink rate.

Locate the main.c file in Source Files->Applications or through the Explorer View of Visual Studio Code. On line 11, change the value of the SLEEP_TIME_MS from 1000 to 100. This will change the interval at which the LED is blinking.

#define SLEEP_TIME_MS   100

8. Rebuild and re-flash the application on your board. You should observe that the LED is now blinking at a higher frequency.

In Lesson 2, we will take an in-depth look at the source code of this application to understand how it works.

v1.6.0 – v1.9.1

Build and flash your first nRF Connect SDK application

In this exercise, we will program a simple application based on the blinky template to toggle an LED on our board. Again, any Nordic Semiconductor development board will work (nRF52, nRF53, nRF91 Series). The idea is to make sure that all the tools needed to build and flash a sample are set up correctly. The focus is to learn how to create an application from a template, build the application, and flash it on a Nordic-powered board.

Basically, we will walk you through the three short videos below to:

  • Create a new application based on a template.
  • Build an application.
  • Flash an application to a board.

Either watch the videos or follow the steps below.

Exercise steps

1. Create a folder close to your root directly that will hold all the exercises that we will be working on throughout this course.

We will create that folder on the C drive in C:\nordic\myapps.

2. In VS Code, click on the nRF Connect Extension. In the Welcome View, click on Create a new application.

nRF Connect for VS Code: Create new application

3. In the New Application View, do the following:

nRF Connect for VS Code: New application window

3.1. Select a Freestanding application. This will link and use a version of the nRF Connect SDK that you have already installed on your machine.

The other type of application is the Workspace application, which will have a dedicated SDK instance for each application. Workspace applications are out of the scope of this course.

3.2. Choose the directory to store your application folder, nordic\myapps.

3.3. Name your application fund_less1_exer2 as this is the naming convention that we will use for exercises throughout this course. Note that this will create a folder for your application called fund_less1_exer2.

3.4. Choose the version of nRF Connect SDK to compile and build your application with. The nRF Connect Toolchain should match the version of the SDK.

The list contains versions already installed on your machine. If you want to install a version that is not already present on your machine, press on the Install button. This invokes the Toolchain Manager, where you can download different versions of the SDK.

3.5. Chose the template to base your application on. This will make a copy of the template and store it in the directory you specified in step 3.2 and name it as you specified in step 3.3.

Note

The SDK contains a rich set of templates. In the context of templates, Samples are simple and showcase a single feature or library (e.g. SHA256 sample, UART sample, PWM sample, etc.), while Applications are complex and include a variety of libraries to implement a specific use case (e.g. Asset tracker application, Keyboard application, Mouse application).

We will base our first application on the blinky sample. Type blinky in the Application template field and choose the one that says zephyr\samples\basic\blinky . This is the relative path directly within the nRF Connect SDK where the template is stored.

The Browse… button in the Application template field allows you to filter templates by several criteria including Wireless Communication Protocol, Device (SoC or SiP), and more (crypto, drivers, etc.). It also shows documentation for the templates.

Template documentation accessed through the Browse button

3.6. Finally, click on the Create Application button.

This will add an unbuilt application to VS Code as shown below:

4. Add a build configuration.

One of the many advantages of the nRF Connect SDK is the high decoupling between an application source code and the software configuration/hardware description, making it extremely easy to switch the build for a new hardware or software configuration.

In this step, we will specify which development board or custom board (hardware) we want to build the application for. We will also select the software configuration (*.conf and possible overlays) to be used in the build.

Hover over the application name and click on the small Add Build Configuration icon.

nRF Connect for VS Code: Add Build Configuration icon

This will open the Add Build Configuration View shown below:

4.1. Using the board ID, choose the board you want to flash your application to. See the table below for an overview of board ID’s for Nordic devices.

DeviceBoard ID
nRF5340 DKnrf5340dk_nrf5340_cpuapp_ns
nRF52840 DKnrf52840dk_nrf52840
nRF52833 DKnrf52833dk_nrf52833
nRF52 DKnrf52dk_nrf52832
nRF9160 DKnrf9160dk_nrf9160_ns
Thingy:91*thingy91_nrf9160_ns
Board ID for Nordic devices

In the screenshot, we select the nRF52833 DK by specifying its ID, nrf52833dk_nrf52833. You can also create your own board and define your own board ID, however this is beyond the scope of this course.

4.2. Depending on the template you chose in step 3, you will be presented with at least one application configuration prj.conf. Some templates contain more than one application configuration file (eg: prj.conf, prj_minimal.conf, prj_cdc.conf). These different configurations, if found in the template, are explained in the template documentation. The blinky template has only one option.

4.3. The Kconfig fragments field will list the Kconfig overlays (aka Kconfig Fragments) that are available in the template or have been added in the application folder. These are modifiers to the application configuration file (covered in Lesson 3). The blinky template has none.

4.4. The Extra CMake arguments field allows you to pass arguments to the build system if needed. We will leave that blank for this demo.

4.5. The Build directory name field gives you the option to manually name the build directory where the final binary files and temporary build files will be stored. We will leave this to be the default name specified by the tool, which is build.

4.6. Leave the Build after generating configuration option enabled to trigger the build process after clicking on the Build Configuration button.

4.7. Select the Enable debug options to enable debugging of the application.

4.8. Click on the Build Configuration button to create the configuration and initiate the build process.

The build process will take some time to finish. Open the Terminal (View->Terminal) to see the progress of the build.

nRF Connect for VS Code: Terminal

A successful build is indicated by displaying the memory usage of the application as shown in the screenshot above.

Note

A key component in the build process is west, which is a core command-line utility in nRF Connect SDK/Zephyr. West provides several services like repository management, building applications, and flashing, and is internally invoked by nRF Connect for VS Code.

You can also issue these commands manually in the command line prompt if you prefer to work in the terminal.

Type west help in the Terminal Panel if you are interested in learning how to use the command-line interface with the nRF Connect SDK. Alternatively, you could checkout this short video.

If you switch to the Explorer in VS Code or browse to the application directory, you will notice that there is a new subdirectory created from the build process called build. This folder contains the build output including the binary file that we will flash on the board in the next step.

5. Make sure that your development kit is connected to your computer and that it is switched on. It should be listed in the Connected Devices View in the nRF Connect for VS Code extension.

nRF Connect for VS Code: Connected Devices

If you don’t see your board listed, press on the Refresh Connected Devices icon in the Connected Devices View.

6. From the Actions View, click Flash to flash the application to the board. You can open the Terminal Panel to see the progress of the flashing as shown below.

Note

The difference between Flash and Erase And Flash To Board is that the latter erases the whole device, including all data the application has saved.

For example, if the device is added to a mesh network, Erase And Flash To Board will make the device forget this.

LED1 on your board should be blinking now in 1-second intervals.

Important (Thingy:91)

If you are using a Thingy:91, please see the flash procedure here.

7. Just for the sake of this demonstration, let’s change the LED blink rate.

Locate the main.c file in Source Files->Applications or through the Explorer View of Visual Studio Code. On line 11, change the value of the SLEEP_TIME_MS from 1000 to 100. This will change the interval at which the LED is blinking.

8. Rebuild and re-flash the application on your board. You should observe that the LED is now blinking at a higher frequency.

In Lesson 2, we will take an in-depth look at the source code of this application to understand how it works.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

Forgot your password?
Enter your email address, and we will send a link to reset your password.