Jun 21, 2016

Arduvision (II): OV7670 + FIFO module and arduino Mega

After several people asking for instructions to use an Arduino Mega to interface the ov7670 sensor, I decided to publish a tutorial about it...








HARDWARE NEEDED:

- Voltage level translator: To properly connect a 3.3v sensor to a 5v microcontroller you need bi-directional voltage level translators, for example like this:
There is already a lot of info on how these work, so I won't get into that.

- Arduino Mega or compatible development board. In this tutorial I use a Mega2560 clone.

- A CMOS module with FIFO and an ov7670 or an ov7725 sensor (I've tried both, and setup the register parameters). At the moment I'm working only through the FIFO, but may try synchronize the microcontroller clock directly with the CMOS sensor, as int the AVRcam project.
For this setup the module must have the following available pins (numbering may differ):

                      GND --- 1     2 --- VCC3.3
                   OV_SCL --- 3     4 --- FIFO_WRST
                   OV_SDA --- 5     6 --- FIFO_RRST
                  FIFO_D0 --- 7     8 --- FIFO_OE
                  FIFO_D2 --- 9    10 --- FIFO_D1
                  FIFO_D4 --- 11   12 --- FIFO_D3
                  FIFO_D6 --- 13   14 --- FIFO_D5
                FIFO_RCLK --- 15   16 --- FIFO_D7
                 FIFO_WEN --- 17   18 --- OV_VSYNC

Some other modules have these pin numbers/labeling:

                      VCC --- 1     2 --- GND
                    SIO_C --- 3     4 --- SIO_D
                    VSYNC --- 5     6 --- HREF
                  FIFO_D7 --- 7     8 --- FIFO_D4
                  FIFO_D5 --- 9    10 --- FIFO_D2
                  FIFO_D3 --- 11   12 --- FIFO_D0
                  FIFO_D1 --- 13   14 --- FIFO_D6
                    RESET --- 15   16 --- PWDN
                   STROBE --- 17   18 --- FIFO_RCK
                  FIFO_WR --- 19   20 --- FIFO_OE
                FIFO_WRST --- 21   22 --- FIFO_RRST

ov7670-fifo-v3:

                      VCC --- 1     2 --- GND
                    SIO_C --- 3     4 --- SIO_D
                    VSYNC --- 5     6 --- HREF
                      WEN --- 7     8 --- XCLK
                FIFO_RRST --- 9    10 --- FIFO_OE
                  RCLK/WR --- 11   12 --- GND
                  FIFO_D0 --- 13   14 --- FIFO_D1
                  FIFO_D2 --- 15   16 --- FIFO_D3
                  FIFO_D4 --- 17   18 --- FIFO_D5
                  FIFO_D6 --- 19   20 --- FIFO_D7


I've tested an ELH 30w FIFO module and the Alientek OV7670 v2.2 module. Both use a 12MHz clock. Other modules use a 24MHz clock. While this affects only the framerate selection parameters, some newer versions have a different schematic/pinout which need VSYNC signal to be inverted, these will not work with this setup.

- Jumper wires and breadboard to make the connections.


CONNECTIONS:


Connect the boards as follows:

    
          OV+FIFO                           MEGA2560
                  GND ----------------------- GND
                 3.3V ----------------------- 3.3V
              FIFO_OE ----------------------- GND
     FIFO_WR/FIFO_WEN --> level tranlator --> PH5 / D8
   FIFO_RCK/FIFO_RCLK --> level tranlator --> PH6 / D9
            FIFO_WRST --> level tranlator --> PB6 / D12
            FIFO_RRST --> level tranlator --> PB7 / D13
         SIO_D/OV_SDA <-> level tranlator <-> PD1 / SDA
         SIO_C/OV_SCL <-> level tranlator <-> PD0 / SCL
             OV_VSYNC <-- level tranlator <-- PE4 / D2
              FIFO_D3 <-- level tranlator <-- PF3 / A3
              FIFO_D4 <-- level tranlator <-- PF4 / A4
              FIFO_D5 <-- level tranlator <-- PF5 / A5
              FIFO_D6 <-- level tranlator <-- PF6 / A6
              FIFO_D7 <-- level tranlator <-- PF7 / A7
  (some modules) PWDN ----------------------- GND / not connected
 (some modules) RESET ----------------------- 3.3V / not connected
  (some modules) HREF ----------------------- not connected
(some modules) STROBE ----------------------- not connected


You may edit the file IO_config.h of the arduino sketch if you want to make some changes, just pay attention on the VSYNC pin, that is tied to INT0 interruption.





SOFTWARE:

You can download from this repo the arduino sketch and the processing code to communicate with the arduino via USB:


The code will either stream a 80x60 pixels image, in different pixel depths, or detect the boundaries of the brightest/darkest blob within a threshold.

Edit: you get too much garbage in the processing console you may need to change the baudrate to match your system capabilities.


In the arduino sketch look for the line:
   static const unsigned long _BAUDRATE = 500000;

And in the processing sketch, in the globalDefinitions.java tab:
   public final static int BAUDRATE = 500000;

Try with substituting the 500000 for 115200.



NOTES ON THE ARDUINO SIDE:
Some people have added pullup resistors to the SDA and SCL lines and disabled the internal pullups. However, in this setup things worked straight away without these steps.

NOTES IN THE PROCESSING SIDE:
You will have to change the serial port name to match your system. In the processing code, look for the line:

   serialPort = new Serial(this, "/dev/ttyUSB0", G_DEF.BAUDRATE);

And chage the port name "/dev/ttyUSB0".

This is a video of the demo:

RESOUCES / LINKS:

I'll try that all the information published here is related only to 100% open hardware and software.

Two inspiring fully open hardware and software embedded computer vision projects:

AVRcam by John Orlando
One of the pioneers pushing the little microcontroller capabilities.

OpenMV by Ibrahim Abdelkader 
Currently in development, it will be an amazing and affordable platform with outstanding capabilities, and the only one in its class completely open.


Useful documentation


Source code related to microcontrollers and CMOS sensors/modules:




Forum discussion threads about microcontrollers and CMOS sensors/modules


54 comments:

  1. Hi, thank you so much, as it is a long time I am searching for something serious on the subject.
    Best regards from Paris !

    ReplyDelete
  2. Im your little fans form Arduvision(I) and in this following my problem while I test with Arduino Mega is like this screenshot
    https://postimg.org/image/mz45gou69/
    I have try to setup baudrate as 19200, 38400 and 115200 and 5000000..
    How can I receive the fresh image like you ??
    I really need your support to remake the final project of my school
    Hope and thank you so much :D

    ReplyDelete
    Replies
    1. Hmmm... maybe something with the wiring?
      I guess you are using the voltage level tranlators like indicated, right?
      The strange thing is that the arduino is communicating with the sensor...
      Some other guy had problems with his sensor, have you some other ov7670 module to test?

      Delete
    2. ok, in the graphic schematic there is a missing line from FIFO_OE to GND, I'll correct it right now...

      Delete
    3. Im have ov7670 none fifo and my Ov+Fifo is this device
      http://www.nyplatform.com/index.php?route=product/product&product_id=63
      I also use it on the previous project (your Arduvision (I)) and I think It'll work well with this project :D

      Delete
  3. Ah I solve the problem of this project, but It still have a darkness than your demo & fps too low than you :D
    But it work stable with all baudrate. Thank you

    ReplyDelete
  4. I got it but the pixel is too low :D
    https://s31.postimg.org/c5f54ccff/check2.jpg

    ReplyDelete
    Replies
    1. This is my problem
      https://s32.postimg.org/xl977wq05/4444.jpg
      how to make it brightness :D Thank you

      Delete
    2. Hehe, I was asking what was the problem before (why did you got this: https://postimg.org/image/mz45gou69/ and how did you solved it)

      For the dark image you probably need to play with the sensor settings.
      First try commenting line 58 in sensor.cpp :
      // sensor_writeRegs(common_reglist);

      If that doesn't help, in ov7670_regs.h check the values of:

      qqqvga_yuv_ov7670[]
      and
      common_reglist_ov7670[]

      Look for other registers configurations in the internet or have a look at the sensor datasheet.

      Delete
  5. hey My best result :)
    https://www.youtube.com/watch?v=RgsVu1NOBIM
    but still need light enought to see the image better :D

    ReplyDelete
    Replies
    1. hey, nice fps :)
      at least there are improvements

      Delete
    2. why did you got this?:

      https://postimg.org/image/mz45gou69/

      how did you solved it?

      Delete
    3. https://postimg.org/image/mz45gou69/
      This just my wiring problem of OE pin (I haven't connect OE to GND !! lol
      Can I get some knowledge if the Arduino send the image stream data to the Bluetooth or Wifi via UART (Like some SmartHome streaming project which receive video stream to smartphone) ? The Init and mechanism method on Arduino is coding like your project .uno + library ? Need some advice :D
      Thank you

      Delete
    4. This comment has been removed by the author.

      Delete
    5. Look for info about the HC-05 serial BT module. You will have to increase the module's baudrate via AT commands. It should work straight away with your current setup.
      On the android side you can try to use the processing sketch with the APDE app.

      Delete
  6. Can I re-define the pin config (from your define pin library) pin_config.h to change the function of any pin-config ?
    _Haven't any pin which must be standand & can not change to another pin (like SCL SDA on ArduinoMega)?

    ReplyDelete
    Replies
    1. You can change the pins in IO_config.h, the names correspond to the chip ports and pins
      SDA and SCL pins have to match your board's hardware SDA and SCL
      OV_VSYNC pin has to be the one corresponding to the interrupt number specified by VSYNC_INT
      All data pins have to be in the same port, and data pin numbers have to be the same as the bit positions:
      data bit 7 - pin 7 of the X port
      data bit 6 - pin 6 of the X port
      etc.

      Delete
  7. okay got it!! thank your support too much :D

    ReplyDelete
  8. Can you write a post to interpret your code in Arduino Mega
    or comment more in your code for user easy to understand your code :D thank you

    ReplyDelete
  9. Thank you for your post. Only a question, it is posible to do it with an arduino UNO clone? Or is mandatory the use of arduino Mega?

    ReplyDelete
    Replies
    1. Hi!
      You can do it with an UNO, with the same voltage level translators but using the code/pins of this post:
      https://therandomlab.blogspot.com.es/2014/09/arduvision-i-embedded-computer-vision.html

      Delete
    2. I will try using Arduino Pro Mini (3.3V) so the final result could be smaller and more compact.

      Thanks for your help!

      Delete
  10. Hi friend! Do you have any schematic for connecting this module in a TFT display 3.2" or larger? Thanks

    ReplyDelete
  11. How do you use Arduino UNO with this code? What do I change?

    ReplyDelete
    Replies
    1. You can use the following info/code code, but with the voltage level translators:
      http://therandomlab.blogspot.com.es/2014/09/arduvision-i-embedded-computer-vision.html

      Delete
  12. why my arduino tell me

    Initializing sensor...
    retrying...
    retrying...
    retrying...
    retrying...
    retrying...
    PANIC! sensor init keeps failing!

    ReplyDelete
    Replies
    1. There is a problem in the communication with the sensor:
      a) the wiring is not right
      b) the arduino is a 16MHz 5v version

      Are you using a 8MHz (3.3v) arduino?

      Delete
    2. sorry, I don't understand you mean ..8 MHz(3.3v)

      but I use 3.3v between mega 2560 and ov7670-fifo,

      is that worng?

      I'm from Taiwan, so my English translation use of may be wrong...

      Delete
    3. Sorry, I mixed posts... if you are using

      MEGA <-> voltage level translator <-> sensor

      you a right.

      Check the wirings.

      Delete
    4. I checked the wirings two times , but still tell me

      retrying...
      PANIC! sensor init keeps failing!

      there are ways to detect whether ov7670-fifo can operate ?
      I worry that ov7670 is bad...

      Thanks :)

      Delete
  13. why my arduino tell me

    Initializing sensor...
    retrying...
    retrying...
    retrying...
    retrying...
    retrying...
    PANIC! sensor init keeps failing!

    Is camera burned ??

    ReplyDelete
  14. First of all, thank you very much for sharing your code and for the tutorial you have written. I was trying to compile (verify) the code from arduvision_01 folder, but I am getting the following error message (I did not modify any file):

    "C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avr-g++" -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD -flto -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO=10611 -DARDUINO_AVR_UNO -DARDUINO_ARCH_AVR "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard" "-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire\src" "C:\Users\Little\AppData\Local\Temp\build4b980085cde7535f83d731951fbb0ae6.tmp\sketch\ov_fifo_test.ino.cpp" -o "C:\Users\Little\AppData\Local\Temp\build4b980085cde7535f83d731951fbb0ae6.tmp\sketch\ov_fifo_test.ino.cpp.o"
    C:\Users\Little\Documents\Arduino\ov_fifo_test\ov_fifo_test.ino: In function 'void vsyncIntFunc()':

    ov_fifo_test:164: error: ambiguating new declaration of 'void vsyncIntFunc()'

    void static __inline__ vsyncIntFunc() {

    ^

    C:\Users\Little\Documents\Arduino\ov_fifo_test\ov_fifo_test.ino:164:19: note: old declaration 'int vsyncIntFunc()'

    void static __inline__ vsyncIntFunc() {

    ^

    Using library Wire at version 1.0 in folder: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\Wire
    exit status 1
    ambiguating new declaration of 'void vsyncIntFunc()'

    Any idea of what is happening? Thank you!

    ReplyDelete
    Replies
    1. Hi,

      Can you please try removing the "static" from the function declaration?

      void __inline__ vsyncIntFunc()

      Delete
    2. Hello David! I thought I had done removing "static" before, but now, I tried it again and it worked (maybe I had removed "__inline__" also! Thank you very much! Would you explain the reason why I had to remove "static" and why it's compiling now?

      Delete
    3. Glad it works now! I'll change the github sources.
      Sincerely no idea why :)
      It might have to do with changes in different versions of the Arduino core libraries.
      Saludos!

      Delete
  15. Hi, I have the newer version with inverted VSYNS. How can I get this code to work with it. I have read that you have to put one into COM10 register. But i can not seem two find out how I do it in your code.
    Best Regards.
    Henrik.

    ReplyDelete
    Replies
    1. Can you try changing, in the file ov_fifo_test_mega.ino, lines 148 and 178, the interrupt to RISING instead of FALLING

      attachInterrupt(VSYNC_INT, (void(*)())&vsyncIntFunc, FALLING);

      to

      attachInterrupt(VSYNC_INT, (void(*)())&vsyncIntFunc, RISING);

      and see what happens?

      Delete
  16. Hi, I have the newer version with inverted VSYNS. How can I get this code to work with it. I have read that you have to put one into COM10 register. But i can not seem two find out how I do it in your code.
    Best regards.
    Henrik.

    ReplyDelete
  17. Hi, i used that ov_fifo_test.ino & got some op
    Initializing sensor...
    inited OK, sensor PID: 7673

    How to stream image?
    how to use that receive_stream_mega.pde?
    Can you help me saving image file in sdcard?
    i interfaced sdcard.But how to save image.

    ReplyDelete
    Replies
    1. solved streaming.
      Need help with storing in sd card

      Delete
    2. Good to hear the streaming worked. At the moment I'm quite busy so I wont be able to help you with the SD card stuff.

      Delete
  18. Replies
    1. To reduce the amount of data to be transferred in the streaming.

      Delete
  19. I was wondering if the Arduvision (II): OV7670 + FIFO module and arduino
    Mega is capable of taking a picture.

    ReplyDelete
  20. I would use this camera to view the picture on a 7" LCD screen, for example! Or via Wi-Fi Module!

    ReplyDelete
  21. How to get raw image data without processing software.may be on arduino serial monitor.
    How to get still image instead of video stream

    ReplyDelete
  22. hi there. I`d like to know what should be the name of the serial port to match with my system?
    thank you for helping me

    ReplyDelete
    Replies
    1. hi, what OS are you using? OSX, Windows, Linux?
      The serial port should be the same as the one selected in the Arduino IDE (under Tools -> Port )

      Delete
  23. hi,thank you for great tutorial
    I need a little help with something
    I don't know how to match my port name in my computer with the processing code.
    what should I chane it to?

    ReplyDelete
    Replies
    1. what OS are you using? OSX, Windows, Linux?
      The serial port should be the same as the one selected in the Arduino IDE (under Tools -> Port )

      Delete


  24. these are my ov7670 camera having

    VDD GND
    SDIOC SDIOD
    VSYNC HREF
    PCLK XCLK
    D7 D6
    D5 D4
    D3 D2
    D1 D0
    RESET PWDN
    but in yours description these are having
    FIFO_OE ----------------------- GND
    FIFO_WR/FIFO_WEN --> --> PH5 / D8
    FIFO_RCK/FIFO_RCLK --> --> PH6 / D9
    FIFO_WRST --> --> PB6 / D12
    FIFO_RRST --> --> PB7 / D13
    STROBE ---------------- not connected
    tell me about this

    ReplyDelete
    Replies
    1. It seems your ov7670 module doesn't have FIFO, it will not work with my code.

      Delete
    2. See for example this: http://www.instructables.com/id/OV7670-Without-FIFO-Very-Simple-Framecapture-With-/

      Delete