loading

In this tutorial,we will demonstrate how to use color recognition to control any actuator connected to any digital pin of arduino uno,we have considered LED as an actuator in this tutorial which is connected to pin 13 of UNO. In this video we have drawn a symbol of Pi in red color which will be reconised.

This tutorial have two parts:

First Step: Programing Arduino UNO for recieving serial data from MATLAB

Second Step: Red color recognition and serial communication(MATLAB/ Image processing)

.

Step 1: Program Arduino UNO for Recieving Serial Data From MATLAB

First part is Arduino programming.

For establishing proper serial communication between arduino and MATLAB , we need to first program arduino for recieving serial data as it will act as reciever of serial command. We have assigned 2 values for different cases, whenever arduino will recieve value 100 it willl turn ON the LED and 101 will turn OFF the LED(You can also assign your desired value). Make sure to assign similar values while programming serial communication in MATLAB(will be discussed in step 2). Upload the program which is mentioned below:

const int ledpin=13;int recValue;

void setup()

{

Serial.begin(9600);

pinMode(13, OUTPUT);

}

void loop()

{

if(Serial.available()>0)

{

recValue=Serial.read();

if (recValue == 100) // If use will send value 100 from MATLAB then LED will turn ON

{ digitalWrite(ledpin, HIGH); }

if(recValue == 101) // If use will send value 101 from MATLAB then LED will turn OFF

{ digitalWrite(ledpin, LOW); }

}

}

Step 2: Red Color Recognition and Serial Communication(MATLAB)

First is, red color recognition and serial communication.

We will start with red color recognition, we have developed a real time red color tracking, which will trigger the LED to toggle as per conditions. Conditions which we have assigned are ,whenever there will be presence of one or more than one red colored object in the frame the LED wll turn ON otherwise LED will remain OFF. Color recognition algorithm also contains program for sending serial command to arduino as a if conditions. Basic algorithm for red color recognition and serial communication to send command to arduino uno is explaned in above images: The results of tracking of Pi drawn on white paper in RGB as well as Binary is also shown in above images: Red color recognition algorithm along with serial command sending to arduino uno code is attached as file below, open and execute this file in MATLAB.

Step 3: Results and Basic Information on How to Execute Above Steps

First upload the program mentioned in first step of this tutorial on arduino uno.

Second, open and execute the .m file attached in step 2.

Things to keep in mind.

1.Threshold value in MATLAB program may vary as per lighting conditions, if program did'nt work as mentioned try to tinker with theshold value for refrence see imae in this step.

2.Make sure to select correct COM port for serial communication.

3.Keep in mind to match the serial data value mentioned in MATLAB program and arduino program.

Results:

The video attached in this step demonstrates real-time red color tracking and binary result of red colored object tracking.

Thank you!

