ov7670 with both arduino uno and now mega

96
Arduino Forum Using Arduino => Sensors => Topic started by: Mr_arduino on Apr 10, 2013, 04:50 am Title: ov7670 with both arduino uno and now mega Post by: Mr_arduino on Apr 10, 2013, 04:50 am Have you been told by someone that you an external ram of some sort such as a fifo or spi ram. I admit to thinking that was needed and telling people they need one or the other (fifo being the better option) but if you just want to send data from the camera to your PC using the arduino you don't need external ram. Check out this https://github.com/ComputerNerd/ov7670-no-ram-arduino-uno It requires only the ov7670 without fifo and a way to get 5v to 3.3v no other parts such as spi ram or al422. Edit: As of 2014-07-05 I have decided to try and improve this initial post. I would like to hear your feedback. I want to try and avoid redundant questions and I think a better writeup in the initial post would be beneficial. Tell me what you think about this post. When creating this post on the 2013-04-09 I would have never imagined that there would be so much interest in the ov7670. I am happy that I have enabled so many people to explore the world of image processing introducing people to a valuable and fun concept. What is the ov7670? Although I think most people who click on this thread know what the ov7670 is, I think it is still good to have an explanation for the people curious about what all the commotion is. The ov7670 is an image sensor manufactured by Omnivision. This image sensor is quite old by image sensor standards. According to Omnivision (http://www.ovt.com/uploads/newsreleases/english/OV7670 %20%20OV9655%20in%20Mass%20Production_release%20FINAL.pdf) they started mass production of the sensor in February of 2006. The image sensor has a resolution of 640x480 and is an Soc meaning it has built in image processing capabilities. Why the ov7670? I am quite surprised that out of all the sensors available this one has the most success. I like to play with image sensors of all types not just the ov7670 however the ov7670 seems to spark the most interest in people. I bought the module because at the time it was (and still is) the lowest cost camera module that you can buy. The quality you can get from it would be be described as just okay. There are nicer modules you can buy for not much more money if you are looking for quality. What can I do with the ov7670? The reality is you are limited not only by the sensor but by the processor you are using. This thread focuses primarily on arduino boards and interfacing the ov7670 to microcontrollers featured on an arduino board. The arduinos that I have used are both based on 8bit AVRs running at 16mhz so that is quite the limitation. I do however welcome discussion of any microcontroller. Do not expect something ridiculous like 30fps 640x480 on the arduino uno. The original reason that I wanted to use the ov7670 was time lapse purposes. I could live with the slow speed as all I need was an image every few seconds. However using a faster microcontroller can mean reading pixels faster but remember unoptimized code on a fast microcontroller can be slower than well optimized code on a slower device. So with all that said: What can I actually do with this sensor? The answer is pretty much anything that does not involve subjecting the sensor to unreasonable conditions or expecting something unreasonable from it like 1000fps full resolution. What attracts me to the idea of a camera module vs just buying a webcam is the fact that I have more control over it. You are able to program it to do various tasks that suite your needs and lots of the automatic stuff on the ov7670 can be overridden if needed. How can I get started? Read the documentation and check out code that I have written. Also check the code to figure out some aspects of wiring. I have already explained many aspects of wiring. Just read the thread. Where can I get documentation? http://www.electronicaestudio.com/docs/sht001.pdf https://github.com/dalmirdasilva/ArduinoCamera/blob/master/CameraAL422B/datasheet/OV7670%20Implementation%20Guide%20%28V1.0%29.pdf https://github.com/luckasfb/Development_Documents/blob/master/MTK-Mediatek-Alps-Documents/OV7670%20software%20application%20note.pdf I however would like to expand this list. If you have ANYTHING from Omnivsion that would help me understand undocumented stuff in regards to the ov7670 I will take it. If you cannot or do not want it to be posted on the internet I will respect that wish fully. I will take anything regarding the ov7670 even a newer version of what I have already. In fact I would take unrelated documentation that comes from Omnivsion. It is nice to have. You never know seemly unrelated documentation might prove to be relevant in non direct ways. Where do you keep up to date code Here https://github.com/ComputerNerd/ov7670-simple/ and here https://github.com/ComputerNerd/arduino-camera-tft also if you don't have any other parts besides the ov7670 check out https://github.com /ComputerNerd/ov7670-no-ram-arduino-uno it also contains how to enable pwm to get 8mhz. I also made a data convert that allows you to convert raw data from the camera into a png file https://github.com/ComputerNerd/RawCamera-data-converter For windows users who don't want to bother setting up MinGW here is a windows binary. https://github.com/ComputerNerd/RawCamera-data-converter/blob/master/convert.exe.7z I also posted some pastebin links that is old code but it covers some stuff that the code above does not How I can I help the author? Responding to you all guys does not happen magically. There are some things you can do to help ensure that I will continue to help you. 1. Give back: Know something that I don't know regarding the ov7670? Post it. 2. Avoid asking repeat questions. 3. Write stuff. Maybe you can explain something better than I can. If that is the case please do such. I am always open to suggestions on how I can improving my writing. The initial post below the line break is the second revision of my initial post. I had to move the first revision down a post to work around a character limit. Edit: I now recommend the fifo version over the non-fifo version. However if you have the regular version without the fifo you can still get an image using external spi ram. If you have the fifo version lots of this is still relevant all except the external ram part and reading pixels. The reason for using the fifo version is because of faster readout and you can capture a 640x480 images without breaking it up into pieces however to do such you must use raw bayer data. Also I have since figured out how to generate an 8mhz clock using PWM Using PWM to generate XCLK has several advantages including: You don't need a special programmer to set the fuse bits. You can change the output clock speed the fastest it goes is F_CPU/2 which results in 8mhz in the case of the arduino. However it is best to set it as max speed and use the divider that the ov7670 has. Also a good way to keep filesize down thus improving transfer speed is to use raw bayer data. Raw bayer appears to be about the same or better quality as yuv422 but takes half the speed thus doubling transfer speed. I would recommend you use it instead of yuv422 I have posted new versions that use raw bayer data. Here is an FAQ of some questions that may come up: Q: Some arduino outputs a 5v clock but the OV7670 will only accept 3.3v what should I do A: use a buffer of some sort it just needs to be fast enough to pass the 8mhz signal. Q: Will the ov7670 accept twi/sccb/i2c (same thing different names) commands without XCLK? A: No Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa... 1 of 96 01/19/2015 08:49 PM

Upload: thiago-cavalcante

Post on 15-Sep-2015

516 views

Category:

Documents


68 download

DESCRIPTION

Arduino Forum post

