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.
- What is Nanopb
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.
- Memory-Efficient Design: Optimized for minimal RAM and flash usage.
- Flexible Serialization: Efficient encoding and decoding with configurable memory allocation.
- Seamless Interoperability: Reliable data exchange across embedded devices and applications.
- Features
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 it works
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.
- Installation
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
#include
#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);
- Use cases
Nanopb — Powering Real-World Embedded Applications
Enabling efficient, structured communication across connected devices, industrial platforms, and resource-constrained environments.
- IoT & Sensor Networks: Efficient data transmission across connected sensors and low-power IoT devices.
- Embedded-to-Mobile Communication: Seamless data exchange between microcontrollers and mobile applications.
- Inter-Processor Communication: Reliable transfer of commands and telemetry between embedded processors.
- Device Configuration & Firmware: Structured management of device settings and firmware update metadata.
- Cloud & Backend Integration: Compact binary communication between embedded devices and cloud services.
- Embedded Testing & Validation: Consistent data structures for firmware testing, simulation, and system validation.
- Comparison
Nanopb vs protobuf and other options
| Criteria | Nanopb | Google protobuf (C++) | protobuf-c | JSON (e.g. cJSON) |
|---|---|---|---|---|
| Language | C | C++ | C | C |
| Typical flash cost | ~2–10 kB | Hundreds of kB+ | Tens of kB | ~10–20 kB parser |
| Heap required | No (optional) | Yes | Yes on decode (custom allocator possible) | Usually |
| Protobuf wire compatible | Yes | Yes | Yes | No (text) |
| Reflection / dynamic messages | No | Yes | Limited | n/a |
| Built-in RPC | No | Via gRPC | Basic service stubs | No |
| Payload size | Compact binary | Compact binary | Compact binary | 2–5× larger |
| Best fit | MCUs, RTOS, bare metal | Servers, desktop, mobile | Embedded Linux | Human-readable debugging |
- Safety
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.
- Controlled Memory Allocation: Predictable allocation with defined buffer limits.
- Robust Error Handling: Detect malformed messages, buffer overflows, and decoding failures through built-in validation and error reporting.
- Reliable Data Serialization: Maintain consistent message structures and Protocol Buffers compatibility for dependable communication between systems.
- Pros & Cons
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.
- Pros — Key Advantages
- Minimal Memory Footprint: Optimized for low RAM and flash consumption.
- Predictable Memory Usage: Supports static allocation and fixed-size buffers.
- Efficient Serialization: Compact binary encoding and decoding.
- Cross-Platform Compatibility: Works across diverse microcontroller architectures.
- Open-Source Flexibility: Permissive zlib License for commercial and personal projects.
- Cons — Key Limitations
- Additional Code Generation: Requires generating C files from .proto definitions.
- Manual Memory Configuration: Variable-length fields may require size limits or callbacks.
- Limited Dynamic Allocation: Heap-based allocation requires explicit configuration.
- Callback Complexity: Advanced message structures may need custom callbacks.
- Embedded-Focused Design: Less convenient for applications requiring extensive runtime reflection
- FAQ's
Frequently Asked Questions About Nanopb
What is Nanopb, and what is it used for?
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.
Why is Nanopb suitable for embedded systems?
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.
How does Nanopb differ from Google Protocol Buffers?
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.
Which programming languages does Nanopb support?
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.
Does Nanopb support 8-bit, 16-bit, and 32-bit microcontrollers?
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.
How much RAM and flash memory does Nanopb require?
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.
Does Nanopb require dynamic memory allocation?
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.
How does Nanopb generate C code from Protobuf files?
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.
What are Nanopb .options files used for?
Nanopb .options files configure generated message structures, including maximum string lengths, repeated field counts, allocation types, and other memory-related settings.
How does Nanopb encode and decode messages?
Nanopb uses pb_encode() and pb_decode() to serialize C structures into Protocol Buffers binary messages and deserialize incoming messages back into structured data.
Can Nanopb communicate with cloud and backend applications?
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.
Does Nanopb support streaming data?
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.
Is Nanopb compatible with STM32 and Arduino?
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.
Can Nanopb be used with Zephyr RTOS?
Yes. Nanopb can be integrated with Zephyr RTOS to provide Protocol Buffers serialization for embedded applications running on supported microcontrollers.
Which build systems are compatible with Nanopb?
Nanopb can be integrated with CMake, Make, SCons, Bazel, Meson, Conan, and PlatformIO, offering flexibility across embedded development workflows.
How does Nanopb handle variable-length fields?
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.
How does Nanopb handle decoding errors?
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.
Is Nanopb suitable for IoT and wireless communication?
Yes. Its compact binary serialization makes Nanopb useful for IoT devices, wireless sensor networks, and embedded systems where bandwidth and memory efficiency are important.
Is Nanopb free for commercial projects?
Yes. Nanopb is distributed under the permissive zlib License, allowing use in personal, commercial, and open-source projects subject to the license conditions.
What is the best way to get started with Nanopb?
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.