Go Down

Topic: Two SPI devices with Arduino Uno (Read 248 times) previous topic - next topic

Nowotny

Hi everyone,
I am building/prototyping a telemetry system. It is very easy at first but I wanted to make it work before getting too complicated. My plan is to generate a CSV Data to collect all the data I want and then save on the microSD for later analysis.
SPI Devices: ADXL362 / MicroSD writer/reader from Sparkfun
Both devices works perfectly alone but I cannot find my mistake when they are both connected.
On my program you can see on your computer screen through the serial what is going on, and I noticed that when I disconnected the clock Pin from the SDwriter I get clean measures; but only when I disconnect the clock pin (SCK).
When both SPI devices are with the same SCK connected as they should I got no good measurement at all.
I first suspect the CS pin to be the problem. I double check both SPI devices require their commanding CS pin to be low to communicate (SPI mode 0). I also put pull-ups resistors to 5V on those outputs and also on the clock pin to be sure that the noises are very limited. 
I put then manually those digitalWrite inside the program to be sure that my pins switch when I want them to, but again, it didnot change.
Please find my code below:
Code: [Select]
#include <SD.h>
 #include <SPI.h>
 #include <ADXL362.h>
 
ADXL362 accelerometer;
 

int16_t temp;
int16_t XValue = 0;
int16_t YValue = 0;
int16_t ZValue = 0;
int16_t Temperature = 0;

unsigned long starttime;
unsigned long i;

 
File data;             // instance de la classe File


void setup(){
 Serial.begin(9600);
 while (!Serial) {
    ;
 }
 SPI.begin();
 SPI.setDataMode(SPI_MODE0);
 pinMode(10, OUTPUT);
 accelerometer.begin(10);                   
 accelerometer.beginMeasure();
 pinMode(9, OUTPUT);             
 if (!SD.begin(9)) {
    Serial.println("initialization failed!");
    while (1);
  }                                 
 
 data = SD.open("Data.csv",FILE_WRITE);   // ouverture du fichier "Data_DHT.csv" en mode ecriture
 data.println("Acceleration X, Acceleration Y, Acceleration Z, Temperature");     // le titre des colonnes de données
 data.close();                                // fermeture du fichier
 starttime=millis();
}

void loop(){ 
  digitalWrite(10,LOW);
  accelerometer.readXYZTData(XValue, YValue, ZValue, Temperature);  //on lit otut en meme temps pour avoir les valeurs au meme moment
  digitalWrite(10,HIGH);
 
  i = millis()-starttime;
 
  digitalWrite(9,LOW);
  data = SD.open("Data1.csv",FILE_WRITE); // ouverture du fichier
  data.print(i);
  Serial.print(i);Serial.print("//");
  data.print(",");
  data.print(XValue);
  Serial.print(XValue);Serial.print("//"); Serial.print(YValue); Serial.print("//");Serial.println(ZValue);
  data.print(",");
  data.print(YValue);   
  data.print(",");
  data.print(ZValue);
  data.print(",");
  data.print(Temperature);
  data.println(",");          // la lecture des données fournie par le capteur puis leurs écriture dans le fichier
  data.close();   // fermeture du fichier
  digitalWrite(9,HIGH);
  i++;
 
  if(i > 5000){
  data.close();
  Serial.println("Done!");
  delay(900000);
  }
   
}


What did I misunderstand or what is wrong with my code?
Thanks a lot,
Nowotny

gbafamily

https://forum.arduino.cc/index.php?topic=592691.0

pylon

Unfortunately Sparkfun did a bad job for this module. Although the used level converter chip supports putting the outputs into tri-state, they didn't use it because it's reference is VccA based, so it would have needed another MOSFET or so on the board.

So the problem is not in your code but in the hardware. The easiest solution to fix this is by using a PNP MOSFEt connected to CS and providing power to the SD card board. This way you switch it completely off while not active. If you have a spare pin I would use that one to switch the power to the board because I don't know how fast an SD card must power up.

Go Up