Issue with MT9D111 board and Digital Camera code

Hi,

I have Arducam MT9D111 board and using Digital Camera code for capturing a picture.

The problem is that

  1. I can't change the resolution. it only takes pictures with 800x600. But I want to use its maximum ability.
  2. I want to save pictures with JPEG format instead of RAW.
I tried to modify the code to address the aforementioned problems, but it didn't work.

I appreciate it if you could help me out.

Hello,

Don’t worry and I will try my best to help you.

I need to ensure a key point.

Which Platform are you using? Can you send me the code you are using? I will help you check it in detail.

]

 

Hi,

I appreciate that,

I use the MT9D111 camera module.

And the code is the ArduCAM_Shield_V2_Digital_Camera as follows:

// 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//This demo can only work on ARDUCAM_SHIELD_V2 platform.
//This demo is compatible with ESP8266
// It will run the ArduCAM as a real 2MP/3MP/5MP digital camera, provide both preview and JPEG capture.
// The demo sketch will do the following tasks:
// 1. Set the sensor to BMP preview output mode.
// 2. Switch to JPEG mode when shutter buttom pressed.
// 3. Capture and buffer the image to FIFO.
// 4. Store the image to Micro SD/TF card with JPEG format.
// 5. Resolution can be changed by myCAM.OV5642_set_JPEG_size() function.
// This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM shield V2
// and use Arduino IDE 1.6.8 compiler or above

#include <UTFT_SPI.h>
#include <SD.h>
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include “memorysaver.h”
//This demo was made for Omnivision MT9D111A/MT9D111B/MT9M112/MT9V111_CAM/
// MT9M001/MT9T112/MT9D112/OV7670/OV7675/
// OV7725/OV2640/OV5640/OV5642 sensor.
#if !(defined ARDUCAM_SHIELD_V2 && (defined MT9D111A_CAM|| defined MT9D111B_CAM || defined MT9M112_CAM
|| defined MT9V111_CAM || defined MT9M001_CAM || defined MT9T112_CAM
|| defined MT9D112_CAM || defined OV7670_CAM || defined OV7675_CAM
|| defined OV7725_CAM || defined OV2640_CAM || defined OV5640_CAM
|| defined OV5642_CAM || defined OV3640_CAM))
#error Please select the hardware platform and camera module in the …/libraries/ArduCAM/memorysaver.h file
#endif
#if defined(arm)
#include <itoa.h>
#endif

#if defined(ESP8266)
#define SD_CS 0
const int SPI_CS = 16;
#else
#define SD_CS 9
const int SPI_CS =10;
#endif

