ArduCAM / Arduino

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

ArduCAM not working with NodeMCU 1.0 #434

Closed anku2424 closed 4 years ago

anku2424 commented 4 years ago

Hello Team ArduCAM, thanks for the great work that you are doing. I tried to get Arducam mini 2MP camera working with nodemcu. I connected the arducam and sd card through SPI and used the following code but all the images that are saved are corrupted. The same setup of arducam and sd card works well on arduino uno but not on nodemcu. Here's the code i used.

// ArduCAM demo (C)2016 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 OV2640 2MP sensor. // It will run the ArduCAM ESP8266 2MP as a real 2MP 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 ESP8266 2MP shield // and use Arduino IDE 1.5.2 compiler or above

include

include

include

include

include "memorysaver.h"

if !(defined ESP8266 )

error Please select the ArduCAM ESP8266 UNO board in the Tools/Board

endif

//This demo can only work on OV2640_MINI_2MP or ARDUCAM_SHIELD_V2 platform.

if !(defined (OV2640_MINI_2MP)||(defined (ARDUCAM_SHIELD_V2) && defined (OV2640_CAM)))

error Please select the hardware platform and camera module in the ../libraries/ArduCAM/memorysaver.h file

endif

// set GPIO16 as the slave select : const int CS = 16; //Version 2,set GPIO0 as the slave select : const int SD_CS = 0; ArduCAM myCAM(OV2640, CS);

void myCAMSaveToSDFile(){ char str[8]; byte buf[256]; static int i = 0; static int k = 0; static int n = 0; uint8_t temp, temp_last; File file; //Flush the FIFO myCAM.flush_fifo(); //Clear the capture done flag myCAM.clear_fifo_flag(); //Start capture myCAM.start_capture(); Serial.println("star Capture"); while(!myCAM.get_bit(ARDUCHIP_TRIG , CAP_DONE_MASK)); Serial.println("Capture Done!");

//Construct a file name k = k + 1; itoa(k, str, 10); strcat(str, ".jpg"); //Open the new file file = SD.open(str, O_WRITE | O_CREAT | O_TRUNC); if(! file){ Serial.println("open file faild"); return; } i = 0; myCAM.CS_LOW(); myCAM.set_fifo_burst();

if !(defined (ARDUCAM_SHIELD_V2) && defined (OV2640_CAM))

SPI.transfer(0xFF);

endif

//Read JPEG data from FIFO while ( (temp !=0xD9) | (temp_last !=0xFF)){ temp_last = temp; temp = SPI.transfer(0x00);

//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(); file.write(buf ,256); i = 0; buf[i++] = temp; myCAM.CS_LOW(); myCAM.set_fifo_burst(); } delay(0);
}

//Write the remain bytes in the buffer if(i > 0){ myCAM.CS_HIGH(); file.write(buf,i); } //Close the file file.close(); Serial.println("CAM Save Done!"); }

void setup(){ uint8_t vid, pid; uint8_t temp; Wire.begin(); Serial.begin(115200); Serial.println("ArduCAM Start!");

//set the CS as an output: pinMode(CS,OUTPUT);

//initialize SPI: SPI.begin(); SPI.setFrequency(4000000); //4MHZ

delay(1000); //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("SPI1 interface Error!"); while(1); }

//Initialize SD Card

if(!SD.begin(SD_CS)){ Serial.println("SD Card Error"); } else Serial.println("SD Card detected!");

//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("Can't find OV2640 module!"); else Serial.println("OV2640 detected."); myCAM.set_format(JPEG); myCAM.InitCAM(); }

void loop(){ delay(5000); myCAMSaveToSDFile(); }

ArducamSupport commented 4 years ago

@anku2424 Hi, I'll be glad to solve your problem.After checking your code, I think the picture in the sd card lacks the head data, so the picture cannot be opened.I advice you to try again by using the new 'void myCAMSaveToSDFile()'.If there are still problems, I will reply to you as soon as possible. `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("Star 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); //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; } i = 0; 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;
} } }`

anku2424 commented 4 years ago

It worked splendidly, thanks!