Build One Zephyr App for Several Boards With Board Overlays
Use per-board .overlay and .conf files in a boards folder so one Zephyr application builds for several boards, with the naming rules for Zephyr 4.2.

A Zephyr application should not contain the words "nrf52" or "stm32" in its source. Pin numbers, peripheral choices and buffer sizes change from one board to the next, and Zephyr provides a mechanism that keeps those differences out of C code: board-specific devicetree overlays and Kconfig fragments stored next to the application. This post builds one small program for two boards and one emulator, using Zephyr 4.2 naming rules, and shows how to confirm which files a build actually picked up. The overlay syntax itself is explained in the post on writing a devicetree overlay.
The program uses an alias, not a pin
The application asks for "an LED" and "a UART" by alias. It never mentions a port or a pin:
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>
#define LED0_NODE DT_ALIAS(led0)
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
int main(void)
{
if (!gpio_is_ready_dt(&led)) {
return 0;
}
gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
while (1) {
gpio_pin_toggle_dt(&led);
k_msleep(CONFIG_APP_BLINK_MS);
}
return 0;
}Two things come from outside the C file. The led0 alias must exist in the board's devicetree, and CONFIG_APP_BLINK_MS must be defined by a Kconfig file. Official boards that have an LED already define led0, so this builds for them without any overlay. Boards that do not, or boards where you want a different LED, get an overlay.
Define the Kconfig symbol in your application's own Kconfig file:
mainmenu "Blink"
config APP_BLINK_MS
int "Blink period in milliseconds"
default 1000
source "Kconfig.zephyr"The source "Kconfig.zephyr" line is required, because it pulls in all of Zephyr's own options after your symbol. With it, prj.conf can set CONFIG_APP_BLINK_MS like any other symbol.
The boards folder
Overlays go in a folder named boards/ at the application root. Zephyr looks there by name:
blink/
├── CMakeLists.txt
├── Kconfig
├── prj.conf
├── src/main.c
└── boards/
├── nrf52840dk_nrf52840.overlay
├── nrf52840dk_nrf52840.conf
└── native_sim.overlayThe file name is the board target with every / replaced by _. The board target in Zephyr 4.2 is <board>[@revision]/<soc>[/<cpu cluster>]. So nrf52840dk/nrf52840 becomes nrf52840dk_nrf52840, and native_sim/native can be abbreviated, because when the SoC part is the board's only one, Zephyr also accepts the board name alone. A revision is appended with an underscore in the file name, as in nrf52840dk_nrf52840_1_0_0.overlay, and the revision-specific file is merged after the general one.
The build merges, in order, the board's own devicetree, then any file named by DTC_OVERLAY_FILE, which defaults to your matching boards/<target>.overlay. Later definitions win, so an overlay can change a property the board file set.
Per-board devicetree changes
Suppose the nRF52840 kit should blink a different LED than its first one. Its overlay redirects the alias:
/ {
aliases {
led0 = &led2;
};
};The leading / { opens the root node, and aliases is the standard node that holds aliases. This assigns led0 the node labeled led2, which the board file already defines. Because overlays are merged over the board file, only the changed alias needs to be written.
For native_sim, which has no LED at all, the overlay defines a fake one on the emulated GPIO controller:
/ {
aliases {
led0 = &sim_led;
};
leds {
compatible = "gpio-leds";
sim_led: led_0 {
gpios = <&gpio0 0 GPIO_ACTIVE_HIGH>;
};
};
};Here gpio0 is the emulated controller that the native_sim board declares. The label names are the board's, so confirm them in the board's devicetree source before relying on them.
Per-board Kconfig changes
A file named boards/<target>.conf is merged after prj.conf. Use it for options that only make sense on one board:
CONFIG_APP_BLINK_MS=250
CONFIG_PM=yCONFIG_APP_BLINK_MS=100prj.conf holds the settings shared by every board, and the board files hold the differences. That keeps prj.conf short and stops one board's driver choices from leaking into another's build.
Build for each target
Give each board its own build directory, so switching never needs a clean build:
west build -b nrf52840dk/nrf52840 -d build/nrf blink
west build -b native_sim -d build/sim blinkEach build prints which overlay it found near the top of the CMake output:
-- Found devicetree overlay: /home/user/blink/boards/nrf52840dk_nrf52840.overlay
-- Found devicetree overlay: /home/user/blink/boards/native_sim.overlayThe two lines are from separate builds, and the path will be yours. If the line is missing, the file name does not match the board target. Read the first CMake lines for the Board: entry and spell the file accordingly.
To prove the merge happened, grep the final devicetree:
grep -A2 "aliases" build/nrf/zephyr/zephyr.dts aliases {
led0 = &led2;
led1 = &led1;Alias order follows the merged file, so other aliases will appear around it.
Add a board that has no overlay
You do not need to touch the application to add a board that already defines led0 and runs main(). You add an overlay only when the board lacks something. For a custom board, add a boards/<target>.overlay that supplies it, or define a proper board as described in the hardware model v2 post.
Where this approach breaks down
Overlays work well for a handful of boards. At twenty boards the boards/ folder becomes a second board support package, and a shared concern such as "enable the sensor" ends up copied into each file. Two tools help: snippets, which package a devicetree and Kconfig change you can apply to any board with -S, and sysbuild configurations. A project whose boards differ in architecture, not just pins, should also be tested on each target, because an overlay that compiles proves only that the devicetree is valid.
Verifying each board before you trust it
A build that succeeds proves the devicetree is syntactically valid and the Kconfig symbols exist. It does not prove the application does the right thing on that board. Three cheap checks catch most overlay mistakes.
First, search the generated header for the macro your code depends on. For the alias used above, the build writes build/<dir>/zephyr/include/generated/zephyr/devicetree_generated.h, and it contains DT_N_ALIAS_led0 definitions only when the alias resolves. If the line is absent, the overlay did not apply or the alias name is wrong.
Second, read the final .config for the options from the board-specific file. A symbol you set in boards/<target>.conf that depends on a missing driver is reported as a warning during configuration and is dropped, and a build that only prints warnings still succeeds. Treat warnings from Kconfig as errors in your own workflow with -DCMAKE_COMPILE_WARNING_AS_ERROR=ON, or by scanning the log in CI.
Third, run the emulated board in a pipeline. The native_sim target produces a normal Linux executable, so a continuous integration job can run it and check its console output on every commit. Hardware boards then need testing only for the parts the emulation cannot cover, such as timing and real peripherals.
Overlay order and conflicts
When more than one file contributes to the same property, the last one wins, and the order is predictable: the board's devicetree first, then the SoC overlay if any, then files from DTC_OVERLAY_FILE, then EXTRA_DTC_OVERLAY_FILE. The extra variable is meant for command-line additions that must not replace your default overlays. For example, a bring-up session can add -DEXTRA_DTC_OVERLAY_FILE=debug.overlay to turn on a test UART without editing the checked-in files.