TRANSCRIPT

  • Arduino Forum

    Using Arduino => Sensors => Topic started by: Mr_arduino on Apr 10, 2013, 04:50 am

    Title: ov7670 with both arduino uno and now mega

    Post by: Mr_arduino on Apr 10, 2013, 04:50 am

    Have you been told by someone that you an external ram of some sort such as a fifo or spi ram. I admit to thinking that was needed and telling people they need one or the other (fifo being the better option) but

    if you just want to send data from the camera to your PC using the arduino you don't need external ram.

    Check out this https://github.com/ComputerNerd/ov7670-no-ram-arduino-uno

    It requires only the ov7670 without fifo and a way to get 5v to 3.3v no other parts such as spi ram or al422.

    Edit: As of 2014-07-05 I have decided to try and improve this initial post. I would like to hear your feedback. I want to try and avoid redundant questions and I think a better writeup in the initial post would be

    beneficial. Tell me what you think about this post.

    When creating this post on the 2013-04-09 I would have never imagined that there would be so much interest in the ov7670.

    I am happy that I have enabled so many people to explore the world of image processing introducing people to a valuable and fun concept.

    What is the ov7670?

    Although I think most people who click on this thread know what the ov7670 is, I think it is still good to have an explanation for the people curious about what all the commotion is.

    The ov7670 is an image sensor manufactured by Omnivision.

    This image sensor is quite old by image sensor standards. According to Omnivision (http://www.ovt.com/uploads/newsreleases/english/OV7670

    %20%20OV9655%20in%20Mass%20Production_release%20FINAL.pdf) they started mass production of the sensor in February of 2006.

    The image sensor has a resolution of 640x480 and is an Soc meaning it has built in image processing capabilities.

    Why the ov7670?

    I am quite surprised that out of all the sensors available this one has the most success.

    I like to play with image sensors of all types not just the ov7670 however the ov7670 seems to spark the most interest in people.

    I bought the module because at the time it was (and still is) the lowest cost camera module that you can buy.

    The quality you can get from it would be be described as just okay. There are nicer modules you can buy for not much more money if you are looking for quality.

    What can I do with the ov7670?

    The reality is you are limited not only by the sensor but by the processor you are using.

    This thread focuses primarily on arduino boards and interfacing the ov7670 to microcontrollers featured on an arduino board. The arduinos that I have used are both based on 8bit AVRs running at 16mhz so that

    is quite the limitation. I do however welcome discussion of any microcontroller. Do not expect something ridiculous like 30fps 640x480 on the arduino uno.

    The original reason that I wanted to use the ov7670 was time lapse purposes.

    I could live with the slow speed as all I need was an image every few seconds. However using a faster microcontroller can mean reading pixels faster but remember unoptimized code on a fast microcontroller

    can be slower than well optimized code on a slower device.

    So with all that said: What can I actually do with this sensor? The answer is pretty much anything that does not involve subjecting the sensor to unreasonable conditions or expecting something unreasonable

    from it like 1000fps full resolution. What attracts me to the idea of a camera module vs just buying a webcam is the fact that I have more control over it. You are able to program it to do various tasks that suite

    your needs and lots of the automatic stuff on the ov7670 can be overridden if needed.

    How can I get started?

    Read the documentation and check out code that I have written. Also check the code to figure out some aspects of wiring. I have already explained many aspects of wiring. Just read the thread.

    Where can I get documentation?

    http://www.electronicaestudio.com/docs/sht001.pdf

    https://github.com/dalmirdasilva/ArduinoCamera/blob/master/CameraAL422B/datasheet/OV7670%20Implementation%20Guide%20%28V1.0%29.pdf

    https://github.com/luckasfb/Development_Documents/blob/master/MTK-Mediatek-Alps-Documents/OV7670%20software%20application%20note.pdf

    I however would like to expand this list. If you have ANYTHING from Omnivsion that would help me understand undocumented stuff in regards to the ov7670 I will take it. If you cannot or do not want it to be

    posted on the internet I will respect that wish fully. I will take anything regarding the ov7670 even a newer version of what I have already. In fact I would take unrelated documentation that comes from Omnivsion.

    It is nice to have. You never know seemly unrelated documentation might prove to be relevant in non direct ways.

    Where do you keep up to date code

    Here https://github.com/ComputerNerd/ov7670-simple/ and here https://github.com/ComputerNerd/arduino-camera-tft also if you don't have any other parts besides the ov7670 check out https://github.com

    /ComputerNerd/ov7670-no-ram-arduino-uno it also contains how to enable pwm to get 8mhz.

    I also made a data convert that allows you to convert raw data from the camera into a png file

    https://github.com/ComputerNerd/RawCamera-data-converter

    For windows users who don't want to bother setting up MinGW here is a windows binary.

    https://github.com/ComputerNerd/RawCamera-data-converter/blob/master/convert.exe.7z

    I also posted some pastebin links that is old code but it covers some stuff that the code above does not

    How I can I help the author?

    Responding to you all guys does not happen magically. There are some things you can do to help ensure that I will continue to help you.

    1. Give back: Know something that I don't know regarding the ov7670? Post it.

    2. Avoid asking repeat questions.

    3. Write stuff. Maybe you can explain something better than I can. If that is the case please do such. I am always open to suggestions on how I can improving my writing.

    The initial post below the line break is the second revision of my initial post. I had to move the first revision down a post to work around a character limit.

    Edit: I now recommend the fifo version over the non-fifo version. However if you have the regular version without the fifo you can still get an image using external spi ram.

    If you have the fifo version lots of this is still relevant all except the external ram part and reading pixels.

    The reason for using the fifo version is because of faster readout and you can capture a 640x480 images without breaking it up into pieces however to do such you must use raw bayer data.

    Also I have since figured out how to generate an 8mhz clock using PWM

    Using PWM to generate XCLK has several advantages including:

    You don't need a special programmer to set the fuse bits.

    You can change the output clock speed the fastest it goes is F_CPU/2 which results in 8mhz in the case of the arduino. However it is best to set it as max speed and use the divider that the ov7670 has.

    Also a good way to keep filesize down thus improving transfer speed is to use raw bayer data.

    Raw bayer appears to be about the same or better quality as yuv422 but takes half the speed thus doubling transfer speed.

    I would recommend you use it instead of yuv422 I have posted new versions that use raw bayer data.

    Here is an FAQ of some questions that may come up:

    Q: Some arduino outputs a 5v clock but the OV7670 will only accept 3.3v what should I do

    A: use a buffer of some sort it just needs to be fast enough to pass the 8mhz signal.

    Q: Will the ov7670 accept twi/sccb/i2c (same thing different names) commands without XCLK?

    A: No

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    1 of 96 01/19/2015 08:49 PM

  • Q: Will I need to buffer any other pins besides the XCLK?

    A: No but be sure you have the i2c/twi/sccb lines pulled up to 3.3v instead of 5v or you risk damage to the ov7670

    Also don't set any of the pins that go to the ov7670 as output.

    Q: where can I get current registers and useful functions for the ov7670?

    A: On my github page https://github.com/ComputerNerd/arduino-camera-tft

    More specifically here is some stat-up registers here https://github.com/ComputerNerd/arduino-camera-tft/blob/master/ov7670_regs.h

    And here are some useful functions here https://github.com/ComputerNerd/arduino-camera-tft/blob/master/twicam.c also don't forget the header file https://github.com/ComputerNerd/arduino-camera-tft/blob

    /master/twicam.h

    And you will notice that I have vga qvga rgb565 yuv422 defined somewhere else that is here https://github.com/ComputerNerd/arduino-camera-tft/blob/master/config.h

    Q: the code you posted runs on the arduino mega but I have an arduino uno what can I do?

    A; The functions that setup the camera will also work on the arduino uno and I have posted some code for the arduino uno see the pastebin links below or my github page ov7670-simple (link below)

    Q: The code you posted (above) is complex give me something more simple

    A: For a while I had this code available I just thought I would make it more obvious that it exists. https://github.com/ComputerNerd/ov7670-simple

    Q: The FAQ does not answer my question what should I do

    A: try reading through this topic your question might be answered if not post a reply I'd rather that you not send a pm because someone else may have the same question that you do later on.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: nonzung on Apr 11, 2013, 07:21 am

    Hello, I would like to see your connection in detail. I want the camera to be used in image processing by arduino.

    Thanks for answer and sorry for my English. Nice job.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 12, 2013, 01:30 am

    Edit I had to move down the first revision of my inital post to here to work ardound a character limition below that I explained how I conntect the ov7670 to the arduino uno.

    Here is the my old original post below kept for historical reasons:

    I have recently been working with the ov7670 unfortunately I made the mistake of cheeping out and buying the cheaper one without the fifo. At first I though it would be a challenge to get this to work but it turns

    out not to be that hard at all. The problems: the arudino does not have enough ram and is slow additionally it runs on 5v when both the ov7670 and the sd card need 3.3v. The solutions. Break the image into

    smaller pieces and transfer the smaller pieces into spi ram in this case I have the 23LC1024 128kb of ram that means if I want a vga image 640*480*2 bytes per pixel. I must divide it into 5 equal parts. So I

    transfer part of the image and save that part into the sd card and do this for a total of 5 times each time skipping different regions and saving others. The sensor can output both rgb565 or yuv422 I found that

    yuv422 is better quality so I made a converter for that and you can get the source code here https://github.com/ComputerNerd/RawCamera-data-converter. Here is code for the capture program.

    http://pastebin.com/1nnRc5qL I used sdfatlib to write and based my code off of his fastlogger code. Note that by defining useLed using #define useLed will assume you have an led on pin 0 and a button on pin 1

    to stop the capture hold down the button and wait for the led to blink very fast make sure that the button sinks the pin to ground. If you do not define useLed then it uses serial to start stop image send one letter

    to the arduino and it will save 1 image you can send more for more images. Also Note that I found some registers online somewhere and I could not find documentation on those registers. The only ov7670

    datasheet that I could find online does not explain all the registers it gives the name but the description is blank. If someone could explain with a reply or PM what these:

    Code: [Select]

    wrReg(AWBC7,0x88); wrReg(AWBC8,0x88); wrReg(AWBC9,0x44); wrReg(AWBC10,0x67); wrReg(AWBC11,0x49); wrReg(AWBC12,0x0e);

    wrReg(REG_GFIX,0x00);//wrReg(GGAIN,0x40);

    wrReg(AWBCTR3,0x0a); wrReg(AWBCTR2,0x55); wrReg(AWBCTR1,0x11); wrReg(AWBCTR0,0x9f);

    wrReg(0xb0,0x84);//not sure what this doesregisters do that would be awesome.

    To solve the 5v issue I use a buffer. To solve the fact that the arduino is slow I set the divider to 9 also I am getting the clock with the CLKOUT pin you will have to change your fuse settings for that. Also for twi

    communication the ov7670 is picky about what pullup resistor values you use I found 4.7k and 10k to work. I tried 1k and 2.2k and those do NOT work. You will need twi to write to the registers the image out is

    parallel. Here are some images taken with the ov7670.

    (http://img812.imageshack.us/img812/696/v5w6.png)

    (http://img51.imageshack.us/img51/7659/blinds.png)

    Here is the arduino uno itself. Image is very large click here to view http://imageshack.com/a/img849/4632/zuvj.jpg

    Below here lies my response (the original aspect of the post).

    The connections are simple. Start by plugging in SIO_C to A5 and SIO_D to A4 since this is i2c/twi you must have a 4k7 resistor 10k also works. Make sure you have the resistor to pull up 3.3v not 5v. Also edit

    twi.c in the arduino ide folder/libraries/Wire/utility search for these lines and comment them out or remove them

    Code: [Select]

    // activate internal pullups for twi. digitalWrite(SDA, 1); digitalWrite(SCL, 1);

    This is to remove the internel pullup resistor by not removing these 2 lines you could cause damage to the sensor. Then plug d7 to digital pin 7 d6 to 6 d5 to 5 d4 to 4 d3 to A3 d2 to A2 d1 to A1 and d0 to A0 now

    plug in VSYNC to digital pin 3 and PCLK to pin 2. Now change the fuse bits to be exactly the same except enable the CLKOUT pin. This will output a clock run this through a buffer or level shifter to 3.3v if you do

    not get the 5v signal down to 3.3v it could damage the sensor. The sd card and spi ram is just standard spi wiring nothing special.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: nonzung on Apr 12, 2013, 07:10 am

    Thanks for the answer, I will try. :) :)

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 12, 2013, 10:06 pm

    I wish you the best of luck if anything does not work right just reply and I will help you.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: michinyon on Apr 13, 2013, 07:57 am

    Could you post a photo of your device sometime ? I got one of these and the connections were so tiny that

    I could not see then with a magnifying glass and certainly not actually connect anything to them.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Saren on Apr 14, 2013, 12:20 am

    Hi,

    I'm working on a project at university about computer vision and Arduino.

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    2 of 96 01/19/2015 08:49 PM

  • I'm trying to capture an image using Arduino Uno and OV7670 camera without FIFO. Other components like SD card, LCD, additional RAM, shields, etc. are unfortunately not available at this time (I've read

    some tutorials about this, but they were using special shields or some stuff I don't have or camera with FIFO...).

    Is it possible to directly communicate with camera from Arduino and get some images? It doesn't have to be fast, the point is to get some results.

    Since I'm just a beginner with Arduino, I'm a little bit confused about how to connect my camera to it, when I don't use RAM that was mentioned above. I really don't want to damage it...

    Could you please describe it or provide some connection schema for a case like mine? Could you please also give me some advice how to change the code for the capture program?

    Any help is appreciated.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 14, 2013, 03:03 am

    michinyon I bought an ov7670 camera module that means some of the pins are routed to a 2.54mm pitch header I think you have bought just the sensor. I am busy right now I will take a picture soon.

    Saren it is possible I will have to get back with you on this one though. I am busy right now and don't have any code written right now for using no ram. I do however have a program that is compiled with avr-gcc

    that sends the image to a frame grabber program that I wrote.

    Here is the sender program it is in 3 files main.c ov7670.h ov7670.c

    main.c:

    http://pastebin.com/dF3pc23T

    I will warn you that I made some changes to the code but forgot to change the comments like the captureImg function says it missed a byte but now it does not I posted it to paste bin before realising this.

    ov7670.h

    http://pastebin.com/CqYTwNqn

    ov7670.c

    http://pastebin.com/T1nN5Ni4

    Here is the frame grabber source code

    http://pastebin.com/dxMJhZTq

    This one is only one file. If on Gnu/Linux you will need to install the development package of SDL or if gentoo just emerge sdl. If on windows get the development library from http://www.libsdl.org/download-

    1.2.php

    Also I made an update to my program that captures an image to the sd card it nows saves raw bayer data.

    http://pastebin.com/zV0ahzPv

    The link is different than the previous code that I posted.

    Here is a program to debayer/demosaic the data it is still in very beta needs work

    It will also convert yuv422 to rgb (the old image to sd card program saved data in that format)

    Code: [Select]

    //to compile type gcc -Wall -Wextra -lm -lpng -o yuv main.c#include #include #include #include #include #include #include #define img_w 640#define img_w_2 1280#define img_h 480#define CLIP(X) ( (X) > 255 ? 255 : (X) < 0 ? 0 : X)#define C(Y) ( (Y) - 16 )#define D(U) ( (U) - 128 )#define E(V) ( (V) - 128 )#define YUV2R(Y, U, V) CLIP(( 298 * C(Y) + 409 * E(V) + 128) >> 8)#define YUV2G(Y, U, V) CLIP(( 298 * C(Y) - 100 * D(U) - 208 * E(V) + 128) >> 8)#define YUV2B(Y, U, V) CLIP(( 298 * C(Y) + 516 * D(U) + 128) >> 8)char buf[1024];void showHelp(){

    puts("Yuv422 raw image data to png");puts("-n x replace x with the image number you want to convert");puts("-h shows this help file");puts("-o x replace x with a real integer between 0 and 7 this sets the offset");

    }int savePNG(char * fileName,uint32_t width,uint32_t height,void * ptr){

    //saves a 24bit png with rgb byte orderpng_byte * dat=ptr;//convert to uint8_t//png_byte **row_pointers = malloc(height*sizeof(png_byte));//if (row_pointers==0)// return 1;FILE * fp=fopen(fileName,"wb");if (fp==0)

    return 1;png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)0,0,0);if (!png_ptr)

    return 1;png_infop info_ptr = png_create_info_struct(png_ptr);if (!info_ptr){

    png_destroy_write_struct(&png_ptr,(png_infopp)NULL);return 1;

    }if (setjmp(png_jmpbuf(png_ptr))){

    png_destroy_write_struct(&png_ptr, &info_ptr);fclose(fp);return 1;

    }png_init_io(png_ptr, fp);png_set_IHDR(png_ptr, info_ptr, width, height,8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);//must be called before other png_set_*() functions//png_set_compression_level(png_ptr,Z_BEST_COMPRESSION);//since we are doing timelapses we may be dealing with many files it is good to keep disk space usage downuint32_t y;//for (y=0;y

  • uint32_t x,y;for (y=0;y
  • continue;}if (strcmp(argv[arg],"-o") == 0){

    arg++;offset=atoi(argv[arg]);if (offset>img_w){

    printf("you must specify a number between 0 and %d for argument -o\n",img_w);showHelp();return 1;

    }continue;

    }if (strcmp(argv[arg],"-h") == 0){

    showHelp();continue;

    }}

    }uint8_t * Dat;//in case some of the file was not saved we use calloc instead of malloc to garentte that the unsaved pixels are set to 0if (debayer!=0)

    Dat = calloc(img_w*img_h+img_w,1);else

    Dat = calloc(img_w*img_h+img_w,2);uint8_t * outImg = malloc(img_w*img_h*3);//all bytes in the array will be overwritten no need for callocif (useNum){

    processImg(Dat,outImg,useImg,debayer,offset);}else{

    uint32_t imgC;for (;;imgC++){

    printf("Saving image %d\n",imgC);if (processImg(Dat,outImg,imgC,debayer,offset))

    return 1;}

    }free(Dat);free(outImg);return 0;

    }

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: nonzung on Apr 16, 2013, 07:49 am

    I forget that. I bought the camera ov7670 fifo.

    Sorry and thank you so much.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 16, 2013, 11:53 pm

    Quote from: nonzung on Apr 16, 2013, 07:49 am

    I forget that. I bought the camera ov7670 fifo.

    Sorry and thank you so much.

    Smart choice the one with the fifo is much better I wish I would have bought it but I bought the one without the fifo.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Saren on Apr 23, 2013, 07:01 pm

    Mr_arduino: I didn't have much time lately, but now I'm going to try it. I have some questions about connection:

    1. You mentioned that it's necessary to get the 5V signal down to 3.3V. Is it possible to do this without level shifter/buffer or do I have to get this stuff?

    2. Are connections the same (as in one of your posts) even in my case (without RAM, etc)?

    Thanks for your effort to help. I really appreciate it :)

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 26, 2013, 10:32 pm

    The connections will be mostly the same but it could be abit different as I do not own the one with the fifo I am not sure but I think instead of PCLK you use RCLK and and RCLK is output you strobe that to get

    data. Check you this for more info about the fifo version http://wiki.beyondlogic.org/index.php/OV7670_Camera_Module_with_AL422_FIFO_Theory_of_Operation Also you could try a resistor divider but I have

    heard that they are too slow you may need a buffer/level-shiftier. Also this would not require the ram as you have the fifo.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 27, 2013, 04:28 pm

    A recent discovery that I made is you can get an F_CPU/2 (8mhz in the arduino's case) clock output with pwm this removes the need to edit fuse bits. Note that this is for the arduino mega 2560 I switched to

    from the uno to the mega recently. This will work with the arduino uno also just change the timer number.

    Code: [Select]

    DDRL|=8; ASSR &= ~(_BV(EXCLK) | _BV(AS2)); //generate 8mhz clockTCCR5A =67;TCCR5B=17;

    Also change DDRL to proper port and pin or be lazy and replace with pinMode();

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: emueyes on Apr 28, 2013, 02:24 am

    There's a good page on generating clock signals at http://hekilledmywire.wordpress.com/2011/05/28/introduction-to-timers-tutorial-part-6/#more-57

    I too have the question about level shifting; what sort of buffers are needed, would a 74HC245 do?

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Apr 28, 2013, 08:18 pm

    I think the 74HC245 would work just fine also a unidirectional one would work too. Pretty much any buffer that is fast enough to handle a 16mhz signal and can accept 3.3v as a supply voltage which the

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    5 of 96 01/19/2015 08:49 PM

  • 74HC245 can. You do not need to shift the d7-d0 as those are always output from the sensor and never input. Only the clock signal needs to go though the buffer as that is an input and the sd card if you are

    using it. From twi just disable internal pullup resistors and have them to 3.3v instead of 5v. Also I think I may have forgot to mention that RESET needs to be tided to 3.3v and PWDN to ground or you could use

    more GPIO pins if you need to use the PWDN and RESET features although a reset can also be triggered by writing to a register so driving that pin with the arduino has little purpose.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: emueyes on Apr 29, 2013, 09:42 am

    Thanks, that's very helpful. I'd thought that the D0..D7 signals would need shifting and so lots of converters would be needed, but that's simplified things a lot. Still waiting for my camera to turn up.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Saren on Apr 30, 2013, 08:54 pm

    Quote from: Mr_arduino on Apr 26, 2013, 10:32 pm

    The connections will be mostly the same but it could be abit different as I do not own the one with the fifo I am not sure but I think instead of PCLK you use RCLK and and RCLK is output you strobe that to get data. Check you this for more info about the fifo version

    http://wiki.beyondlogic.org/index.php/OV7670_Camera_Module_with_AL422_FIFO_Theory_of_Operation Also you could try a resistor divider but I have heard that they are too slow you may need a buffer/level-shiftier. Also this would not require the ram as you have the

    fifo.

    I think there has been a misunderstanding. My camera is without fifo...

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on May 01, 2013, 02:39 am

    Quote from: Saren on Apr 30, 2013, 08:54 pm

    I think there has been a misunderstanding. My camera is without fifo...

    So is mine so will need the same connections as I use and the external sram. Also the reason d0-d7 not need shifting is the arduino will never drive those pins so it is up to you to make sure that those pins are

    always set to input on the arduino.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: sherry254 on May 11, 2013, 02:30 pm

    Hi, i have the ov7670 camera, an arduino uno rev 3 and ST M27C512-12F1 Eprom. Is it possible to take pictures with these components? and if so, how do i connect them together. i have seen that you used the

    spi ram, can i replace that with the eprom??

    Please assist me on how to connect the arduino, camera and the eprom together to give me a picture.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on May 11, 2013, 03:43 pm

    This is the old school eprom with UV erase. There are several reason why this would not work. First of all how are you going to erase it in a reasonable time. Even if you had a UV led driven by the arduino it

    would still take awhile to erase and UV is bad for your eyes so you would need to cover it with something. Also you would not have enough pins are the eprom is parallel. Also how fast are the write speed will it

    even accept data fast enough. Another problem is that you would wear it out fast. Eproms only have a certain amount of writes (They do have unlimited reads though). Also this Eprom has only 64kb of memory

    you would have to break the frame up even more which would reduce performance even more. In short the Eprom would not work for this purpose save it for repairing a bios or make your own NES cartridge or

    do something fun with it but not in a camera. Just buy or sample the 23LC1024 it is kinda cheap at $2.59 if you only order one at digi-key and you will not regret it. It is even in dip package. The dip package one

    is called 23LC1024-I/P by the way

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: sherry254 on May 11, 2013, 05:28 pm

    Clearly getting the eprom was a wrong move. My other option(or idea, but might not be a very good one) is to connect the camera to a SIM5216E 3g shield. the datasheet indicates that its possible to connect the

    camera pins on the shield but the pins are marked 'NC'.

    this is the shield i have ftp://imall.iteadstudio.com/Shield/IM121026002/DS_IM121026002_3G_Shield.pdf

    this is its schematic ftp://imall.iteadstudio.com/Shield/IM121026002/SCH_IM121026002_3G_Shield.pdf

    Could i connect the camera to the shield instead of using the arduino directly?

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: hlankoande on May 12, 2013, 12:10 am

    Hello Mr_arduino

    I have planed to use the OV7670 camera module to capture picture with arduino mega 2560 R3. I m using it to drive robot and get few data from robot.

    I m using arduino programming IDE to write code.

    The code you gave is compiled with another compiler and as I do not have deep kwnowledge on this it is liltle bit hard for me.

    Is it possible to give us(I guess I m not alone in that case) a hello world example to capture picture and store it in an array( in order to save it in SD card or send it other serial) written with arduino IDE.

    Thanks lot in advance.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: isaiful on May 17, 2013, 05:36 am

    hye...i wanna ask if this camera is same with JPEG TTL CMOS camera???

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on May 18, 2013, 12:59 am

    @hlankoande

    I made some code for the arduino mega awhile ago check it out it uses the arduino ide

    http://www.youtube.com/watch?v=sEsZeDC6co0

    @isaiful

    It is no were near the jpeg TTL camera. The Jpeg TTL CMOS camera is way easier to use but cost more money.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: rebelion on May 22, 2013, 12:20 pm

    Hello

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    6 of 96 01/19/2015 08:49 PM

  • I have the same problem with OV7670 v2 + Mega R3, a lack of experience stops me to get those things work together..

    Mr_arduino it would be really great if could share the code and wiring with us?

    Thank you.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on May 24, 2013, 01:27 pm

    Quote from: Mr_arduino on Apr 28, 2013, 08:18 pm

    I think the 74HC245 would work just fine also a unidirectional one would work too. Pretty much any buffer that is fast enough to handle a 16mhz signal and can accept 3.3v as a supply voltage which the 74HC245 can. ... Only the clock signal needs to go though the buffer

    as that is an input and the sd card if you are using it. From twi just disable internal pullup resistors and have them to 3.3v instead of 5v...

    Hello, I'm trying too to get the ov7670 communicate with Arduino UNO.

    I read this post a week ago, I bought some 74HC245 but I realized they are not fully bidirectional, you have to choose the direction using a pin. So they can't be used with SIO_D and SIO_C, as far as I know. As

    I'm reading this post again, I guess A5 and A4 pins, using the code supplied, will output 3.3V instead of 5V (assuming internal pullup is OFF). Am I right?

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on May 25, 2013, 06:24 pm

    It said in the post it does not matter if they are bidirectional or not. Also you do not need a buffer for i2c if you disable the internal pull-up and make sure that you have the pull-up resistor to 3.3v.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on May 26, 2013, 03:44 pm

    Mr_Arduino, thank you for your response.

    I managed to connect SIO_C and SIO_D, I also changed atmega fuses to get the CLKOUT at pin 8. Yet, I don't have extra RAM chip and right now the only thing I want to do is checking if the ov7670 is

    responding.

    What's the simplest way to know? Let's say using and oscilloscope.

    Is it possible to capture an image from the sensor right into the SD (without a RAM)?

    Thank you again!

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on May 29, 2013, 02:07 am

    First verify that CLKOUT is correct connected to the buffer and the output from the buffer does not exceed 3.3v and is correctly connected to XCLK then verify that with an osciloscope (you should see a square

    wave) after that see if SIO_C and SIO_D work it may just be a quick burst but that is normal. Then see if you get a most square wave from PCLK there should be a very brief burst where PCLK stops every so

    often. This is because by default the ov7670 outputs unwanted dummy pixels that serve no good we don't want those dummy pixels. If PCLK never stops it may not be communicating properly but the part where

    it stops may be too fast to see I can not remember off the top of my head.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 01, 2013, 01:21 am

    Thank you!

    I'm doing better now. Yet it isn't working. I had a communication problem between Arduino and the sensor. Now that's working and I'm able to configure the sensor. I'm having a somewhat coherent reading. I

    read 512 bytes blocks from the sensor, but I get blocks of zeroes every two or three blocks.

    I'm configuring a 3MHz PCLK but I guess it's too fast for arduino. What do you think?

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 01, 2013, 04:48 pm

    I set the divider to 4 see this code wrReg(0x11,4);//using pre-scaler for divider Also what you can do to verify communication is increment the divider and wait awhile and increment again in a loop and see if

    the OUTPUT PCLK changes on an oscilopsoce. Also you MUST have this register

    Code: [Select]

    wrReg(0x15,32);//pclk does not toggle on HBLANK COM10 vsync falling

    That may another reason why you are getting invalid data. If you don't have it just use all registers from my sample code would be best.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 16, 2013, 05:47 pm

    Ok, I have been working and I see some advances now. Yet I'm not done.

    I've incremented the prescaler and I realized there is a point (as I incremente PCLK frequency) where I start missing bytes. So I'm using the slower PCLK possible now. I have got a 23LC1024 chip and removed

    the SD card. My test program reads 50 lines of 640 from the sensor and puts the in the chip. Then dumps it in base64 to the PC through the USB. I'm setting the sensor to output a color bar pattern.

    Here is my YUV output for that image's portion. And what I see when I convert it using ffmpeg

    Code: [Select]

    $ ./ffmpeg -f rawvideo -s 640*50 -pix_fmt yuvj422p -i frame0.yuv -f image2 -vcodec png img.png

    I think I may be getting valid data from the sensor, and my problem could be in the ffmpeg conversion. But I'm not sure how to check that.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 17, 2013, 04:51 pm

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    7 of 96 01/19/2015 08:49 PM

  • Good job the only thing is that you have the sensor configured for raw bayer data. I attached the image I got.

    Here is the source code to the program that converts the image it does both yuv422 and raw bayer data and saves it to a png file. https://github.com/ComputerNerd/RawCamera-data-converter/blob/master

    /main.c

    To compile it simply type

    Code: [Select]

    gcc -Wall -Wextra -lm -lpng -o yuv main.c

    As you may have noticed you need libpng installed.

    The only thing is that the file name must be called F(x).YUV replace (x) with a number this was because I made it for time lapses originally and need to may pictures at once. If you don't specify which number x

    is when running the program it will start from 0 and count up until a file is not found note that if you have F0.YUV and F2.YUV it will stop at F0.YUV as F1.YUV does not exist so in this case if you wanted to

    convert F2.YUV you would need to use the -n x argument so in this case do ./yuv -n 2 to convert it. Also speaking of missing a byte glitch I found out that sometimes at the beginning of capture a byte would be

    missed I fixed this by waiting for PCLK to go low read the pixel then wait for it to go high and then loop. I previously had it backwards.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 18, 2013, 12:40 am

    Wow... thank you.

    I thought I was configuring the sensor for YUV output. After reading your reply I went to check that. I think I'm misreading the documentation or the document is wrong. Because I can't manage to change the

    output.

    What I'm doing now:

    Code: [Select]

    // Max prescaler divider ovcam.toggle(REG_CLKRC, NONE, CLKRC_PRESCALER_MASK & 31); ovcam.toggle(REG_DBLV, NONE, DBLV_PLL_BYPASS);

    // No PCLK during HBLANK ovcam.toggle(REG_COM10, NONE, COM10_PCLK_NO_TOGGLE);

    // Set up camera ovcam.write(REG_COM7, 0x00); // YUV ovcam.write(REG_COM15, 0xC0); // Set normal rgb with Full range ovcam.toggle(REG_ABLC1, NONE, ABLC_ENABLE); // Auto black level

    ovcam.write(REG_COM13, 0x88); ovcam.write(REG_TSLB, 0x05);

    The problem is no matter what I put in COM7, COM13 and TSLB. I can't manage to change to YUV. I have a PDF dated "Version 1.3, April 5, 2006" but I see some registers do not act according to the

    configuration. Do you know where is the last document for this sensor?

    Anyway, I guess I can live with RAW Bayer data by now.

    What is the clock frequency you are sending to the module?

    What is the max PCLK frequency you are able to read from Ardunino UNO or Mega?

    Is you conversion program discarding color information? Or is it me doing something wrong?

    By the way: I just noticed I had a byte in zero at the start. I fixed it waiting for the high state of the HREF signal, before starting to read the line. But then, as I change from bar pattern to the image, I start getting

    zeroes again. :/

    Thank you very much!

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 18, 2013, 01:49 am

    Here is a picture taken in three stages of 640x100 pixels:

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 18, 2013, 03:05 am

    Ok I think you were configuring the output for yuv however here is what I use for yuv also I am having the same problem with raw bayer data it lacks color. Yuv is much more colorful Here is what I use for

    yuv422. Also the documentation for the ov7670 is very bad don't blame yourself. Although here is a newer version 1.4 that may help abit http://www.haoyuelectronics.com/Attachment

    /OV7670%20+%20AL422B%28FIFO%29%20Camera%20Module%28V2.0%29/OV7670%20datasheet%28V1.4%29.pdf

    If you noticed in the changelog of the 1.4 from 1.3 they changed the TSLB and COM13 documentation.

    Code: [Select]

    wrReg(0xb0,0x84);//Undocumented but writing this fixes colors I do not know whywrReg(REG_COM7, 0x00); // YUVwrReg(REG_COM15, 0xC0); //Full rangewrReg(REG_TSLB,0x04); // 0D = UYVY 04 = YUYV wrReg(REG_COM13,0xC8); // connect to REG_TSLB

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 18, 2013, 03:39 am

    I keep getting the RAW data. I will keep trying tomorrow morning (it's 11:40PM here). Thank you again.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 18, 2013, 04:04 am

    Also did you look at the linux driver it has a complete register configuration which has parts based on official registers provided by omnivison http://dev.openaos.org/browser/trunk/buildroot/gen7/linux/drivers

    /media/video/ov7670.c

    Also at the bottom of the page click download as plain text to copy paste without the line numbers. And with my program are you telling it to yuv422 conversion try passing the argument '-c y' without the quotes.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 18, 2013, 02:33 pm

    I'm going to check that link with the registers.

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    8 of 96 01/19/2015 08:49 PM

  • I tried your program with "-c y" and "-c d". I only get a reasonable image with D (debayer). But I don't like having something like this sensor, and not be able to configure it correctly.

    As a mater of fact, RAW bayer could be the best choice for my project. I don't need color information and I see bayer format is one byte per pixel, which takes half space and time.

    Have you tried the FIFO version of this module? I'm going to buy one, because I will save a lot of space if the RAM is on the sensor itself.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 18, 2013, 03:06 pm

    There are several advantages of the fifo version however it is the same exact sensor the ov7670 so you will still have the same register issues.Here are some advantages of the fifo version you control when

    pixels are read out and you have enough ram to read the entire pixel at once. What I would do is just use the linux driver registers copy paste the entire large struct

    before the large struct add

    Code: [Select]

    struct regval_list {uint8_t reg_num;uint8_t value;

    };

    After that replace

    Code: [Select]

    static struct regval_list ov7670_default_regs[] = {

    with

    Code: [Select]

    const struct regval_list ov7670_default_regs[] PROGMEM = {

    Also make sure you #include

    and use this function to write the registers

    Code: [Select]

    void wrSensorRegs8_8(const struct regval_list reglist[]){

    uint8_t reg_addr,reg_val;const struct regval_list *next = reglist;while ((reg_addr != 0xff) | (reg_val != 0xff)){

    reg_addr = pgm_read_byte(&next->reg_num);reg_val = pgm_read_byte(&next->value);//wrReg(reg_addr, reg_val);ovcam.write(reg_addr,reg_val);

    next++;}

    }

    After writing all those registers configure PCLK and make sure the PCLK does not toggle during HBLANK.

    Then in the file that you downloaded (the linux driver) look for

    Code: [Select]

    static struct regval_list ov7670_fmt_yuv422[] = {

    And adapt it to your code

    Also if you want change

    Code: [Select]

    { REG_COM9, 0x18 }, /* 4x gain ceiling; 0x8 is reserved bit */

    to

    Code: [Select]

    { REG_COM9, 0x6A }, /* 128x gain ceiling; 0x8 is reserved bit */

    Note that it does not mean gain will always be 128x it just means it can go up there if needs to

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 01:31 am

    I copied the configuration from the driver, pretty much in the way you suggested me. But I see voltage levels in the signaling change. And that messes up with my electrical interface, so the Arduino can't read the

    signals. I didn't connect the sensor to the arduino because I was afraid I could damage the sensor and it takes 25 days to arrive from Hong Kong. So I used a bidirectional 3.3V to 5V interface using FET. I made

    an article, it's in Spanish but you will be able to see the circuit and some waveforms. And there is a link to the application note in english too: http://blog.drk.com.ar/2013/interfaz-logica-bidireccional-entre-

    5v-y-3-3v

    I guess I could make it work spending some time in it. But I have to go on with other parts of this project. I'm going to work in other stuff as I wait for the FIFO version I bought yesterday, to arrive. Then I will have

    to build the circuit again, and I could give it another try.

    Do you have any document/information about the interface with the FIFO memory?

    I hope that memory is going to let me read the bytes at any given speed. Because timing with this sensor is a pain.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 20, 2013, 02:59 am

    Sorry I forgot to mention that it changes CLCK speed. I have uploaded the registers I use here

    https://github.com/ComputerNerd/arduino-camera-tft/blob/master/twicam.c

    They are pretty much the ones from the linux driver with some slight changes that may help what you described.

    Note that init_cam() takes one parameter uint8_t useBayer what this means is if you pass 1 to it then it will set the camera to use raw bayer data Note that when useing bayer after this function add a 200

    millosecond delay and rewrite the clock value (register 0x11).pass 0 to make it use something other than bayer data. You can use setColor() to pick what you want. SetRes() sets what resolution you want.

    SetRes() changes the clock divider speed (register 0x11). You may need to change that.

    Also for setRes() and setColor() you will some of the #defines here https://github.com/ComputerNerd/arduino-camera-tft/blob/master/config.h

    or you could just add

    Code: [Select]

    #define vga 0#define qvga 1#define qqvga 2

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    9 of 96 01/19/2015 08:49 PM

  • #define yuv422 0#define rgb565 1#define bayerRGB 2

    to your code.

    Also here is documentation on the fifo version http://wiki.beyondlogic.org/index.php/OV7670_Camera_Module_with_AL422_FIFO_Theory_of_Operation

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 04:21 am

    Great!

    The default configuration was changing register 0x6B (DBLV) which has control over the PLL afecting the PCLK output. Which was causing all the problem with the clock. Now it seems to be working. And the

    output format has clearly changed. But now I can't convert the image using neither "-c d" nor "-c y" flags. Maybe I'm misreading the second byte. According to the datasheet, each byte is to be read during the

    high stage of PCLK. Which is what I am doing, as far as I know.

    I'm attaching the frame read from the sensor (Just 640x200) and the PNG I get.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 04:33 am

    Update. I was missing a fix, in the reading logic... yet it isn't done...

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 04:45 am

    Sorry for the repeated posting. But there are great news... it's working now.

    I'm getting just 640x200 portion of the frame for the sake of speed in the tests :)

    THANK YOU!

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 20, 2013, 04:47 am

    Wow I am glad to see that it is working it makes me happy too. The colors look nice.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 04:53 am

    I'm sure that could be improved. I didn't setup filter for 50Hz, and the camera is pointing a little towards the light. But what I really wanted was to be able to configure the sensor. So it's great. Thank you again!

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 20, 2013, 04:56 am

    You could disable digital gain. For some reason your image reminds me of what digital gain enabled looks like.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 05:01 am

    Ok! I will try that tomorrow and will let you know.

    I have to get some sleep now :)

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 20, 2013, 04:00 pm

    Hint to disable digital gain change register 0x74 to 0x10

    I just edited the struct https://github.com/ComputerNerd/arduino-camera-tft/commit/7809cb37eb072d6325f33b6491284c6615a13bff

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 20, 2013, 05:16 pm

    Digital gain was disabled already. Now I'm using day light and commented the Gamma matrix setting from the linux driver code...

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 20, 2013, 11:07 pm

    I am glad to see that it is working well for you. Something seems different about the image noise/artifacts you posted I just cannot place my finger on it. Did you change some registers or something?

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 21, 2013, 01:48 am

    Nope. I mean, I copied the basic configuration and the YUV configuration sets, from the source you posted. Here is what I'm running:

    Code: [Select]

    static struct regval_list ov7670_default_regs[] = {// { REG_COM7, COM7_RESET },//// Clock scale: 3 = 15fps// 2 = 20fps// 1 = 30fps

    { REG_CLKRC, 20 }, // OV: clock scale (30 fps) [26 funciona]{ REG_TSLB, 0x04 }, // OV{ REG_COM7, 0 }, // VGA//// Set the hardware window. These values from OV don't entirely// make sense - hstop is less than hstart. But they work...

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    10 of 96 01/19/2015 08:49 PM

  • //{ REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 },{ REG_HREF, 0xb6 }, { REG_VSTART, 0x02 },{ REG_VSTOP, 0x7a }, { REG_VREF, 0x0a },

    { REG_COM3, 0 }, { REG_COM14, 0 },// Mystery scaling numbers{ 0x70, 0x3a }, { 0x71, 0x35 },{ 0x72, 0x11 }, { 0x73, 0xf0 },{ 0xa2, 0x02 },{ REG_COM10, COM10_PCLK_HB }, // No PCLK during HBLANK

    // Gamma curve values //

    { 0x7a, 0x20 }, { 0x7b, 0x10 },{ 0x7c, 0x1e }, { 0x7d, 0x35 },{ 0x7e, 0x5a }, { 0x7f, 0x69 },{ 0x80, 0x76 }, { 0x81, 0x80 },{ 0x82, 0x88 }, { 0x83, 0x8f },{ 0x84, 0x96 }, { 0x85, 0xa3 },{ 0x86, 0xaf }, { 0x87, 0xc4 },{ 0x88, 0xd7 }, { 0x89, 0xe8 },

    // AGC and AEC parameters. Note we start by disabling those features,// then turn them only after tweaking the values. { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_BFILT },{ REG_GAIN, 0 }, { REG_AECH, 0 },{ REG_COM4, 0x40 }, // magic reserved bit{ REG_COM9, 0x18 }, // 4x gain + magic rsvd bit{ REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 },{ REG_AEW, 0x95 }, { REG_AEB, 0x33 },{ REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 },{ REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, // magic{ REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 },{ REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 },{ REG_HAECC7, 0x94 },{ REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC },

    // Almost all of these are magic "reserved" values.{ REG_COM5, 0x61 }, { REG_COM6, 0x4b },{ 0x16, 0x02 }, { REG_MVFP, 0x07 },{ 0x21, 0x02 }, { 0x22, 0x91 },{ 0x29, 0x07 }, { 0x33, 0x0b },{ 0x35, 0x0b }, { 0x37, 0x1d },{ 0x38, 0x71 }, { 0x39, 0x2a },{ REG_COM12, 0x78 }, { 0x4d, 0x40 },{ 0x4e, 0x20 }, { REG_GFIX, 0 },{ REG_DBLV, 0x00 }, { 0x74, 0x10 }, // DVL 0x4A ??{ 0x8d, 0x4f }, { 0x8e, 0 },{ 0x8f, 0 }, { 0x90, 0 },{ 0x91, 0 }, { 0x96, 0 },{ 0x9a, 0 }, { 0xb0, 0x84 },{ 0xb1, 0x0c }, { 0xb2, 0x0e },{ 0xb3, 0x82 }, { 0xb8, 0x0a },

    // More reserved magic, some of which tweaks white balance { 0x43, 0x0a }, { 0x44, 0xf0 },{ 0x45, 0x34 }, { 0x46, 0x58 },{ 0x47, 0x28 }, { 0x48, 0x3a },{ 0x59, 0x88 }, { 0x5a, 0x88 },{ 0x5b, 0x44 }, { 0x5c, 0x67 },{ 0x5d, 0x49 }, { 0x5e, 0x0e },{ 0x6c, 0x0a }, { 0x6d, 0x55 },{ 0x6e, 0x11 }, { 0x6f, 0x9f }, // "9e for advance AWB"{ 0x6a, 0x40 }, { REG_BLUE, 0x40 },{ REG_RED, 0x60 },{ REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC|COM8_AWB },

    // Matrix coefficients{ 0x4f, 0x80 }, { 0x50, 0x80 },{ 0x51, 0 }, { 0x52, 0x22 },{ 0x53, 0x5e }, { 0x54, 0x80 },{ 0x58, 0x9e },

    { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 },{ 0x75, 0x05 }, { 0x76, 0xe1 },{ 0x4c, 0 }, { 0x77, 0x01 },{ REG_COM13, 0xc3 }, { 0x4b, 0x09 },{ 0xc9, 0x60 }, { REG_COM16, 0x38 },{ 0x56, 0x40 },

    { 0x34, 0x11 }, { REG_COM11, COM11_EXP|COM11_HZAUTO|COM11_50HZ },{ 0xa4, 0x88 }, { 0x96, 0 },{ 0x97, 0x30 }, { 0x98, 0x20 },{ 0x99, 0x30 }, { 0x9a, 0x84 },{ 0x9b, 0x29 }, { 0x9c, 0x03 },{ 0x9d, 0x4c }, { 0x9e, 0x3f },{ 0x78, 0x04 },

    // Extra-weird stuff. Some sort of multiplexor register{ 0x79, 0x01 }, { 0xc8, 0xf0 },{ 0x79, 0x0f }, { 0xc8, 0x00 },{ 0x79, 0x10 }, { 0xc8, 0x7e },{ 0x79, 0x0a }, { 0xc8, 0x80 },{ 0x79, 0x0b }, { 0xc8, 0x01 },{ 0x79, 0x0c }, { 0xc8, 0x0f },{ 0x79, 0x0d }, { 0xc8, 0x20 },{ 0x79, 0x09 }, { 0xc8, 0x80 },{ 0x79, 0x02 }, { 0xc8, 0xc0 },{ 0x79, 0x03 }, { 0xc8, 0x40 },{ 0x79, 0x05 }, { 0xc8, 0x30 },{ 0x79, 0x26 },

    /**/

    { 0xff, 0xff },};

    static struct regval_list ov7670_fmt_yuv422[] = {{ REG_COM7, 0x0 }, /* Selects YUV mode */{ REG_RGB444, 0 }, /* No RGB444 please */{ REG_COM1, 0 },{ REG_COM15, COM15_R00FF },{ REG_COM9, 0x18 }, /* 4x gain ceiling; 0x8 is reserved bit */{ 0x4f, 0x80 }, /* "matrix coefficient 1" */{ 0x50, 0x80 }, /* "matrix coefficient 2" */{ 0x51, 0 }, /* vb */{ 0x52, 0x22 }, /* "matrix coefficient 4" */{ 0x53, 0x5e }, /* "matrix coefficient 5" */{ 0x54, 0x80 }, /* "matrix coefficient 6" */{ REG_COM13, COM13_GAMMA|COM13_UVSAT },{ 0xff, 0xff },

    };

    My changes are for PCLK speed only...

    Code: [Select]

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    11 of 96 01/19/2015 08:49 PM

  • { REG_CLKRC, 20 }, // OV: clock scale (30 fps) [26 funciona]{ REG_DBLV, 0x00 }

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 21, 2013, 02:51 am

    Your problem is writting 0x38 to COM16 that is a very bad idea.It should be 0x08 instead. Here is why that is bad is because it enables edge enhancement and de-noising this sounds good but it actually makes

    the image worse it makes it blurry and gives it hard edges. Another problem is you left in the 4x gain ceiling. You want 128x. Check out my tweaked structs here https://github.com/ComputerNerd/arduino-

    camera-tft/blob/master/twicam.c

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 22, 2013, 12:19 am

    Nice! Now it's much better. I copied your values for YUV422

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 22, 2013, 12:50 am

    It does look better. No more outline on the edges. I noticed the drawing on the music stand changed colors. Is that because it is a different picture or is it because of the camera?

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: drkblog on Jun 22, 2013, 01:08 am

    No, it's the same book. I guess the change in colors has to do with commenting COM13_GAMMA.

    I'm feeding the OV7670 with 16MHz clock from Arduino, but that implies modifying fuse configuration.

    How are you feeding the XCLK (clock in) of your sensor?

    Here is another take.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 22, 2013, 04:21 am

    On the arduino uno version I am feeding it with the CLKOUT from the avr (modified fuse setting). However after I posted this topic I found an easier way to give it an 8mhz clock using pwm this is half of the

    16mhz clock you are getting out right now if you modified your fuse settings

    On the mega vs uno it is slightly different.

    Here is what I have in my code for the arduino mega with tft screen version

    Code: [Select]

    DDRL|=8;ASSR &= ~(_BV(EXCLK) | _BV(AS2));//generate 8mhz clockTCCR5A =67;TCCR5B=17;OCR5A = 0;

    You will have to modify this abit though.

    Here is what I did for the uno.

    Code: [Select]

    DDRB|=(1

  • }}

    }

    Here is how to enable spi at maximum speed

    Code: [Select]

    //enable spiSPCR=80;//spi enable masterSPSR=1;//double speed

    To read the full 640x480 image do this

    Code: [Select]

    captureImg(0,0,1280,96);//each pixel is 2 bytes so 1280 instead of 640 for widthsendRam(1280,96);captureImg(1280,96,1280,96);sendRam(1280,96);captureImg(1280,192,1280,96);sendRam(1280,96);captureImg(1280,288,1280,96);sendRam(1280,96);captureImg(1280,384,1280,96);sendRam(1280,96);

    The sendRam function just sends the image to the computer

    Code: [Select]

    void sendRam(uint16_t w,uint16_t h){

    spiCSt();spiWrB(3);//sequental read modespiWrB(0);//24 bit addressspiWrB(0);spiWrB(0);uint16_t wl,hl;for (hl=0;hl

  • Post by: drkblog on Jun 23, 2013, 01:38 am

    haha no, I just asked out of curiosity. Don't worry I didn't see any non-sense in your posts. And, in the end, It's a forum. We aren't writing a book here :)

    Besides, knowing the place you are from, I have an idea how easy is to access technology for you. For example, here in Argentina it is hard to get some components. I had to buy the RAM 23LC1024 at Mouser

    in USA and send it to a friend who was vacationing in Boston. So he brought it back here. Otherwise I would have paid 100USD shipping for an order of 20 bucks, which is senseless.

    Fortunately, we have a several big shops here were you can find a lot of stuff. Even some rare pieces. But there is some stuff we have to buy in USA, Hong Kong, China. Specially for testing purposes.

    Title: Re: arduino uno + ov7670 (vga cmos sensor) = awesome

    Post by: Mr_arduino on Jun 23, 2013, 02:06 am

    International shipping can be expensive It is annoying when shipping cost make up a high percentage of the order. Lots of chinese ebay sellers offer free shipping is that you mean by buying from Hong Kong? I

    do that alot for modules also they have much better deals on zif sockets.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: xKoldFuzionx on Jul 11, 2013, 05:37 am

    I'm new to all this electronics stuff and finally coming to terms with my inner nerd. Lol, I've fallen in love with the idea of creating things that anyone can use . I bought an ov7670 on ebay a few weeks back and

    after reading this thread, I'm completely in the dark. So, I was hoping you could shed some light on some things for me?

    Obviously, you're using C to code all this, but do you know of any tutorials that will help me learn about registers? I get that you call them with hex, though hex and bin math are way over my head, I kinda sorta

    follow it.

    Im absorbing as much as i can through google/wikipedia as often as i can and picking up any book i can find on arduino and coding it. I understand the basics of it and it feels very comfortable, so switching over

    to C right now would be a bit much.

    I've only been doing the whole electronics thing for about a month, so I really don't understand a lot of the components, so bare with me.

    You mentioned early on that you added the ram to speed up the data transfer. How long did it take to write to the SD card without the memory? Breaking it up into 5 blocks was still faster? How can that be?

    Thanks in advance for the help and patience. So many people I've seen online said it was impossible to use this camera, but then I found one Guy who shared a link, but it was in Russian or something, so I

    couldn't even read it to find out more. I've seen in this thread and another one that not only have you gotten it to work, you understand how it works. Teach me, oh great and powerful Oz!

    EDIT-- I should add, I understand I can just use your code, but it does me no good if I don't understand why it does what it does. I'm sure it would get me by, and I might even plug it all in tomorrow just to see if

    the thing works, but I'd really like to understand it so I can write my own code. I seem to learn better by doing.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: drkblog on Jul 11, 2013, 03:35 pm

    Hello xKoldFuzionx, welcome to the electronics world. :)

    Your questions is a little too broad to cover it up in one reply, so I guess I'm going to start with some basic stuff:

    To really understand what's going on here, you will have to give it a lot of time. And you will have to start from the beginning. This module is really hard to get working, and is too complex if you don't even know

    about C. My first recommendation would be that you start experimenting with simpler circuits.

    As you should know by now, Arduino is an experimental board with a microcontroller. Even when the specific microcontroller may vary, as well as its machine code. The Arduino family share an interface for

    programming it using C or C++. I haven't seen anyone using another language for Arduino, which doesn't mean there isn't. But all the stuff I came across is written in C/C++. So, for the sake of simplicity let's say

    that you program Ardunio in C/C++.

    Here are the terms what I think you should start learning about:

    Ohm law, diode, transistor (as a switch, at least), power dissipation

    Binary logic (from the logical and electrical point of view)

    Logical gates

    Microcontroller (or microprocessor, or CPU) and DSP

    Memories

    Parallel interface

    Serial interface

    I2C and SPI interfaces

    And then, you will learn about registers when reading about microcontrollers and memories.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: xKoldFuzionx on Jul 11, 2013, 04:22 pm

    Sorry for such a broad question. But, greatly appreciate the input.

    I understand ohm's law, the fTransistors e books I started to read drilled it into me, though I don't entirely understand it's use. Let me clarify. I understand the V=IR, I=V/R, R=V/I, but I don't get the power part of

    it(the Watts).

    I understand simple diodes that block current, but zener diodes confuse me as to how they work.

    Transistors, I get the idea of them, but they confuse me and no text I've read so far seems to simplify it enough.

    By binary logic, you just mean the basic 1 == high, 0 == low? I get that it's like a switch(1 is on, 0 is off), sending a pulse of 5v for high, 0v for low. I was also just reading the math of the base 2 system. Is there

    more behind the logic?

    Logic gates was something I was also just trying to read about, but I felt like it was completely over my head. The problem I have with reading this stuff is I don't see it in action. I'm a very mechanically inclined

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    14 of 96 01/19/2015 08:49 PM

  • person. I can usually look at something, watch it function and figure out how it works. Computers/electronics are hidden, so I use EveryCircuit on my phone a lot to simulate circuits to see how they function. I

    haven't experimented with that stuff on there yet, but I may start today after seeing some of the stuff I was reading.

    I don't want to buy another camera module as they're all so dang expensive. I was excited to buy this one for less than $4. I'm unemployed right now, so money is a big issue. But, I was hoping throwing myself

    into this would help me learn. I don't expect to get it working overnight, but within a year would be great.

    As for the other stuff to look up, thanks. I'll be reading up on that stuff very soon.

    I guess, I was just really hoping(as I've read so far) that the Arduino language was all I needed to learn, but I guess knowing c/c++ is needed in almost all of Arduino, huh?

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: drkblog on Jul 11, 2013, 07:40 pm

    In the first place you have to accept that learning electronics isn't easy. Specially if you want to really know what is going on there. A degree in electronics takes 5 or 6 years, at least in Argentina. Being realistic

    and pragmatic, you have to accept some things as they are given. You won't be able to see a circuit working like a mechanical device. You have to learn what components do under certain circumstances and

    then apply that knowledge. Having that in mind:

    All you mind about power is P = V x I ~~ P [Watt]= V [Volt] x I [Ampere]

    And you have to check that your components have a standard power dissipation. If you place the under certain current and voltage, and that multiplication results in more Watts than this component tolerates,

    you will break it. You can even forget about power dissipation until you start breaking components :)

    You have to learn about the transistor as a switch [http://en.wikipedia.org/wiki/Transistor#Transistor_as_a_switch] (http://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Transistor_as_switch.svg/150px-

    Transistor_as_switch.svg.png)

    When you make a small current flow through the base, the collector-emitter juncture behaves like a cable (conducting). When the base current disappears, the collector-emitter is like an open switch (not

    conducting). You can think of it like a switch controlled by an electrical signal, instead of a human hand. Don't even try to understand why the transistor do this. At least by now.

    Yes, logic is implemented in circuits using 5V or zero volt. But that's a convention and depends whether you are using TTL or CMOS, you can use a different value, like 3.3V (very common today). And you can't

    easily mix this kind of signals, if you connect a 5V output to a 3.3V input you will likely damage the device. Unless otherwise stated in the datasheet.

    Logical gates are predecessors to microprocessors, but of course, they are widely used today. Again, you have to learn its behavior as an enclosed unit. You don't need to learn how they work inside. If you have

    an two-input AND gate, you will get a 1 at the output if and only if you have 1 at the two inputs. A practical use could be: "You have a digital output from arduino and another digital output from a light sensor. You

    have a 1 from the sensor if there is light detected. And you want to turn a LED on and off according to the sensor BUT, you want to turn the LED off from the Arduni at any given time. If you connect the led to the

    sensor it will work all the time, and Arduni won't be able to turn the LED off. Then you place a AND gate connecting the sensor and the arduino output to its input and the LED to the gate output. Now, if the

    Arduino output is 1, the LED will turn on and off according to the sensor. But if Arudino's output is 0, then the LED will be off no matter if the sensor outputs a 1 or a 0".

    You can connect the module you bought according to the circuits and code from this post. Making it work isn't easy anyway. And you are working with a 3.3V module, so be careful about the outputs from Arduino

    going into the camera inputs. You could damage the sensor forever. If money is an issue, I would recommend you start playing with cheaper stuff. So you can understand what you are doing. And you start by

    breaking cheaper things. It's really unusual you start learning electronics without burning some stuff.

    Do you have an oscilloscope?

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: xKoldFuzionx on Jul 11, 2013, 08:50 pm

    Ok, I understand that, but with an Arduino, why would I ever need to use a transistor or a gate. Through code, I can program the same actions. For instance:

    Quote

    if ((12 == HIGH) && (10 == HIGH))

    {

    digitalWrite(13, HIGH);

    }

    else

    {

    digitalWrite(13, LOW);

    }

    [/tt]

    Obviously, you would have to program something to trigger 12 or ten to go high, but you get it.

    In essence, isn't that the whole point of having a Microcontroller? One component does the job of many?

    No, I don't have an oscilloscope. Used one once, but that's the closest I've gotten. I know it would be invaluable, but just can't swing it right now.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: drkblog on Jul 11, 2013, 11:11 pm

    First note that a transistor is not a logical gate, and you can't replace one by another because they have different functionality. Of course, you can build a logical gate using one or more transistors and resistors,

    but stick to the idea that they aren't the same.

    Said that, I placed an example (an excuse) for using a logical gate, it doesn't matter if you can solve it with an Arduino. But if you want to play hard, let's say that you have all I/O pins used in your arduino and

    you have only one left for the problem I presented before. Will you buy another Arduino for doing something you can solve with a single logical gate of USD 0.50?

    And for the transistor as a switch, let's say you have to turn a DC motor on and off with an arduino output. You have two motors A and B. Motor A works with 5V and uses 150mA, and motor B works with 12V

    and uses 30mA. You can't connect A to an output because Arduino supports 40mA (maximum) current per IO pin. If you connect motor A you will break the IO or the whole Arduino. You can't connect motor B

    neither because you have to provide it with 12V and IO pin outputs 5V. Both cases can be solved using a transistor suited for the work. Transistor have parameters of maximum current also. So you have to

    check that.

    There is a couple of projects for building a low-budget oscilloscope using an Arduino, you can google that but here is a link: http://code.google.com/p/xoscillo/

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: xKoldFuzionx on Jul 11, 2013, 11:41 pm

    Ok, that makes complete sense. All the example sketches I've worked through so far have just been really basic as far as components. So, say motor A has a transistor hooked up to it(just so I can get this

    straight) the 5v pin would connect to the base, 12v supply to the collector and motor's positive wire connected to the emitter? Then, when high, it electrically flips the switch allowing the 12v through? Then in that

    case, would you hook up a 12v supply to a regulator then the Arduino, and and a lead from +12v to the collector?

    And yeah, I see what you mean with the gate. It makes a lot more sense to use that rather than buy another Arduino just to do the same thing.

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    15 of 96 01/19/2015 08:49 PM

  • Ok, so one more thing, can you recommend any good beginner's electronics books that are simple but cover everything in a good amount of detail?

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: drkblog on Jul 12, 2013, 12:46 am

    You will have to do something like this:

    (http://embedded-lab.com/blog/wp-content/uploads/2010/12/Circuit_SingleTransistorDCMotor.png)

    The base resistor limits the base current when there is 5V on it. The base current will be (Vcc - Vbe) / R, in this case (5V - 0.7V) / 1k? = 4.3mA which is enough for 100mA current between collector and emitter.

    How much current you need in the base for a given collector-emitter current, comes out from figute 3 in this datasheet: http://pdf.datasheetcatalog.com/datasheet/mcc/548B.pdf

    The diode is a protection which prevents the inverse current generated inside the motor when it's turned on. Such diode is needed when you are switching an inductor/coil on and off (a motor, a relay, etc), to

    protect the transistor.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: sampullman on Jul 16, 2013, 04:07 am

    Hey all,

    I've been using this thread to try and get my OV7670 working, and I seem to have hit a roadblock. Currently, my images mostly look similar to this: http://imgur.com/SoKGYNP

    I'm using this to get my 8Mhz clock down to 3.3v https://www.sparkfun.com/products/8745 (generated on pin 10 using fast pwm).

    I'm setting reg. 0x11 to 0x03, which is giving me a 250khz PCLK, although I have tried other values - 0x01 to 0x09

    I'm using this code to set up the camera for QCIF (176x144) mode with YUYV422 color.

    Code: [Select]

    writeReg(0x15, 32); // PCLK does not toggle on HBLANK COM10 vsync falling

    writeReg(REG_TSLB, 0x04); // 0D = UYVY, 04 = YUYV writeReg(REG_COM13, 0x88); // with REG_TSLB

    writeReg(REG_COM7, 0x08); writeReg(REG_COM17, 0x00); writeReg(REG_COM15, 0xC0); writeReg(REG_COM3, 0x08);

    The full test code for image capture I'm using is here:

    http://pastebin.com/0tPSt4Kp

    I should note that at the moment I'm attempting to capture the image and send it over serial using only the 2k sram on the arduino (this is just a proof of concept until I can get ahold of some external ram).

    Therefore, I have to break the image up into 36 chunks or so, but I don't see any reason why it shouldn't be possible - just really slow.

    Any help would be greatly appreciated. I can provide more detail as well if necessary.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: drkblog on Jul 16, 2013, 04:22 am

    In the first place you have to be sure your cam is working. If you have a problem with the clock you are feeding, you will waste a lot of time. In my case, I used the 16MHz clock from Arduino and I feed it right into

    the camera, because at 16MHz you don't have 5V signal. You should check with an oscilloscope to see what's the real amplitude of your 8MHz clock. To see if the camera is working you have to check the PCLK

    output.

    If you don't have an oscilloscope, I would recommend you check PCLK using a frequency meter. You have to be sure the module is working. After that, capture covering the camera lens to get a frame of a black

    image. You should see these chain of bytes: 80 00 80 00 80....

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: sampullman on Jul 16, 2013, 05:15 am

    Thanks for the response.

    I do think the camera is working. I'm able to read and write to registers, and from my oscilloscope it seems like VSYNC is going high at a reasonable rate. I'm also able to see the PCLK output, which is 250khz

    when reg 0x11 is set to 0x03. Here is a picture of the oscilloscope when measuring the PCLK: http://imgur.com/YQhWI8d

    Actually, I just noticed the peak to peak is ~5v, and the min/max are -1.8v/3.6v - is that bad?

    Here is the signal going into the OV7670 XCLK: http://imgur.com/syTEAat

    When I try to capture with the lens covered I get mostly 00 00 00... and sometimes some garbage.

    Out of curiosity, how would I go about getting the 16Mhz arduino clock output? I've never burned a bootloader and I only have a usb connector, can I do it with just that?

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: drkblog on Jul 16, 2013, 02:51 pm

    Ok, if the module is working, and you are able to read/write registers you are almost done. The next hard-to-do thing is, configuring all the undocumented registers. If you fail to do that, you won't get the camera

    working. You can be sure about that.

    You MUST write all the registers in ov7670_default_regs[] and then those in ov7670_fmt_yuv422[] :

    http://dev.openaos.org/browser/trunk/buildroot/gen7/linux/drivers/media/video/ov7670.c

    Also, start by getting PCLK as low as possible first. As far as I remember I wasn't able to get a good reading with REG_CLKRC under 20 (decimal). But, I'm feeding 16MHz clock and you have half this clock,

    which makes you PCLK half mine when using the same divider in REG_CLKRC. So (I'm totally guessing here), you should be able to make it work with REG_CLKRC set at 10. The reading code has to be as

    quick as possible and you have to use cli() before start reading. Please post the portion of code you are using to read from the camera.

    For burning fuses I needed a second Ardunio, with a program called ISP http://arduino.cc/en/Tutorial/ArduinoISP but I wouldn't recommend that approach for this project. Using 8MHz clock is simpler and better.

    Yet, it's the limit for OV7670, you won't be able to make it work with less than that. As the datasheet says the minimum clock should be 10MHz.

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    16 of 96 01/19/2015 08:49 PM

  • Title: Re: ov7670 with both arduino uno and now mega

    Post by: Mr_arduino on Jul 16, 2013, 05:48 pm

    About the 10mhz limit. I don't think that is true I have left the ov7670 on with an 8mhz clock for hours and nothing bad has happened. Also I have looked at other new sensors from omnivision datasheets to see if

    I can figure out what the undocumented registers mean and most of the newer datasheets say 6mhz is the lower limit for the clock. Usually overclocking is more dangerous than under-clocking.

    Actually I made some corrections to the linux driver registers. Also the code I have on my github page uses divider settings is for people with the 8mhz clock. I have posted about the registers on my github page

    before but I have sense made some changes. Here are the files you need.

    https://github.com/ComputerNerd/arduino-camera-tft/blob/master/config.h change #define MT9D111 to #define ov7670

    https://github.com/ComputerNerd/arduino-camera-tft/blob/master/twicam.c update the function void errorD(uint8_t err) for your hardware for example send the string error over serial instead of displaying it on

    the lcd screen or you can just remove that function. Also remove the code that hflips the image. That is specific to the tft screen I am using if you are not sending it to the seeedstudio v1 lcd screen you don't want

    the image hfliped.

    https://github.com/ComputerNerd/arduino-camera-tft/blob/master/twicam.h can be used as is but must be used if you use twicam.c

    https://github.com/ComputerNerd/arduino-camera-tft/blob/master/captureimage.c Here is an example of how to capture an image there are many different functions for different resolutions. change PINE&32 to

    where ever your VSYNC pin is also change PINE&16 to where ever the PCLK output is also change the code that sends the data to the tft screen to were ever you are sending it. Once you have adapted those

    files to your system you can call the function setRes(); and use the defines in config.h for example setRes(qvga); sets the resolution to 320x240. When calling initCam() You also have a choice of 3 different

    register presets (0 1 and 2). I would recommend doing initCam(0); that seems to work best. Also you have setColor() I would recommend yuv422 for qvga and qqvga by calling it with setColor(yuv422);. For vga I

    would recomend bayer call it with setColor(bayerRGB). rgb565 is very low quality only use it if you are sending to a tft screen that uses rgb565 data.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: sampullman on Jul 16, 2013, 08:20 pm

    @drkblog

    Ok, I'm going to spend some time making sure my register setup is correct. I saw a few tutorials that didn't seem to bother with the defaults (such as this http://embeddedprogrammer.blogspot.com/2012/07

    /hacking-ov7670-camera-module-sccb-cheat.html ), which is why I didn't spend too much time on it before.

    Here is the code I'm using to capture very small chunks from the camera (small enough to fit in arduino SRAM, so ~4 QCIF lines at a time).

    Code: [Select]

    void captureChunk(unsigned long wait, unsigned long chunk, uint8_t *pixels) { unsigned int n = 0; while(!(PIND & 8)) {} // Wait for VSYNC high while(PIND & 8) {} // Wait for VSYNC low

    while(wait--) { while(!(PIND & 4)) {}; while(PIND & 4) {}; } while(chunk--) { while(!(PIND & 4)) {}; pixels[n++] = (uint8_t)((PINC & 0x0F) | (PIND & 0xF0)); while(PIND & 4) {}; } //.... write to serial }

    @Mr_arduino

    I'll look through that, thanks for the link. Is there any particular reason to use custom twi functions instead of the built in Wire library? I'm able to read and write registers using that at the moment. Also, would you

    happen to have a link for a newer version of the datasheet? I'm using this one right now: http://www.eleparts.co.kr/data/design/product_file/Board/OV7670_CMOS.pdf

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: Mr_arduino on Jul 16, 2013, 10:04 pm

    ^^^ I made my own twi functions because I am not using the arduion ide. I don't like the arduion ide. Note that I am not forcing you to avoid using the arduino ide. Most likely the code that I posted will work just

    fine with the arduino ide. Here is a newer version of the datasheet

    http://www.haoyuelectronics.com/Attachment/OV7670%20+%20AL422B%28FIFO%29%20Camera%20Module%28V2.0%29/OV7670%20datasheet%28V1.4%29.pdf

    Also looking at your code using an unsigned long is bad. The avr is an 8bit processor but it does have some 16bit math support. Try to use the typedefs from stdint.h

    for example uint16_t is a 16bit unsigned variable. In C an int could be either 16bit or 32bit depending on the compiler. int16_t will always be 16bit so that is better to use.

    What I do is have two loops one that reads a line and another that tells how many lines to read.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: technonewb on Jul 17, 2013, 06:09 pm

    Hello Mr_arduino, drkblog, and sampullman!

    I am working on a project for one of my university proffesors and I am ran into a problem with the OV7670 camera. I have been trying to read some registers on the module but without luck. It appears that all

    three of you are able to use the Wire library but I had no luck with it.

    When I try using the Wire commands, the endTransmission returns 2 meaning the camera could not be addressed properly.

    Code: [Select]

    Wire.beginTransmission(sensor_addr >> 1); Wire.write(0x01 & 0x00FF); Serial.println(Wire.endTransmission());

    Also, the following code (which sampullman provided) returns the address of the register instead of the actual value stored inside the register:

    Code: [Select]

    #include

    uint8_t sensor_addr = 0x42;

    void setup(){ Wire.begin(); Serial.begin(115200);}

    void loop(){ uint8_t* dat; readReg8(0x01, dat); Serial.println(*dat); delay(1000);}

    /* Write 2 byte value regDat to the camera register addressed by regID */byte writeReg(int regID, int regDat) { Wire.beginTransmission(sensor_addr >> 1); Wire.write(regID & 0x00FF); Wire.write(regDat & 0x00FF); byte result = Wire.endTransmission(); delay(1); return result;

    Print Page - ov7670 with both arduino uno and n... http://forum.arduino.cc/index.php?action=printpa...

    17 of 96 01/19/2015 08:49 PM

  • }/* Read a 2 byte value from the camera addressed at regID, and store it at * memory location pointed to by regDat. Return 1 on success, 0 on failure. */byte readReg8(uint8_t regID, uint8_t* regDat) { Wire.beginTransmission(sensor_addr >> 1); Wire.write(regID & 0x00FF); Wire.endTransmission(); Wire.requestFrom((sensor_addr >> 1),1); if(Wire.available()) { *regDat = Wire.read(); delay(1); return 1; } else { return 0; }}

    Could you please give me a hand?

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: Mr_arduino on Jul 17, 2013, 06:49 pm

    Actully my latest code does not use the wire library see https://github.com/ComputerNerd/arduino-camera-tft/blob/master/twicam.c or if you feel the need to use the Wire library my very old code uses it see

    http://pastebin.com/1nnRc5qL

    Also the ov7670 is picky about which pullup resitors you use. I found that 4.7k and 10k work but 1k and 2.2k do NOT work. And make sure that you are using the pullup resistor to 3.3v and if you use the Wire

    library you must edit twi.c which is located in (arduino ide folder)/libraries/Wire/Utility

    And remove these lines:

    Code: [Select]

    // activate internal pullups for twi. digitalWrite(SDA, 1); digitalWrite(SCL, 1);

    By not doing that you could damage the ov7670 although when I first got the ov7670 I made the mistake of not removing those lines and nothing happened however your millage may vary. After removing those

    lines be sure to recompile your program.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: giantsfan3 on Jul 18, 2013, 03:40 pm

    @Mr_Arduino, Terrific effort by you on this code, as well as answering people's questions! I see you are also adding support for the MT9D111. I'm definitely going to try the Omnivision module with this code.

    Here is a suggestion: Perhaps you could take a few minutes and turn this into a standard structured Arduino-importable library (along with an example usage sketch of .INO filetype).

    Along with that, you could make a few lines of tutorial out of your build including the hardware connections, and put it on the first post of the thread, so that others can reproduce your setup (there will also be

    fewer code-troubleshooting questions that way).

    Also, here's a cool video by someone in Japan who made a TFT-camera out of it (this is where I first learned about the OV7670):

    Not sure how they got this kind of almost real-time video from it using just the Atmega:

    http://www.youtube.com/watch?v=RYh1389IEng

    Quote from: Mr_arduino on Jul 17, 2013, 06:49 pm

    Actully my latest code does not use the wire library see https://github.com/ComputerNerd/arduino-camera-tft/blob/master/twicam.c or if you feel the need to use the Wire library my very old code uses it see http://pastebin.com/1nnRc5qL

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: Mr_arduino on Jul 18, 2013, 10:06 pm

    ^^^ About creating an *.ino file Everyone has different needs I like to keep my files organized so that if someone has trouble with something for example communicating with the camera I can point them to the

    right file and they don't have to look through lots of code to find what they need. I try to keep the filename related to what the functions in it do see https://github.com/ComputerNerd/arduino-camera-tft Also I

    don't use the arduino ide anymore. I have deleted it a long time ago and never looked back(I know I can always re-download it but I don't ever want to use it again) My code should just be copy pastable into the

    arduino ide. It is just standard C. I do have older code that does use the arduino ide you can find that on the first page. I think I did already explain the hardware connections in the first few posts or do you mean

    make a video? Also what that person from Japan did was have the arduino configure the ov7670 but instead of plugging the video signals into the arduino they plugged it into the tft screen. What I had to do for

    my tft project was read the data with the arduino and send it to the tft screen or send it external ram. Speaking of which I now recommend the fifo version version over the non-fifo version. I would have done it

    with my tft screen but it needs to be configured with the arduino uno as well. The tft they have just needs to be powered on and it starts working. A disadvantage of directly plugging in the ov7670 into the tft

    screen is that you can't have a menu that allows you to configure the camera. My program includes a menu to configure the ov7670.

    Title: Re: ov7670 with both arduino uno and now mega

    Post by: sampullman on Jul 18, 2013, 11:23 pm

    @technonewb

    I'm not sure if you posted all of your code, but you need to add something like this after the Wire.begin() call in order to set up TWI communication so that it works with the OV7670:

    Code: [Select]