0
\$\begingroup\$

I am trying to log data received from a CAN bus to an SD card using an arduino Uno.

Each module works perfectly on its own, but when joining both modules together the CAN bus receives data normally, but the SD card does not record any data; it just creates a new empty file to which nothing is written.

SD card module I am using: SD Module

CAN module I am using: CAN Module

Connections:
MCP2515 CAN Bus to Arduino UNO
CS ->   8
SO ->   12
SI ->   11
SCK ->  13
INT ->  2
VCC -> 5V
GND -> GND


SD Reader to Arduino UNO
CS -> 7
SCK -> 13
MOSI -> 11
MISO -> 12
VCC ->  5V
GND -> GND

SD alone Code:

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

File myFile;

int pinCS = 7; // Pin 10 on Arduino Uno

void setup() {
    
  Serial.begin(9600);
  pinMode(pinCS, OUTPUT);
  
  // SD Card Initialization
  if (SD.begin()) {
    Serial.println("SD card is ready to use.");
  } else {
    Serial.println("SD card initialization failed");
    return;
  }
  
  // Create/Open file 
  myFile = SD.open("test.txt", FILE_WRITE);
  
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.println("Writing to file...");
    // Write to file
    myFile.println("Testing text 1, 2 ,3...");
    myFile.close(); // close the file
    Serial.println("Done.");
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }

  // Reading the file
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("Read:");
    // Reading the whole file
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    myFile.close();
  }
  else {
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // empty
}

CAN alone code:

#include <SPI.h> //Library for using SPI Communication 
#include <mcp2515.h> //Library for using CAN Communication
   
struct can_frame canMsg; 
MCP2515 mcp2515(8); // SPI CS Pin 8 
    
volatile bool interrupt = false;

void irqHandler() {
  interrupt = true;
}
    
void setup() {
  SPI.begin(); // Begins SPI communication
  Serial.begin(9600); // Begins Serial Communication at 9600 baud rate
     
  mcp2515.reset();                          
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); // Sets CAN at speed 500KBPS and Clock 8MHz 
  mcp2515.setNormalMode(); // Sets CAN at normal mode
      
  attachInterrupt(0, irqHandler, FALLING);
}
    
void loop() {
  if (interrupt) {
    interrupt = false;
    
    uint8_t irq = mcp2515.getInterrupts();
    
    if (irq & MCP2515::CANINTF_RX0IF) {
      if (mcp2515.readMessage(MCP2515::RXB0, &canMsg) == MCP2515::ERROR_OK) {
        // frame contains received from RXB0 message
        int x = canMsg.data[0];         
        int y = canMsg.data[1];
        Serial.print("Kelembaban: ");
        Serial.print(x);
        Serial.print(" Suhu: ");
        Serial.println(y);
      }
    }
  }
}

Both together(not working code):

#include <SPI.h> //Library for using SPI Communication 
#include <mcp2515.h> //Library for using CAN Communication
#include <SD.h>

File MyFile;
int pinCS = 7; 
struct can_frame canMsg; 
MCP2515 mcp2515(8); // SPI CS Pin 8 

volatile bool interrupt = false;
void irqHandler() {
    interrupt = true;
}

void setup() {
  SPI.begin();   //Begins SPI communication
  Serial.begin(9600); //Begins Serial Communication at 9600 baud rate
  SD.begin();
  pinMode(pinCS, OUTPUT);
  MyFile = SD.open("test.txt", FILE_WRITE);
  
  mcp2515.reset();                          
  mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); //Sets CAN at speed 500KBPS and Clock 8MHz 
  mcp2515.setNormalMode();  //Sets CAN at normal mode
  
  attachInterrupt(0, irqHandler, FALLING);
  }

void loop() {
  if (interrupt) {
    interrupt = false;

    uint8_t irq = mcp2515.getInterrupts();

    if (irq & MCP2515::CANINTF_RX0IF) {
      if (mcp2515.readMessage(MCP2515::RXB0, &canMsg) == MCP2515::ERROR_OK) {
        // frame contains received from RXB0 message
        int x = canMsg.data[0];         
        int y = canMsg.data[1];
        digitalWrite(pinCS, LOW);
        digitalWrite(8, HIGH);
        Serial.print("Kelembaban: ");
        Serial.print(x);
        Serial.print(" Suhu: ");
        Serial.println(y);
                
        MyFile = SD.open("test.txt", FILE_WRITE);  
        MyFile.println("Value of X: "+String(x)+ "Value of Y: "+ String(y));
        MyFile.close();
        digitalWrite(pinCS, HIGH);
        digitalWrite(8, LOW);
      }
    }
  }
}
\$\endgroup\$
2
  • \$\begingroup\$ the top code there won't compile, there's multiple definitions of the samething. \$\endgroup\$ Mar 21, 2022 at 22:00
  • \$\begingroup\$ sorry it was an error while copying the code, it is fixed now. \$\endgroup\$ Mar 21, 2022 at 22:14

2 Answers 2

0
\$\begingroup\$

You should probably make sure to not simultaneously have two SPI devices selected. They are normally selected when the pin assigned to their CS is low. Switch which is selected by first entering a state where none is low and staying in that state for a short while, e.g. delay(10). In your use case the SPI bus is a shared resource you have to manage as such.

\$\endgroup\$
2
  • \$\begingroup\$ okay I will give it a try now. \$\endgroup\$ Mar 21, 2022 at 22:35
  • \$\begingroup\$ sadly it did not work. \$\endgroup\$ Mar 21, 2022 at 22:40
0
\$\begingroup\$

Basically, the file is not opened for writing so it can't be written in the code where you want to write data to file.

The file is opened for writing in the setup() function.

The file is always reopened and closed for reading only in the loop() function, so there is no way writing is possible unless the file is opened properly for writing.

\$\endgroup\$
2
  • \$\begingroup\$ i did forgot to add file write when opening the file i changed but sadly also it doesn't write on the sd card \$\endgroup\$ Mar 22, 2022 at 13:05
  • \$\begingroup\$ You are still opening a file, and reopening the same file, without closing it between two open calls. \$\endgroup\$
    – Justme
    Mar 22, 2022 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.