A4988 driver for stepper motor

Good day! I am using an A4988 driver module to drive my nema 17 stepper motor
I tried to translate the arduino code below into a matlab code, but it doesnt work the same way as it does when i use the arduino IDE. What happens instead is that the motor seems to find it hard to make steps
this is the matlab code that i wrote based on the arduino code by Dejan Nedelkovski
clc;
clear;
a = arduino('com5', 'mega2560');
writeDigitalPin(a, 'D4', 1);
for ctr = 1:200
writeDigitalPin(a, 'D3', 1);
pause(0.0005);
writeDigitalPin(a, 'D3', 0);
pause(0.0005);
end
pause(1);
writeDigitalPin(a, 'D4', 0);
for ctr = 1:200
writeDigitalPin(a, 'D3', 1);
pause(0.0005);
writeDigitalPin(a, 'D3', 0);
pause(0.0005);
end
pause(1);
this is the arduino code
/* Simple Stepper Motor Control Exaple Code
*
* by Dejan Nedelkovski, www.HowToMechatronics.com
*
*/
// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}

9 commentaires

Walter Roberson
Walter Roberson le 17 Avr 2019
What is the reason for rewriting it as MATLAB code?
Each time that you invoke writeDigitalPin, MATLAB has to talk to a command interpreter on the arduino, sending it a command and appropriate data, and the command interpreter has to parse and validate the parameters and then execute the appropriate functionality. That will always be slower than dedicated code on the Arduino like you already have.
Adrian Dwight
Adrian Dwight le 18 Avr 2019
i am rewriting the code in matlab since i am using the stepper motor in conjunction with the matlab image processing features.
thanks you sir. so its the time it takes to communicate between matlab and arduino that's causing the motor to run differently? is there any way to go around this problem? thanks again
Adrian Dwight
Adrian Dwight le 18 Avr 2019
i tried the writePWMDutyCycle command, the stepper motor runs smoothly for some time depending on the pause value, but by doing so i am unable to control the number of steps. I need to control the number of steps though
Walter Roberson
Walter Roberson le 18 Avr 2019
Using writeDigitalPin is relatively slow. It is more efficient to have a dedicated program on the arduino that listens for inputs (especially binary instead of text) from the MATLAB side and sends results to the MATLAB side, and takes care of the pin manipulation on the arduino side. You could send pulse duty cycle and duration information. Or, especially with quadrature motors, you could send target positions and let the Arduino take care of the pulses to get there.
Note: if you are using a USB to serial connection to talk between the Arduino and MATLAB, then you are limited to at most 1000 transactions per second because of the way that USB serial is scheduled on the Arduino. There is an alternate device available that is Arduino-like that schedules immediately and so can send more than 1000 times per second... but you could still have some difficulty sending to it from the MATLAB side.
i tried the code below, but the stepper motor doesn't seem to respond. Im guessing this is because the serial data is gone after a few moments?:
MATLAB:
clc;
clear;
serialNano = serial('COM7', 'BaudRate', 9600);
while 1
sendData = input('Enter data: ');
fopen(serialNano);
switch sendData
case 1
fprintf(serialNano, '%s', char(1));
pause(1);
case 2
fprintf(serialNano, '%s', char(2));
pause(1);
end
fclose(serialNano);
end
ARDUINO
int receivedData;
void setup()
{
pinMode(6,OUTPUT); //Step1
pinMode(7,OUTPUT); //Direction1
pinMode(9,OUTPUT); //Step2
pinMode(10,OUTPUT);//Direction2
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
receivedData = Serial.read();
if (receivedData == 1)
{
digitalWrite(7,LOW);
for(int x = 0; x < 349; x++)
{
digitalWrite(6,HIGH);
delayMicroseconds(500);
digitalWrite(6,LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(10,HIGH);
for(int x = 0; x < 25; x++)
{
digitalWrite(9,HIGH);
delay(5);
digitalWrite(9,LOW);
delay(5);
}
delay(1000);
digitalWrite(10,LOW);
for(int x = 0; x < 25; x++)
{
digitalWrite(9,HIGH);
delay(5);
digitalWrite(9,LOW);
delay(5);
}
delay(1000);
digitalWrite(7, HIGH);
for(int x = 0; x < 349; x++)
{
digitalWrite(6,HIGH);
delayMicroseconds(500);
digitalWrite(6,LOW);
delayMicroseconds(500);
}
digitalWrite(11, HIGH);
delay(1000);
}
else if (receivedData == 2)
{
digitalWrite(7,LOW);
for(int x = 0; x < 1297; x++)
{
digitalWrite(6,HIGH);
delayMicroseconds(500);
digitalWrite(6,LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(10,HIGH);
for(int x = 0; x < 25; x++)
{
digitalWrite(9,HIGH);
delay(5);
digitalWrite(9,LOW);
delay(5);
}
delay(1000);
digitalWrite(10,LOW);
for(int x = 0; x < 25; x++)
{
digitalWrite(9,HIGH);
delay(5);
digitalWrite(9,LOW);
delay(5);
}
delay(1000);
digitalWrite(7, HIGH);
for(int x = 0; x < 1297; x++)
{
digitalWrite(6,HIGH);
delayMicroseconds(500);
digitalWrite(6,LOW);
delayMicroseconds(500);
}
digitalWrite(11, HIGH);
delay(1000);
}
}
}
Paul Bayron
Paul Bayron le 24 Sep 2019
Hi, everyone any update on this thread? I'm also trying to do the same thing but I can't get Matlab and Arduini IDE code to "talk" to each other.
Rajbir Singh
Rajbir Singh le 21 Fév 2020
I am facing this same problem
Yasya Khalif
Yasya Khalif le 17 Avr 2021
hi, have you found the solution? i need to interface matlab to arduino, and control the motor using driver motor drv8825
明炀 范
明炀 范 le 9 Mar 2023
I am also concerning this question

Connectez-vous pour commenter.

Réponses (0)

Communautés

Plus de réponses dans  Power Electronics Control

Community Treasure Hunt

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

Start Hunting!

Translated by