Interfacing of MATLAB with Arduino for Object Detection Algorithm Implementation using Serial Communication

DOI : 10.17577/IJERTV3IS100766

Download Full-Text PDF Cite this Publication

Text Only Version

Interfacing of MATLAB with Arduino for Object Detection Algorithm Implementation using Serial Communication

Panth Shap

Electronics and Communication Engineering Shankersinh Vaghela Bapu Institute of Technology Gandhinagar, India

Tithi Vyas2

Electronics and Communication Engineering Shankersinh Vaghela Bapu Institute of Technology Gandhinagar, India

Abstract The paper explains proposed algorithm for object detection using image processing and manipulation of the output pin state of Arduino board with ATmega 8 controller by tracking the motion of the detected object. The object detection algorithm has been developed on MATLAB platform by the combination of several image processing algorithms. Using the theory of Image Acquisition and Fundamentals of Digital Image Processing, the object has been detected in real time. Various features of an object such as the shape, size and color can be used to detect and track the object. The variation in vertical and horizontal axis of detected object is moderated by serial communication port and using serial data communication, the state of Arduino board pin has been controlled. MATLAB programming develops a computer vision system in the real time for object detection and tracking using camera as an image acquisition hardware. Arduino programming provides an interfacing of a hardware prototype with control signals

ATmega 8 microcontroller and USB to RS232 TTL Converter Adapter Module PL2303HX.

The goal of this work is to visually detect and track an object in a region and send the data to the Arduino board to glow LEDs connected with the microcontrollers digital output port. The most challenging issue raised while developing the object detection algorithm is that of selecting the feature to use for segmenting the object. Here, using approach based on objects shape is very difficult and less efficient with the system in real time constraints. So, rather using the segmentation by shape, the color of an object has been taken into consideration. The color of an object is a subject of its lightning condition. Working of the proposed prototype system can be understood by the following flowchart shown in fig. 1

Image Acquisition using camera

generated by real time object detection and tracking.

KeywordsMATLAB, Arduino board, ATmega 8, Digital Image processing, Serial data communication, Thresholding.

