How to Connect OV7670 to Arduino Due

30,443

108

23

This instruction is a quick translation for an instruction in Russian available at

http://privateblog.info/arduino-due-i-kamera-ov7670-primer-ispolzovaniya/

Connection of OV7670 to Arduino Due is easier than to Arduino Uno: lower operation voltage - only 3V, higher internal speed, more memory and more contacts on the board.

Step 1: Schema to Connect OV7670 to Arduino Due

Let's start from connecting wires. Schema is available on the picture. You do not need any resistors. Operation voltage on Arduino Due is 3V. It is optimal for OV7670.

Step 2: Genering Clock Signal

After valid connection we need to get clock signal more than 8MHz. It is easy to get 10.5MHz.

int32_t mask_PWM_pin = digitalPinToBitMask(7);

REG_PMC_PCER1 = 1<<4; // activate clock for PWM controller

REG_PIOC_PDR |= mask_PWM_pin; // activate peripheral functions for pin (disables all PIO functionality)

REG_PIOC_ABSR |= mask_PWM_pin; // choose peripheral option

BREG_PWM_CLK = 0; // choose clock rate, 0 -> full MCLK as reference 84MHz

REG_PWM_CMR6 = 0<<9; // select clock and polarity for PWM channel (pin7) -> (CPOL = 0)

REG_PWM_CPRD6 = 8; // initialize PWM period -> T = value/84MHz (value: up to 16bit), value=8 -> 10.5MHz

REG_PWM_CDTY6 = 4; // initialize duty cycle, REG_PWM_CPRD6 / value = duty cycle, for 8/4 = 50%

REG_PWM_ENA = 1<<6; // enable PWM on PWM channel (pin 7 = PWML6)

Step 3: Configuring Register of OV7670

After generating clock signal we need to write proper values to registers of OV7670. You need to use 0x21 as address since TWI interface support 7bit addresses. It allows to write register, but reading will be with issues.

void write(uint8_t regID, byte regDat){

Wire.beginTransmission(address);

Wire.write(regID & 0x00FF);

Wire.write(regDat & 0x00FF);

if(Wire.endTransmission(true)){

Serial.print("error write reg ");

Serial.println(regID);

}

delay(20);

}

values for register you will find later in the source code.

Step 4: Transmitting an Image

After generating clock signal and writing register we need to transmit image. Arduino Due provides UART interface for that purpose.

...

Serial.begin(460800);

...

it is better to use custom function to speedup transmission.

static inline int uart_putchar(const uint8_t c) {

while(!(UART->UART_SR & UART_SR_TXRDY));

UART->UART_THR = c;

return 0;

}

We can try to prevent some artifacts by using buffer inside Arduino Due. In this case we write image to buffer and only after that transmit:

static void captureImg(uint16_t wg, uint16_t hg){

uint16_t y, x;

Serial.println("*RDY*");

while (!(REG_PIOB_PDSR & (1 << 21)));//wait for high while

((REG_PIOB_PDSR & (1 << 21)));//wait for low

y = hg;

while (y--){

x = wg;

while (x--){

while ((REG_PIOD_PDSR & (1 << 10)));//wait for low

myImage[y][x] = (REG_PIOC_PDSR & 0xFF000) >> 12;

while (!(REG_PIOD_PDSR & (1 << 10)));//wait for high

while ((REG_PIOD_PDSR & (1 << 10)));//wait for low

while (!(REG_PIOD_PDSR & (1 << 10)));//wait for high

}

}

for (y = 0; y < hg; y++) {

for (x = 0; x < wg; x++) {

uart_putchar(myImage[y][x]);

}

}

}

Step 5: Receiving

Now we need to receive the image. For this purpose the same java application as for example with Arduino Uno will be used.

Framegrabber

How to run Framegrabber

How to connect OV7670 and Arduino Uno

Step 6: Results

The result you can see on the picture.

There is problem with Arduino Due: I wasn't able to get speed more that 460800, but it should not have impact on quality of the picture since it buffered.

DIY Summer Camp Challenge

Participated in the
DIY Summer Camp Challenge

