Wednesday, 23 April 2025

Sound Recorder update

This is an update on the ESP32-based sound recorder and problems I've encountered.

File system

As the number of files on the SD card increased over a number of days of recording, the files got shorter and noisier (jittery). I put this down to the battery running down and the processor getting insufficient power. I eventually discovered that the problem was that writing to the SD card took longer and longer as the number of files increased. This is a known issue with the SD file system.

The code reads from the mic and writes to the SD file for a set amount of time e.g. 3 minutes, assuming that the writing takes negligible time. When the write started taking a significant amount of time then less data was being written to the file. As the write time got longer, less and less data was written to the file in the 3 minutes.

The solution was to arrange the files into subfolders, the problem being the number of files in a folder (including the root folder) and not the total number of files on the card. At least one place online said that this did not fix the problem, but it does.

I now create a folder per day, with the assumption that I won't be creating more than 50 files per day. I don't know how many files are needed for writing to start slowing down, but 50 is well within this number. The creation of one folder per day also overcomes the problem of creating artificial file names that had to combine date and time. Now, the folder is in the format YYYYMMDD and the file name hhmmss.

DS3231

The DS3231 RTC lost time over a period. It appeared to lose time while the ESP32 was in deep sleep. I believe this was because the battery in the DS3231 is not getting charged during deep sleep and eventually ran out of juice (the recorder spends most of its time in deep sleep). I noticed that the prototype recorder, which has a non-rechargeable battery, doesn't have this problem, although perhaps it's never on long enough to show this issue. Anyway, leaving the recorder on power (and not in deep sleep) when back at base seems to be enough to charge it.

ESP32 clock and RTC out of sync

The time shown in the file name and the file system creation time drifts. I think this is distinct from the previous issue and caused by the ESP32 clock being inaccurate.

On initial boot, the time is obtained from the DS3231 and set into the ESP32. This is needed because the SD file system gets its time from the EPS32. Initially these are obviously in sync. This only happens on initial boot and not when the recorder reboots after deep sleep.

One internet source says:

This means the ESP32 internal RTC is 31 seconds ahead of real time after 1 hour of deepsleeping.

 so after a number of days the ESP32 clock will be appreciably ahead of the correct time.

The solution is to set the ESP32 clock on every boot.

Batteries

I've yet to determine how long batteries last. The problem with the file system was masking any testing. The recorder is now powered by 2x3300mAh batteries in parallel and I've yet to discover their limit.