You are currently not logged in and your progress will not be saved. Register or Log in

Exercise 1

Creating an application

In this exercise, we will learn how to create a working “Hello World” application in 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.

1. Create a folder located in the exercise folder for this course, named “fund_less3_exer1”.

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_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 drivers for Zephyr and the printk module like this:

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

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).

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

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-page, select Add an existing application and select the directory fund_less3_exer1. Observe the project showing up under the Applications window.

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

8. To check the output from the application, you need to configure a terminal emulator on your machine. You have absolute freedom to select your favored terminal emulator.

For this course, we are using PuTTY, which can be downloaded from here. PuTTY does not require any installation, it is a single executable that you just need to download. 

Note

Be aware that the process of installing PuTTY on a macOS requires installing Xcode and other tools that are out of the scope of this course. For a simpler alternative, use the built-in Terminal in nRF Connect for VS Code.

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. You can find the serial line listed along your board name in the Connected Devices window inside VS code as shown below:

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

9. Open PuTTY, and follow the steps below:

Configuring PuTTY
  1. Select Serial as the connection type.
  2. Set the correct serial line as the COM number assigned to your device.
  3. Set the speed to 115200.
  4. Start the connection by pressing Open and a session will start.

10. Build and flash the exercise to the board, as we have done in previous lessons. Using a serial terminal, such as PuTTY, you should see the below output, with “Hello World!” printed every second.

*** Booting Zephyr OS build v2.7.0-ncs1  ***
Hello World!

Hello World!

Hello World!

You can download the solution for Exercise 1 below.