two SPI devices do not work together ?

Hi
Thank you all for your help
I try to connect a micro SD module via SPI (to arduino nano with CS - D4)
and to connect a pressure sensor via SPI too (CS - D10)

when i try and check each of them alone they work perfectly but both neither on work

hos can i do this?

thanks

Please post your code using code tags when you do

What happens when you run the program ?

When i run the program the the readings are 0.000 and nothing is writen to the SD

here is th code:

#include “SPI.h”
#include “RTClib.h”
#include <SD.h>
#include <LiquidCrystal_I2C.h>

//Constant for Chip select pin used
const uint8_t CS = 10;

//SDcard ChipSelect and initialazation
int CSsd=4;
File myFile;

int mod1=6;
int mod2=7;
int mod3=8;

RTC_DS1307 RTC;

LiquidCrystal_I2C lcd(0x27,16,2);

//Constants for commands to be sent to the LDE/LME device.
const uint8_t READ_LDE = 0x2D;

const uint8_t SEND2DHR = 0x14;

const uint8_t READ_DHR = 0x98;

const float SCALE_FACTOR = 1200.0;

void setup() {

//Initialise Serial UART communication
Serial.begin(9600);
lcd.init();
lcd.backlight();
RTC.begin();

//Initialise SPI communication
SPI.begin();

//Initialise Chip Select pin
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
pinMode(CSsd, OUTPUT);
pinMode (mod1, INPUT_PULLUP);
pinMode (mod2, INPUT_PULLUP);
pinMode (mod3, INPUT_PULLUP);

SD.begin();

lcd.setCursor (0,0);
lcd.print (" Pitot Sensor");
lcd.setCursor (0,1);
lcd.print (" Lab Edition");

delay (3000);

RTC.adjust(DateTime(DATE, TIME)); // to Adjust clock If Battery gone

}

// The main program loop continuously gets a pressure measurement.
void loop() {

// ---------------------------------Pressure read--------------------------

// Create a buffer to read the result into.

uint16_t val;

// Send the command to poll the current pressure reading.
writeByte(READ_LDE);

// Send the command to send the pressure reading to the data register.
writeByte(SEND2DHR);

// Send the command to read the data register.
writeByte(READ_DHR);

// Read the first byte back from the LDE/LME sensor.
val = readByte();

// Shift this byte up.
val <<= 8;

// Read the second byte back from the LDE/LME sensor.
val |= readByte();

// Get the value of the pressure measurement in Pascals.
float result = val / SCALE_FACTOR;
if (result > 27.306) {
result=result-54.612;
}

delay (50);

// -----------------------End of Pressure Read----------------
DateTime now = RTC.now();
String dt; //Date
String tim; //Time
String dnt; //Date And Time To SD
String SDline; //Data to SD
String DispLn0; //First Display line
String DispLn1; //Secound Display line
String Filenm=“temp”; //Filename
String Serline; //Serial display line

// ----------------Calculating Time and date Data-----------------

dt="";
tim="";

if ((now.day()< 10))
dt=dt+“0”;
dt=dt+String(now.day());
Filenm=Filenm+dt;
dt=dt+"/";
if ((now.month()< 10))
dt=dt+“0”;
dt=dt+String(now.month())+"/";
if ((now.month()< 10))
Filenm=Filenm+“0”;
Filenm=Filenm+String(now.month())+".txt";

dt=dt+String(now.year());

if ((now.hour()< 10))
tim=tim+“0”;
tim=tim+String(now.hour())+":";
if ((now.minute()< 10))
tim=tim+“0”;
tim=tim+String(now.minute())+":";
if ((now.second()< 10))
tim=tim+“0”;
tim=tim+String(now.second());

dnt=dt+" "+tim;

// -----------------Finish Calculating time and date--------------

if (digitalRead(mod1)==LOW) {
DispLn0= “Dyn P[Pa]: “;
DispLn1=String (result,3);
}
else if (digitalRead(mod2)==LOW) {
DispLn0= “Air Flow[m/s]: “;
float airs=sqrt((2abs(result))/1.225);
if (airs>=0) {
DispLn1=String (airs,3);
}
else {
DispLn1="-"+ String (airs,3);
}
}
else if (digitalRead(mod3)==LOW) {
DispLn0= “Water Flow[m/s]:”;
float waters=sqrt((2
abs(result))/1013);
if (waters>=0) {
DispLn1=String (waters,3);
}
else {
DispLn1=”-”+ String (waters,3);
}
}
else {
DispLn0=” Huston We Have”;
DispLn1=" A PROBLEM";
}
lcd.clear();
lcd.setCursor (0,0);
lcd.print (DispLn0);
lcd.setCursor (0,1);
lcd.print (DispLn1);

//print result to serial Monitor

Serline=dnt+" Dynamic Pressure="+String(result, 3)+" [Pa]";
Serial.println(Serline);

//Save Data To SD Card

SDline=dnt+" "+String(result,3);
File myFile;
myFile = SD.open(Filenm, FILE_WRITE);
myFile.println (SDline);
myFile.close();

//loop around for a new read every half second
delay(500);
}

