🛠 Store Data on Adafruit Metro ESP32-P4 Hydra
Objectives
This tutorial guides you through creating a basic data logging system. You will learn to use the 16 MB integrated flash memory on the Adafruit Metro ESP32-P4 Hydra board. The goal is to save simple text messages every second, persisting even when the board is powered off, without using an internet connection. This is a fundamental skill for automation and autonomous monitoring.
Required Materials
- Adafruit Metro ESP32-P4 Hydra board.
- A quality USB-C cable (for power and programming).
- A computer with the Arduino IDE installed.
- The Arduino Core for ESP32 package (recent version) installed.
Steps
- IDE Configuration: Open the Arduino IDE. Go to *Tools > Board* and select *Adafruit Metro ESP32-P4*. Ensure the correct serial port is selected.
- Code Preparation: We will use the
LittleFSlibrary to manage the filesystem on the flash. Copy the code below. This code initializes the filesystem, opens a file in append mode, writes a message with the current time, and closes the file.
#include <LittleFS.h>
void setup() {
Serial.begin(115200);
// Mount filesystem
if (!LittleFS.begin(true)) {
Serial.println("LittleFS mount error");
return;
}
Serial.println("LittleFS mounted");
}
void loop() {
File file = LittleFS.open("/journal.txt", "a");
if (file) {
file.print("Data logged at: ");
file.println(millis());
file.close();
Serial.println("Write successful");
} else {
Serial.println("Write failed");
}
delay(1000);
}
- Upload: Click the *Upload* button. Wait for compilation and transfer to finish.
- Data Reading: Open the Serial Monitor at 115200 bauds. You will see confirmation messages. To read the file from your PC, use the *ESP32 Filesystem Uploader* tool or add a read function in the code to display the file content after a restart.
Verification
After uploading, the Serial Monitor should display "LittleFS mounted" and "Write successful" every second. Restart the board by unplugging and replugging USB. If you add a file.read() function in setup(), you should see previous "Data logged at: ..." lines, proving data is preserved in the 16 MB QSPI NOR flash.
Going Further
Once mastered, try logging data from an external sensor connected to GPIO pins. You can also explore reading files from the SDIO 4-bit microSD reader for larger volumes, or use the ESP32-C6 coprocessor to send these files via WiFi once local storage is full.
Other tutorials for this board
- First connection and startup of the Metro ESP32-P4 Hydra
- Controlling GPIOs and a Simple Display on Metro ESP32-P4 Hydra
- First Startup of the Metro ESP32-P4 Hydra
- Controlling an I2C sensor with ESP32-P4 Hydra
- Advanced Startup: Power, USB, and Debugging on ESP32-P4 Hydra
- High-Speed USB Storage Automation on ESP32-P4 Hydra
Code examples for this board
esp32-p4-hydra-gpio-blink.inoC++ (Arduino)esp32-p4-hydra-mpu6050-i2c.inoC++ (Arduino)esp32-p4-hydra-wifi-http.inoC++ (Arduino)esp32-p4-hydra-sdcard-write.inoC++ (Arduino)esp32-p4-hydra-usb-host-mouse.inoC++ (Arduino)esp32-p4-hydra-gpio-led-blink.inoC++ (Arduino)