SD-Card Read/Write Speed Check by iFZero AI
The iFZeroAI board is based on iFZero board (ESP32-PICO-V3 02). ESP32-PICO-V3 02 has featured with 16 GPIOs, 18 RTC_GPIOs, 10 Touch sensors, 8-bit DAC 2 channels, 12-bit ADC 18 channels, 2 channels SPI, 1 channel I2C and so on, for details please check here. It has embedded 8 MB SPI Flash and 2 MB SPI PSRAM, as shown in the following figure.
ESP32-PICO-V3-02 Block Diagram |
In this blog, we will show the initialization of SD-Card with writing and reading integer data from 0 to 99 (which are 8, 16, 32 and 64-bit) in the SD-Card by using and without using while()
loop. We do not use for()
loop because it consumes more time than while()
loop.
1. Here is an overview of the essential peripherals of iFZeroAI board. In the Front-side of iFZeroAI board, the SD-Card, Camera, MEMS Microphone, TFT Display are shown by yellow, orange, blue and green rectangular box, respectively. In the Rare-side of iFZeroAI board, the iFZero board, 5-pin USB port, battery port, and switch are shown by dark red, light green, gray, and red rectangular box.
Front-side of iFZeroAI | Rare-side of iFZeroAI |
SPI Connection between SD-Card and iFZero Board |
2. In the Visual Studio Code, we create a project under the Platform IO IDE package. In the main.cpp file, located in src directory, we have to write the codes which will be described in this blog. Now, we write the following necessary header files:</span>
1 |
3. Declare Pin number of SD-Card to assign in SPI()
function.
1 | // ************ For SD Card *****************// |
4. Declaring the File
object that will be used to open, write, read and close the (.bin) file in the SD-Card.
1 | // ************* Variable for SD Card ************// |
5. Variables for 8, 16, 32 and 64-bit data, to write and read in SD-Card. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// ******** for 8-bit write & read **********//
int8_t i_8bit=0;
int8_t read_sddata_8bit[total_data_write];
// ******** for 8-bit write & read **********//
// ******** for 16-bit write & read **********//
int16_t i_16bit=0;
int16_t read_sddata_16bit[total_data_write];
// ******** for 16-bit write & read **********//
// ******** for 32-bit write & read **********//
int32_t i_32bit=0;
int32_t read_sddata_32bit[total_data_write];
// ******** for 32-bit write & read **********//
// ******** for 64-bit write & read **********//
int64_t i_64bit=0;
int64_t read_sddata_64bit[total_data_write];
// ******** for 64-bit write & read **********//
6. Variables and user-defined function (declaration and definition) to check the size of the SD-Card.
1 | uint32_t usedKb, freeKb; // variables |
Reference: Get SD card free space not updating?
7. Variables to measure the SD-Card writing and reading speed.
1 | unsigned long start_tme, end_tme; |
8. Now in void setup()
function, we initialize the SD-Card, checking SD-Card size, checking SD-Card is ok or not.
1 | void setup() |
In the serial monitor, the output of the
void setup()
code is following:
1 | SD successfully initialized |
9. Now, we write the code to measure SD-Card write-read speed in the void loop()
function.
1 | void loop() |
10. Now, we describe the code of void loop()
function. The void loop()
function is an infinite loop that runs forever and if we print anything in serial monitor terminal, we cannot see them consistently in the terminal. As our goal is to measure the write-read speed of SD-Card and print it in such a way so that we can see it in the serial monitor terminal consistently. Therefore, we write the following segment of code so that user can print the speed of write-read operation of SD-Card when user wishes to press “R”.
1 | if(Serial.available()) |
The necessary variable for the above segment of code is:
1 | String input_m; |
In the serial monitor, the output looks like following:
1 | Press "R" to write, read and check the read and write speed of SD-card |
11. Now, we describe the code of 8-bit int
type SD-Card write-read operation and speed measurement of SD-Card. Here, the following code writes and reads from 0 to 99 in SD-Card and measaures the execution time of write-read operation by using micros()
function. After getting the execution time, we measure the speed in bit/s
. The code looks like following:
Noted: Already the following code is shown in Section 9 in
void loop
function, in theif(bufff[0] == 'R')
block.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 // ########### data-8bit reading and writing speed #############
snprintf(chr_str, sizeof(chr_str), "/data_%dbit.bin", sizeof(i_8bit)*8);
sd_file = SD.open(chr_str, FILE_WRITE);
// ************ test--write integer in SD card ************
Serial.printf("\n\nStart writing 8-bit from 0-%d.....", total_data_write-1);
start_tme = micros();
i_8bit = 0;
while(i_8bit < total_data_write)
{
sd_file.write((const uint8_t *)&i_8bit, sizeof(i_8bit));
i_8bit++;
}
end_tme = micros();
wrt_spd = float(sizeof(i_8bit) * 8.0 * i_8bit) * 1000.0 * 1000.0 / float(end_tme - start_tme);
Serial.printf("\nWriting 0-%d complete--8-bit writing speed: %.2f bit/s", total_data_write-1, wrt_spd);
// ************ test--write integer in SD card ************
sd_file.close();
// ************ test--read data from SD card ************
Serial.printf("\nStart reading 8-bit from sd card.....");
sd_file = SD.open(chr_str, FILE_READ);
int count_read = 0, sdr_ret_val = 1;
start_tme = micros();
while(sdr_ret_val > 0)
{
sdr_ret_val = sd_file.read((uint8_t *)&read_sddata_8bit[count_read], sizeof(read_sddata_8bit[count_read]));
count_read++;
}
end_tme = micros();
// Serial.printf("\n time: %.2f", float(end_tme - start_tme));
red_spd = float(sizeof(read_sddata_8bit[0]) * 8.0) * float(count_read - 1) * 1000.0 * 1000.0 / float(end_tme - start_tme);
Serial.printf("\nReading finish--8-bit reading speed: %.2f bit/s", red_spd);
// ************ test--read data from SD card ************
sd_file.close();
Serial.printf("\nChecking 8-bit read data (0-%d)\n", total_data_write-1);
for(int j=0; j < count_read-1; j++)
{
if(j == count_read-2)
{
Serial.printf("%d", read_sddata_8bit[j]);
}
else
{
Serial.printf("%d, ", read_sddata_8bit[j]);
}
}
// ########### data-8bit reading and writing speed #############The ouput of this code-segment looks like following:
1 | Start writing 8-bit from 0-99..... |
- We can see that the 8-bit
int
type data writing speed in SD-Card is about850 Kb/s
. Here, we write 100 8-bit data 1 by 1 inwhile(i_8bit < total_data_write)
loop. - We read 100 8-bit data from SD-Card 1 by 1 in
while(sdr_ret_val > 0)
loop. In this case, we get the reading speed about350 Kb/s
.
12. In the same way, we write and read 16-bit int
type data in SD-Card and get the following Serial output in the terminal.
1 | Start writing 16-bit from 0-99..... |
- We can see that the 16-bit
int
type data writing speed in SD-Card is about662 Kb/s
. Here, we write 100 16-bit data 1 by 1 inwhile(i_16bit < total_data_write)
loop. - We read 100 16-bit SD-Card data 1 by 1 in
while(sdr_ret_val > 0)
loop. In this case, we get the reading speed about600 Kb/s
.
13. In the same way, we write and read 32-bit and 64-bit int
type data in SD-Card and get the following Serial output in the terminal.
Serial output for 32-bit data
1
2
3
4
5
6 Start writing 32-bit from 0-99.....
Writing 0-99 complete--32-bit writing speed: 1317957.12 bit/s
Start reading 32-bit from sd card.....
Reading finish--32-bit reading speed: 1356507.00 bit/s
Checking 32-bit read data (0-99)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99Serial output for 64-bit data
1
2
3
4
5
6 Start writing 64-bit from 0-99.....
Writing 0-99 complete--64-bit writing speed: 1437556.12 bit/s
Start reading 64-bit from sd card.....
Reading finish--64-bit reading speed: 1719043.75 bit/s
Checking 64-bit read data (0-99)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
- Here, we get writing and reading speed about
1.3 Mb/s
for 32-bitint
type data. - For 64-bit
int
type data, we get writing speed:1.5 Mb/s
and reading speed:1.7 Mb/s
.
We have found that the SD-Card write-read operation speed is quite high for 64-bit
int
type data than 8, 16, 32-bitint
type data.
14. To speed-up the SD-Card write-read operation, user must know the size of data, and store them in a predefined array. Then the SD-Card write-read operation can be performed without using while()
loop and user can also write and read at a time by using sd_file.write()
and sd_file.read()
function, respectively. To do this, we first store from 0 to 99 64-bit int
type data to a int64_t
datatype array. After taht, pass the array to the sd_file.write()
function. In the same way, while we are going to read the data from SD-Card, we have to pass an empty int64_t
type array in sd_file.read()
function. The code looks like following:
1 | // *********** writing at a time ********** |
The necessary variables for the above code is:
1 | int64_t wrt_64bit_arr_insd[total_data_write]; |
The serial output of the above codes looks like following:
1 | Start writing 64-bit from 0-99..... |
- Here, we have found that the writing speed is about
1.8 Mb/s
and the reading speed is about2 Mb/s
which is much higher than the above SD-Card write-read speed, shown from section 11 to 13.