Motor Control With MATLAB Using Arduino Uno
Motor Control With MATLAB Using Arduino Uno
On Arduino Uno
Connect the servo motor to the board with the 5V line (red)
connected to the 5V supply on the board, GND (black) to GND on
Step 5
the board and the motor control line connected to Digital pin 4
(PWM) of the board.
The “Servo” library from the support package is used here. There
are other libraries that can also be used with the motor control
shield. As soon as the servo object is created, the motor spins
once.
Create a new script and type the following code. This will spin
the motor from 0-180 degrees once in steps of 18 degrees.
Step 7
Please refer to the datasheet of the motor to calculate steps and
angles.
Un-comment the “flag” and the “while” loop lines to make an
infinite loop.
To quit the infinite loop, press Ctrl + C on the command window.
clearvars
%flag = 1;
%while flag
for angle = 0:0.1:1
writePosition(s, angle);
current_pos = readPosition(s);
current_pos = current_pos*180;
fprintf('Current motor position is %d degrees\n', current_pos);
pause(1);
end
%end
Final Result: