Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 14 of 14

Thread: Cheap Video with Teensy 3.1 and $5 OV7670 camera chip

  1. #1
    Member
    Join Date
    May 2014
    Location
    Colorado Springs
    Posts
    81

    Cheap Video with Teensy 3.1 and $5 OV7670 camera chip

    I posted this in project guidance but wanted to make sure more people saw it so I re-posted it here. I hope I didn't break any forum rules by doing so.

    I have just been successful interfacing a Teensy 3.1 to the $5 OV7670 Camera Chip ( without fifo) and the Adafruit 1.8" LCD display and wanted to share what I have learned. This hardware software combination captures QQVGA (160x120) images in RGB565 format at the rate of about 1.5 images/second. I'm sure the smart people on this forum could make it much faster and/or higher resolution. I've made the code available to anyone at: craigandheather.net/misc/TeensyCamera.zip

    You are free to use my code anyway you want but if you do something cool with it, please let me know.

    Cheers, for the Teensy's

    Craig Lindley
    calhjh@gmail.com

    Name:  IMG_6100.png
Views: 9088
Size:  130.0 KB

  2. #2
    Junior Member
    Join Date
    Dec 2015
    Posts
    2
    Why did you choose XCLK to be 8MHz? Wouldn't a faster XCLK allow for a higher frame rate at the same resolution? I'd just like to know your reasoning in case I'm missing something.

  3. #3
    Senior Member Constantin's Avatar
    Join Date
    Nov 2012
    Location
    In the yard with a 17' Dia. Ferris Wheel
    Posts
    1,406
    Awesome achievement!

    As for the clock speed, I imagine it could have to do with using a breadboard. High speed signals and lots of wires usually do not play nice with each other.

  4. #4
    Senior Member Ben's Avatar
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    399
    Looks great! Could you explain a bit about the camera? Does it buffer the frame so you can read it over spi after the pic was taken? Can you re-read the data or specific parts of the sensor (e.g. a square area in the middle?) or does the spi directly access the sensor data?
    Ben

  5. #5
    Member
    Join Date
    May 2014
    Location
    Colorado Springs
    Posts
    81
    I'll try and answer both questions here:

    First Constantin was exactly right. Moving high frequency signals from point to point gets more complex the higher the frequency. I didn't have a scope handy when I was doing this so I thought a conservative approach using 8 MHz would be best on my breadboard.

    Second, no there isn't any buffering of the image at all. The Teensy is pulling the data in real time. Also, the data is not read over spi it is read in parallel from the video chip. i2c is used however to interact with the video chip for setting up it registers and what not.

    Hope this helps,

    Craig Lindley

  6. #6
    Junior Member
    Join Date
    Dec 2015
    Posts
    2
    Thanks, Craig.

  7. #7
    Wow, great work! I will get this camera in some days and I will use your code!
    Thank you very much! Maybe it is not optimized but it is simple to understand and easy to modify.

    What I miss is a schema. Did you connected the camera without any resistor, or do you used the simple arduino schematics (with 3-4 resistors?).
    Last edited by Adriano; 01-07-2016 at 11:09 AM.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    28,474
    Any chance you might publish this on github? Long-term, more people will be able to find it there.

  9. #9
    Senior Member
    Join Date
    Jan 2013
    Location
    San Francisco Bay Area
    Posts
    660
    Nicely done!

    I assume this low-end camera doesn't have an IR filter, so it can be used with an infrared emitter to see in the dark?

  10. #10
    Thanks to Craig I was able to using this camera (thank you again!!!).

    I connected SIOC and SIOD to PIN SCL0 and SDA0 (pin 18 and 19 on Teensy 3.2) with a pull up resistor. Without a 4,7k it didn't worked by me.

    I don't have a LCD and not a SD so I sent the data over the serial port (baud rate: 921600).

    And here a simple .NET application screenshot (draws directly the serial data in the picture):
    Click image for larger version. 

