Author Topic: Stepper motor control with matlab and arduino  (Read 684 times)

0 Members and 1 Guest are viewing this topic.

Offline Jagomezh93Topic starter

  • Newbie
  • Posts: 1
  • Country: mx
Stepper motor control with matlab and arduino
« on: April 19, 2023, 02:30:43 am »
Hello all I am José and this is my first post.

I want to move a stepper motor using Matlab but I do not know what is wrong. The motor does not move.

Materials:

Arduino Uno
TB6600 Motor Driver
Motor: 28HB30-401A
Arduino Suport Package for Matlab

The next code works ok, it is for the Arduino IDE.

  int BOTON_1 = 7;    //SELECCIONAR
  int BOTON_2 = 8;    //MOVER
  int LED_1 = 5;         //ENCENDER AVANCE
  int LED_2 = 6;         //ENCENDER REVERSA
  int ENA = 3;            //ENABLE A PIN 3
  int DIR = 2;            //DIRECCION PIN 2
  int PUL = 4;            //PULSO PIN 4

void setup() {
  pinMode(BOTON_1, INPUT_PULLUP);  //SE DECLARA PIN 7 COMO ENTRADA
  pinMode(BOTON_2, INPUT_PULLUP);  //SE DECLARA PIN 8 COMO ENTRADA
  pinMode(LED_1, OUTPUT);                //SE DECLARA PIN 5 COMO SALIDA
  pinMode(LED_2, OUTPUT);                //SE DECLARA PIN 6 COMO SALIDA
  pinMode (PUL, OUTPUT);                   //SE DECLARA PIN 4 COMO SALIDA
  pinMode (DIR, OUTPUT);                   //SE DECLARA PIN 2 COMO SALIDA
  pinMode (ENA, OUTPUT);                  //SE DECLARA PIN 3 COMO SALIDA
}

void loop() {

  if (digitalRead(BOTON_1) == HIGH){       //SELECCION 0-> RETROCEDER; 1-> AVANZAR;
   
    if (digitalRead(BOTON_2) == HIGH){     //BOTON_2
          digitalWrite(LED_1, LOW);
                        digitalWrite(PUL,HIGH);    //PARA MOTOR
    }
    else {
                        digitalWrite(LED_1, HIGH);   //AVANZA
                        digitalWrite(DIR,LOW);
                        digitalWrite(ENA,HIGH);
                        digitalWrite(PUL,HIGH);
                        digitalWrite(PUL,LOW);
    }       
  }
   else{
    if (digitalRead(BOTON_2) == HIGH){
          digitalWrite(LED_2, LOW);
                        digitalWrite(PUL,HIGH);
    }
    else{
          digitalWrite(LED_2, HIGH);              //RETROCEDE
                        digitalWrite(DIR,HIGH);
                        digitalWrite(ENA,HIGH);
                        digitalWrite(PUL,HIGH);
                        digitalWrite(PUL,LOW);
    }   
  }
}

The next code is the same but for Matlab and the motor does not move

a = arduino('COM8','Uno');

configurePin(a,'D2','DigitalOutput');       % DIRECCION - PIN 2
configurePin(a,'D3','DigitalOutput');      % ENABLE - PIN 3
configurePin(a,'D4','DigitalOutput');      % PULSO - PIN 4
configurePin(a,'D5','DigitalOutput');       % LED 1 ENCENDER AVANCE
configurePin(a,'D6','DigitalOutput');      % LED 2 ENCENDER REVERSA
configurePin(a,'D7','Pullup');                 % BOTON 1 SELECTOR
configurePin(a,'D8','Pullup');             % BOTON 2 MOVER

   
while 1
   if readDigitalPin(a,'D7') == 1                  % 1 --> AVANZAR / 0 --> RETROCEDER
      if       readDigitalPin(a,'D8') == 1    % HACER NADA
               writeDigitalPin(a,'D5',0);       % D5 LED 1 APAGADO
               writeDigitalPin(a,'D4',1);       % D4 PULSO ALTO --> PARAR MOTOR
        else
                 writeDigitalPin(a,'D5',1);      % D4 LED 1 ENCIENDE
                 writeDigitalPin(a,'D2',0);           % DIRECCION --> BAJO PARA AVANZAR
               writeDigitalPin(a,'D3',1);            % ENABLE --> ALTO
               writeDigitalPin(a,'D4',1);            % PULSO --> ALTO
               writeDigitalPin(a,'D4',0);            % PULSO --> BAJO
        end
   else
      if      readDigitalPin(a,'D8') == 1    % HACER NADA
                writeDigitalPin(a,'D6',0);              % D6 LED 2 APAGADO
                writeDigitalPin(a,'D4',1);              % D4 PULSO ALTO --> PARAR MOTOR
        else
                writeDigitalPin(a,'D6',1);              % D8 LED 1 ENCIENDE
                writeDigitalPin(a,'D2',1);                    % DIRECCION --> ALTO PARA RETROCEDER
               writeDigitalPin(a,'D3',1);                    % ENABLE --> ALTO
               writeDigitalPin(a,'D4',1);                    % PULSO --> ALTO
                 writeDigitalPin(a,'D4',0);            % PULSO --> BAJO
      end
   end
end

Please help me.


 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Stepper motor control with matlab and arduino
« Reply #1 on: April 23, 2023, 03:25:01 am »
I would probably Google for "stepper motor control with matlab and arduino" and just copy/paste

Maybe this one (but there are others)  https://youtu.be/ZsyLtv77BtQ

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf