Sd card error on serial monitor

Good morning,
I am trying to upload code for 5 mp arducam to take pic and upload to sd card
the code doesnt show error but the serial monitor says spi interface erroe
board is arduino mega
microsd card module
arducam 5mp ov5642 mini

#include <ArduCAM.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "memorysaver.h"
//This demo can only work on OV2640_MINI_2MP or OV5642_MINI_5MP or OV5642_MINI_5MP_BIT_ROTATION_FIXED platform.
#if !(defined OV5642_MINI_5MP || defined OV5642_MINI_5MP_BIT_ROTATION_FIXED || defined OV2640_MINI_2MP || defined OV3640_MINI_3MP)
  #error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file
#endif
#define SD_CS 9
const int SPI_CS = 7;
#if defined (OV2640_MINI_2MP)
  ArduCAM myCAM( OV2640, SPI_CS );
#elif defined (OV3640_MINI_3MP)
  ArduCAM myCAM( OV3640, SPI_CS );
#else
  ArduCAM myCAM( OV5642, SPI_CS );
#endif

void myCAMSaveToSDFile(){
char str[8];
byte buf[256];
static int i = 0;
static int k = 0;
uint8_t temp = 0,temp_last=0;
uint32_t length = 0;
bool is_header = false;
File outFile;
//Flush the FIFO
myCAM.flush_fifo();
//Clear the capture done flag
myCAM.clear_fifo_flag();
//Start capture
myCAM.start_capture();
Serial.println(F("start Capture"));
while(!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK));
Serial.println(F("Capture Done."));  
length = myCAM.read_fifo_length();
Serial.print(F("The fifo length is :"));
Serial.println(length, DEC);
if (length >= MAX_FIFO_SIZE) //384K
{
  Serial.println(F("Over size."));
  return ;
}
if (length == 0 ) //0 kb
{
  Serial.println(F("Size is 0."));
  return ;
}
//Construct a file name
k = k + 1;
itoa(k, str, 10);
strcat(str, ".jpg");
//Open the new file
outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
if(!outFile){
  Serial.println(F("File open faild"));
  return;
}
myCAM.CS_LOW();
myCAM.set_fifo_burst();
while ( length-- )
{
  temp_last = temp;
  temp =  SPI.transfer(0x00);
  //Read JPEG data from FIFO
  if ( (temp == 0xD9) && (temp_last == 0xFF) ) //If find the end ,break while,
  {
    buf[i++] = temp;  //save the last  0XD9     
    //Write the remain bytes in the buffer
    myCAM.CS_HIGH();
    outFile.write(buf, i);    
    //Close the file
    outFile.close();
    Serial.println(F("Image save OK."));
    is_header = false;
    i = 0;
  }  
  if (is_header == true)
  { 
    //Write image data to buffer if not full
    if (i < 256)
    buf[i++] = temp;
    else
    {
      //Write 256 bytes image data to file
      myCAM.CS_HIGH();
      outFile.write(buf, 256);
      i = 0;
      buf[i++] = temp;
      myCAM.CS_LOW();
      myCAM.set_fifo_burst();
    }        
  }
  else if ((temp == 0xD8) & (temp_last == 0xFF))
  {
    is_header = true;
    buf[i++] = temp_last;
    buf[i++] = temp;   
  } 
} 
}

void setup(){
uint8_t vid, pid;
uint8_t temp;
Wire.begin();
Serial.begin(115200);
Serial.println(F("ArduCAM Start!"));
//set the CS as an output:
pinMode(SPI_CS,OUTPUT);
digitalWrite(SPI_CS, HIGH);
// initialize SPI:
SPI.begin();
  
//Reset the CPLD
myCAM.write_reg(0x07, 0x80);
delay(100);
myCAM.write_reg(0x07, 0x00);
delay(100);
  
while(1){
  //Check if the ArduCAM SPI bus is OK
  myCAM.write_reg(ARDUCHIP_TEST1, 0x55);
  temp = myCAM.read_reg(ARDUCHIP_TEST1);
  
  if (temp != 0x55){
    Serial.println(F("SPI interface Error!"));
    delay(1000);continue;
  }else{
    Serial.println(F("SPI interface OK."));break;
  }
}
//Initialize SD Card
while(!SD.begin(SD_CS)){
  Serial.println(F("SD Card Error!"));delay(1000);
}
Serial.println(F("SD Card detected."));

#if defined (OV2640_MINI_2MP)
  while(1){
    //Check if the camera module type is OV2640
    myCAM.wrSensorReg8_8(0xff, 0x01);
    myCAM.rdSensorReg8_8(OV2640_CHIPID_HIGH, &vid);
    myCAM.rdSensorReg8_8(OV2640_CHIPID_LOW, &pid);
    if ((vid != 0x26 ) && (( pid != 0x41 ) || ( pid != 0x42 ))){
      Serial.println(F("Can't find OV2640 module!"));
      delay(1000);continue;
    }
    else{
      Serial.println(F("OV2640 detected."));break;
    } 
  }
#elif defined (OV3640_MINI_3MP)
  while(1){
    //Check if the camera module type is OV3640
    myCAM.rdSensorReg16_8(OV3640_CHIPID_HIGH, &vid);
    myCAM.rdSensorReg16_8(OV3640_CHIPID_LOW, &pid);
    if ((vid != 0x36) || (pid != 0x4C)){
      Serial.println(F("Can't find OV3640 module!"));
      delay(1000);continue; 
    }else{
      Serial.println(F("OV3640 detected."));break;    
    }
 } 
#else
  while(1){
    //Check if the camera module type is OV5642
    myCAM.wrSensorReg16_8(0xff, 0x01);
    myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid);
    myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid);
    if((vid != 0x56) || (pid != 0x42)){
      Serial.println(F("Can't find OV5642 module!"));
      delay(1000);continue;
    }
    else{
      Serial.println(F("OV5642 detected."));break;
    } 
  }