// ----------------------------Subrutines-------------------

// Send one byte to the sensor.
void writeByte(uint8_t toSend) {
// Drive the CS pin low to enable communication with the slave.
digitalWrite(CS, LOW);

//Send the byte to sensor
SPI.transfer(toSend);
delay (50);
// Drive the CS pin high to disable communication with the slave.
digitalWrite(CS, HIGH);
}

// Read one byte from the sensor.
uint8_t readByte() {

// Create a variable to store the received byte.
uint8_t data;

// Drive the CS pin low to enable communication with the slave.
digitalWrite(CS, LOW);

//Read value from sensor
data = SPI.transfer(0x00);
delay (50);
// Drive the CS pin high to disable communication with the slave.
digitalWrite(CS, HIGH);

return data;

}

Thank You

here is th code:

without code tags, I note

Please help us to help you by reading Read this before posting a programming question and follow the advice given

sorry :frowning:

#include "SPI.h"
#include "RTClib.h"
#include <SD.h>
#include <LiquidCrystal_I2C.h>

//Constant for Chip select pin used
const uint8_t CS = 10;

//SDcard ChipSelect and initialazation
int CSsd=4;
File myFile;

int mod1=6;
int mod2=7;
int mod3=8;

RTC_DS1307 RTC;

LiquidCrystal_I2C lcd(0x27,16,2);

//Constants for commands to be sent to the LDE/LME device.
const uint8_t READ_LDE = 0x2D;

const uint8_t SEND2DHR = 0x14;

const uint8_t READ_DHR = 0x98;

const float SCALE_FACTOR = 1200.0;

void setup() {

 //Initialise Serial UART communication
    Serial.begin(9600);
    lcd.init();
    lcd.backlight();
    RTC.begin();
   
  //Initialise SPI communication
  SPI.begin();

  //Initialise Chip Select pin
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
  pinMode(CSsd, OUTPUT); 
  pinMode (mod1, INPUT_PULLUP);
  pinMode (mod2, INPUT_PULLUP);
  pinMode (mod3, INPUT_PULLUP);
 
  SD.begin();   

  lcd.setCursor (0,0);
  lcd.print ("  Pitot Sensor");
  lcd.setCursor (0,1);
  lcd.print ("  Lab Edition");
   
delay (3000);

  RTC.adjust(DateTime(__DATE__, __TIME__)); // to Adjust clock If Battery gone

}

