Create a Reusable ESP-IDF Component With idf_component.yml
Package ESP-IDF code as a reusable component: folder layout, idf_component_register, idf_component.yml dependencies and sharing it.

A firmware project that reads one sensor in three products ends up with three copies of the driver unless the driver is packaged. In ESP-IDF the unit of reuse is the component: a directory with a CMakeLists.txt that registers sources and include paths. The component can live in your project, in a shared folder, or in the IDF Component Manager registry, with an idf_component.yml manifest that declares its version and what it depends on. This post builds a small component for a temperature sensor driver, ESP-IDF v5.4, shows the layout, the registration call, the manifest, how a project consumes it three different ways, and how to test and publish it.
The sensor is a made-up I2C part at address 0x48 that returns a 16-bit signed value in 0.0625 degree steps. The code is written for the example, and the structure is what carries over.
The smallest component
A component is a folder. Its name becomes the component name, which other components use in REQUIRES. A layout for the driver:
components/
tempsensor/
CMakeLists.txt
idf_component.yml
include/
tempsensor.h
tempsensor.c
README.mdPublic headers go in include/ and private ones next to the sources, so users cannot depend on internals. The registration file lists the sources and the public include directory, and states the dependency on the I2C driver:
idf_component_register(
SRCS "tempsensor.c"
INCLUDE_DIRS "include"
REQUIRES driver
)REQUIRES is for components whose headers appear in your public header, since anything that includes your header needs them too. If the I2C driver is used only inside tempsensor.c, put it in PRIV_REQUIRES instead, so consumers do not inherit the include path. In ESP-IDF v5.4 the I2C master API is in the esp_driver_i2c component, and the legacy name driver still works as an umbrella that pulls it in, so check which you want against your target version.
The header
The header defines the interface and keeps hardware details out of it. A handle type hides the state, and functions return esp_err_t so callers can use the usual error handling:
#pragma once
#include "esp_err.h"
#include "driver/i2c_master.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct tempsensor *tempsensor_handle_t;
typedef struct {
i2c_master_bus_handle_t bus;
uint8_t address; /* 7-bit address, 0x48 by default */
uint32_t scl_speed_hz; /* 100000 or 400000 */
} tempsensor_config_t;
esp_err_t tempsensor_create(const tempsensor_config_t *cfg, tempsensor_handle_t *out);
esp_err_t tempsensor_read_celsius(tempsensor_handle_t h, float *celsius);
void tempsensor_delete(tempsensor_handle_t h);
#ifdef __cplusplus
}
#endifThe extern "C" guard lets C++ projects include it. The configuration struct has an explicit address field so the same driver works with a part that has address pins.
The implementation
#include <stdlib.h>
#include "tempsensor.h"
#include "esp_log.h"
static const char *TAG = "tempsensor";
struct tempsensor {
i2c_master_dev_handle_t dev;
};
esp_err_t tempsensor_create(const tempsensor_config_t *cfg, tempsensor_handle_t *out)
{
if (cfg == NULL || out == NULL) {
return ESP_ERR_INVALID_ARG;
}
struct tempsensor *s = calloc(1, sizeof(*s));
if (s == NULL) {
return ESP_ERR_NO_MEM;
}
i2c_device_config_t dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = cfg->address ? cfg->address : 0x48,
.scl_speed_hz = cfg->scl_speed_hz ? cfg->scl_speed_hz : 100000,
};
esp_err_t err = i2c_master_bus_add_device(cfg->bus, &dev_cfg, &s->dev);
if (err != ESP_OK) {
free(s);
return err;
}
*out = s;
return ESP_OK;
}
esp_err_t tempsensor_read_celsius(tempsensor_handle_t h, float *celsius)
{
if (h == NULL || celsius == NULL) {
return ESP_ERR_INVALID_ARG;
}
uint8_t reg = 0x00;
uint8_t raw[2];
esp_err_t err = i2c_master_transmit_receive(h->dev, ®, 1, raw, 2, 100);
if (err != ESP_OK) {
ESP_LOGE(TAG, "read failed: %s", esp_err_to_name(err));
return err;
}
int16_t value = (int16_t)((raw[0] << 8) | raw[1]);
*celsius = (float)value * 0.0625f;
return ESP_OK;
}
void tempsensor_delete(tempsensor_handle_t h)
{
if (h == NULL) {
return;
}
i2c_master_bus_rm_device(h->dev);
free(h);
}The last argument of i2c_master_transmit_receive is a timeout in milliseconds. If the sensor returns the bytes 0x01 0x90, the signed value is 400, and 400 * 0.0625 is 25.0 degrees:
raw 0x01 0x90 -> value 400 -> 25.0 CThe manifest
idf_component.yml is metadata for the IDF Component Manager. For a component that is only used inside your project it is optional, but writing it early costs nothing and makes publication a matter of one command:
version: "1.0.0"
description: "Driver for a 16-bit I2C temperature sensor"
url: https://github.com/example/tempsensor
license: MIT
targets:
- esp32
- esp32s3
- esp32c3
- esp32c6
dependencies:
idf: ">=5.2"version follows semantic versioning, and the manager uses it to resolve compatible updates. targets limits which chips can use the component, which stops a build for an unsupported target with a clear message. dependencies lists other components with version ranges. An entry like espressif/led_strip: "^3.0.0" means any 3.x release, and idf: ">=5.2" states the minimum framework version. Keep ranges as wide as your testing supports; a pin that is too tight makes the component hard to combine with others.
Three ways a project uses it
In-tree. Put the folder in a components/ directory next to main/. ESP-IDF discovers it with no configuration, and main lists it:
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES tempsensor)From another path. Share one checkout between projects with EXTRA_COMPONENT_DIRS before the project call in the top-level CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
set(EXTRA_COMPONENT_DIRS ../shared-components)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(sensor_node)From a Git repository or the registry. Declare it in the consuming component's manifest. The component manager downloads it into managed_components/ on the next build:
dependencies:
tempsensor:
git: https://github.com/example/tempsensor.git
version: "v1.0.0"For a component published in the registry, the entry is namespace/tempsensor: "^1.0.0". The resolved versions are recorded in dependencies.lock next to the project, which you commit so every machine builds the same tree. Do not commit managed_components/, which is regenerated.
Using it
#include "driver/i2c_master.h"
#include "esp_log.h"
#include "tempsensor.h"
void app_main(void)
{
i2c_master_bus_config_t bus_cfg = {
.i2c_port = I2C_NUM_0,
.sda_io_num = GPIO_NUM_8,
.scl_io_num = GPIO_NUM_9,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus;
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_cfg, &bus));
tempsensor_config_t cfg = { .bus = bus, .address = 0x48, .scl_speed_hz = 100000 };
tempsensor_handle_t sensor;
ESP_ERROR_CHECK(tempsensor_create(&cfg, &sensor));
float c;
if (tempsensor_read_celsius(sensor, &c) == ESP_OK) {
ESP_LOGI("app", "temperature %.2f C", c);
}
tempsensor_delete(sensor);
}With a sensor returning 400 counts:
I (402) app: temperature 25.00 CThe application owns the bus and passes it in, so two drivers can share one I2C bus. That is the design choice that makes a component reusable: the component does not create global resources the caller might also want.
Examples and tests inside the component
A component repository should carry an examples/ folder with a minimal project that uses it, and the manager shows these in the registry. Add a test_apps/ folder with a Unity test project for logic that can run on target. Keep hardware-independent logic, such as the 0.0625 scaling, in a function that can be tested on the host with the Linux target, and keep the I2C calls thin.
Publishing
Registering on the component registry uses the compote tool, installed with the ESP-IDF Python environment. After compote component pack, which validates the manifest and builds an archive, compote component upload sends it with a namespace and an API token from your account. A version that is published cannot be replaced, so bump version for each release, and use the -dev or pre-release forms such as 1.1.0-rc1 for testing. If the component should stay private, a Git dependency or a shared path costs nothing and needs no account.
When a component is the wrong unit
Splitting code into components has a price: each has its own CMakeLists.txt, its own include path and its own REQUIRES graph, and circular REQUIRES between components is an error. Do not make a component out of ten lines used once. Make one when code has a clear interface, more than one consumer, or a lifecycle (versions, tests) of its own.
Versioning and compatibility rules
A component used by several projects needs a compatibility promise. Semantic versioning gives it a vocabulary: a patch release changes no interface, a minor release adds functions, and a major release may break callers. In a C component, the interface is the public header, so any change to a function signature, a struct layout in the header, or the meaning of a return code is a major change. Adding a field to the configuration struct is the classic trap: callers who zero-initialize the struct with = { 0 } and set fields by name still work, and callers who use positional initializers break.
For that reason, design configuration structs for growth. Treat a zero field as "use the default", as tempsensor_create does for the address and the clock speed, so a new field can be added at the end with a zero meaning "behave as before". Document each default next to the field.
Keep the manifest honest about the framework. The idf dependency states the minimum ESP-IDF version you tested, and targets lists only the chips you have run on. Listing a target you never tested invites bug reports from boards you cannot reproduce. When a new ESP-IDF major version changes an API your component uses, such as the I2C driver rewrite between v5.1 and v5.2, ship a new component version with the narrower idf range, and keep the old release available for projects that have not moved.