#endif
myCAM.set_format(JPEG);
myCAM.InitCAM();
#if defined (OV2640_MINI_2MP)
  myCAM.OV2640_set_JPEG_size(OV2640_320x240);
#elif defined (OV3640_MINI_3MP)
  myCAM.OV3640_set_JPEG_size(OV3640_320x240);
#else
  myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK);   //VSYNC is active HIGH
  myCAM.OV5642_set_JPEG_size(OV5642_320x240);
#endif
delay(1000);
}
void loop(){
myCAMSaveToSDFile();
delay(5000);
}


picture of what i see

my wiring was off
i fixed the wiring now i am getting spi interface error

Post the entire text of the error message. Paraphrasing errors leaves out important information.

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

there isnt a error message just on serial port


1 Like

You're creating a 10 char file name with .jpg extension. file MUST conform to DOS 8.3 names and 32gb max card size limit.

how do i do that change to conform dos 8.3 names

Shorten the filename from 10 to 8 characters. Did you not write this code?

//Construct a file name
k = k + 1;
itoa(k, str, 10);
strcat(str, ".jpg");

Change the 10 to 8 for a start.
This code could NEVER work so was not tested before posting.
C++ syntax is k++;, not k = k + 1;, you need proper code, not chatGPT or copytpaste

no i got from example and merge 2 codes

i changed 10 to 8 but i am having an spi interface error
i fixed sd card it was a wiring issue

LOL Always double check your work!

yeh i realized after i posted but then i got a spi interface error

Hi Larenzia, I hope what I will say helps you out of your problems allthough I am not quite familiar to your camera device.

Now first consideration you should take is that camera and sdcard holder are both SDI dependent devices, meaning that chip select plays a vital role. From what I can see on your fritzing, chip select pin from your card holder is connected to pin 53 while your code defines the sd chip select to 9, meaning you should move the cable from pin 53 to pin 9, and perhaps your SPI errors will be gone. If not, I would try to connect the MOSI, MISO & SCK to same connections your cam is using, perhaps with the help of a breadboard to connect the wires together. Futher precautions may be defining the chip select pins as outputs and setting them to HIGH at setup start.

You will probably have no need for wiring the SDA & SCL pins of your cam as these will only be needed if you use the I2C interface your cam may have and you are using SPI for the purpose.

Regarding the sd card library you are using, I had my own headaches with that, for some reason, on my own Mega2560 projects, I was never able to write a file, getting the same results as you. A change to SdFat library put things the right way, besides you will find some other interesting features about this library having a look at the examples coming with it, you will have no problems with long file names, get a lot of details about your card and be able to format it without the help of some other equipment.

Hope all said will help you. Best regards.

2 Likes

This explanation was gorgeous with out making me feel incompetent I really appreciate it I’ll let you know if it works
Also how do I change it to sdfat?
I know how to format an SD card but I am new to programming and don’t know how to change that. I’ve never did a SD card module before.

Hi, well, first step will be having the SdFat library installed on your IDE, using the library manager. Once it is installed you will find a new reference to it in the File->Examples list. Take your time to study these examples as besides removing the reference

#include <SD.h>

and replace it with

#include <SdFat.h>

you will have to change the syntax to the sd setup and file commands you will be using in your program and add some declarations like

SdFat sd;
FsFile sdfile;

Have your time studying the examples, some of them refer to earlier SdFat versions, yours will be 2.2.2 if you do as said, and a lot of the examples refer to backward compatibility and earlier versions you will not have to consider, may be somewhat confusing at first sight.

Test the examples on your Mega, make some own program inspired on them to get the understanding on how it works, and then modify your camera proyect to use it

Hope all said will help you. Best regards.

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