#if defined (MT9D111A_CAM)
ArduCAM myCAM(MT9D111_A, SPI_CS);
#elif defined (MT9D111B_CAM)
ArduCAM myCAM(MT9D111_B, SPI_CS);
#elif defined (MT9M112_CAM)
ArduCAM myCAM(MT9M112, SPI_CS);
#elif defined (MT9V111_CAM)
ArduCAM myCAM(MT9V111, SPI_CS);
#elif defined (MT9M001_CAM)
ArduCAM myCAM(MT9M001, SPI_CS);
#elif defined (MT9T112_CAM)
ArduCAM myCAM(MT9T112, SPI_CS);
#elif defined (MT9D112_CAM)
ArduCAM myCAM(MT9D112, SPI_CS);
#elif defined (OV7670_CAM)
ArduCAM myCAM(OV7670, SPI_CS);
#elif defined (OV7675_CAM)
ArduCAM myCAM(OV7675, SPI_CS);
#elif defined (OV7725_CAM)
ArduCAM myCAM(OV7725, SPI_CS);
#elif defined (OV2640_CAM)
ArduCAM myCAM(OV2640, SPI_CS);
#elif defined (OV3640_CAM)
ArduCAM myCAM(OV3640, SPI_CS);
#elif defined (OV5640_CAM)
ArduCAM myCAM(OV5640, SPI_CS);
#elif defined (OV5642_CAM)
ArduCAM myCAM(OV5642, SPI_CS);
#endif
UTFT myGLCD(SPI_CS);
boolean isShowFlag = true;
bool is_header = false;
void setup()
{
uint8_t vid,pid;
uint8_t temp;
#if defined(SAM3X8E)
Wire1.begin();
#else
Wire.begin();
#endif
Serial.begin(115200);
Serial.println(F(“ArduCAM Start!”));
// set the SPI_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;
}
}
//Change MCU mode
myCAM.set_mode(MCU2LCD_MODE);
myGLCD.InitLCD();
#if defined (OV2640_CAM)
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_CAM)
while(1){
//Check if the camera module type is OV3640
myCAM.wrSensorReg16_8(0xff, 0x01);
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;
}
}
#elif defined (OV5640_CAM)
while(1){
//Check if the camera module type is OV5640
myCAM.wrSensorReg16_8(0xff, 0x01);
myCAM.rdSensorReg16_8(OV5640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg16_8(OV5640_CHIPID_LOW, &pid);
if((vid != 0x56) || (pid != 0x40)){
Serial.println(F(“Can’t find OV5640 module!”));
delay(1000);continue;
}else{
Serial.println(F(“OV5640 detected.”));break;
}
}
#elif defined (OV5642_CAM)
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
//Initialize SD Card
while(!SD.begin(SD_CS)){
Serial.println(F(“SD Card Error”));delay(1000);
}
Serial.println(F(“SD Card detected!”));
//Change to BMP capture mode and initialize the OV5642 module
myCAM.set_format(BMP);
myCAM.InitCAM();
}
void loop()
{
char str[8];
File outFile;
byte buf[256];
static int i = 0;
static int k = 0;
uint8_t temp=0,temp_last =0;
uint32_t length = 0;
uint8_t start_capture = 0;
int total_time = 0;
//Wait trigger from shutter buttom
if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK))
{
isShowFlag = false;
myCAM.set_mode(MCU2LCD_MODE);
myCAM.set_format(JPEG);
myCAM.InitCAM();
#if defined (OV2640_CAM)
myCAM.OV2640_set_JPEG_size(OV2640_640x480);delay(1000);
#elif defined (OV3640_CAM)
myCAM.OV3640_set_JPEG_size(OV3640_320x240);delay(1000);
#elif defined (OV5640_CAM)
myCAM.OV5640_set_JPEG_size(OV5640_320x240);delay(1000);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
#elif defined (OV5642_CAM)
myCAM.OV5642_set_JPEG_size(OV5642_320x240);delay(1000);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
#endif
//Wait until buttom released
while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK));
delay(1000);
start_capture = 1;
}
else
{
if(isShowFlag )
{
if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming
{
myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU
myGLCD.resetXY();
myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM
while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone
}
}
}
if(start_capture)
{
//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”));
}
if(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) //8M
{
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,".raw");
//Open the new file
outFile = SD.open(str,O_WRITE | O_CREAT | O_TRUNC);
if (! outFile)
{
Serial.println(F(“File open failed”));
return;
}
i = 0;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
while ( length-- )
{
#if defined (ESP8266)
yield();
#endif
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();
outFile.write(buf, 256);
i = 0;
buf[i++] = temp;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
}

if ( length == 0 ) //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;
}

}
//Clear the capture done flag
myCAM.clear_fifo_flag();
//Clear the start capture flag
start_capture = 0;
myCAM.set_format(BMP);
myCAM.InitCAM();
isShowFlag = true;
}
}

Hi,

I appreciate that,

Please find the code(ArduCAM_Shield_V2_Digital_Camera) below

// 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//This demo can only work on ARDUCAM_SHIELD_V2 platform.
//This demo is compatible with ESP8266
// It will run the ArduCAM as a real 2MP/3MP/5MP digital camera, provide both preview and JPEG capture.
// The demo sketch will do the following tasks:
// 1. Set the sensor to BMP preview output mode.
// 2. Switch to JPEG mode when shutter buttom pressed.
// 3. Capture and buffer the image to FIFO.
// 4. Store the image to Micro SD/TF card with JPEG format.
// 5. Resolution can be changed by myCAM.OV5642_set_JPEG_size() function.
// This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM shield V2
// and use Arduino IDE 1.6.8 compiler or above

