Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Arduino Camera (OV7670) Tutorial - Microcontroller Tutorials

This document provides instructions for using an OV7670 camera module with an Arduino board. It includes a wiring diagram and sample code to capture images with the camera and send them to a computer via the Arduino USB port. While the Arduino doesn't have enough processing power for complex image or video tasks, this camera module allows capturing VGA-sized pictures for simple projects.

Uploaded by

Ivan Berlot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Arduino Camera (OV7670) Tutorial - Microcontroller Tutorials

This document provides instructions for using an OV7670 camera module with an Arduino board. It includes a wiring diagram and sample code to capture images with the camera and send them to a computer via the Arduino USB port. While the Arduino doesn't have enough processing power for complex image or video tasks, this camera module allows capturing VGA-sized pictures for simple projects.

Uploaded by

Ivan Berlot
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

HOME TUTORIALS PROJECTS FEATURES REFERENCE ABOUT

Arduino Camera (OV7670) Tutorial


Ad
Search …

Electronics projects like movement and obstacle detection, drones, SEARCH

and robots require a camera for capturing images. I personally


recommend using a Raspberry Pi or BeagleBone Black for these kind Ad

of projects because the Arduino doesn’t have enough juice to handle


Magnetic encoder manufacturer the complexities of pictures and, more so, videos. However, if the
project doesn’t require a high resolution image then the OV7670
Open
camera module might be for you. This arduino camera tutorial
features such camera and how you can use it to capture VGA-sized
pictures.
Leading encoder manufacturer
RECENT POSTS Cyber Ofertas MSC - Tu momento es ahora
Cruceros Bs As Brasil - Salidas 14 y 23 de Enero 69.990 Pesos -
Hasta 6 cuotas s/interés promocionesmsc.com.ar Open

Featured: Seeeduino Nano


The Seeeduino Nano is I managed to buy a OV7670 camera module that looks like this:
Seeedstudio’s version of the
new Arduino …
Related posts:

Solid State Relay Tutorial | WiFi Switch


Relays in electronics are
useful for controlling a high-
power load …

Detect Objects Arduino


with Camera and NRF24L01
Automobile Wire Harness - The Ultimate Guide
Sponsored Post by Cloom
Cloom, a reliable wire
harness, and …

Custom PCB- To Getting The Best Results Uploading Camera HC-SR04 and
PCB is an indispensable unit Images from Arduino
in the electronic products
that …

As you can see, the module has 18 output pins with the following
functions:
Optimal Circuit Boards Information Guide
Guest Post from OurPCB Do
you know what the PCB …

RECOMMENDED PRODUCT:

ESP32 WiFi Camera

Some modules contain 16 pins; these lack the RESET and PWON
pins. Since I own the 18-pin OV7670 module, the rest of the tutorial
will be based on such module.

If you noticed, the OV7670 camera module has a serial data and
clock pin. This means that the Arduino can communicate with the
module using I2C. Also, the module operates on 3.3 V so if we are to
send data to the OV7670 from the Arduino, we must use a voltage
divider to bring the level down.

Here is a common wiring diagram used to build an Arduino camera


using the OV7670 camera module:

Ad

Encoder products company

Open

Note that there is no e cient way for the Arduino to display the
captured image. What it only does is command the module to take a
picture, acquire the image and then send it to a computer via the
Arduino’s USB port. Thus, you need to have another application that
will display the image from the USB port to your desktop computer.

Here is a code to be used to capture an image using the Arduino and


OV7670 camera module:

1 #define F_CPU 16000000UL


2 #include <stdint.h>
3 #include <avr/io.h>
4 #include <avr/interrupt.h>
5 #include <util/twi.h>
6 #include <util/delay.h>
7 #include <avr/pgmspace.h>
8 #include "ov7670.h"
9 /* Configuration: this lets you easily change between different reso
10 * You must only uncomment one
11 * no more no less*/
12 #define useVga
13 //#define useQvga
14 //#define useQqvga
15
16 static inline void serialWrB(uint8_t dat){
17 while(!( UCSR0A & (1<<UDRE0)));//wait for byte to transmit
18 UDR0=dat;
19 while(!( UCSR0A & (1<<UDRE0)));//wait for byte to transmit
20 }
21 static void StringPgm(const char * str){
22 do{
23 serialWrB(pgm_read_byte_near(str));
24 }while(pgm_read_byte_near(++str));
25 }
26 static void captureImg(uint16_t wg,uint16_t hg){
27 uint16_t lg2;
28 #ifdef useQvga
29 uint8_t buf[640];
30 #elif defined(useQqvga)
31 uint8_t buf[320];
32 #endif
33 StringPgm(PSTR("RDY"));
34 //Wait for vsync it is on pin 3 (counting from 0) portD
35 while(!(PIND&8));//wait for high
36 while((PIND&8));//wait for low
37 #ifdef useVga
38 while(hg--){
39 lg2=wg;
40 while(lg2--){
41 while((PIND&4));//wait for low
42 UDR0=(PINC&15)|(PIND&240);
43 while(!(PIND&4));//wait for high
44 }
45 }
46 #elif defined(useQvga)
47 /*We send half of the line while reading then half later */
48 while(hg--){
49 uint8_t*b=buf,*b2=buf;
50 lg2=wg/2;
51 while(lg2--){
52 while((PIND&4));//wait for low
53 *b++=(PINC&15)|(PIND&240);
54 while(!(PIND&4));//wait for high
55 while((PIND&4));//wait for low
56 *b++=(PINC&15)|(PIND&240);
57 UDR0=*b2++;
58 while(!(PIND&4));//wait for high
59 }
60 /* Finish sending the remainder during blanking */
61 lg2=wg/2;
62 while(!( UCSR0A & (1<<UDRE0)));//wait for byte to tr
63 while(lg2--){
64 UDR0=*b2++;
65 while(!( UCSR0A & (1<<UDRE0)));//wait for by
66 }
67 }
68 #else
69 /* This code is very similar to qvga sending code except we
70 while(hg--){
71 uint8_t*b=buf,*b2=buf;
72 lg2=wg/5;
73 while(lg2--){
74 while((PIND&4));//wait for low
75 *b++=(PINC&15)|(PIND&240);
76 while(!(PIND&4));//wait for high
77 while((PIND&4));//wait for low
78 *b++=(PINC&15)|(PIND&240);
79 while(!(PIND&4));//wait for high
80 while((PIND&4));//wait for low
81 *b++=(PINC&15)|(PIND&240);
82 while(!(PIND&4));//wait for high
83 while((PIND&4));//wait for low
84 *b++=(PINC&15)|(PIND&240);
85 while(!(PIND&4));//wait for high
86 while((PIND&4));//wait for low
87 *b++=(PINC&15)|(PIND&240);
88 UDR0=*b2++;
89 while(!(PIND&4));//wait for high
90 }
91 /* Finish sending the remainder during blanking */
92 lg2=320-(wg/5);
93 while(!( UCSR0A & (1<<UDRE0)));//wait for byte to tr
94 while(lg2--){
95 UDR0=*b2++;
96 while(!( UCSR0A & (1<<UDRE0)));//wait for by
97 }
98 }
99 #endif
100 }
101 int main(void){
102 cli();//disable interrupts
103 /* Setup the 8mhz PWM clock
104 * This will be on pin 11*/
105 DDRB|=(1<<3);//pin 11
106 ASSR &= ~(_BV(EXCLK) | _BV(AS2));
107 TCCR2A=(1<<COM2A0)|(1<<WGM21)|(1<<WGM20);
108 TCCR2B=(1<<WGM22)|(1<<CS20);
109 OCR2A=0;//(F_CPU)/(2*(X+1))
110 DDRC&=~15;//low d0-d3 camera
111 DDRD&=~252;//d7-d4 and interrupt pins
112 _delay_ms(3000);
113 //set up twi for 100khz
114 TWSR&=~3;//disable prescaler for TWI
115 TWBR=72;//set to 100khz
116 //enable serial
117 UBRR0H=0;
118 UBRR0L=1;//0 = 2M baud rate. 1 = 1M baud. 3 = 0.5M. 7 = 250k
119 UCSR0A|=2;//double speed aysnc
120 UCSR0B = (1<<RXEN0)|(1<<TXEN0);//Enable receiver and transmi
121 UCSR0C=6;//async 1 stop bit 8bit char no parity bits
122 camInit();
123 #ifdef useVga
124 setRes(VGA);
125 setColorSpace(BAYER_RGB);
126 wrReg(0x11,25);
127 #elif defined(useQvga)
128 setRes(QVGA);
129 setColorSpace(YUV422);
130 wrReg(0x11,12);
131 #else
132 setRes(QQVGA);
133 setColorSpace(YUV422);
134 wrReg(0x11,3);
135 #endif
136 /* If you are not sure what value to use here for the divide
137 * Values I have found to work raw vga 25 qqvga yuv422 12 qv
138 * run the commented out test below and pick the smallest va
139 while (1){
140 /* captureImg operates in bytes not pixels in some c
141 * So for the width (if you were reading 640x480) yo
142 /*uint8_t x=63;//Uncomment this block to test divide
143 do{
144 wrReg(0x11,x);
145 _delay_ms(1000);*/
146 #ifdef useVga
147 captureImg(640,480);
148 #elif defined(useQvga)
149 captureImg(320*2,240);
150 #else
151 captureImg(160*2,120);
152 #endif
153 //}while(--x);//Uncomment this line to test divider
154 }
155 }

