Skip to main content

MicroSD

The RP2040 has a set of GPIO pins that can be used to interface with an external MicroSD card module.

To use a MicroSD card with the RP2040, we use the SPI (Serial Peripheral Interface) protocol to connect the SD card module to the GPIO pins of the microcontroller. This involves connecting the following pins on the RP2040 to the corresponding pins on the MicroSD card module:

SPI SCK(such as GPIO10) to SD_SCK SPI TX(such as GPIO11) to SD_MOSI SPI RX(such as GPIO12) to SD_MISO A single GPIO pin (such as GPIO13) to the CS (chip select) pin on the SD card module

 // Initialize the SPI interface for the SD card
const int chipSelect = 13;
SPI1.setSCK(10);
SPI1.setTX(11);
SPI1.setRX(12);

Once the hardware connection is established, you can use a software library, such as Arduino's SD library, to read and write data to the MicroSD card. The SD library provides functions to initialize the SD card, open and close files, read and write file data, and perform other file system operations.

Note: The performance of the MicroSD card interface on the RP2040 will depend on factors such as the speed of the SD card, wiring quality and software efficiency, and a maximum SD card of 32GB is supported

Example Code

This example code realizes reading CO2 data and storing the data in SD.

#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SensirionI2CScd4x.h>


SensirionI2CScd4x scd4x;
//Initialize a string to store data for writing to the SD card
String SDDataString = "";

void sensor_power_on(void) {
pinMode(18, OUTPUT);
digitalWrite(18, HIGH);
}
// Function to power on the sensor
void sensor_scd4x_init(void) {
uint16_t error;
char errorMessage[256];

scd4x.begin(Wire);

// stop potentially previously started measurement
error = scd4x.stopPeriodicMeasurement();
if (error) {
Serial.print("Error trying to execute stopPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}

// Start Measurement
error = scd4x.startPeriodicMeasurement();
if (error) {
Serial.print("Error trying to execute startPeriodicMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
}
}

void sensor_scd4x_get(void) {
uint16_t error;
char errorMessage[256];

Serial.print("sensor scd4x: ");
// Read Measurement
uint16_t co2;
float temperature;
float humidity;
error = scd4x.readMeasurement(co2, temperature, humidity);
if (error) {
Serial.print("Error trying to execute readMeasurement(): ");
errorToString(error, errorMessage, 256);
Serial.println(errorMessage);
} else if (co2 == 0) {
Serial.println("Invalid sample detected, skipping.");
} else {
Serial.print("Co2:");
Serial.print(co2);
Serial.print("\t");
Serial.print("Temperature:");
Serial.print(temperature);
Serial.print("\t");
Serial.print("Humidity:");
Serial.println(humidity);
}
// Add data to the SD data string
SDDataString += "scd4x,";
if (error) {
SDDataString += "-,-,-,";
} else {
SDDataString += String(co2);
SDDataString += ',';
SDDataString += String(temperature);
SDDataString += ',';
SDDataString += String(humidity);
SDDataString += ',';
}
}



int cnt = 0;
void setup() {
Serial.begin(115200);

sensor_power_on();
Wire.setSDA(20);
Wire.setSCL(21);
Wire.begin();
// Initialize the SPI interface for the SD card
const int chipSelect = 13;
SPI1.setSCK(10);
SPI1.setTX(11);
SPI1.setRX(12);
// Check if the SD card is initialized
if (!SD.begin(chipSelect, 1000000, SPI1)) {
Serial.println("Card failed, or not present");
} else {
Serial.println("card initialized.");
}

sensor_scd4x_init();
}

void loop() {

delay(5000);
// Clear the SD data string and print a message to the serial monitor
SDDataString = "";
Serial.printf("\r\n\r\n--------- start measure %d-------\r\n", cnt);

SDDataString += String(cnt);
SDDataString += ',';

cnt++;
sensor_scd4x_get();
// Open the datalog.csv file for writing
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(SDDataString);
dataFile.close();
// print to the serial port too:
Serial.print("sd write: ");
Serial.println(SDDataString);
} else {
Serial.println("error opening datalog.txt");
}

}


Tech Support

Don't worry, we've got you covered! Please visit our Seeed Official Discord Channel to ask your questions!

If you have large order or customization requirement, please contact [email protected]

Loading Comments...