#include <UTFT_SPI.h>
#include <SD.h>
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include “memorysaver.h”
//This demo was made for Omnivision MT9D111A/MT9D111B/MT9M112/MT9V111_CAM/
// MT9M001/MT9T112/MT9D112/OV7670/OV7675/
// OV7725/OV2640/OV5640/OV5642 sensor.
#if !(defined ARDUCAM_SHIELD_V2 && (defined MT9D111A_CAM|| defined MT9D111B_CAM || defined MT9M112_CAM
|| defined MT9V111_CAM || defined MT9M001_CAM || defined MT9T112_CAM
|| defined MT9D112_CAM || defined OV7670_CAM || defined OV7675_CAM
|| defined OV7725_CAM || defined OV2640_CAM || defined OV5640_CAM
|| defined OV5642_CAM || defined OV3640_CAM))
#error Please select the hardware platform and camera module in the …/libraries/ArduCAM/memorysaver.h file
#endif
#if defined(arm)
#include <itoa.h>
#endif

#if defined(ESP8266)
#define SD_CS 0
const int SPI_CS = 16;
#else
#define SD_CS 9
const int SPI_CS =10;
#endif

#if defined (MT9D111A_CAM)
ArduCAM myCAM(MT9D111_A, SPI_CS);
#elif defined (MT9D111B_CAM)
ArduCAM myCAM(MT9D111_B, SPI_CS);
#elif defined (MT9M112_CAM)
ArduCAM myCAM(MT9M112, SPI_CS);
#elif defined (MT9V111_CAM)
ArduCAM myCAM(MT9V111, SPI_CS);
#elif defined (MT9M001_CAM)
ArduCAM myCAM(MT9M001, SPI_CS);
#elif defined (MT9T112_CAM)
ArduCAM myCAM(MT9T112, SPI_CS);
#elif defined (MT9D112_CAM)
ArduCAM myCAM(MT9D112, SPI_CS);
#elif defined (OV7670_CAM)
ArduCAM myCAM(OV7670, SPI_CS);
#elif defined (OV7675_CAM)
ArduCAM myCAM(OV7675, SPI_CS);
#elif defined (OV7725_CAM)
ArduCAM myCAM(OV7725, SPI_CS);
#elif defined (OV2640_CAM)
ArduCAM myCAM(OV2640, SPI_CS);
#elif defined (OV3640_CAM)
ArduCAM myCAM(OV3640, SPI_CS);
#elif defined (OV5640_CAM)
ArduCAM myCAM(OV5640, SPI_CS);
#elif defined (OV5642_CAM)
ArduCAM myCAM(OV5642, SPI_CS);
#endif
UTFT myGLCD(SPI_CS);
boolean isShowFlag = true;
bool is_header = false;
void setup()
{
uint8_t vid,pid;
uint8_t temp;
#if defined(SAM3X8E)
Wire1.begin();
#else
Wire.begin();
#endif
Serial.begin(115200);
Serial.println(F(“ArduCAM Start!”));
// set the SPI_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;
}
}
//Change MCU mode
myCAM.set_mode(MCU2LCD_MODE);
myGLCD.InitLCD();
#if defined (OV2640_CAM)
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_CAM)
while(1){
//Check if the camera module type is OV3640
myCAM.wrSensorReg16_8(0xff, 0x01);
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;
}
}
#elif defined (OV5640_CAM)
while(1){
//Check if the camera module type is OV5640
myCAM.wrSensorReg16_8(0xff, 0x01);
myCAM.rdSensorReg16_8(OV5640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg16_8(OV5640_CHIPID_LOW, &pid);
if((vid != 0x56) || (pid != 0x40)){
Serial.println(F(“Can’t find OV5640 module!”));
delay(1000);continue;
}else{
Serial.println(F(“OV5640 detected.”));break;
}
}
#elif defined (OV5642_CAM)
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
//Initialize SD Card
while(!SD.begin(SD_CS)){
Serial.println(F(“SD Card Error”));delay(1000);
}
Serial.println(F(“SD Card detected!”));
//Change to BMP capture mode and initialize the OV5642 module
myCAM.set_format(BMP);
myCAM.InitCAM();
}
void loop()
{
char str[8];
File outFile;
byte buf[256];
static int i = 0;
static int k = 0;
uint8_t temp=0,temp_last =0;
uint32_t length = 0;
uint8_t start_capture = 0;
int total_time = 0;
//Wait trigger from shutter buttom
if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK))
{
isShowFlag = false;
myCAM.set_mode(MCU2LCD_MODE);
myCAM.set_format(JPEG);
myCAM.InitCAM();
#if defined (OV2640_CAM)
myCAM.OV2640_set_JPEG_size(OV2640_640x480);delay(1000);
#elif defined (OV3640_CAM)
myCAM.OV3640_set_JPEG_size(OV3640_320x240);delay(1000);
#elif defined (OV5640_CAM)
myCAM.OV5640_set_JPEG_size(OV5640_320x240);delay(1000);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
#elif defined (OV5642_CAM)
myCAM.OV5642_set_JPEG_size(OV5642_320x240);delay(1000);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
#endif
//Wait until buttom released
while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK));
delay(1000);
start_capture = 1;
}
else
{
if(isShowFlag )
{
if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming
{
myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU
myGLCD.resetXY();
myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM
while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone
}
}
}
if(start_capture)
{
//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”));
}
if(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) //8M
{
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,".raw");
//Open the new file
outFile = SD.open(str,O_WRITE | O_CREAT | O_TRUNC);
if (! outFile)
{
Serial.println(F(“File open failed”));
return;
}
i = 0;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
while ( length-- )
{
#if defined (ESP8266)
yield();
#endif
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();
outFile.write(buf, 256);
i = 0;
buf[i++] = temp;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
}

