Nanopb — Lightweight Protocol Buffers for Embedded Systems

Nanopb is a lightweight, open-source Protocol Buffers implementation in pure ANSI C, purpose-built for memory-constrained embedded systems. Deliver efficient, reliable binary serialization across 8-bit, 16-bit, and 32-bit microcontrollers while maintaining seamless compatibility with applications and services.

Efficient Protocol Buffers for Embedded Systems

Nanopb brings the power of Protocol Buffers to resource-constrained devices, enabling developers to manage structured data efficiently without the complexity and memory overhead of traditional serialization libraries.

Nanopb designed for maximun Efficiency and Reliability

Powerful capabilities, minimal resource requirements. Explore the core features that make Nanopb a reliable choice for memory-constrained systems and modern embedded development.

Predictable Memory Management

Maintain precise control over memory usage with static allocation, configurable field sizes, and minimal reliance on dynamic memory.

Ultra-Lightweight Footprint

Run Protocol Buffers on resource-limited hardware with a compact C implementation designed to minimize flash and RAM consumption.

Protocol Buffers Compatibility

Exchange structured binary data seamlessly with applications and services using the standard Protocol Buffers wire format.

Configurable Code Generation

Transform .proto schemas into optimized C source files with customizable field limits, data types, and allocation strategies.

Stream-Based Serialization

Encode and decode data efficiently through flexible streams and callbacks, reducing the need for large intermediate buffers.

Portable Embedded Integration

Integrate across diverse microcontrollers, RTOS environments, and build systems without platform-specific dependencies.

How Nanopb Works From Schema to Embedded Integration

A streamlined four-step workflow for efficient Protocol Buffers implementation, from message definition to reliable binary communication on embedded devices.

Design Your Protocol Schema

Define structured messages in a .proto file using standard Protocol Buffers syntax. Configure field sizes and memory limits through optional .options settings.

Generate Optimized C Code

Use the Nanopb generator to transform .proto definitions into lightweight .pb.c and .pb.h files, including the required message descriptors.

Build Into Your Firmware

Integrate the generated files with pb_encode.c, pb_decode.c, and pb_common.c to enable Protocol Buffers support within your embedded application.

Encode, Decode & Communicate

Use pb_encode() and pb_decode() to exchange compact binary messages through memory buffers or custom communication streams.

Install Nanopb and encode your first message

A complete Nanopb example in six steps. Works on Windows, macOS and Linux; the generated C code runs on any target.

Download the source

Grab the ZIP below (or clone the repository) and unpack it next to your project.

				
					git clone https://github.com/nanopb/nanopb.git
				
			

Install the generator dependencies

The generator is a Python script. With Python 3 installed, add the protobuf tooling. Prefer a single package?
pip install nanopb installs the generator directly.

				
					pip install --upgrade protobuf grpcio-tools
				
			

Describe your message

Create reading.proto and an optional reading.options that caps the string length.

				
					syntax = "proto3";

message Reading {
  uint32 device_id = 1;
  sint32 temp_c    = 2;
  string status    = 3;
  bool   alarm     = 4;
}
Copy
options
# reading.options – 16 characters + terminating null
Reading.status max_size:17
				
			

Generate the C files

This produces reading.pb.h and reading.pb.c.

				
					python nanopb/generator/nanopb_generator.py reading.proto
				
			

Add Nanopb to your build

Compile pb_encode.c, pb_decode.c, pb_common.c and the generated file, and add the Nanopb folder to your include path.

Encode and decode

Fill the struct and call pb_encode(). Decoding on the other side is the mirror image.

				
					#include <pb_encode.h>
#include <pb_decode.h>
#include "reading.pb.h"

uint8_t buffer[Reading_size];
Reading msg = Reading_init_zero;
msg.device_id = 4217;
msg.temp_c = -12;
strcpy(msg.status, "OK");

pb_ostream_t out = pb_ostream_from_buffer(buffer, sizeof(buffer));
if (!pb_encode(&out, Reading_fields, &msg)) {
    printf("Encode failed: %s\n", PB_GET_ERROR(&out));
}

// ...send out.bytes_written bytes, then on the receiver:
Reading rx = Reading_init_zero;
pb_istream_t in = pb_istream_from_buffer(buffer, out.bytes_written);
bool ok = pb_decode(&in, Reading_fields, &rx);
				
			

Nanopb — Powering Real-World Embedded Applications

