A4988 driver for stepper motor
Afficher commentaires plus anciens
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
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
le 18 Avr 2019
Adrian Dwight
le 18 Avr 2019
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.
Adrian Dwight
le 26 Avr 2019
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
le 21 Fév 2020
I am facing this same problem
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
Réponses (0)
Communautés
Plus de réponses dans Power Electronics Control
Catégories
En savoir plus sur Device Driver Blocks dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!