control stepper motor using MATLAB and Arduino

조회 수: 56 (최근 30일)
LO
LO 2021년 2월 26일
댓글: Ernest Arranz Pasqual 2022년 10월 31일
I am trying to set up a stepper motor connected to an Arduino Uno with motor shield mounted on it.
The motor works through Arduno IDE.
I am trying to translate the code in MATLAB ...without success
here is my arduino code:
// Include the Stepper library<br>#include
#include <Stepper.h>
// Declare the used pins
int dirA = 12;
int dirB = 13;
int pwmA = 3;
int pwmB = 11;
// Declare a Stepper motor with 200 steps
Stepper stepper1(200, dirA, dirB);
void setup() {
// PWM pins require declaration when used as Digital
pinMode(pwmA, OUTPUT);
pinMode(pwmB, OUTPUT);
// Set PWM pins as always HIGH
digitalWrite(pwmA, HIGH);
digitalWrite(pwmB, HIGH);
// Set stepper motor speed
stepper1.setSpeed(30);
}
void loop(){
// Turn the stepper 100 steps which means 180 degrees
stepper1.step(2000);
// Wait half second
delay(500);
// Turn the stepper 100 steps back which means 180 degrees
stepper1.step(-200);
// Wait half second
delay(500);
}
and here is my MATLAB code:
clear all
global a;
% a = arduino('COM3','Uno');
a = arduino('COM3','Uno','Libraries','Adafruit\MotorShieldV2'); % load arduino package including motor shield
%% create shield object to control the motor
shield = addon(a,'Adafruit\MotorShieldV2');
sm = stepper(shield,1,200);
%% specify speed and movement direction
for i = 1:5
sm.RPM = 30;
move(sm, 200);
pause(2);
move(sm, -200);
end
release(sm);
any idea why it does not move ? the code per se should be correct (I do not get any error).
Thanks !
  댓글 수: 1
Ernest Arranz Pasqual
Ernest Arranz Pasqual 2022년 10월 31일
Hi, I'm also working on a stepper matlab arduino project. From what I undersand here:
it seems the problem is the pause function which can't operate with values low enough.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by