if ( length == 0 ) //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;
}

}
//Clear the capture done flag
myCAM.clear_fifo_flag();
//Clear the start capture flag
start_capture = 0;
myCAM.set_format(BMP);
myCAM.InitCAM();
isShowFlag = true;
}
}

Hi,

I appreciate that,
Please find the code (ArduCAM_Shield_V2_Digital_Camera) below.

// 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//This demo can only work on ARDUCAM_SHIELD_V2 platform.
//This demo is compatible with ESP8266
// It will run the ArduCAM as a real 2MP/3MP/5MP digital camera, provide both preview and JPEG capture.
// The demo sketch will do the following tasks:
// 1. Set the sensor to BMP preview output mode.
// 2. Switch to JPEG mode when shutter buttom pressed.
// 3. Capture and buffer the image to FIFO.
// 4. Store the image to Micro SD/TF card with JPEG format.
// 5. Resolution can be changed by myCAM.OV5642_set_JPEG_size() function.
// This program requires the ArduCAM V4.0.0 (or later) library and ArduCAM shield V2
// and use Arduino IDE 1.6.8 compiler or above

#include <UTFT_SPI.h>
#include <SD.h>
#include <Wire.h>
#include <ArduCAM.h>
#include <SPI.h>
#include “memorysaver.h”
//This demo was made for Omnivision MT9D111A/MT9D111B/MT9M112/MT9V111_CAM/
// MT9M001/MT9T112/MT9D112/OV7670/OV7675/
// OV7725/OV2640/OV5640/OV5642 sensor.
#if !(defined ARDUCAM_SHIELD_V2 && (defined MT9D111A_CAM|| defined MT9D111B_CAM || defined MT9M112_CAM
|| defined MT9V111_CAM || defined MT9M001_CAM || defined MT9T112_CAM
|| defined MT9D112_CAM || defined OV7670_CAM || defined OV7675_CAM
|| defined OV7725_CAM || defined OV2640_CAM || defined OV5640_CAM
|| defined OV5642_CAM || defined OV3640_CAM))
#error Please select the hardware platform and camera module in the …/libraries/ArduCAM/memorysaver.h file
#endif
#if defined(arm)
#include <itoa.h>
#endif

#if defined(ESP8266)
#define SD_CS 0
const int SPI_CS = 16;
#else
#define SD_CS 9
const int SPI_CS =10;
#endif

