LoRa Ra02 not working with MicroSD card module

I am building a device to receive data from LoRa and save them into a MicroSD card.
I am using an esp32 Dev Kit V1, LoRa Ra 02, and a MicroSD card module for this.
I wired everything as below,

LoRa module -------------------- ESP32 board
MOSI -------------------- GPIO 23
MISO -------------------- GPIO 19
CLK -------------------- GPIO 18
CS -------------------- GPIO 5

MicroSD module -------------------- ESP32 board
MOSI -------------------- GPIO 23
MISO -------------------- GPIO 19
CLK -------------------- GPIO 18
CS -------------------- GPIO 25

Afterwards I uploaded the following code

#include <SPI.h>
#include <LoRa.h>
#include "FS.h"
#include "SD.h"

//define the pins used by the LoRa transceiver module
#define ss 5
#define rst 14
#define dio0 4 //originally it was 2

void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
    Serial.printf("Listing directory: %s\n", dirname);

    File root = fs.open(dirname);
    if(!root){
        Serial.println("Failed to open directory");
        return;
    }
    if(!root.isDirectory()){
        Serial.println("Not a directory");
        return;
    }

    File file = root.openNextFile();
    while(file){
        if(file.isDirectory()){
            Serial.print("  DIR : ");
            Serial.println(file.name());
            if(levels){
                listDir(fs, file.path(), levels -1);
            }
        } else {
            Serial.print("  FILE: ");
            Serial.print(file.name());
            Serial.print("  SIZE: ");
            Serial.println(file.size());
        }
        file = root.openNextFile();
    }
}

void createDir(fs::FS &fs, const char * path){
    Serial.printf("Creating Dir: %s\n", path);
    if(fs.mkdir(path)){
        Serial.println("Dir created");
    } else {
        Serial.println("mkdir failed");
    }
}

void removeDir(fs::FS &fs, const char * path){
    Serial.printf("Removing Dir: %s\n", path);
    if(fs.rmdir(path)){
        Serial.println("Dir removed");
    } else {
        Serial.println("rmdir failed");
    }
}

void readFile(fs::FS &fs, const char * path){
    Serial.printf("Reading file: %s\n", path);

    File file = fs.open(path);
    if(!file){
        Serial.println("Failed to open file for reading");
        return;
    }

    Serial.print("Read from file: ");
    while(file.available()){
        Serial.write(file.read());
    }
    file.close();
}

void writeFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Writing file: %s\n", path);

    File file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }
    if(file.print(message)){
        Serial.println("File written");
    } else {
        Serial.println("Write failed");
    }
    file.close();
}

void appendFile(fs::FS &fs, const char * path, const char * message){
    Serial.printf("Appending to file: %s\n", path);

    File file = fs.open(path, FILE_APPEND);
    if(!file){
        Serial.println("Failed to open file for appending");
        return;
    }
    if(file.print(message)){
        Serial.println("Message appended");
    } else {
        Serial.println("Append failed");
    }
    file.close();
}

void renameFile(fs::FS &fs, const char * path1, const char * path2){
    Serial.printf("Renaming file %s to %s\n", path1, path2);
    if (fs.rename(path1, path2)) {
        Serial.println("File renamed");
    } else {
        Serial.println("Rename failed");
    }
}

void deleteFile(fs::FS &fs, const char * path){
    Serial.printf("Deleting file: %s\n", path);
    if(fs.remove(path)){
        Serial.println("File deleted");
    } else {
        Serial.println("Delete failed");
    }
}

void testFileIO(fs::FS &fs, const char * path){
    File file = fs.open(path);
    static uint8_t buf[512];
    size_t len = 0;
    uint32_t start = millis();
    uint32_t end = start;
    if(file){
        len = file.size();
        size_t flen = len;
        start = millis();
        while(len){
            size_t toRead = len;
            if(toRead > 512){
                toRead = 512;
            }
            file.read(buf, toRead);
            len -= toRead;
        }
        end = millis() - start;
        Serial.printf("%u bytes read for %u ms\n", flen, end);
        file.close();
    } else {
        Serial.println("Failed to open file for reading");
    }


    file = fs.open(path, FILE_WRITE);
    if(!file){
        Serial.println("Failed to open file for writing");
        return;
    }

    size_t i;
    start = millis();
    for(i=0; i<2048; i++){
        file.write(buf, 512);
    }
    end = millis() - start;
    Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
    file.close();
}


void setup() {
  Serial.begin(9600);
  while (!Serial);

  if(!SD.begin(25)){
      Serial.println("Card Mount Failed");
      return;
  }
  uint8_t cardType = SD.cardType();

  if(cardType == CARD_NONE){
      Serial.println("No SD card attached");
      return;
  }

  Serial.print("SD Card Type: ");
  if(cardType == CARD_MMC){
      Serial.println("MMC");
  } else if(cardType == CARD_SD){
      Serial.println("SDSC");
  } else if(cardType == CARD_SDHC){
      Serial.println("SDHC");
  } else {
      Serial.println("UNKNOWN");
  }

  uint64_t cardSize = SD.cardSize() / (1024 * 1024);
  Serial.printf("SD Card Size: %lluMB\n", cardSize);

  listDir(SD, "/", 0);
  readFile(SD, "/data/samples.txt");

  Serial.println("LoRa Receiver");
  LoRa.setPins(ss, rst, dio0); //setup LoRa transceiver module
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }

    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

After running this, I can see that the SD card is being initialized and read but the LoRa initialization fails. I checked all the connections they are ok.
I used the LoRa module without the SD card using a different sketch, then it worked.

Below is the serial monitor output for the above sketch.

Card Type: SDHC
SD Card Size: 7680MB
Listing directory: /
  DIR : System Volume Information
  FILE: test.txt  SIZE: 1040384
  DIR : mydir
  FILE: foo.txt  SIZE: 13
  FILE: hello.txt  SIZE: 13
  DIR : data
Reading file: /data/samples.txt
Read from file: 005,11.00,9.00,27.00,1333.00,40.00
005,67.00,7.00,45.00,9147.00,69.00
005,0.00,3.00,76.00,2173.00,9.00
005,40.00,0.00,49.00,6397.00,80.00
005,6.00,43.00,78.00,6985.00,24.00
005,76.00,47.00,54.00,1524.00,16.00

LoRa Receiver
Starting LoRa failed!

Does anybody have any suggestions to fix this ?

A lot of the commonly used SD card adapters wont work with another SPI device on the bus.

Can you provide a link or picture of your SD card adapter ?


This is the module that I am using

I am using a cheap SD card that I had laying around. Do you think that can be a reason ?

Those are the modules that dont work together with another SPI device.

Your using an ESP32, which is 3.3V logic, so just use one of the plain SD card adapters thats actually designed for 3.3V logic systems.

Screenshot - 15_03_2024 , 07_53_50

No. A cheap crappy card reader is the reason. See previous reply.

See here for a fix:

https://forum.arduino.cc/t/lora-shield-and-sd-card-with-arduino/1005571/

But you do have to power this module with 5V on its Vcc pin.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.