SoftSPI: SD Card read using SoftSPI

I want to send SD Card data using SoftSPI library because the hardware spi is already used by nrf2401

#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <SoftSPI.h>

SoftSPI mySPI(32, 33, 34);

File dataFile;
const int chipSelect = 53;
float waypoint [60];
int wayp;
int wayp2;
int index = 0;
float lat [60];
float lon [60];


void BigArrayGanjil(){
  int jumlahElemen = sizeof(waypoint) / sizeof(waypoint[0]);
  float ganjil[ jumlahElemen /2];
  int jumlahElemenGanjil = 0;

  for (int i = 1; i < jumlahElemen; i += 2) {
    ganjil[jumlahElemenGanjil] = waypoint[i];
    jumlahElemenGanjil++;
  }  

  for (int i = 0; i < jumlahElemenGanjil / 2; i++) {
    float temp = ganjil[i];
    ganjil[i] = ganjil[jumlahElemenGanjil - i - 1];
    ganjil[jumlahElemenGanjil - i - 1] = temp;
  }

  Serial.println("Array Terbalik:");
  for (int i = 0; i < jumlahElemenGanjil; i++) {
    // Serial.println(ganjil[i],5);
    if (ganjil[i] == 0){
      continue;
    }
    // Serial.println(ganjil[i],5);
    lon[wayp] = ganjil[i];
    wayp++;
  }

}

void BigArrayGenap(){
  int jumlahElemen = sizeof(waypoint) / sizeof(waypoint[0]);
  float genap[ jumlahElemen /2];
  int jumlahElemenGenap = 0;

  for (int i = 0; i < jumlahElemen; i += 2) {
    genap[jumlahElemenGenap] = waypoint[i];
    jumlahElemenGenap++;
  }  

  for (int i = 0; i < jumlahElemenGenap / 2; i++) {
    float temp = genap[i];
    genap[i] = genap[jumlahElemenGenap - i - 1];
    genap[jumlahElemenGenap - i - 1] = temp;
  }

  Serial.println("Array Terbalik:");
  for (int i = 0; i < jumlahElemenGenap; i++) {
    if (genap[i] == 0){
      continue;
    }
    // Serial.println(genap[i],5);
    lat[wayp2] = genap[i];
    wayp2++;
  }
  

}
 
void wp(){

  Serial.print("Initializing SD Card....");
  if(!SD.begin(chipSelect, mySPI)) {
    Serial.println("Card failed, or not Present");
    return;
  }
  Serial.println("Card Initialized");
  File dataFile = SD.open("GPS_DATA.txt");
  if (dataFile) {
    for (index = 0; index <= 61; index++) {
      float input = dataFile.parseFloat();
      if (input == 0){
        break;
      }
      waypoint[index] = input;
      //Serial.println(waypoint[1],5);
      }
    dataFile.close();
    BigArrayGanjil();
    BigArrayGenap();
   
  } else {
    Serial.println("File does not exist or named wrong");
  }
}



void setup() {
  mySPI.begin();
  Serial.begin(9600);
  

  wp();
  // BigArrayGanjil();
  // BigArrayGenap();
  Serial.println(lat[1],5);
  Serial.println(lon[1],5);

}
void loop() {
  
}


The sd card is always failed to init
thankyou :grin:

Which of those is SCK, MOSI, MISO ?

And of course you also forgot to tell us which Arduino you are using, which SD card adapter you are using etc ........

1 Like

You can have multiple miso/mosi devices as long as they have different CS pins. No software spi driver needed.

1 Like

It is "mosi, miso,Sck" and im using arduino mega with general sd card module like this

Im using mega and only need 1 cs pin which is 53, can you tell me how to do it?

It seems that you do not understand SPI communications. CS is "client select" pin, you should use separate CS pin for every SPI slave device. It doesn't matter that Mega has only one CS pin - you can use as CS any digital pin on the board.
So, for example you can select CS 10 for NRF24 and CS 53 for SD card reader

thankyou for the answer anyway ,the problem is i want to use another pin on arduino mega as miso,mosi and sck, because the hardware SPI is used by nrf24 (nrf24 doesn't use cs)

Why do you want that?
The SPI is a mult-device bus, you can connect several devices to the same MISO MOSI and SCK pins

1 Like

hmm thankyou for the infromation, is it possible if I'm using breadboard to connect miso,mosi sck from nrf24 and sd card module?

I am not sure that understand you...
The connection should be like this

See the docs

thankyou, it help me a lot :grin:

Are you really sure that SD card adapter is for a 5V logic Arduino like the Mega ?

SD cards are 3.3V logic and that adapter looks more like one for 3.3V logic Arduinos.

1 Like

Oh yes it does.

The pin called CSN is the CS pin as far as the SPI bus is concerned.

1 Like

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