Project showcase
Stepper Motor Control with STSPIN820 Evaluation Board

Stepper Motor Control with STSPIN820 Evaluation Board © GPL3+

Drive a bipolar stepper motor with the EVALSP820 -XS and Arduino Mega 2560.

  • 2,487 views
  • 1 comment
  • 7 respects

Components and supplies

About this project

The project shows how to control a stepper motor using the IC STSPIN820 product by the STMicroelectronics. Using the Arduino Mega 2560 (can be used other Arduino models) and the Evaluation board EVALSP820-XS of the STSPIN820 we can control a bipolar stepper motor in term of speed, direction and step ( until 1/256) with more protection (over current, over temperature, short protection, VM UVLO). This board is very simple to use and very small as shown in the schematic. With Arduino it is possible control the motor speed by changing the frequency of the STEP signal, the direction ( clock wise and counter clock wise ) by changing the state of the signal DIR and the driving mode ( from full step to 1/256) by the input M0, M1, M2 as described in the datatsheet of the IC. The evaluation board requires the power supply VCC for the comand signals ( 5V) picked from Arduino board and the power supply VM for the output stage ( VM max= 45V) supplied externaly using battery or regulated power supply. By serial monitor the user can do more actions on the motor: turn on/off ( comand e/o), set the number of step sent to the motor (comand sxx), set the motor speed ( comand fxx), set the drive mode (comand mxx) and the direction CCW or CW ( comand r/l).

Code

STEPPER MOTOR CONTROL WITH STSPIN820 EVAL BOARDArduino
[code]

/*
BIPOLAR STEPPER MOTOR CONTROL WITH EVALUATION BOARD OF THE STSPIN820 BY STMicroelectronicss
Versione 1.0 del 07/03/2018
AUTORE: microst
*/

// set pin numbers:
const int EN = 23;     
const int M0 = 25;      
const int M1 = 27;
const int M2 = 29;
const int STDBY = 33;
const int STEP = 35;
const int DIR = 37;


// variables will change:

int number = 0;        
int freq = 10;
int swpulse = 0;
int lettura = 0;
int dur = 0.1;
int mode = 0;
int m0 = 0;
int m1 = 0;
int m2 = 0;
int dir= 0;
unsigned long tempMillis;  di PWM
byte caratteri; 
byte tempChar; 


