I'm trying to build an ESP32 (ESP32-WROOM-32D 4MB) device to collect at least 30 seconds of analog sensor data (i.e. accelerometer) by 20.000 Hz and store the data in array later in an SD card.
Since each stored the data is Float32; the data will take 30 x 20000 x 4 = 2.400.000 bytes in memory. I couldn't achieve to allocate this amount of data because the program keep crashes with below statement. How can I overcome this problem? (sample code is below. If I keep duration as dataSeconds = 1, everything is fine. But 2 and above exceeds the RAM I guess)
Guru Meditation Error: Core 1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC : 0x400d0bc6 PS : 0x00060530 A0 : 0x800e9996 A1 : 0x3ffb1f80
A2 : 0x3ffc2480 A3 : 0x00000001 A4 : 0x0000c381 A5 : 0x00000001
A6 : 0x00000000 A7 : 0x00000000 A8 : 0x800d0bc4 A9 : 0x3ffb1f60
A10 : 0x00000751 A11 : 0x00000005 A12 : 0x0000000a A13 : 0x00000003
A14 : 0x00000001 A15 : 0x00000000 SAR : 0x0000001b EXCCAUSE: 0x0000001d
EXCVADDR: 0x00000001 LBEG : 0x400d1dcc LEND : 0x400d1dd8 LCOUNT : 0x00000000
Backtrace: 0x400d0bc6:0x3ffb1f80 0x400e9993:0x3ffb1fa0
Rebooting...
Code: Select all
#include <esp_heap_caps.h>
const unsigned int samplingFr = 20000;
const unsigned int dataSeconds = 1; // 1 second is OK, 2 and above generates error (treshold around 23000 sample)
const unsigned int dataScan = dataSeconds * samplingFr;
byte *dataPart = (byte *) heap_caps_malloc(dataScan * sizeof(int), MALLOC_CAP_8BIT);
void setup() {
Serial.begin(115200);
int i = 1;
for (i = 1; i < dataScan + 1; i++) {
dataPart[i] = 1; // Normally I put analogRead(pinNo) here, I put "1" to shorten the code here
delayMicroseconds(1000000 / samplingFr);
}
for (i = 1; i < dataScan + 1; i++) {
Serial.println(dataPart[i]);
delayMicroseconds(1);
}
}
void loop() {
}