ov7670 camera interfacing-with-arduino-microcontroller
TRANSCRIPT
INTRODUCTION
In this project our aim is to interface OV7670 camera module with ATmega based Arduino Microcontroller board. Camera modules are widely used in the world of electronics. Our aim is to show how to connect, configure and get a test image using a small program on java, it will be an excellent starting point for further experiments. Currently ov7670 camera is the affordable image acquisition module for embedded applications.
Our goal is to interface the camera with Arduino which are much efficient, low cost easily available and easy to configure. These are the major reasons for which here an Arduino is used. In spite of the following advantages there are some disadvantages too, that are:
o Very limited I/O pins o Limited memory capacityo Slow Processing speed
The above disadvantages are the challenges in interfacing the camera with an Arduino and these are to be overcome by applying some Data handling and manipulation techniques. The OV7670 Camera module talks over a modified I2C protocol that is known as SCCB (Serial Camera Control Bus). This protocol is intended to be used with OmniVision’s camera modules.
This project is intended to provide a low cost and easily configurable image capturing solution, that can be used in real-time Time-lapse photography, image acquisition systems or as security cameras.
The camera module is accessed by SCCB (I2C) protocol by the Arduino. The Data sent by the camera is parallel over data channel D0-D7, Data received by the Arduino is sent to the computer over serial communication and this follows the rule of sending pixel data of one row at a time. This help to overcome the problem of slow processing and slow data transmission rate of the Arduino boards
1
Figure 1: Block diagram for Camera-Arduino interface
2
OV7670 camera module
Arduino UNO I2C
COMPUTER Serial
HARDWARE
2.1 CAMERA MODULE (OV7670)
The camera module has an onboard CMOS sensor designed for mobile application where low power consumption and small size are of utmost importance. Proprietary sensor technology utilizes advanced algorithms to cancel Fixed Pattern Noise (FPN), eliminate smearing, and drastically reduce blooming. All required camera functions are programmable through the Serial Camera Control Bus (SCCB) interface.
The device can be programmed to provide image output in various fully processed and encoded formats.
Resolution VGA (640 x 480); - QVGA (320 x 240); - CIF (352 x 240); - QCIF (176 × 144)
Transfer rate up to 30 fps
Image encoding RGB 565/555, YUV / YCbCr 4: 2: 2
Interface I2C interface interaction / SCCBTable 1: Camera module Specification
2.2 Two Wire SCCB Interface
The modified 2-wire implementation allows for a SCCB master device to interface with only one Slave device. This 2-wire application is implemented in the camera chip reduced pin package products where the SCCB_E signal is not available externally.
Figure 2: Two Wire SCCB Interface
3
2.3 Pin Functions
Signal Name Signal Type DescriptionSCCB_Ea Output Master drives SCCB_E at logical 1
When the bus is idle. Drives at logical 0 when the master asserts Transmissions or the system is in Suspend mode.
SIO_C Output Master drives SIO_C at logical 1 when The bus is idle. Drives at logical 0 and 1 when SCCB_E is driven At 0. Drives at logical 0 when the system is Suspend mode.
SIO_D I/O Serial I/O Signal 0 Input and Output - remains floating when the bus is idle and drives to logical 0 when the system is in Suspend Mode.
Table-2: Master Device Pin Descriptions
Signal Name Signal Type DescriptionSCCB_Ea Input Input pad can be shut down when the System
is in Suspend mode.SIO_C Input Input pad can be shut down when the System
is in Suspend mode.SIO_D I/O Serial I/O Signal 0 Input and Output - input
pad can be shut down When the system is in Suspend mode.
Table-3: Slave Device Pin Descriptions
2.4 Application
Some of the applications of camera module are: PC Camera, and Cellular phones, Video conference equipment, Machine vision, Security camera, Biometrics, Digital Still Cameras
4
2.5 ARDUINO
Arduino is an open-source platform used for building electronics projects. Arduino consists of both a physical programmable circuit board (often referred to as a microcontroller) and a piece of software, or IDE (Integrated Development Environment) that runs on your computer, used to write and upload computer code to the physical board.
Microcontroller ATmega 328-pOperating Volts 5VInput Voltage 7-20vDigital I/O Pins 14Analog Input 6PWM pins 6Flash Memory 32 KbSRAM 2 KbEEPROM 1 KbClock Speed 16 MHzCurrent 50 mADC Current at Digital I/O 20 mA
Table 4: Arduino UNO Specification
5
CAMERA-ARDUINO INTERFACING CIRCUIT
For interfacing the camera module with the Arduino we need a circuit that will provide the mount points as well as the voltage Pull-up and Pull-down facilities. below is a Typical Block representation of the hardware connection between OV7670 and Arduino UNO. Oscillator used here is optional, we will be Arduino’s onboard crystal oscillator is being used to generate the XCLK.
3.1 Block diagram for Arduino-Camera interfacing
Arduino
Figure 3: Block representation of Connection
6
Camera Module
SCL, SDA
HREF, VSYNC
DATA
PCLK
PWDN
+3.3V, GND
Oscillator
XCLK
3.2 Pin Configuration
Pin configuration is one of the most important data that must be kept in mind while designing the interfacing circuit and must be followed to ensure proper communication Arduino and Camera Module.
Pin Type Description
VDD Supply Power supply
GND Supply Ground level
SDIOC Input SCCB clock
SDIOD Input/output SCCB data
VSYNC Output Vertical synchronization
HREF Output Horizontal synchronization
PCLK Output Pixel clock
XCLK Input System clock
D0-D7 Output Video parallel output
RESET Input Reset (Active low)
PWDN Input Power down (Active high)
Table 5: Arduino Pin Configuration
7
3.3 Circuit DiagramThe Arduino is rated for 5V and the camera module is of 3.3V. Hence, the voltage divider for the XCLK will be used (two resistors of 4.7KΩ) to pull down the voltage to ~3V. Furthermore, OV7670 does not provide output voltages higher than 3.3V which is a quite low for Arduino to reads the input as high (5V), so we will connect pull-up resistors.
Here external signal is provided to D12 to capture image and to D8 for image to PDF conversion, both are active high with 0.3 sec high pulse.
Figure 4: Circuit Diagram for OV7670-Arduino Interfacing [Source: privateblog.info]
PROGRAM FLOW FOR INTERFACING8
OV7670 camera module captures image during LOW at VSYNC and sends out row data during the HIGH at HREF. The Arduino receives the parallel data input from camera module image sensor and converts it to the serial data and also provides initialization to the registers of the OV7670 Module.
Figure 5: Typical Data flow diagram
Basic steps in the code are:
1. Generation of PWM
2. Start I2C Communication
3. Write default register values
4. Receive data from camera
9
Arduino
Figure 6: Flow diagram for initialization of camera module
4.1 Configure Arduino that will give 8 MHz at Pin D11
DDRB | = (1 << 3); // pin 11,
The ASSR & = ~ (_BV (EXCLK) | _BV (the AS2));
TCCR2A = (1 << COM2A0) | (1 << WGM21) | (1 WGM20 <<);
TCCR2B = (1 << WGM22) | (CS20 1 <<);
OCR2A = 0; // (F_CPU) / (2 * (X + 1))of d0 of low-
DDRC & = ~ 15; // d3 camera
DDRD & = ~ 252; // d7-d4 and interrupt pins
10
4.2 Configure I2C interface
TWSR & = ~ 3; // preset for the disable the TWI
TWBR = 72; // set to 100 kHz
Figure 7: Flow Diagram of I2C communication
4.3 Initialization of Camera Module
Figure 8: Write default Register values
11
ARDUINOWrite Default register
Values with I2C Camera Module
WrReg (0x12, 0x80);
_delay_ms (100);
wrSensorRegs8_8 (ov7670_default_regs);
WrReg (REG_COM10, 32); // PCLK does not HBLANK Locality toggle on.
4.4 Receiving Image bytesThe camera module sends parallel image data bytes where it is buffered till receiving full row data then this data is sent over serial communication to the pc as shown in figure 9.
Figure 9: Receiving Image Bytes
12
OV7670 camera Module
Arduino UNO
ComputerParallel Data Serial DataMUX
Buffer
RAM
OV7670 MODULE DATA TRANSFER
The SIO_C signal is a single-directional, active-high, control signal that must be driven by the master device. It indicates each transmitted bit. The master must drive SIO_C at logical 1 when the bus is idle. A data transmission starts when SIO_C is driven at logical 0 after the start of transmission as shown in figure 10 & 11. A logical 1 of SIO_C during a data transmission indicates a single transmitted bit. Thus, SIO_D can occur only when SIO_C is driven at 0.
5.1 Timing Diagram
Figure 10: SCCB Timing Diagram
Figure 11: Horizontal Timing Diagram
13
Camera module sends row data between every HSYNC LOW and during each HSYNC HIGH, the HREF follows the HIGH and both of this occurs between the two HIGH of the VSYNC signal as shown in figure 12, that means all the row data is sent over the VSYNC at LOW.
Figure 12: VGA Frame Timing
14
PIXEL DATA BYTE TO IMAGE CONVERSIONFor this purpose a java program is being used, that will grab the serial input data (bits) stream from Arduino at specific COM port that will generate a valid BMP image from it , the code works as illustrated below in figure 13.
6.1 Program Work Flow
Figure 13: Algorithm of data to image generation
15
6.2 COM Port selection (JAVA)
if(!enumeration.hasMoreElements())
break;
portId = (CommPortIdentifier)enumeration.nextElement();
if(portId.getPortType() == 1)
System.out.println((new StringBuilder()).append("Port name:
").append(portId.getName()).toString());
SimpleRead simpleread;
if(portId.getName().equals("COM5"))
simpleread = new SimpleRead();
6.3 Port parameters (JAVA)
serialPort = (SerialPort)portId.open("SimpleReadApp", 1000);
inputStream = serialPort.getInputStream();
serialPort.setSerialPortParams(0xf4240, 8, 1, 0);
int i = 0;
6.4 Waiting for an (data stream) input from Arduino (JAVA)
do // infinite loop starts here
System.out.println("started the if else ");
16
while(!isImageStart(inputStream, 0))
// image Generation code goes here
while(true);
catch(Exception exception)
6.5 Image generation from received data stream (JAVA)
Figure 14: Algorithm for image generation from received data steam
17
while(!isImageStart(inputStream, 0))
System.out.println("Looking for image");
System.out.println((new StringBuilder()).append("Found image:
").append(i).toString());
for(int j = 0; j < 240; j++)
for(int l = 0; l < 320; l++)
int j1 = read(inputStream);
ai[j][l] = (j1 & 0xff) << 16 | (j1 & 0xff) << 8 | j1 &0xff;
for(int k = 0; k < 240; k++)
for(int i1 = 0; i1 < 320; i1++)
ai1[i1][k] = ai[k][i1];
BMP bmp = new BMP();
bmp.saveBMP((newStringBuilder()).append("c:/out/").append(i+
+).append(".bmp").toString(), ai1); System.out.println((new
StringBuilder()).append("Saved image: ").append(i).toString());
18
HANDLING MULTIPLE SCANS (CMD)It is sometime needed to perform multiple scans. That means the scanning mechanism is provided input for scan again and again, then this program as shown in figure 15 proves to be useful it takes the user inputs to perform a certain number of scans and then control the Arduino or any other microcontroller to perform the scanning and PDF conversion till it reaches the provided number of scans.
Figure 15: Algorithm to handle multiple Scans
19
IMAGE TO PDF CONVERSION (CMD)
Now after generating the images we need to convert those images to a PDF file for which we will use the software ConvertITP.EXE. following is the algorithm for Image to PDF conversion.
Figure 16: Algorithm Image to PDF generation
8.1 Code for the Image to PDF conversion
@echo off
echo by somnath
echo .
set CITP="%ProgramFiles%\Softinterface, Inc\Convert Image To PDF\
ConvertITP.EXE"
set InputFolder=C:\out\
20
set OutputFolder=C:\out\
REM Convert from JPG to PDF
%CITP% /S%InputFolder%\*.bmp /T%OutputFolder%\all_img.PDF /F0 /+
ren %InputFolder%\all_img.PDF all_img-%date:~10,4%%date:~7,2%
%date:~4,2%_%time:~0,2%%time:~3,2%.pdf
mkdir %InputFolder%\original_images_%date:~10,4%%date:~7,2%
%date:~4,2%_%time:~0,2%%time:~3,2%
move /Y %InputFolder%\*.bmp %InputFolder%\original_images_%date:~10,4%
%date:~7,2%%date:~4,2%_%time:~0,2%%time:~3,2%
echo `
echo DONE!
echo .
exit
SOFTWARE SETUP GUIDE
21
-Extract the Extra.zip file. Then copy:
"win32com.dll" to "..\jdk1.8.0_74\jre\bin” Directory.
the "comm.jar" in "...\jdk1.8.0_74\jre\lib\ext"
"javax.comm.properties" in "...\jdk1.8.0_74\jre\lib"
Copy “out” folder to C:\\ Drive
Install the software “Installme.exe”
Double click on SCAN.bat from c:\\out folder
It will start the program
CONCLUSION
22
Interfaced the OV7670 Camera module with Arduino at 8 MHz XCLK pulse which is generated by the Arduino board itself. Arduino talk to the camera module using I2C to write/read camera registers. Frames are read one by one in a brute-force manner. Currently the camera module is configured to work with QCIF (176x144) format in order to eliminate timing issues. Pixels of a given frame are stored in the java program and on completion of whole row data the JAVA program generates the valid BMP image. The most challenging task for interfacing Arduino &Camera is the low memory capacity of Arduino microcontroller due to which we are unable to store a full image at once, to overcome this the JAVA program is implemented that receive the Data Bits of one row at a time. It has also to be noted that there are microcontrollers out there that actually have dedicated peripherals/devices for interfacing with camera modules such as the OV7670, but the interface of OV7670 Camera Module with Arduino Microcontroller provides us a easy to use, maintain, low cost and a configurable system that can be used in various fields of image acquisition.
References
23
[1] N. Sriskanthan and Tan Karand. “Bluetooth Based Home Automation System”.Journal of Microprocessors and Microsystems, Vol. 26, pp.281-289, 2002. [2] Al-Ali, Member, IEEE & M. AL-Rousan,“Java-Based Home Automation System R.” IEEE Transactions on Consumer Electronics, Vol. 50, No. 2, MAY 2004
[3] The Java Tutorials: All About Sockets [Online]. Available:http://docs.oracle.com/javase/tutorial/networking/sockets, March2013
[4] L. Lo Presti, M. La Cascia, “Real-Time Object Detection in Embedded Video Surveillance Systems,” Ninth International Workshop on Image Analysis for Multimedia Interactive Services, 7-9 May 2008, pp. 151-154.
[5] http://arduino.cc/en/Main/ArduinoBoardADK
[6] https://forum.arduino.cc/index.php?topic=211741.0
[7] http://www.dejazzer.com/coen4720/labs/lab11_camera.pdf
24