Name:	OV7670.jpg 
Views:	848 
Size:	41.3 KB 
ID:	6084

    Because the camera sends the data with a 8MHz/31 frequency (if I understood it right), it can only get still images, the camera is not able to get moving objects with this frame rate.

    Edit:
    This function is called 38400 times (160x120) for each frame:
    // Read a byte of the pixel data
    inline byte Camera::readPixel(void)
    {
    byte b = 0;

    if (digitalReadFast(d7)) b |= 0x80;
    if (digitalReadFast(d6)) b |= 0x40;
    if (digitalReadFast(d5)) b |= 0x20;
    if (digitalReadFast(d4)) b |= 0x10;
    if (digitalReadFast(d3)) b |= 0x08;
    if (digitalReadFast(d2)) b |= 0x04;
    if (digitalReadFast(d1)) b |= 0x02;
    if (digitalReadFast(d0)) b |= 0x01;
    return b;
    }
    But if you defines this data PINS in this way:
    const byte D7 = 5; // from 20;
    const byte D6 = 21; // from 17;
    const byte D5 = 20; // from 16;
    const byte D4 = 6; // from 15;
    const byte D3 = 8; // from 14;
    const byte D2 = 7; // from 9;
    const byte D1 = 14; // from 8;
    const byte D0 = 2; // from 7;
    You can get the data with 1 call:
    // Read a byte of the pixel data
    inline byte Camera::readPixel(void)
    {
    return (GPIOD_PDIR & 0xFF);
    }
    Last edited by Adriano; 01-12-2016 at 12:30 PM. Reason: Code optimisation

  11. #11
    Junior Member
    Join Date
    Jul 2016
    Location
    FL
    Posts
    1
    Adriano, would you mind sharing some more info. on how you displayed the photo from the serial data?

  12. #12
    Quote Originally Posted by holydivar View Post
    Adriano, would you mind sharing some more info. on how you displayed the photo from the serial data?
    Writing a new image from the buffer sent over the serial:

    Code:
                    byte[] aby = new byte[3];
                    int iBuffLen = Math.Min(4096, serialPort1.BytesToRead - (serialPort1.BytesToRead % 2));
                    int iBuffPos = 0;
                    serialPort1.Read(buffer, 0, iBuffLen);
    
                    while (iBuffPos < iBuffLen)
                    {
                        aby[0] = buffer[iBuffPos++];
                        aby[1] = buffer[iBuffPos++];
    
                        aby[2] = (byte)((aby[1] & 0x1F) * 8 + 7);
                        aby[1] = (byte)(((aby[1] >> 5) + ((aby[0] & 0x07) << 3)) * 4 + 3);
                        aby[0] = (byte)(((aby[0] & 0xF8) >> 3) * 8 + 7);
    
                        tempImage.SetPixel(iImagePixelIndex % 160, iImagePixelIndex / 160, Color.FromArgb(aby[0], aby[1], aby[2]));
                        iImagePixelIndex++;
                        if (iImagePixelIndex >= 160 * 120) iImagePixelIndex = 0;
                    }
    
                    pictureBox1.Image = tempImage;
    If you want, I can send you the VS project - I didn't continued it, because it was to detect something on the cam and because the camera doesn't take photos but streams every single pixel, you can see the same (moving) object many times on the same frame.

  13. #13
    Quote Originally Posted by craiglindley View Post
    I posted this in project guidance but wanted to make sure more people saw it so I re-posted it here. I hope I didn't break any forum rules by doing so.

    I have just been successful interfacing a Teensy 3.1 to the $5 OV7670 Camera Chip ( without fifo) and the Adafruit 1.8" LCD display and wanted to share what I have learned. This hardware software combination captures QQVGA (160x120) images in RGB565 format at the rate of about 1.5 images/second. I'm sure the smart people on this forum could make it much faster and/or higher resolution. I've made the code available to anyone at: craigandheather.net/misc/TeensyCamera.zip

    You are free to use my code anyway you want but if you do something cool with it, please let me know.

    Cheers, for the Teensy's

    Craig Lindley
    calhjh@gmail.com

    Name:  IMG_6100.png
Views: 9088
Size:  130.0 KB
    Hello my name is Carlos

    I write to you because I need your help with a project with Tennsy 3.1, please, can you send me your programming codes and the circuit as you connected it? I would really appreciate it. I want to build a camera module in a Tennsy 3.6 and record the iamgenes in a micro sd card and your project would be very useful to continue with my project.

    Thank you
    email: km93@hotmail.es

  14. #14
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Since I couldn't find a github repo, and clearly people have already offered improvements, I took the liberty. https://github.com/sfinktah/teensy3-ov7670

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •