Introduction: Red Color Recognition Based Arduino Control(Using MATLAB and Arduino)
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.
Attachments
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!

Participated in the
Pi/e Day Contest

Participated in the
Coded Creations

Participated in the
Automation Contest
21 Comments
6 years ago
which matlab edition is required for this project?
Reply 4 years ago
R2017b and later
5 years ago
I know its kinda odd but how can you change the input camera in matlab? i was working with webcam() comand and no videoinput(). However when i run the program it does run with the built-in camera of the nootebook, however not with the one i want to. What can i do?
Regard from Chile
Reply 4 years ago
You need to install the hardware package. (USB webcam) from Matlab add-on service.
5 years ago
Can you please tell me which type of Arduino is used in this project
6 years ago
how camera is connected to arduino?
6 years ago
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 :)
clear;
clc % Clearing Matlab desktop
vid=videoinput('winvideo',1,'YUY2_320x240'); % Defining the video input object
set(vid,'FramesPerTrigger',1); % Setting frames per trigger
preview(vid); %////// Showing the video of the moving Ball(TO BE USED %
% WHILE TESTING)
pause(10);% Waiting for a certain time for the system to get initialised
rgb_image = getsnapshot(vid); % Storing Image in an array variable
[a b c]= size(rgb_image); % Determining the size of the captured frame.
y=a;
x=b;
% Defining Boundaries
x1=x/2-120;
x2=x/2+120;
y1=y/2-30;
y2=y/2+30;
ser=serial('COM34'); % Defining the specified COM Port to be used
fopen(ser); % starting serial Communication,opening serial port
while(1)
rgb_image = getsnapshot(vid); % storing image in an array variable
flushdata(vid); %Flushing the buffer
rbar=0;
cbar=0;
e=0;
fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);% Storing RGB components of the image in seperate arrays
I=((fR<=70) & (fG>=80) & (fB<=70)); % Converting the RGB Image into binary image///Detecting only the red component
% Following are the steps For Detecting the red ball
se=strel('disk',20);
B=imopen(I,se);
final=imclose(B,se);
[L,n]=bwlabel(final);
%imshow(rgb_image); %////THIS IS TO BE USED ONLY WHILE TESTING
%hold on % ////THIS IS TO BE USED ONLY WHILE TESTING
for k=1:n
[r,c]=find(L==k);
rbar=mean(r);
cbar=mean(c);
%plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B' ,'MarkerSize',20) %////THIS IS TO BE USED ONLY WHILE TESTING
e=(((cbar>=x1)*2*2*2) + ((cbar<=x2)*2*2) + ((rbar>=y1)*2) + (rbar<=y2)) % Converting to decimal number
end
% Decision Making Conditions
switch (e)
case 5
disp('Move left'),fprintf(ser,'L');
case 6
disp('Move left'),fprintf(ser,'L');
case 7
disp('Move left'),fprintf(ser,'L');
case 9
disp('Move right'),fprintf(ser,'R');
case 10
disp('Move right'),fprintf(ser,'R');
case 11
disp('Move right'),fprintf(ser,'R');
case 13
disp('Move forward'),fprintf(ser,'F');
case 14
disp('Move back'),fprintf(ser,'B');
otherwise
disp('Stop Moving'),fprintf(ser,'S');
end
end
fclose(ser); % closing serial port
6 years ago
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 :)
clear;
clc % Clearing Matlab desktop
vid=videoinput('winvideo',1,'YUY2_320x240'); % Defining the video input object
set(vid,'FramesPerTrigger',1); % Setting frames per trigger
preview(vid); %////// Showing the video of the moving Ball(TO BE USED %
% WHILE TESTING)
pause(10);% Waiting for a certain time for the system to get initialised
rgb_image = getsnapshot(vid); % Storing Image in an array variable
[a b c]= size(rgb_image); % Determining the size of the captured frame.
y=a;
x=b;
% Defining Boundaries
x1=x/2-120;
x2=x/2+120;
y1=y/2-30;
y2=y/2+30;
ser=serial('COM34'); % Defining the specified COM Port to be used
fopen(ser); % starting serial Communication,opening serial port
while(1)
rgb_image = getsnapshot(vid); % storing image in an array variable
flushdata(vid); %Flushing the buffer
rbar=0;
cbar=0;
e=0;
fR=rgb_image(:,:,1);fG=rgb_image(:,:,2);fB=rgb_image(:,:,3);% Storing RGB components of the image in seperate arrays
I=((fR<=70) & (fG>=80) & (fB<=70)); % Converting the RGB Image into binary image///Detecting only the red component
% Following are the steps For Detecting the red ball
se=strel('disk',20);
B=imopen(I,se);
final=imclose(B,se);
[L,n]=bwlabel(final);
%imshow(rgb_image); %////THIS IS TO BE USED ONLY WHILE TESTING
%hold on % ////THIS IS TO BE USED ONLY WHILE TESTING
for k=1:n
[r,c]=find(L==k);
rbar=mean(r);
cbar=mean(c);
%plot(cbar,rbar,'Marker','*','MarkerEdgeColor','B' ,'MarkerSize',20) %////THIS IS TO BE USED ONLY WHILE TESTING
e=(((cbar>=x1)*2*2*2) + ((cbar<=x2)*2*2) + ((rbar>=y1)*2) + (rbar<=y2)) % Converting to decimal number
end
% Decision Making Conditions
switch (e)
case 5
disp('Move left'),fprintf(ser,'L');
case 6
disp('Move left'),fprintf(ser,'L');
case 7
disp('Move left'),fprintf(ser,'L');
case 9
disp('Move right'),fprintf(ser,'R');
case 10
disp('Move right'),fprintf(ser,'R');
case 11
disp('Move right'),fprintf(ser,'R');
case 13
disp('Move forward'),fprintf(ser,'F');
case 14
disp('Move back'),fprintf(ser,'B');
otherwise
disp('Stop Moving'),fprintf(ser,'S');
end
end
fclose(ser); % closing serial port
6 years ago
I was trying to use my computer's built-in webcam.. How do I solve this?
"
Error using videoinput (line 217)
Invalid ADAPTORNAME specified. Type 'imaqhwinfo' for a list of available ADAPTORNAMEs. Image acquisition adaptors may be
available as downloadable support packages. Open Support Package Installer to install additional vendors.
Error in redobjTrack_arduino_conntrol (line 3)
vid = videoinput('winvideo', 1, 'YUY2_640x480');
"
7 years ago
Hi dude. Nice project there
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?
Reply 7 years ago
Image processing is done by matlab. Arduino is not for image processing.
7 years ago on Introduction
wooow...nice project..
how to detect some types of colors other than red? and how the program if it is added to the tracking servo for color?
thank you
a greeting,
fungky King
fungkyking01@gmail.com
Reply 7 years ago
diff_im = imsubtract(IMRED(:,:,1), rgb2gray(IMRED));
in the above line change 1 to 2 or 3 for detecting the blue or green.
and for servo as Mat said only 1 pin is needed to trigger(center wire of motor, other wires for vcc and ground).
Reply 7 years ago on Introduction
Thanks for appreciation.
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.
Thank you
Regards
7 years ago
The output window screen hangs after 1-2 seconds.... please help me regarding this
7 years ago
Hello Friend
I am trying to
modified this code as per of my requirement, here in the provided code
I think the function of the
s=serial('COM3','BAUD',9600);
is to communicate or build the serial
communication with the webcam which is connected to the port COM3, am I right?
But I am trying to use ip or let’s
suppose the laptop integrated camera rather to use the webcam. So if you help
me to modified as per of my requirement I will really appreciate your ideas and
suggestion.
Thank you,
7 years ago
Hi dude. Nice project there
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?
7 years ago
Hello Mate,
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.
regards,
Biswas
7 years ago on Introduction
screen hangs after few seconds
7 years ago on Introduction
This is so cool! I didn't know that you could do something like detecting color with an arduino! Thanks for sharing!