Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Exercise 1

Creating an application

In this exercise, we will learn how to create a working “Hello World” application in the nRF Connect SDK from scratch. We are only doing this for educational purposes to expose you to all the elements of an nRF Connect SDK application.

Keep in mind that this is not the recommended way to create an application. As we explained in lesson 1, the recommended way is by using one of the samples that you can find in the SDK as a baseline.

Exercise steps

1. In the exercise folder for this lesson, create a folder and name it l3_e1.

2. Open this folder and create the files needed for a minimum working application.

  • prj.conf
  • CMakeLists.txt
  • src/main.c (this means create a subdirectory called src and then create the empty file main.c)

3. Open CMakeLists.txt and add the minimum functions needed for the application to build (use any editor).

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hello_world)
target_sources(app PRIVATE src/main.c)
CMake

cmake_minimum_required ensures the build fails if the CMake version is too old.
find_package pulls in the Zephyr build systems, which creates a CMake target named app.
project sets the name of the project.
target_sources adds the source file to the project.

4. In main.c, include the header file for Zephyr Kernel and the printk module like this:

#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
C

5. In main.c, define the main function to continuously print the statement “Hello World!” and then sleep for a small amount of time (to avoid flooding the console).

int main(void)
{
	while (1) {
		printk("Hello World!\n");
		k_msleep(1000);
	}
}
C

Note

prj.conf is left empty because the console drivers needed by printk() are enabled by default by the board configuration file. This is covered in-depth in Lesson 4.

6. Let’s add this as an existing application in nRF Connect for VS Code.

Go to nRF Connect for VS Code and under the Welcome View, select Open an existing application and select the directory l3_e1. Observe the project showing up under the Applications View.

7. Just like in Lesson 1, select Add Build Configuration by hovering your mouse over the project name in the Applications View and selecting the icon that appears. Choose the correct board, then select Build Configuration.

8. Flash the application to your board.

9. To view the output of the application, you need to configure a terminal emulator on your machine. You can work in any terminal emulator you want. In this lesson we will show you how to do it using either the built-in terminal emulator in the nRF Connect extension in VS Code (called nRF Terminal), or using the Serial Terminal application nRF Connect for Desktop. You can choose either one using the tabs below.

There are two important parameters when using the UART console:

  • Speed (baud rate) – the default speed set in the devicetree of Nordic Semiconductor development kits is 115200 bps.
  • Serial line (COM number) – unlike the speed, this differs from machine to machine. A single board can have multiple serial lines, depending on the board.

Note

Which Virtual COM (VCOM) port to choose?

Nordic development kits come with an interface MCU that serves as a debugger for the development kit. The interface MCU provides additional features such as UART-to-USB conversion and one or more virtual COM ports, depending on your board (VCOM0/COMX, VCOM1/COMX, etc..). These different VCOM ports are intended for specific use cases. For example, one port might be used for TrustedFirmware-M, and another for the application. If you connect to a VCOM port and don’t see the expected output, try selecting the other VCOM port and pressing the reset button on your development kit to get the logs from the beginning.

1. To use the terminal emulator in VS code, first go to the Connected Devices tab and open the drop list.

Then open the drop list for that connected device.

Then click the Connect To Serial Port In nRF Terminal icon as shown below.

Then choose the serial terminal which has the following configuration:

  • Baud rate: 115200 baud/sec
  • 8 bits/character, no parity, 1 stop bit (8n1)
  • No flow control: rts and cts = off

As shown below:

You should be able to observe the output as shown below

1. Install the Serial Terminal app.

Open nRF Connect for Desktop, navigate to the Serial Terminal app and click Install.

2. Launch the Serial Terminal app.

3. Connect to your board and find the COM ports.

  1. In the upper right hand corner, Select Device.
  2. Select your board, in the example below we are using the nRF7002 DK. This will automatically connect to one of the COM ports on the device.
  3. Select Disconnect from port to configure the settings.
  4. Select the arrow next to the COM port to view all available COM ports for the device

Note the COM numbers assigned to your board. In the screenshot above, it’s COM8 and COM11, but this value is likely to be different on your host machine.  

4. Configure the baud rate.

  1. Select Serial Settings to see all available settings for the terminal.
  2. Select the drop down arrow under baud rate to change the baud rate to another value.

We will keep the default value of 115200 for now.

5. Connect to the device.

Once everything is configured, select Connect to port.

Reset your board, and you should be able to observe the output, “Hello World!” printed every second.

*** Booting nRF Connect SDK v2.9.0-preview1-11645184a54d ***
*** Using Zephyr OS v3.7.99-adcffa835a8e ***
Hello World!
Hello World!
Hello World!
Terminal

The solution for this exercise can be found in the GitHub repository, l3/l3_e1_sol of whichever version directory you are using.

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

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.