MATLAB > Arduino > Servo Motors

i'm doing a project involving interfacing matlab and arduino and control servo motors as output.

my project is to drive a robotic arm consist of several motors. At the mean time, i'm trying to drive the servo motor one by one. I can't seems to drive any of them to the desired positions. I need help on this matter.

i have upload adiosrv file into arduino and run arduino.m in matlab. I have used a.servoAttach(1) command but it still does not work. when i attach my servo to pin 10 and get the reading of servo using a.servoRead(1), it gives me random number within the range of 0 -180. FYI, i did not use motor shield in this case, because i need to drive more than 2 servos.

then i use the pulse width modulation config which i have learn in my uni, however, its still does not work.
the following MATLAB code is to drive the servo to reach a 90 degree position and hold it, however it will just rotate to the maximum degree.

%% Start connection with Arduino
clc
clear
a = arduino('Com3');

%% Set your pin # here
%Configure output pins
a.pinMode(9,'Output'); %testing with servo
%configure input pins
a.pinMode(3,'Input'); %switch1
a.pinMode(4,'Input'); %switch2

sw1 = a.digitalRead(3);
sw2 = a.digitalRead(4);
%% Type your command here
while(1)
    sw1 = a.digitalRead(3);
    sw2 = a.digitalRead(4);
        
    if (sw1==1)
        a.digitalWrite(9,1);
        pauser(0.0015);
        a.digitalWrite(9,0);
        pauser(0.0185);
        
    elseif (sw2==1)
        break;
    end

end

display('done')

%% extra
sw1status = a.digitalRead(3)
sw2status = a.digitalRead(4)

the pauser command is just to make sure it can give a delay which is less than 10 miliseconds. Based on my servo motor that i'm using (http://www.cytron.com.my/viewProduct.php?pid=CCQ1Nhg5Gg8OCBYwEg0fCe5erqikGAdVlLw7bgmtzC0=), the pulse that i need to provide to give a 90 degree position is 1.5ms where this data can be found in this pdf file (http://www.cytron.com.my/usr_attachment/RC_Servo_User_Manual.pdf)

hope you can help me out. Thanks

I can't comment on your MATLAB code as I don't know it's language. Are you attempting to generate a proper servo control pulse in MATLAB and keep refreshing it at the proper rate?

I would think it would be much better to utilize the servo library code in the arduino code and just pass the desired servo position value you want from MATLAB to the arduino and let the arduino handle the proper servo pulse width generation and automatic refresh rate that the servo requires?

Lefty

I can't comment on your MATLAB code as I don't know it's language. Are you attempting to generate a proper servo control pulse in MATLAB and keep refreshing it at the proper rate?

yes i would like it to refresh it so that the servo can maintain at a desired position

I would think it would be much better to utilize the servo library code in the arduino code and just pass the desired servo position value you want from MATLAB to the arduino and let the arduino handle the proper servo pulse width generation and automatic refresh rate that the servo requires?

is it possible to do that? do i require additional codes to sent values to arduino and arduino will do the job? how do i relate servo library code in matlab and sent it to arduino to do the job?

i did tried to position the servo using arduino's servo library. it works perfectly fine. but i'm having a big problem when i try to communicate arduino and MATLAB as my knowledge in programming language is still shallow. that's why i rely on Arduino Support from MATLAB - Hardware Support - MATLAB & Simulink to help me.

hello;
i am having the same problem only i need the servo ti turn the eyes of my robot.
Can u assist me if you found a way to control the servo using arduino support for matlab?

thx

hey, do you guys know how to use MATLAB GUI to control servo motor?