RTL8720DN to MicroSD Card Reader

Hello! I seem to be having trouble utilizing the <SPI.h> library for my RTL8720DN. I’m creating an air sensor and continue to get various issues with my code. Here is my code:

#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include “BME280.h”
#include “PMS5003.h”
#include <SD.h>

// WiFi credentials
char ssid = “LDEO-Wireless”; // Change to your open WiFi SSID

// NTP Client
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, “pool.ntp.org”, -14400, 60000); // -14400 seconds for EDT (UTC-4)

// Sensors
BME280 bme280;
PMS5003 pms5003(PB2, PB1); // RX, TX
const int chipSelect = 10;

void epochToDateTime(unsigned long epoch, int &year, int &month, int &day, int &hour, int &minute, int &second) {
second = epoch % 60;
epoch /= 60;
minute = epoch % 60;
epoch /= 60;
hour = epoch % 24;
epoch /= 24;

unsigned long days = epoch;
year = 1970;

while (true) {
    bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    unsigned long daysInYear = leap ? 366 : 365;
    if (days >= daysInYear) {
        days -= daysInYear;
        year++;
    } else {
        break;
    }
}

static const int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (month = 0; month < 12; month++) {
    int dim = daysInMonth[month];
    if (month == 1) {  // February
        bool leap = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
        if (leap) {
            dim++;
        }
    }
    if (days >= dim) {
        days -= dim;
    } else {
        break;
    }
}
month++;
day = days + 1;

}

void setup() {
Serial.begin(115200);
delay(1000);

// Connect to WiFi
WiFi.begin(ssid);
Serial.println("Connecting to WiFi...");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
}

Serial.println();
Serial.println("Connected to WiFi");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

// Initialize sensors
if (!bme280.begin()) {
    Serial.println("BME280 initialization failed");
    while (1);
}
pms5003.begin();

// Initialize NTP Client
timeClient.begin();

// Initialize SD card
if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed");
    while (1);
}

}

void loop() {
// Update NTP Client
timeClient.update();

// Get current time
unsigned long epochTime = timeClient.getEpochTime();

// Convert epoch time to local time
int year, month, day, hour, minute, second;
epochToDateTime(epochTime, year, month, day, hour, minute, second);

// Print current date and time
String dateTime = String(month) + "/" + String(day) + "/" + String(year) + " " + String(hour) + ":" + String(minute) + ":" + String(second);
Serial.print("Current NYC time: ");
Serial.println(dateTime);

// Read PMS5003 data
int pm10, pm25, pm100;
if (pms5003.read(pm10, pm25, pm100)) {
    Serial.print("PM1.0: ");
    Serial.print(pm10);
    Serial.println(" ug/m3");
    Serial.print("PM2.5: ");
    Serial.print(pm25);
    Serial.println(" ug/m3");
    Serial.print("PM10: ");
    Serial.print(pm100);
    Serial.println(" ug/m3");
} else {
    Serial.println("Failed to read PMS5003 data");
}

// Read BME280 data
float temperature, humidity;
bme280.read(temperature, humidity);
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

// Log data to SD card
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
    dataFile.print(dateTime);
    dataFile.print(", PM1.0: ");
    dataFile.print(pm10);
    dataFile.print(" ug/m3, PM2.5: ");
    dataFile.print(pm25);
    dataFile.print(" ug/m3, PM10: ");
    dataFile.print(pm100);
    dataFile.print(" ug/m3, Temperature: ");
    dataFile.print(temperature);
    dataFile.print(" °C, Humidity: ");
    dataFile.print(humidity);
    dataFile.println(" %");
    dataFile.close();
    Serial.println("Data logged to SD card");
} else {
    Serial.println("Failed to open datalog.txt");
}

delay(1000); // Wait for 1 second before the next reading

}

and my Error(s) /Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino: In function ‘void epochToDateTime(long unsigned int, int&, int&, int&, int&, int&, int&)’:
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:51:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (days >= dim) {
~^~
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino: In function ‘void setup()’:
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:91:13: error: request for member ‘begin’ in ‘0’, which is of non-class type ‘int’
if (!SD.begin(chipSelect)) {
^~~~~
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino: In function ‘void loop()’:
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:140:5: error: ‘File’ was not declared in this scope
File dataFile = SD.open(“datalog.txt”, FILE_WRITE);
^~~~
/Users/trevd/Desktop/CODE4KIDS/CODE_NOSD/CODE_NOSD.ino:141:9: error: ‘dataFile’ was not declared in this scope
if (dataFile) {
^~~~~~~~
Multiple libraries were found for “NTPClient.h”
Used: /Users/trevd/Library/Arduino15/packages/realtek/hardware/AmebaD/3.1.7/libraries/NTPClient
Not used: /Users/trevd/Documents/Arduino/libraries/NTPClient
Multiple libraries were found for “PMS5003.h”
Used: /Users/trevd/Documents/Arduino/libraries/PMS5003
Not used: /Users/trevd/Documents/Arduino/libraries/GuL_Plantower
exit status 1

Compilation error: request for member ‘begin’ in ‘0’, which is of non-class type ‘int’

Please help! This compilation error has plagued me for two days. Here I’m trying to bypass utilizing the spi.h library. Is that okay? I need any help i can get, thank you!

Hi @Trevor_Durn,

It seems like you are using the wrong library for SD card for AmebaD boards. Instead of <SD.h>, you should be using #include “FatFs_SD.h”. You may refer to FatfsSDIO – File system in SD card – Realtek IoT/Wi-Fi MCU Solutions for more info on how to use the SD library.

Thank you.

1 Like

Thank you! I will try this out tomorrow.

Hello! This doesn’t seem to work; are we positive this library works for RTL8720DN? If so, what should my wiring diagram look like? Maybe that’s my issue. The basic example in arduino is not working I’m getting this print out:
#calibration_ok:[2:19:11]

FATFS Register: disk driver 0

FATFS mount logical drive fail:3

create dir at “(null)testdir”

File function error.

File function error.

create file at “(null)testdir/test.txt”

read back from “(null)testdir/test.txt”

from this code:
/*
This sketch shows how to create a folder and open a file under it.

Example guide:
FatfsSDIO – File system in SD card – Realtek IoT/Wi-Fi MCU Solutions
*/

#include “FatFs_SD.h”

char dirname = “testdir”;
char filename = “test.txt”;
char write_content = “hello world!”;

FatFsSD fs;

void setup() {
char buf[128];
char absolute_filename[128];

fs.begin();

sprintf(absolute_filename, "%s%s", fs.getRootPath(), dirname);
fs.mkdir(absolute_filename);
printf("create dir at \"%s\"\r\n", absolute_filename);

sprintf(absolute_filename, "%s%s/%s", fs.getRootPath(), dirname, filename);
SdFatFile file = fs.open(absolute_filename);
file.println(write_content);
file.close();
printf("create file at \"%s\"\r\n", absolute_filename);

printf("read back from \"%s\"\r\n", absolute_filename);
file = fs.open(absolute_filename);

memset(buf, 0, sizeof(buf));
file.read(buf, sizeof(buf));

file.close();
printf("==== content ====\r\n");
printf("%s", buf);
printf("====   end   ====\r\n");

fs.end();

}

void loop() {
delay(1000);
}

sorry I’m sure new to this board! thanks!

Hi @Trevor_Durn,

Are you connecting external SD card reader?

I think only AMB23 has the SD card holder embedded within the board.

Thank you.