Enabling efficient, structured communication across connected devices, industrial platforms, and resource-constrained environments.

Nanopb vs protobuf and other options

CriteriaNanopbGoogle protobuf (C++)protobuf-cJSON (e.g. cJSON)
LanguageCC++CC
Typical flash cost~2–10 kBHundreds of kB+Tens of kB~10–20 kB parser
Heap requiredNo (optional)YesYes on decode (custom allocator possible)Usually
Protobuf wire compatibleYesYesYesNo (text)
Reflection / dynamic messagesNoYesLimitedn/a
Built-in RPCNoVia gRPCBasic service stubsNo
Payload sizeCompact binaryCompact binaryCompact binary2–5× larger
Best fitMCUs, RTOS, bare metalServers, desktop, mobileEmbedded LinuxHuman-readable debugging
Nanopb

Reliable Data Handling for Embedded Systems

Nanopb provides a dependable foundation for Protocol Buffers serialization in resource-constrained environments.

Its configurable memory allocation, structured message validation, and explicit error handling help developers manage data safely while maintaining consistent performance across embedded applications.

Nanopb — Key Advantages and Limitations

Explore the key strengths and practical limitations of Nanopb to understand its suitability for embedded systems and resource-constrained applications.

Frequently Asked Questions About Nanopb

Nanopb is a lightweight, open-source implementation of Google Protocol Buffers written in ANSI C. It enables efficient binary serialization and structured data exchange in embedded systems with limited RAM and flash memory.

Nanopb is specifically designed for resource-constrained devices. Its compact codebase, configurable memory allocation, and efficient serialization make it suitable for microcontrollers and embedded applications.

Unlike the standard Protocol Buffers implementations commonly used in desktop and server applications, Nanopb prioritizes minimal memory consumption, compact generated code, and predictable resource usage.

Nanopb is implemented in ANSI C and can also be integrated into C++ applications. Its generated code works with standard Protocol Buffers messages used by applications written in other supported languages.

Yes. Nanopb supports various microcontroller architectures, including 8-bit, 16-bit, and 32-bit platforms, making it suitable for a wide range of embedded development environments.

Nanopb typically requires only a few kilobytes of program memory. Actual RAM and flash usage depend on the target architecture, compiler settings, message definitions, and enabled library features.

No. Nanopb supports static memory allocation by default. Dynamic allocation is optional and can be enabled through the PB_ENABLE_MALLOC compilation setting when required.

Nanopb uses a Python-based generator or Protocol Buffers compiler plugin to convert .proto message definitions into .pb.c and .pb.h files for integration into C projects.

Nanopb .options files configure generated message structures, including maximum string lengths, repeated field counts, allocation types, and other memory-related settings.

Nanopb uses pb_encode() and pb_decode() to serialize C structures into Protocol Buffers binary messages and deserialize incoming messages back into structured data.

Yes. Nanopb uses the standard Protocol Buffers wire format, allowing embedded devices to exchange compatible messages with cloud services, backend systems, and applications using other Protobuf implementations.

Yes. Nanopb provides stream-based encoding and decoding through pb_ostream_t and pb_istream_t, allowing developers to process messages using memory buffers or custom stream callbacks.

Yes. Nanopb can be integrated into STM32, Arduino, and other microcontroller projects. Its portable C implementation supports a variety of hardware platforms and embedded development environments.

Yes. Nanopb can be integrated with Zephyr RTOS to provide Protocol Buffers serialization for embedded applications running on supported microcontrollers.

Nanopb can be integrated with CMake, Make, SCons, Bazel, Meson, Conan, and PlatformIO, offering flexibility across embedded development workflows.

Nanopb supports configurable fixed-size storage, callback-based processing, and optional pointer-based allocation for variable-length fields. Developers can select an appropriate strategy based on memory requirements.

Nanopb reports decoding failures through function return values and optional error messages. It can detect conditions such as malformed data, invalid field values, and insufficient buffer capacity.

Yes. Its compact binary serialization makes Nanopb useful for IoT devices, wireless sensor networks, and embedded systems where bandwidth and memory efficiency are important.

Yes. Nanopb is distributed under the permissive zlib License, allowing use in personal, commercial, and open-source projects subject to the license conditions.

Start by defining a message in a .proto file, generate the corresponding C source files, integrate the Nanopb library into your project, and use pb_encode() and pb_decode() to exchange structured binary messages.

Scroll to Top