<p>which matlab edition is required for this project?</p>
<p>how camera is connected to arduino?</p>
<p>Hello i am using this code to control motor using arduino and matlab. But after running this program output is always stop moving even if i move the ball in all directions. Please help on this. Thanks :)</p><p>clear;<br>clc % Clearing Matlab desktop<br>vid=videoinput('winvideo',1,'YUY2_320x240'); % Defining the video input object<br>set(vid,'FramesPerTrigger',1); % Setting frames per trigger<br>preview(vid); %////// Showing the video of the moving Ball(TO BE USED %<br>% WHILE TESTING)<br>pause(10);% Waiting for a certain time for the system to get initialised<br>rgb_image = getsnapshot(vid); % Storing Image in an array variable<br>[a b c]= size(rgb_image); % Determining the size of the captured frame.<br>y=a;<br>x=b;<br><br><br>% Defining Boundaries<br>x1=x/2-120;<br>x2=x/2+120;<br>y1=y/2-30;<br>y2=y/2+30;<br>ser=serial('COM34'); % Defining the specified COM Port to be used<br>fopen(ser); % starting serial Communication,opening serial port<br><br>while(1)<br>rgb_image = getsnapshot(vid); % storing image in an array variable<br>flushdata(vid); %Flushing the buffer<br>rbar=0;<br>cbar=0;<br>e=0;<br>fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);% Storing RGB components of the image in seperate arrays<br>I=((fR&lt;=70) &amp; (fG&gt;=80) &amp; (fB&lt;=70)); % Converting the RGB Image into binary image///Detecting only the red component<br>% Following are the steps For Detecting the red ball<br>se=strel('disk',20);<br>B=imopen(I,se);<br>final=imclose(B,se);<br>[L,n]=bwlabel(final);<br>%imshow(rgb_image); %////THIS IS TO BE USED ONLY WHILE TESTING<br>%hold on % ////THIS IS TO BE USED ONLY WHILE TESTING<br>for k=1:n<br>[r,c]=find(L==k);<br>rbar=mean(r);<br>cbar=mean(c);<br>%plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B' ,'MarkerSize',20) %////THIS IS TO BE USED ONLY WHILE TESTING<br>e=(((cbar&gt;=x1)*2*2*2) + ((cbar&lt;=x2)*2*2) + ((rbar&gt;=y1)*2) + (rbar&lt;=y2)) % Converting to decimal number<br>end<br>% Decision Making Conditions<br>switch (e)<br>case 5<br>disp('Move left'),fprintf(ser,'L');<br>case 6<br>disp('Move left'),fprintf(ser,'L');<br>case 7<br>disp('Move left'),fprintf(ser,'L');<br>case 9<br>disp('Move right'),fprintf(ser,'R');<br>case 10<br>disp('Move right'),fprintf(ser,'R');<br>case 11<br>disp('Move right'),fprintf(ser,'R');<br>case 13<br>disp('Move forward'),fprintf(ser,'F');<br>case 14<br>disp('Move back'),fprintf(ser,'B');<br>otherwise<br>disp('Stop Moving'),fprintf(ser,'S');<br>end<br>end<br>fclose(ser); % closing serial port</p>
<p>Hello i am using this code to control motor using arduino and matlab. But after running this program output is always stop moving even if i move the ball in all directions. Please help on this. Thanks :)</p><p>clear;<br>clc % Clearing Matlab desktop<br>vid=videoinput('winvideo',1,'YUY2_320x240'); % Defining the video input object<br>set(vid,'FramesPerTrigger',1); % Setting frames per trigger<br>preview(vid); %////// Showing the video of the moving Ball(TO BE USED %<br>% WHILE TESTING)<br>pause(10);% Waiting for a certain time for the system to get initialised<br>rgb_image = getsnapshot(vid); % Storing Image in an array variable<br>[a b c]= size(rgb_image); % Determining the size of the captured frame.<br>y=a;<br>x=b;<br><br><br>% Defining Boundaries<br>x1=x/2-120;<br>x2=x/2+120;<br>y1=y/2-30;<br>y2=y/2+30;<br>ser=serial('COM34'); % Defining the specified COM Port to be used<br>fopen(ser); % starting serial Communication,opening serial port<br><br>while(1)<br>rgb_image = getsnapshot(vid); % storing image in an array variable<br>flushdata(vid); %Flushing the buffer<br>rbar=0;<br>cbar=0;<br>e=0;<br>fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);% Storing RGB components of the image in seperate arrays<br>I=((fR&lt;=70) &amp; (fG&gt;=80) &amp; (fB&lt;=70)); % Converting the RGB Image into binary image///Detecting only the red component<br>% Following are the steps For Detecting the red ball<br>se=strel('disk',20);<br>B=imopen(I,se);<br>final=imclose(B,se);<br>[L,n]=bwlabel(final);<br>%imshow(rgb_image); %////THIS IS TO BE USED ONLY WHILE TESTING<br>%hold on % ////THIS IS TO BE USED ONLY WHILE TESTING<br>for k=1:n<br>[r,c]=find(L==k);<br>rbar=mean(r);<br>cbar=mean(c);<br>%plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B' ,'MarkerSize',20) %////THIS IS TO BE USED ONLY WHILE TESTING<br>e=(((cbar&gt;=x1)*2*2*2) + ((cbar&lt;=x2)*2*2) + ((rbar&gt;=y1)*2) + (rbar&lt;=y2)) % Converting to decimal number<br>end<br>% Decision Making Conditions<br>switch (e)<br>case 5<br>disp('Move left'),fprintf(ser,'L');<br>case 6<br>disp('Move left'),fprintf(ser,'L');<br>case 7<br>disp('Move left'),fprintf(ser,'L');<br>case 9<br>disp('Move right'),fprintf(ser,'R');<br>case 10<br>disp('Move right'),fprintf(ser,'R');<br>case 11<br>disp('Move right'),fprintf(ser,'R');<br>case 13<br>disp('Move forward'),fprintf(ser,'F');<br>case 14<br>disp('Move back'),fprintf(ser,'B');<br>otherwise<br>disp('Stop Moving'),fprintf(ser,'S');<br>end<br>end<br>fclose(ser); % closing serial port</p>
<p>I was trying to use my computer's built-in webcam.. How do I solve this?</p><p>&quot;</p><p>Error using videoinput (line 217)</p><p>Invalid ADAPTORNAME specified. Type 'imaqhwinfo' for a list of available ADAPTORNAMEs. Image acquisition adaptors may be</p><p>available as downloadable support packages. Open Support Package Installer to install additional vendors.</p><p>Error in redobjTrack_arduino_conntrol (line 3)</p><p>vid = videoinput('winvideo', 1, 'YUY2_640x480');</p><p>&quot;</p>
<p>Hi dude. Nice project there</p><p>BTW how did you send the output from MATLAB to the Arduino? I mean you connected the Arduino to the PC, and the Webcam to pc as well? Or did you connect the webcam to Arduino and then the Arduino to the PC?</p>
<p>Image processing is done by matlab. Arduino is not for image processing. </p>
<p>wooow...nice project..</p><p>how to detect some types of colors other than red? and how the program if it is added to the tracking servo for color?</p><p>thank you</p><p>a greeting,</p><p>fungky King</p><p>fungkyking01@gmail.com</p>
<p>diff_im = imsubtract(IMRED(:,:,1), rgb2gray(IMRED));</p><p>in the above line change 1 to 2 or 3 for detecting the blue or green.</p><p>and for servo as Mat said only 1 pin is needed to trigger(center wire of motor, other wires for vcc and ground). </p>
<p>Thanks for appreciation.</p><p>Yes any color could be detected,but i would suggest only red , blue or green color because these colors are prominent and easy to detect, For detecting green or blue color very minimal change is required in the code. For servo I am not pretty sure but ya you can control any actuator so i guess if you connect motor to any pin of arduino it can be trigered.</p><p>Thank you </p><p>Regards</p>
<p>The output window screen hangs after 1-2 seconds.... please help me regarding this</p>
<p>Hello Friend</p><p> I am trying to<br>modified this code as per of my requirement, here in the provided code </p><p>I think the function of the </p><p>s=serial('COM3','BAUD',9600);</p><p>is to communicate or build the serial<br>communication with the webcam which is connected to the port COM3, am I right? </p><p>But I am trying to use ip or let&rsquo;s<br>suppose the laptop integrated camera rather to use the webcam. So if you help<br>me to modified as per of my requirement I will really appreciate your ideas and<br>suggestion.</p><p>Thank you,</p>
<p>Hi dude. Nice project there</p><p>BTW how did you send the output from MATLAB to the Arduino? I mean you connected the Arduino to the PC, and the Webcam to pc as well? Or did you connect the webcam to Arduino and then the Arduino to the PC?</p>
<p>Hello Mate,</p><p>I am able to detect and track the red color in Matlab in real time. if you have some free time could you please help me how can I go further to communicate with Arduino and to control the DC motor with respect to the color tracking. I am also suppose to do all in Matlab and I have already install Matlab support package for Arduino.</p><p>regards,</p><p>Biswas</p>
<p>screen hangs after few seconds </p>
<p>This is so cool! I didn't know that you could do something like detecting color with an arduino! Thanks for sharing! </p>
<p>Thank you for appreciation, but color detection is done using MATLAB image processing not arduino and arduino is beign triggered using serial counication from MATLAB.</p><p>Thank you</p>

About This Instructable

14,540views

36favorites

License:

More by MATuinoR:Red Color Recognition based Arduino control(Using MATLAB and Arduino) 
Add instructable to: