Using a Buffer to Smooth Your Signals
Got an Arduino project that uses an analog sensor, but the readings keep jumping around? What you need is a moving average. This allows you to get a sense of the general change in your values over time, without being affected by random noise. It's good to use a moving average when you are measuring a continuous signal, such as:
- a Distance Sensor
- a stretch or flex sensor
- an accelerometer
The general idea of the moving average is to store the last n values, and every time a new value is recorded, average these out. These are stored in a data structure called an array.
Ring Buffer
To store our values we will be using a 'ring buffer'. This allows us to keep storing the most recent n values, without having to move the position of all the elements in the array. In this example, n=7. The arrow on top points to where the most recently recorded value was written into the array.
When we start, our buffer is empty.
As we record more values, our buffer begins to fill up:
When we reach the end, we start writing values in again from the beginning. This way, the oldest values get overwritten, and we keep the last n values we want to record.
Example
In this example, we are measuring change on a knitted stretch sensor. A photo of the layout is shown on the left, and the circuit diagram for this layout on the right.
simple analog read code
To start with, we're going to use generic analog read code to measure the voltage on our A0 pin.
#define SENSOR A0
int sensorValue=0;
void setup() {
// put your setup code here, to run once:
pinMode(SENSOR, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
sensorValue = analogRead(SENSOR);
Serial.println(sensorValue);
delay(50);
}
seeing values using the serial plotter
Whenever you are dealing with an analog signal, it's always a good idea to take a look at the values you're getting first. It can be really helpful to see these plotted on a graph, rather than printed out, as this can give you a visual representation of how they change over time. For this we want to use the serial plotter. You can open this by clicking the 'graph' icon next to the serial monitor icon.
The raw readings look like this on the serial plotter. Pretty noisy! If we were using this as a volume control, our values would be jumping all over the place.


The serial plotter is a pretty powerful tool that lets you log multiple different variables. The simplest way to use it is to use a single Serial.println() statement, which will log one value.
multiple print statements: If you have multiple Serial.print() statements, you need to make sure they're formatted right in order to see the graph on the output. If you're not seeing anything in the serial plotter, try just logging a single variable!
Adding a Buffer
Now we're going to try a version of the same code, but using our ring buffer. Like before, we're going to read the sensor every 50ms.
#define SENSOR A0
#define WINDOW_SIZE 10
int index = 0;
int buffer[WINDOW_SIZE];
float average = 0;
void setup() {
pinMode(SENSOR, INPUT);
}
void loop() {
// write a new value into the buffer
buffer[index] = analogRead(SENSOR);
// update the index location
index=(index+1)%WINDOW_SIZE;
// sum the values
int sum = 0;
for(int i=0; i<WINDOW_SIZE; i++) {
sum += buffer[i];
}
// calculate the average
average = float(sum) / WINDOW_SIZE;
Serial.println(average);
delay(50);
}
In this code, we use an array, called buffer to store our values. Every time we go around the loop, we are updating the location that we write our sensor value to. The modulo operator (% symbol) is used to reset the index value to zero every time it reaches the end of the window.
// write a new value into the buffer
buffer[index] = analogRead(SENSOR);
// update the index location
index=(index+1)%WINDOW_SIZE;
To create our average, we sum all the values in the array, and then divide by the length.
using the serial plotter to compare values
As well as just logging a single value, we can also use the serial plotter to log multiple parallel values. This can be helpful to compare what's happening with the raw values to the new smoothed code.
Here's the code with a window size of 5:
And the same code, with a window size of 10. It's a little hard to see from these charts, but in general, the larger the window size you have, the smoother the signal ends up being.
Trade offs
The bigger your window size, the more chance there is of missing quick changes in the signal.