Write a code in MATLAB software to use a stepper motor. The stepper motor is con
ID: 2268286 • Letter: W
Question
Write a code in MATLAB software to use a stepper motor. The stepper motor is connected to Arduino using the stepper motor driver board. Write a code which can rotate the stepper motor for certain time and stop for certain time and run again for certain time.Can you please send me the MATLAB code it self?
I was confused if it is possible to write the code for stepper motor directly in MATLAB or I have to connect MATLAB and Arduino software together using some Add-ons? Some specification of Setup is:
Stepper Motor- Nema 17(Model-17D3003)
Stepper Motor Driver Board-A4988
Arduino Board Type- MEGA
Connected using a external power supply which can supply maximum 12V
Explanation / Answer
Syntax
move(sm,steps)
move(sm,steps) rotates the stepper motor for the specified number of steps
Connect to Arduino hardware and create an add-on connection to an Adafruit® Motor Shield.
a = arduino('COM7','Uno','Libraries','Adafruit/MotorShieldV2');
shield = addon(a,'Adafruit/MotorShieldV2');
Create a stepper motor connection to motor number 1 on the shield, with steps per revolution of 200 and an RPM of 10.
include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
other program
function motor(step,delay,direction)
%Controlling a stepper motor
%step must be integer.
%delay is in seconds.
%direction: 1 or 2.
%--------------------------------------
%Author: Diego Barragn Guerrero
%For more information, visit: www.matpic.com
%E-mial: diegokillemall@yahoo.com
%--------------------------------------
warning off
if nargin==1
delay=1;direction=1;
elseif nargin==2
direction=1;
end
if isnan(step)||step<0
error('Step value must be positive integer');
elseif direction~=1 && direction~=2
error('Direction options: 1 or 2')
elseif isnan(delay)||delay<0
error('Delay value must be positive integer')
end
step=ceil(step);
inout=digitalio('parallel','LPT1');
dato=addline(inout,0:3,'out');
if direction ==1
sent=[3 6 12 9];
else
sent=[9 12 6 3];
end
m=1;
for n=1:step
putvalue(dato,sent(m));
pause(delay);
m=m+1;
if m>4
m=1;
end
end
sm = stepper(shield,1,200,'RPM',10);
Rotate the motor 10 steps.
move(sm,10);
The move command blocks MATLAB while it executes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.