void setup() {
  // pin definitions
  Serial.begin(9600);
  pinMode(STDBY, OUTPUT);
  pinMode(M0, OUTPUT );
  pinMode(M1, OUTPUT );
  pinMode(M2, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(STEP, OUTPUT);
  digitalWrite(STDBY, LOW); //device in stand-by
  pinMode(EN, OUTPUT); digitalWrite(EN, LOW); // output stage disabled
  digitalWrite(M0, HIGH);digitalWrite(M1, LOW);digitalWrite(M2, LOW); //initial mode configuration 
  delay (1000);
  // Serial monitor menu
  Serial.println(F(" m = mode")); 
  Serial.println(F(" r = dir anti clockwise "));
  Serial.println(F(" l = dir clockwise"));
  Serial.println(F(" e = driver on "));
  Serial.println(F(" o = driver in stand by"));
  Serial.println(F(" s = step"));
  Serial.println(F(" f = frequency or speed"));
  //
}

void loop() {
 //check serial
  if (Serial.available()) {
    byte command = Serial.read(); //leggo il primo byte
    switch (command) { //controllo che sia un comando valido
  
  // enable the device ( out of stand by )
    case 'e': 
          Serial.println(" driver on ");
          digitalWrite(STDBY, HIGH);
          delay(1);
    break;
  // disable the device - stand by
    case 'o': 
          Serial.println(" driver in stand-by");
          digitalWrite(STDBY, LOW);
    break;
  // define the cw motor direction
    case 'l': 
          Serial.println(" direction clock wise");
          dir=1;
    break;
  // define the ccw motor direction 
    case 'r': 
          Serial.println(" direction counter clock wise");
          dir=0;
    break;
    case 's': //step number
           readnumb();
           swpulse=lettura;
           Serial.println(F("step number  is  "));
           Serial.println(swpulse, DEC);
           Serial.println(F(" motor actived  "));
           stepper(swpulse,m0,m1,m2,dir,dur);
           Serial.println(F("motor stopped  "));
    break;
    case 'f': // speed number
           readnumb();
           freq=lettura;
           dur= freq/2;
           Serial.print(F("speed is  "));
           Serial.println(freq, DEC);
    break;
    case 'm': //mode selection 
           readnumb();
           mode=lettura;
           if(mode>6){mode=6;}
           m0=0;m1=0;m2=0;
           if ( mode==0 ) { Serial.println(F("mode 1/1 step selected"));}
           if ( mode==1 ) { Serial.println(F("mode 1/2 step selected"));m0=1;};
           if ( mode==2 ) { Serial.println(F("mode 1/4 step selected"));m1=1;}
           if ( mode==3 ) { Serial.println(F("mode 1/8 step selected"));m0=1;m1=1;}
           if ( mode==4 ) { Serial.println(F("mode 1/16 step selected"));m2=1;}
           if ( mode==5 ) { Serial.println(F("mode 1/128 step selected"));m2=1;m1=1;}
           if ( mode==6 ) { Serial.println(F("mode 1/256 step selected"));m2=1;m1=1;m0=1;}
    break;
    }
    }
}



// function stepper for EVALSP820-XS
int stepper (int swpulse, int m0, int m1, int m2, int dir, int dur){
  if (m0==0) {digitalWrite(M0, LOW);} else {digitalWrite(M0, HIGH);}
  if (m1==0) {digitalWrite(M1, LOW);} else {digitalWrite(M1, HIGH);}
  if (m2==0) {digitalWrite(M2, LOW);} else {digitalWrite(M2, HIGH);}
  if (dir==0) {digitalWrite(DIR, LOW);} else {digitalWrite(DIR, HIGH);}
  pinMode(EN, INPUT_PULLUP); // output stage  on
  delay(10);
  if (digitalRead(EN)==LOW)  {Serial.println(F(" VM UVLO condition: check the VM power supply ")); }
  while (digitalRead(EN)==LOW) {;}
  // STEP GENERATOR
  for ( int i = 0; i < swpulse; i++) {
    digitalWrite(STEP, HIGH);
    delay(dur);
    digitalWrite(STEP, LOW); 
    delay(dur);   
  if (digitalRead(EN)==LOW)  {Serial.println(F("fault  ")); i=swpulse; }
  }
  pinMode(EN, OUTPUT);  
  digitalWrite(EN, LOW); // output stage off
  return;
}
  
  int readnumb (){
  lettura = 0; tempMillis = millis(); caratteri = 0;
  do {
      if (Serial.available()) {
               tempChar = Serial.read();
               caratteri++;
               if ((tempChar >= 48) && (tempChar <= 57)) { //è un numero? ok
                   lettura = (lettura * 10) + (tempChar - 48);
                   } else if ((tempChar == 10) || (tempChar == 13)) {
                   //con invio e/o nuova linea esco
                   break;
                   }
                }
     } while ((millis() - tempMillis < 500) && (lettura <= 100) && (caratteri < 10));
          //se il valore di PWM è valido, lo imposto sul pin
          if (lettura >= 500) {lettura=500;}
  return (lettura);
}


[/code]

Schematics

STSPIN820 Evalution board with arduino mega 2560
Stspinp820 stepper driver 0x9k3znv0t

Comments

Similar projects you might like

TB6600 Stepper Motor driver Tester

Project in progress by ashleyblack

  • 6,961 views
  • 4 comments
  • 14 respects

Control The Stepper Motor

Project tutorial by Haoming Weng

  • 6,742 views
  • 1 comment
  • 17 respects

Control a 220VAC Universal AC Motor with Arduino

Project showcase by microst

  • 1,060 views
  • 0 comments
  • 3 respects
Add projectSign up / Login