Share

    Recommendations

    • Frozen Treats Challenge

      Frozen Treats Challenge
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest
    • Classroom Science Contest

      Classroom Science Contest

    23 Discussions

    0
    None
    mohsen_mokrian

    Question 9 months ago on Step 4

    What does this part of the code do?

    -----------------------------------------------------

    while ((REG_PIOD_PDSR & (1 << 10)));//wait for low

    myImage[y][x] = (REG_PIOC_PDSR & 0xFF000) >> 12;

    while (!(REG_PIOD_PDSR & (1 << 10)));//wait for high

    while ((REG_PIOD_PDSR & (1 << 10)));//wait for low

    while (!(REG_PIOD_PDSR & (1 << 10)));//wait for high

    -------------------------------------------------------

    Can you explain about REG_PIOD_PDSR ? What is that ? What's it's role here?

    0
    None
    Pervinnn

    10 months ago

    Hi, thanks for sharing this project. But I have problem with code, I am using with Arduino Mega.This code is suitable for Arduino Mega? Please help me((

    0
    None
    JohnR769

    10 months ago

    Here's a Matlab code I did very fast to read a picture :

    delete(instrfind);

    s = serial('COM17','BaudRate', 250000, 'Terminator', 'CR/LF');

    fopen(s);

    a = zeros(320,240);

    for i=1:240

    a(:,i) = fread(s,320);

    end

    image(a);

    colormap(gray);

    fclose(s)

    I changed the baud rate in the Arduino code.

    It's very noisy but I get a picture !!

    0
    None
    PangK

    Question 1 year ago on Step 2

    I encountered the following errors when compiling the sketch.

    Arduino: 1.6.5 (Windows 8.1), Board: "Arduino Duemilanove or Diecimila, ATmega328"

    test_ov7670.ino: In function 'void setup()':
    test_ov7670:503: error: 'REG_PMC_PCER1' was not declared in this scope
    test_ov7670:504: error: 'REG_PIOC_PDR' was not declared in this scope
    test_ov7670:505: error: 'REG_PIOC_ABSR' was not declared in this scope
    test_ov7670:506: error: 'REG_PWM_CLK' was not declared in this scope
    test_ov7670:507: error: 'REG_PWM_CMR6' was not declared in this scope
    test_ov7670:508: error: 'REG_PWM_CPRD6' was not declared in this scope
    test_ov7670:509: error: 'REG_PWM_CDTY6' was not declared in this scope
    test_ov7670:510: error: 'REG_PWM_ENA' was not declared in this scope
    test_ov7670.ino: In function 'void captureImg(uint16_t, uint16_t)':
    test_ov7670:528: error: 'REG_PIOB_PDSR' was not declared in this scope
    test_ov7670:529: error: 'REG_PIOB_PDSR' was not declared in this scope
    test_ov7670:535: error: 'REG_PIOD_PDSR' was not declared in this scope
    test_ov7670:536: error: 'REG_PIOC_PDSR' was not declared in this scope
    test_ov7670:537: error: 'REG_PIOD_PDSR' was not declared in this scope
    test_ov7670:538: error: 'REG_PIOD_PDSR' was not declared in this scope
    test_ov7670:539: error: 'REG_PIOD_PDSR' was not declared in this scope
    test_ov7670.ino: In function 'int uart_putchar(uint8_t)':
    test_ov7670:564: error: 'UART' was not declared in this scope
    test_ov7670:564: error: 'UART_SR_TXRDY' was not declared in this scope
    test_ov7670:565: error: 'UART' was not declared in this scope
    'REG_PMC_PCER1' was not declared in this scope

    Please advise. Thanks

    1 answer
    0
    None
    electrical inventorPangK

    Answer 1 year ago

    Hi,

    It looks like you are compiling the code for the atmega328, the processor in the uno. This code is specific to the processor on the arduino due, the SAM3X8E.

    Make sure that the IDE is set to the arduino due. If it doesn't show up, add it via the boards manager.

    0
    None
    abeerkd00

    1 year ago

    I take the picture but it not clear , how I make it clear . Please help me

    1 reply
    0
    None
    jlsilicon

    1 year ago

    Why is this Only in Black-and-White ?

    - As your Image example, same on my Camera...

    The Registers look correct.

    How to get it in Color ... ???

    0
    None
    jbc8

    2 years ago

    Here's a simpler Frame Grabber written in Processing. Processing has Serial and Image processing built-in. You have to download the Processing IDE. Here's the source code to the Processing Frame Grabber: https://pastebin.com/weAEcrQG

    0
    None
    IbsonM

    2 years ago

    nice tutorial,pls ineed your help o the project am working on.am using arduino mega with ov 7670 ,the camera snaps the picture and saves it on sd card behind the tft display and the tft displays the image on the memory card

    2 replies
    0
    None
    shivam.gupta2015aIbsonM

    Reply 2 years ago

    Hey Buddy! I'm working on exact same project and I'm using arduino mega with ov7670 camera to capture image and save it on sd card.....did you find any tutorials or code or libraries anything....If you did then please give me the link.....I'm really in need.

    0
    None
    СергейЧ5IbsonM

    Reply 2 years ago

    Sorry. I didn't try with mega - it is as fast as Uno. No sence.

    0
    None
    LashaP

    2 years ago

    Due is on port 26, after run eclipse project, console shows:
    Port name: COM1

    Port name: COM26

    Looking for image
    Please help !!!

    1 reply
    0
    None
    LashaP

    2 years ago

    after run eclipse project, where will be frames shown?

    1 reply
    0
    None
    AdityaP120

    2 years ago

    I am getting error while generating Clocl Signal

    REG-PMC_PCER1 was not declared in this scope

    0
    None
    AP_tech

    2 years ago

    Do you have an Arduino Due code monochromatic at the lowest resolution for this Camera?

    0
    None
    HuseyinM

    2 years ago

    That's a great work and i have a question about this projec how can i save picture to sd card with button ??is it possible??

    0
    None
    Dylan91

    2 years ago

    Thanks for sharing nice tutorial. can it work on a uno or a mega?