#if defined (MT9D111A_CAM)
ArduCAM myCAM(MT9D111_A, SPI_CS);
#elif defined (MT9D111B_CAM)
ArduCAM myCAM(MT9D111_B, SPI_CS);
#elif defined (MT9M112_CAM)
ArduCAM myCAM(MT9M112, SPI_CS);
#elif defined (MT9V111_CAM)
ArduCAM myCAM(MT9V111, SPI_CS);
#elif defined (MT9M001_CAM)
ArduCAM myCAM(MT9M001, SPI_CS);
#elif defined (MT9T112_CAM)
ArduCAM myCAM(MT9T112, SPI_CS);
#elif defined (MT9D112_CAM)
ArduCAM myCAM(MT9D112, SPI_CS);
#elif defined (OV7670_CAM)
ArduCAM myCAM(OV7670, SPI_CS);
#elif defined (OV7675_CAM)
ArduCAM myCAM(OV7675, SPI_CS);
#elif defined (OV7725_CAM)
ArduCAM myCAM(OV7725, SPI_CS);
#elif defined (OV2640_CAM)
ArduCAM myCAM(OV2640, SPI_CS);
#elif defined (OV3640_CAM)
ArduCAM myCAM(OV3640, SPI_CS);
#elif defined (OV5640_CAM)
ArduCAM myCAM(OV5640, SPI_CS);
#elif defined (OV5642_CAM)
ArduCAM myCAM(OV5642, SPI_CS);
#endif
UTFT myGLCD(SPI_CS);
boolean isShowFlag = true;
bool is_header = false;
void setup()
{
uint8_t vid,pid;
uint8_t temp;
#if defined(SAM3X8E)
Wire1.begin();
#else
Wire.begin();
#endif
Serial.begin(115200);
Serial.println(F(“ArduCAM Start!”));
// set the SPI_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;
}
}
//Change MCU mode
myCAM.set_mode(MCU2LCD_MODE);
myGLCD.InitLCD();
#if defined (OV2640_CAM)
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_CAM)
while(1){
//Check if the camera module type is OV3640
myCAM.wrSensorReg16_8(0xff, 0x01);
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;
}
}
#elif defined (OV5640_CAM)
while(1){
//Check if the camera module type is OV5640
myCAM.wrSensorReg16_8(0xff, 0x01);
myCAM.rdSensorReg16_8(OV5640_CHIPID_HIGH, &vid);
myCAM.rdSensorReg16_8(OV5640_CHIPID_LOW, &pid);
if((vid != 0x56) || (pid != 0x40)){
Serial.println(F(“Can’t find OV5640 module!”));
delay(1000);continue;
}else{
Serial.println(F(“OV5640 detected.”));break;
}
}
#elif defined (OV5642_CAM)
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
//Initialize SD Card
while(!SD.begin(SD_CS)){
Serial.println(F(“SD Card Error”));delay(1000);
}
Serial.println(F(“SD Card detected!”));
//Change to BMP capture mode and initialize the OV5642 module
myCAM.set_format(BMP);
myCAM.InitCAM();
}
void loop()
{
char str[8];
File outFile;
byte buf[256];
static int i = 0;
static int k = 0;
uint8_t temp=0,temp_last =0;
uint32_t length = 0;
uint8_t start_capture = 0;
int total_time = 0;
//Wait trigger from shutter buttom
if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK))
{
isShowFlag = false;
myCAM.set_mode(MCU2LCD_MODE);
myCAM.set_format(JPEG);
myCAM.InitCAM();
#if defined (OV2640_CAM)
myCAM.OV2640_set_JPEG_size(OV2640_640x480);delay(1000);
#elif defined (OV3640_CAM)
myCAM.OV3640_set_JPEG_size(OV3640_320x240);delay(1000);
#elif defined (OV5640_CAM)
myCAM.OV5640_set_JPEG_size(OV5640_320x240);delay(1000);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
#elif defined (OV5642_CAM)
myCAM.OV5642_set_JPEG_size(OV5642_320x240);delay(1000);
myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH
#endif
//Wait until buttom released
while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK));
delay(1000);
start_capture = 1;
}
else
{
if(isShowFlag )
{
if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming
{
myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU
myGLCD.resetXY();
myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM
while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone
}
}
}
if(start_capture)
{
//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”));
}
if(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) //8M
{
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,".raw");
//Open the new file
outFile = SD.open(str,O_WRITE | O_CREAT | O_TRUNC);
if (! outFile)
{
Serial.println(F(“File open failed”));
return;
}
i = 0;
myCAM.CS_LOW();
myCAM.set_fifo_burst();
while ( length-- )
{
#if defined (ESP8266)
yield();
#endif
temp_last = temp;
temp = SPI.transfer(0x00);

  //Write image data to buffer if not full
  if (i &lt; 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();
  }  
  
  if ( length == 0 ) //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;
  }  
    
  

}
//Clear the capture done flag 
myCAM.clear_fifo_flag();
//Clear the start capture flag
start_capture = 0;
myCAM.set_format(BMP);
myCAM.InitCAM();
isShowFlag = true;	

}
}

Hi,

Thanks for your detail code.

Changing other resolution need to modify the sensor configuration. We have a demo which is 1600x1200 jpeg format demo https://github.com/ArduCAM/Arduino/tree/master/ArduCAM/examples/Shield_V2/ArduCAM_Shield_V2_MT9D111_Camera_JPEG

 

Thanks for your response,

I changed the code to “ArduCAM_Shield_V2_MT9D111_Camera_JPEG”, but it doesn’t capture a picture and save it to the SD card.

So, Is this the “ArduCAM_Shield_V2_MT9D111_Camera_JPEG” right code for my camera?

Hi,

Please attach me your hardware connection diagram and I will help you check it in detail.

Notice, the image does not display on shield v2 screen due to it is encoded in JPEG format.

Let me know if you need more help.

 

Hi,

Please find the attached.

I just want to save the picture in the SD card with the highest resolution.

If RAW format display the image, it’s fine with me. Also, how can I change it?

Hi,

Our engineer has tested the code and it can work normally. https://github.com/ArduCAM/Arduino/tree/master/ArduCAM/examples/Shield_V2/ArduCAM_Shield_V2_MT9D111_Camera_JPEG

You should press the button to start capture

 

Hi,

Yes, As i mentioned before, when i press the button, nothing happens.

Please find the attached which shows the serial monitor.As you can see when i press the button nothing shows up.

Also, when i check the SD card,there is no image.

Hi,

Very strange. Are you sure your hardware connection is right? Please attach me your hardware connection diagram. Our engineer suspect your connection may be wrong.

Let me know if you need more help.

Hi,

Please find the attached.

I don’t have a hardware connection diagram because I didn’t assemble it (I took it from my colleague). I disassembled the boards. So you can send me the right hardware connection diagram.

Hi,

Oh, maybe I know the differences. What the board you are using is the DUE while what I am using is Mega2560. Can you test it using mega2560 board ?

 

 

 

Hi,

Thanks,

Could you make sure that the mega2560 board works for my application?Because, I need to get my supervisor permission to purchase mega2560. If so, could you verify that the following link is the mega2560 which I need to get.

https://www.digikey.com/products/en?mpart=A000067&v=1050

Hi,

Yes, it is the 2560 board. Or you can use UNO board.

I have tested it using mega2560 and uno board and they can work normally.

 

Hi,

I bought Mega 2560 and tested it. It worked very well. Now I have camera’s highest resolution (1600x1200). Apparently, for image processing purpose, we need to get RAW format. So, How can I change JPEG to RAW in MT9D111 code? Or I should use another code?

 

Thanks

Hello,

Great to hear it works very well. At present, we don’t have 1600x1200 raw configuration.

We just have QVGA RGB565 configuration. You can get it here https://github.com/ArduCAM/Arduino/blob/master/ArduCAM/mt9d111_regs.h

 

 

 

 

Hi,

is this QVGA : 1280 x 960 ?

I don’t know how to use this configuration?

Do I still need to use MT9D111 camera example?

Hi,

QVGA is a slip of the pen when writing comments, please ignore it, we will update the new modification later.

This configuration can be used in this example.