// The main program loop continuously gets a pressure measurement.
void loop() {

// ---------------------------------Pressure read--------------------------

  // Create a buffer to read the result into.

  uint16_t val;

  // Send the command to poll the current pressure reading.
  writeByte(READ_LDE);

  // Send the command to send the pressure reading to the data register.
  writeByte(SEND2DHR);

  // Send the command to read the data register.
  writeByte(READ_DHR);

  // Read the first byte back from the LDE/LME sensor.
  val = readByte();

  // Shift this byte up.
  val <<= 8;
 
  // Read the second byte back from the LDE/LME sensor.
  val |= readByte();
 
  // Get the value of the pressure measurement in Pascals.
  float result = val / SCALE_FACTOR;
  if (result > 27.306) {
    result=result-54.612;
    }
 
  delay (50);

// -----------------------End of Pressure Read----------------
    DateTime now = RTC.now(); 
    String dt;          //Date
    String tim;         //Time
    String dnt;         //Date And Time To SD
    String SDline;      //Data to SD
    String DispLn0;     //First Display line
    String DispLn1;     //Secound Display line     
    String Filenm="temp";      //Filename 
    String Serline;      //Serial display line

// ----------------Calculating Time and date Data-----------------
   
    dt="";
    tim="";
 
 

    if ((now.day()< 10))
        dt=dt+"0";
        dt=dt+String(now.day());
        Filenm=Filenm+dt;
        dt=dt+"/";
    if ((now.month()< 10))
        dt=dt+"0";
        dt=dt+String(now.month())+"/";
    if ((now.month()< 10))
        Filenm=Filenm+"0";
        Filenm=Filenm+String(now.month())+".txt";

      dt=dt+String(now.year());

    if ((now.hour()< 10))
        tim=tim+"0";
        tim=tim+String(now.hour())+":";
    if ((now.minute()< 10))
        tim=tim+"0";
        tim=tim+String(now.minute())+":";
    if ((now.second()< 10))
        tim=tim+"0";
        tim=tim+String(now.second());


    dnt=dt+"   "+tim;
 
         
// -----------------Finish Calculating time and date--------------   

if (digitalRead(mod1)==LOW) {
    DispLn0= "Dyn P[Pa]:     ";
    DispLn1=String (result,3);
 }
else if (digitalRead(mod2)==LOW) {
    DispLn0= "Air Flow[m/s]:  ";
    float airs=sqrt((2*abs(result))/1.225);
    if (airs>=0) {
    DispLn1=String (airs,3); 
    }
    else {
    DispLn1="-"+ String (airs,3);     
    }
}
else if (digitalRead(mod3)==LOW) {
    DispLn0= "Water Flow[m/s]:";
    float waters=sqrt((2*abs(result))/1013);
    if (waters>=0) {
    DispLn1=String (waters,3); 
    }
    else {
    DispLn1="-"+ String (waters,3);     
    }
}
else {
  DispLn0=" Huston We Have";
  DispLn1="   A PROBLEM";
}
    lcd.clear();
    lcd.setCursor (0,0);
    lcd.print (DispLn0); 
    lcd.setCursor (0,1);
    lcd.print (DispLn1); 
   
  //print result to serial Monitor

  Serline=dnt+"   Dynamic Pressure="+String(result, 3)+" [Pa]"; 
  Serial.println(Serline);

  //Save Data To SD Card

  SDline=dnt+"     "+String(result,3);
  File myFile;
  myFile = SD.open(Filenm, FILE_WRITE);
  myFile.println (SDline);
  myFile.close();     

  //loop around for a new read every half second
  delay(500);
}





// ----------------------------Subrutines-------------------



// Send one byte to the sensor.
void writeByte(uint8_t toSend) {
  // Drive the CS pin low to enable communication with the slave.
  digitalWrite(CS, LOW);

  //Send the byte to sensor
  SPI.transfer(toSend);
delay (50);
  // Drive the CS pin high to disable communication with the slave.
  digitalWrite(CS, HIGH);
}

// Read one byte from the sensor.
uint8_t readByte() {

  // Create a variable to store the received byte.
  uint8_t data;

  // Drive the CS pin low to enable communication with the slave.
  digitalWrite(CS, LOW);

  //Read value from sensor
  data = SPI.transfer(0x00);
delay (50);
  // Drive the CS pin high to disable communication with the slave.
  digitalWrite(CS, HIGH);
 
  return data;

}

Thanks - as you can see it makes it much easier to copy the code to an editor and it also stops the code being interpreted as HTML commands, an array index of i being the most common problem

Some SD card modules will not play well with other devices on the SPI bus. The problem is with the way the level shifter on the SD module is wired. The ones that do not work well have the MISO signal running through the level shifter. That causes the MISO signal to not be released so that the other devices can control it. The modules made by Adafruit (and some others) have the MISO signal going from the SD card straight to the MISO, bypassing the level shifter. Look at the schematic for the Adafruit module to see what to look for. DO (data out) is the MISO signal.

but it disturbing the function of the pressure sensor
Is there a reason for that?

I don't know. I posted my best guess based on the information available. I don't know which SD module or pressure sensor that you have and I don't know how they are wired. A schematic of the your wiring, a clear photo of your wiring and data sheets for the components would help us to help you.

Have you tried to write a simpler version of the code with only the pressure sensor and SD card? That way you narrow down the possible sources of problems.

Post a close-up picture of your SD card reader. I had a very similar problem, the SD reader is simply mis-designed, and cannot be used on the same SPI bus as anything else.

I got around the problem by putting the 'good' stuff on the regular SPI lines, then connected the SD card to separate lines and used software SPI to drive it.

Here's another thread, with some pointers to yet other threads, with more info.