ArduCAM / Arduino

This is ArduCAM library for Arduino boards
MIT License
470 stars 348 forks source link

Error when using SD card with the ArduCAM 2MP (Capture2SD code) #285

Open Sunshine-Leona opened 6 years ago

Sunshine-Leona commented 6 years ago

Hello and Happy New Year to you @ArduCAM ,

I have a problem with the following code: "ArduCAM_Mini_Capture2SD"

I am using the Arduino MEGA 2560; the original Arduino Wi-Fi Shield mounted on it (this one https://www.arduino.cc/en/Guide/ArduinoWiFiShield) + the ArduCAM Mini 2MP without the camera shield. I added a small change which is initializing Pin 53 as an output:

` // ArduCAM demo (C)2017 Lee // Web: http://www.ArduCAM.com // This program is a demo of how to use most of the functions // of the library with a supported camera modules, and can run on any Arduino platform. // // This demo was made for Omnivision 2MP/5MP sensor. // It will run the ArduCAM 2MP/5MP as a real 2MP/5MP digital camera, provide both JPEG capture. // The demo sketch will do the following tasks: // 1. Set the sensor to JPEG mode. // 2. Capture and buffer the image to FIFO every 5 seconds // 3. Store the image to Micro SD/TF card with JPEG format in sequential. // 4. Resolution can be changed by myCAM.set_JPEG_size() function. // This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM 2MP/5MP shield // and use Arduino IDE 1.6.8 compiler or above

include

include

include

include

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); pinMode(53, OUTPUT): // initialize SPI: SPI.begin(); 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); }

`

My connections are as follows: MISO -> 50 MOSI -> 51 SCK -> 52 VCC -> 5V SDA -> 20 SCL -> 21 Pin 53 of the Arduino board is connected to Pin 9 ( the SD_CS Pin).

When I upload the code and check the serial monitor, I see reverse question marks. The ArduCAM host app just shows "ArduCAM Single Capture" when I open the port and click "Capture".

The "ArduCAM_Mini_2MP_OV2640_functions" code works fine with the same connections, excluding the SD pin, of course. I was hoping you would be able to point out my mistake. Thanks in advance!

This is the serial monitor display: 0f⸮⸮⸮\⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮\⸮⸮⸮޹⸮

pkvader commented 6 years ago

I couldn't find the arducam online for the fritzing diagram but you get the point. If you have it wired like this properly, its your code that could be the problem. The SD card readers usually require a stable connection meaning very short wires that connect it to the Arduino. If the wire is extended the data transfer will not work. @KlydeLencioco

93450101 commented 6 years ago

@supprot hello ,I want to display the image inside the SD card with the host app، can you help me?