Note that this code requires the ov7670 library which was originally
done by ComputerNerd. Download the library on this repository.

Next, we need a desktop application that will read the image data
from the Arduino’s USB port and display it on screen. Download this
C/C++ package and run it using G++ for Linux or DevC++ for
Windows. Unfortunately, C/C++ programming on desktop computers
is beyond the scope of this tutorial so I can’t help you with this
further.

Hopefully I’ve helped you in setting up the OV7670 camera module


with the Arduino UNO. However, I would still recommend you use
more advanced boards like Raspberry Pi or BeagleBone Black to
handle images as the Arduino UNO is simply not powerful enough.
Check out my tutorial on using a web camera with the Raspberry Pi
and create cool projects!

4 thoughts on “Arduino Camera (OV7670) Tutorial”

Partha
June 7, 2019 at 11:27 am

Thank you very much for sharing your hard work. It helps people like me immensely

Reply

Christophe Trouillefou
June 20, 2019 at 11:31 pm

Hello,
I'm trying desparately to make the camera OV7670 that contains a FIFO work with an
arduino uno and record pictures on an SD card. If you have a simple code to make this
dream become true (or even a good link to help).
Everywhere on the web I only nd info about OV7670 (without Fifo) + Uno.
Thx

Reply

lady saori
July 12, 2019 at 11:56 am

very usefull .thanks for this hard work. I am also working on this project and i really
want to know how to print images and treat them to prohibit that a car start.thank
i am waiting

Reply

Jeevan
October 11, 2019 at 9:15 pm

i'm working with the OV7670 module on an altera FPGA Board and i'm encountering
some problems wich i cannot solve.
The YCbCr422 encoding is the default format used by the camera module right?
If so what are the exact data position/order that the camera is using on it's D0-7 pin?
When you explain how the data is stored for the YCbCr format you make a block
scheme.
What those blocks represents? Are they Bytes? If so one block would be the
information present on D0-7 pins at a certain pclk edge?

Reply

Leave a Reply
Your email address will not be published. Required elds are marked *

Comment

Name *

Email *

Website

POST COMMENT

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CATEGORIES TAGS RECENT COMMENTS

Arduino Projects (10) accelerometer ajax apache asm breakout board camera ccp control Devendra on Intro to NodeMCU and
Arduino Tutorial (58) Arduino IDE
system esp8266 gprs gps gsm hall e ect hc-sr04
Teach Me Microcontrollers! BeagleBone Tutorial (16) Adri on Arduino GPRS Tutorial
ESP8266 Projects (3) hd44780 lcd humidity i2c IMU Internet interrupt iot Mahmoud on Beaglebone Black
Arduino, PIC, Raspberry Pi, STM32, ESP8266 Tutorial (15) javascript LED linux gyro motion sensor motor mpu6050 mqtt Serial Connection to Arduino
Beaglebone Tutorials Features (7) oled pdf pic16f877a pinout pwm python schematic sensor maynerd on Using an Arduino
General (4) serial serial-to-usb sim800 spi temperature ttl-to-usb ubuntu Turbidity Sensor
All Rights Reserved 2019 Guest Post (7) continuous adc web server xc8
Meg on LPG Sensor with MQ-6 and
PIC Projects (5) Arduino
PIC Tutorial (20)
Product Reviews (3)
Raspberry Pi Projects (1)
Raspberry Pi Tutorial (14)
Reference (8)
STM32 Tutorial (6)

Developed by Dessign

You might also like