Servo Motor Control using MATLAB

Published  September 2, 2015   2
A Apoorve
Author
Servo Motor Control using Arduino with MATLAB

In this tutorial you will learn to make direct connection between your Arduino and Matlab, so that you can program your Arduino directly through Matlab. It is very useful when you are working on complex robotics like Robotic Hand, Humanoid etc. as you can process your complex program on your PC in Matlab and then directly program the results in Arduino.

 

So let’s get started:

First of all you have to download Arduino support package from Matlab. You can download it by using following link:

http://in.mathworks.com/hardware-support/arduino-matlab.html

Matlab1

It will download a compressed file, so first of all uncompressed it and save it in the folder and name the folder ArduinoIO. Now open the Arduino IDE, I am using Arduino 1.0.6, click on the file menu and then open the ArduinoIO folder and select the .pde file of your interest from the Pde folder. I selected adioes.pde as it allows us to control servo motors. You may select other programs according to your choice. You may learn about various pde file by just opening it in Arduino IDE.

MATLAB2

Now the final step will be to upload the program adioes.pde on your Arduino and now we will move on to Matlab.

I will try to teach you about Matlab code using picture so that it can be easy in understanding.

The most important thing is to select the ArduinoIO folder as your current folder in Matlab. You can do it by clicking on ‘browse the folder’.

MATLAB3

Now after your current folder is selected, you can click on arduino.m to get all the function and commands which you can use. You can see that, first command is arduino(comport).

MATLAB4

Now your first command will be to create a variable a=arduino(“COM3”); , it will create a variable ‘a’ which will be used to communicate between Arduino and Matlab. After that it will take few second to connect your Arduino to Matlab. You can see it in picture given below. In () you have to write about that COM port on which our Arduino is connected.

MATLAB5

And after the connection is successful, you can see what different hardware you can use.

MATLAB6

After that you have to attach your Servo to one of the PWM pins provided in Arduino. I used PIN 3 but you can select anyone which have PWM, You have to use servoAttach(a,3); command to do it.

MATLAB7

After your servo is attached you can use servo Write command to rotate it in any angle. Like in above example, I rotate it for 45 degree. You have to servoWrite(a,3,45) command to do it.

 

Circuit:

Servo Motor Control with Arduino using MATLAB

I have used servo motor which requires 7.4 V voltage supply. So I provide it power supply by using LiPo battery. One of the most Important thing is to short the ground of power supply to the ground of the Arduino otherwise the servo will not work properly. It is the most common mistake which happens. And also don’t give power to servo from your Arduino as it can destroy your Arduino.

Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Submitted by mohamed eid on Thu, 02/16/2017 - 04:02

Permalink

thanks for your efforts but i had a problem , i followed your steps but matlab gives me error notification at writing this command servoWrite(a,3,45) how i can solve this problem ?
thanks