How to use SD card as a ring buffer for data logging ?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
John102837
 
Posts: 1
Joined: Wed Dec 29, 2021 8:01 pm

How to use SD card as a ring buffer for data logging ?

Post by John102837 »

Hello,
I am working on a project which involves Arduino and an SD card along with a couple of analog sensors.
I am able to log the values of the analog sensor to the SD card in a CSV file with a timestamp and later send the values to a mobile app using HC05.

But I see that if I don't connect my Arduino to my APP over a period of time my SD becomes full. I can solve this issue by using an SD card with a higher storage capacity.
But I was wondering if I can implement the concept of Ring buffer or any such concepts in which if the memory is full then the first written data will be overwritten with the next value.

The question is :
.. Can We use a ring buffer to store data in an SD card?
If Yes then how, If No then is there any way I can solve this issue?

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: How to use SD card as a ring buffer for data logging ?

Post by adafruit_support_bill »

The SD library does support a seek function. https://www.arduino.cc/en/Reference/FileSeek
So you can seek to any position in the file to implement a ring buffer. But to know the actual position you want to seek to, the data needs to be logged in fixed size increments. CSV format is not very conducive to that. Logging binary data would be the way to go.

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: How to use SD card as a ring buffer for data logging ?

Post by westfw »

You must be really recording a lot of data, to run out of space on a modern SD card!

The last time I needed to do something like this, the program involved would store its log in a file whose name changed each day, something like "20211225.log", "20211226.log" and etc. When space ran short (or after a configurable number of days), it would just delete the oldest file(s), which it could derive from the dates, or (IIRC) returned in order (which turned out to be alphabetical) by the "list files" system calls.

I don't know if the SD card filesystems are quite as capable of doing the sorting for you, but you should still be able to calculate the oldest filenames.

User avatar
jim_lee
 
Posts: 709
Joined: Thu May 24, 2012 8:24 pm

Re: How to use SD card as a ring buffer for data logging ?

Post by jim_lee »

You may want to have a look at LC_blockFile. For managing an SD card file containing multiple blocks of user data. Good for setting up databases. You can find it in the Arduino library manager. (Install it from there NOT from the Github link)
The github link with some info on it. : https://github.com/leftCoast/LC_blockFile

-jim lee

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”