Adjusting Region of Interest


    Serial Communication

    Detection of Horizontal and Vertical axis of an object

    Calibration for color detection

    The revolutionized computers open up the chances of using images and video frames as an input signals of the signal processing. Such signal processing is named as image processing. Image processing transforms various sets of characteristics of image parameters into output as control signals. The constant revolution in the field of digital image processing opens up a multitude of application in various areas, in which innovative technologies could have been developed. The best platform on which many image processing algorithms have been developed so far is MATLAB. Major advantage of using MATLAB as an image processing algorithm development environment is its built in image processing functions and its compatibility with hardware such as cameras, Arduino Raspberry Pie and many more.

    Proposed output

    The aim of this paper is to propose a prototype model which both detects and tracks an object with distinct features and generates and sends a control signal to the hardware according to the position of the object. This proposed

    prototype system contains both software and hardware tools. Software includes MATLAB and Arduino IDE software whereas hardware includes Camera, Arduino board with

    Fig 1. Flowchart of proposed prototype system


    Monitiring the motion of an object can be done by identifying and tracking distinct feature of the moving object such as color of that object. Here, a hardware set up is required which can be used as an image acquisition hardware such as camera. This camera is connected to a computer. Computer captures the video from the camera. An algorithm for object detection and tracking is written in MATLAB envirnment. This program detects the object in real time. Position of the object is tracked by refering its axis values which has been defined in the MATLAB program. Accordingly, control signal is generated and sent through serial communication to Arduino board. State of LEDs connected to digital output pin of ATmega 8 microcintroller shows the position of that detected object. USB to RS232 TTL Converter Adapter Module PL2303HX is used for the serial communication between Arduino board and computer. Experimental setup of this system is shown in Fig. 2

    Fig. 2. Experimental setup


    The entire algorithm for object detection and tracking is a base on image processing. The proposed system uses MATLAB as a platform on which image processing algorithm has been developed and tested. As an image acquisition devise, camera is used. A camera can be an in- built camera of laptop or it can be a USB camera as well. To get the detail of the hardware device interfaced with the computer, imaqhwinfo command of MATLAB is used. Entire MATLAB programme for this algorithm can be divided in parts as follows.

    1. Image Acquisition:

      The first task in image processing algorithm is to get the live video feed from the camera connected. This live video feed has further been converted into sequence of frames and these frames are used in order to apply further image processing algorithm. For that getsnapshot command of MATLAB is used, which converts video feed into image array. Conditionally, ROI (Region of Interest) can also be defined for capturing specified area of the frame. Image acquisition toolbox of MATLAB can also be used for image acquisition purpose.

    2. Determining axis of an image:

      In this section of an algorithm, an image is divided into sections using its parameters such as length and width. These parameters can be determined using size command of MATLAB. Following is the logic behind this algorithm.

      x1=x/2-120; x2=x/2+120; y1=y/2-30; y2=y/2+30;

      Here, x is defined as X- axis and y is defined as Y- axis. The fundamental diagram of determination of these axis is shown below. The resolution of this image is 640×480.

      Fig. 3. Fundamental diagram of axis

    3. Calibration for color detection:

      After getting an image from video object, the next part of the algorithm is to do the calibration for detecting the color of an object. RGB component of the image is stored in separate arrays. A lower and upper limit is set for these Red, Green and Blue components during calibration. After calibration, if value of pixel lies in that range then that pixel is set white, otherwise it will be black. This is the method of color detection used in this system in which thresholding technique is used by converting RGB image in a binary image.

      1. (b)

        Fig. 4. (a) Original Image, (b) Binary image (Color detection)

    4. Object tracking:

      After successful competition of calibration, the binary image is directly accessed. The white patch in the binary image indicates the colored object. The centroid of the object

      is computed using the horizontal and vertical mean of the object. Bwlabel is a MATLAB function used to extract the features from a binary image generated after calibration. This function genertes a 2-D matrix. Components of this matrix is used for determining the centroid of the detected object.

      Fig. 5. Object tracking

    5. Serial communication:

    Communication of object detection algorithm with Arduino board is done through serial data transfer. To access the serial port of a computer using MATLAB, couple of lines of coding should be done. MATLAB function for serial port access is shown below.


    Here, COM18 is a serial communication port on which Arduino board is connected. Moreover, a program has to be booted on ATmega 8 using Arduino IDE. This program makes serial communication port as an input port for the Arduino board and LEDs connected at digital output pins as an output.

    Fig. 6. Arduino IDE

  4. HARDWARE IMPLEMENTATION Camera is the first and the most important hardware in

    this system used for image acquisition purpose. Secondly, implementation of an object detection algorithm on hardware is a challenging task. Proper selection of a hardware is important for the effective working of the system. Here, Arduino board is used which is powered by ATmega 8 microcontroller. Arduino with ATmega 8 can be programmed using Arduino IDE with precise serial port adjustment. Information regarding hardware and their roles in the system is explained below.

    1. Image acquision device

      Various types of cameras are available today. The one with good resolution should be used for the effectiveness of the system. Generally, a laptop in-built web cam or USB camera is used as an imaging device. Here, the device takes continuous snapshots with predefined triggering rate. These continuous snapshots looks like a video as the have been take in very small fraction of time. Image acquisition toolbox is the best tool for the adjustment of parameters of an imaging device used in the system.

    2. Aduino board ATmega 8

      Here, for the better understanding of programming on Arduino board, ATmega 8 is used. The board used in this system is self-designed and self-made according to the requirement of the system. Figure below shows an Arduino board used in this system.

      Fig. 7. Arduino ATmega 8 board

      Circuit diagram for this board is shown in Fig. 8. Arduino is an open-source electronics prototyping platform which can be used to develop interactive objects, taking input from a varity of switches or sensors, and controlling a variety of lights, motors and other physical outputs. Arduino can be programmed according to the users requirement and used as a standalone system. The main advantages of using Arduino board are the board is inexpensive, used with cross-platform, programming environment is simple and clear and the software is open source and flexible to modify.

      Fig. 8 Arduino ATmega 8 board circuit diagram

    3. USB to RS232 TTL Converter Adapter Module PL2303HX.

    PL-2303HX is a convenient solution for the connection between RS-232 like full duplex asynchronous serial device and Universal Serial Bus (USB) capable host. PL-2303HX is supported by highly compatibe drivers which can work with any operating system and the COM port of that operating system for the application based on COM port. The use of PL-2303HX in this system is because of the capability of PL- 2303HX of achieveing higher throughput compared to traditional UART ports. In this system, this adeper is used to set up a serial communication between MATLAB and Arduino board using COM port.


    After developing the whole system, testing of object detection algorithm and its communication with Arduino board has been tested. In the MATLAB program, the thresholding technique is used to detect an object. After that, to track the motion of an object, for every certain moment in x and y axis a character is sent to the Arduino board using MATLAB function fprint. Arduino board is programmed such that when a board receives a character from the MATLAB via PL-2303HX adapter, digital output pin or a board sets HIGH or LOW accordingly. LEDs are connected on this digital output orts and by receiving the signals from the MATLAB, they changes their states from ON to OFF and OFF to ON. Entire image frame is divided into six decimal values which are 5,7,6,13,15,14,9,11,10 and when an object

    is tracked on those frame sections, digital output pins on Arduino board sets up HIGH or LOW accordingly. Table shown below states various outputs.



    Position of an object in frame

    LED ON/OFF state

    Mean decimal



    No motion








Main aim of this prototype system is to detect an object, track it and accordingly set digital pin of Arduino board HIGH or LOW. Using MATLAB, object detection algorithm has been developed with the technique of thresholding. RGB component of any object is read and accordingly threshold values is set which eventually make an object, a white spot and rest background black. Taking this white spot in consideration, an algorithm for tracing an object is developed and successfully implemented on a hardware using serial communication.

Discussing about the future extension of this system, there are chances of improvisation with this system. Addition with the system is also possible. Like instead of using LEDs on the

board, digital output could have been used to control some other equipment such as motor. Using this prototype system, image processing based person counting machine or any such counting mechanism can also be developed. Moreover, using different image processing algorithms, multiple object detection in one frame can also be possible.

To epitomize this paper, a system has been developed through which motion of an object is traced and accordingly control signal is generated and processed on a hardware board. MATLAB and Arduino ATmega8 board make this system efficient.


[1]. Aji Joy, Ajith P Somaraj, Amal Joe, Muhammed Shafi, Nidheesh T M, Ball Tracking Robot Using Image Processingand Range Detection, IJIRCCE, Vol. 2, Issue 3, March 2014, ISSN: 2320-9801

[2]. Raquib Buksh, Soumyajit Routh, Parthib Mitra, Subhajit Banik, Abhishek Mallik, Sauvik Das Gupta, Implementation of MATLAB based object detection technique on Arduino Board and iROBOT CREATE, IJSRP, Vol. 4, Issue 1, Jan 2014, ISSN: 2250-3153

[3]. Nikhil Sawake, Intelligent Robotic Arm, Submitted to Innovation Cell, IIT- Bombay, July 2013

[4]. V. Subburaman and S. Marcel. Fast Bounding Box estimation based face detection in Workshop on Face Detection of the European Conference on Computer Vision (ECCV), 2010.



Leave a Reply