I am new to Arduino and this forum, so please forgive me if I am asking something really trivial and stupid.

I have an Arduino board which collects sensor readings from an MPU6050 sensor (acceleration+gyroscope). The next step for me would be to process these data directly on the board. I want to somehow store N last sensor values and then query this set (collection, vector, array, whatever you call it) to fetch certain values. This set will be constantly updated with new values, whereas older values will be pushed out, so that only the most up-to-date values are stored in the set. For example, I want to know if the acceleration exceeded 10 m/s in the last minute.

In the literature this kind of set with most recent values is sometimes called window - this approach allows limiting an unbounded sequence of constantly generated tuples to a bounded bag and then apply traditional SQL-like query operators. For example, Oracle has come with CQL - an engine and a query language for handling streaming data.

Before I start implementing this, I am wondering if there is a similar (and, of course, much simpler) library for Arduino? If not, which data structure should I use to implement such functionality?

Also, my intention is to query not just individual values, but certain patterns and sequences - e.g., I want to know if the acceleration first decreased and then increased again (something similar to the WHERE condition of an SQL query). Does it look realistic to implement with Arduino?

  • A circular buffer sounds like what you want. The library mentioned by Majenko sounds like it implements that.
    – Nick Gammon
    Commented Sep 24, 2015 at 21:44

1 Answer 1


It sounds like you are after my Average library:

It allows you to store a rolling set of data where you get to "push" values into it and the oldest one drops off the end. (It's implemented internally using a circular buffer).

You can then do various forms of analysis on the data - get the mean average, the mode average, calculate the standard deviation, find the maximum and minimum values of the set, and even access individual entries within the set.

It's a fully templated library, so you can use it with any numerical data type (float, double, int, long, etc).

  • Thanks for your reply! A few more questions. Given that I am running your code on Arduino Yun, am I right that the maximum number of elements I can store in the rolling set is limited to 2.5k (which is the size of RAM of ATmega32u4, where variables are stored)? Also, are you still working on your library? Any plans to further extend it with features I mentioned at the bottom of my previous post (e.g. querying sequences and patterns)? Thanks!
    – orkes
    Commented Sep 24, 2015 at 16:44
  • The number of variables depends on the type of variable and how much space each entry takes up. I am always open to suggestions for enhancements to my libraries.
    – Majenko
    Commented Sep 24, 2015 at 17:00
  • I presume you can always query the current contents of the window at any time. Thus a simple set of tests would let you conduct any custom queries you want.
    – Nick Gammon
    Commented Sep 24, 2015 at 21:45
  • Yep. The .get(n) method lets you access any raw entry in the circular buffer you like. There's also a Least Squares function, and one that uses that function to predict the value at some finite point in the future. I use it to control the heaters in my reflow oven to prevent overshoot.
    – Majenko
    Commented Sep 24, 2015 at 21:46
  • Thank you both for your comments and suggestions! I'll give it a try.
    – orkes
    Commented Sep 25, 2015 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.