A circular buffer is one of the most useful data structures in embedded programming. It helps microcontrollers store and manage data efficiently, especially when information is received continuously from sensors, communication interfaces, or external devices. In embedded systems, memory resources are often limited. Therefore, developers need efficient ways to handle incoming and outgoing data without continuously allocating new memory. A circular buffer in embedded systems provides a practical solution by reusing a fixed-size memory area. Circular buffers are commonly used in UART communication, sensor data acquisition, interrupt-driven programming, real-time applications, and embedded firmware development. In this guide, we will understand what a circular buffer is, how it works, how to implement a circular buffer in C, and where it is used in real-world embedded applications.
A circular buffer is a fixed-size data structure that stores information in a continuous memory area and treats the last position as connected to the first position. It is also known as a ring buffer because its memory locations behave like a circular arrangement.
Unlike a traditional linear buffer, where data is stored sequentially until the available space is exhausted, a circular buffer reuses previously occupied positions when they become available.
It generally uses two important pointers:
When the head or tail reaches the last memory position, it wraps around to the beginning of the buffer. This approach makes circular buffer data structures useful for applications that continuously receive and process data.
A circular buffer works using a fixed-size array and two position indicators called head and tail.
Consider a circular buffer containing five memory locations. Each location can store one data element. Initially, both head and tail point to the first position.
When new data arrives, it is inserted at the head position. The head pointer then moves to the next available location. Similarly, when the application reads data, the tail pointer moves forward to the next stored element. When either pointer reaches the final position, it returns to the first position. This process is called circular indexing or wraparound.
Suppose a circular buffer has five positions:
When the head reaches the final position, it wraps around to position 0. The buffer can reuse memory locations as data is consumed. This makes circular buffer implementation efficient for continuous data handling.
A basic circular buffer consists of the following components:
A fixed-size memory array stores the actual data elements.
The head pointer identifies the next position where incoming data should be written.
The tail pointer identifies the next position from which data should be read.
The buffer size defines the total number of available memory locations.
The implementation needs a method to identify whether the buffer is empty or full. Some implementations use an additional counter, while others reserve one array position to distinguish full and empty conditions.
Circular buffer implementation in C is commonly used in embedded firmware because it provides predictable memory usage and simple data management. If you are new to the language, start with C programming and then move to Embedded C.
Let’s understand how to create a basic circular buffer using an array.
First, define an array along with head and tail variables.
#include <stdio.h>
#include <stdint.h>
#define BUFFER_SIZE 5
typedef struct
{
uint8_t buffer[BUFFER_SIZE];
uint8_t head;
uint8_t tail;
} CircularBuffer;Here:
buffer[] stores the data.head identifies the next writing position.tail identifies the next reading position.BUFFER_SIZE defines the total array capacity.Before storing data, initialize both pointers.
void buffer_init(CircularBuffer *cb)
{
cb->head = 0;
cb->tail = 0;
}Both pointers start at position zero, indicating that the buffer is empty.
The write operation inserts new data into the circular buffer.
int buffer_write(CircularBuffer *cb, uint8_t data)
{
uint8_t next = (cb->head + 1) % BUFFER_SIZE;
if (next == cb->tail)
{
return 0; // Buffer full
}
cb->buffer[cb->head] = data;
cb->head = next;
return 1;
}The modulo operator % ensures that the head pointer returns to position zero after reaching the last position.
This implementation reserves one memory location to distinguish a full buffer from an empty buffer. Therefore, a five-element array can store a maximum of four elements.
The read operation retrieves the oldest available data.
int buffer_read(CircularBuffer *cb, uint8_t *data)
{
if (cb->head == cb->tail)
{
return 0; // Buffer empty
}
*data = cb->buffer[cb->tail];
cb->tail = (cb->tail + 1) % BUFFER_SIZE;
return 1;
}The tail pointer moves forward after reading each element. When the buffer reaches the final position, it wraps around to the beginning.
The following program demonstrates how to insert and retrieve data.
int main(void)
{
CircularBuffer cb;
uint8_t data;
buffer_init(&cb);
buffer_write(&cb, 10);
buffer_write(&cb, 20);
buffer_write(&cb, 30);
while (buffer_read(&cb, &data))
{
printf("%d\n", data);
}
return 0;
}Expected Output:
10 20 30
This example demonstrates FIFO (First In, First Out) data handling. The first element inserted is the first element retrieved.
For production embedded applications, developers must also consider concurrency, interrupt safety, buffer overflow handling and data synchronisation.
Both circular and linear buffers store data temporarily, but their memory management approaches are different.
| Feature | Circular Buffer | Linear Buffer |
| Memory arrangement | Circular indexing | Sequential indexing |
| Memory reuse | Reuses available positions | Usually requires reset or shifting |
| Pointer movement | Wraps around | Moves forward |
| Memory allocation | Fixed-size memory | Can be fixed or dynamically allocated |
| Data handling | Suitable for continuous streams | Suitable for simple data storage |
| Applications | UART, sensors, real-time systems | Temporary data storage |
A circular buffer is particularly useful when an embedded device continuously receives data and needs to process it without repeatedly resetting the memory area.
Circular buffers are used in several embedded applications where data needs to be transferred or processed continuously.
UART communication is one of the most common applications of a ring buffer in embedded systems.
When a microcontroller receives serial data from another device, the incoming bytes can be stored in a circular buffer. The UART interrupt handler stores received bytes, while the main application reads and processes them later.
This prevents the application from needing to process every received byte immediately inside the interrupt service routine.
Embedded devices frequently collect data from temperature sensors, pressure sensors, accelerometers and other input devices.
A circular buffer temporarily stores sensor readings before they are processed or transmitted. This is useful when sensors generate data continuously.
Interrupt-driven programming allows microcontrollers to respond to events without continuously checking hardware status.
A circular buffer can store information generated by interrupt service routines. The main program can process the stored information outside the interrupt context.
Real-time embedded systems frequently handle continuous streams of information. A circular buffer provides a predictable memory structure for temporarily storing incoming data.
It is useful in audio processing, communication systems, industrial monitoring and control applications.
Firmware developers use circular buffers to store diagnostic messages, error information and system events.
When the buffer becomes full, an implementation can overwrite older entries or reject new entries, depending on the application’s requirements. This helps maintain a fixed memory footprint.
Some important advantages include:
Although circular buffers are useful, developers should understand their limitations.
The buffer has a predefined size. If incoming data exceeds its capacity, the application must handle the overflow condition.
When multiple execution contexts access the same buffer, proper synchronisation is required.
Developers must correctly distinguish between full and empty conditions using an additional counter, a reserved slot or another status mechanism.
Some circular buffer designs overwrite older data when new data arrives. This can cause information loss if the application does not handle it correctly.
When interrupts or multiple tasks access a shared buffer, incorrect updates to head and tail pointers can cause data corruption.
When implementing a circular buffer in embedded C, consider the following practices:
These practices help developers build reliable and efficient embedded firmware.
A circular buffer is an important data structure in embedded programming that helps microcontrollers manage continuously incoming and outgoing data using a fixed-size memory area.
Its ability to reuse memory locations makes it suitable for UART communication, sensor data acquisition, interrupt-driven programming, real-time data processing and embedded logging systems.
Understanding circular buffer implementation in C helps ECE students and aspiring embedded engineers develop better firmware solutions and work with practical microcontroller applications.
For anyone learning embedded C programming, implementing a ring buffer is a valuable hands-on exercise that improves understanding of arrays, pointers, memory management and data structures. To practise these concepts with guided, hands-on training, explore the Embedded Systems Course with Placement at IIES.
A circular buffer is a fixed-size data structure that stores information in a memory array and reuses available locations through circular indexing. It is commonly used for continuous data handling in microcontrollers.
There is no fundamental difference. Circular buffer and ring buffer are two names for the same data structure, which uses wraparound indexing to reuse memory locations.
Circular buffers are used in embedded C because they provide efficient memory usage, predictable storage capacity, and fast data insertion and retrieval. They are particularly useful in UART communication, sensor data acquisition, and real-time applications.
When a circular buffer becomes full, the implementation can reject new data, overwrite the oldest data, or use another overflow-handling strategy. The correct approach depends on the application.
Circular buffers are commonly used in UART communication, SPI data handling, sensor data acquisition, audio processing, embedded logging, and real-time firmware applications.
Indian Institute of Embedded Systems – IIES