Stepper motor control with arduino via serial interface and AccelStepper library


The library for stepper motors that comes along with the development environment of arduino has several but not enough control possibilities. A good library with many control options are the AccelStepper from airspayce that can be freely downloaded from internet.

Whith AccelStepper library we can control more than one motor simultaneously from arduino. Also important characteristic is the acceleration and deceleration control .

A pilot project in operation shown in the following video.

The controller - driver and the stepper motor is from  a faulty printer.
The driver receives the inputs I01, I11 to the first (H) bridge ant the inputs I02, I2 for the second (H) bridge to control the current according to the table below.
Ampere Control via input driver.
Via the serial interface available to the arduino we give three control parameters, steps we want to move (positive or negative, left or right) maximum speed (in steps per second) that we want to catch and acceleration-deceleration (in steps per second squared).
If we give 6 or 9 numbers, separated by commas or other non-numeric character and then ENTER the motor will make in series two or three different movements, notifying us in computer screen which ones.
Not allowed negative number (or zero) to speed or acceleration so via the program we avoids this possibility.
bipolar stepper motor

The stepper motor in the video takes 200 steps to a full circle so it makes a full circle when we give to make 200 steps.

Below the control program. Anyone can copy - paste the code


// Έλεγχος bipolar stepper μοτέρ με τον οδηγό ΑΜ2170 της ΑΜtek από
// το serial interface δίνοντας 3 νούμερα:
// αριθμός βημάτων, μέγιστη ταχύτητα, επιτάχυνση(+επιβράδυνση).
// http://greekelectrician-arduino.blogspot.gr/

#include <AccelStepper.h>


// Ορισμός pin του ΑΜ2170 που συνδέονται στο arduino uno

#define I01 5
#define I11 6
#define phase1 7
#define I02 8
#define I12 9
#define phase2 10

// Ορισμός του μοτέρ και των pin που συνδέεται.

AccelStepper stepper (2,phase1,phase2); // FULL2WIRE = 2 (2 pins) στα 7 και 10

int vimata; // Από -32768 έως 32767

float taxitita; //βήματα το sec
float epitaxinsi; // βήματα το sec^2
    
void setup()
{
    Serial.begin(9600);

    //  Ορισμοί εξόδων
    pinMode(I01, OUTPUT);
    pinMode(I11, OUTPUT);
    pinMode(I02, OUTPUT);
    pinMode(I12, OUTPUT);

    //   Αρχικοποίηση  

    digitalWrite(I01, LOW);
    digitalWrite(I11, HIGH);
    digitalWrite(I02, LOW);
    digitalWrite(I12, HIGH);

    Serial.println("\nDose (+-)vimata,taxitita,epitaxinsi(ENTER)\n");
}

void loop()


  // Αν υπάρχουν διαθέσιμα νούμερα διαβασέτα:
  while (Serial.available() > 0)
  {
    // έλεγχος για τα επόμενα 3 έγκυρα νούμερα από την σειριακή είσοδο:
    vimata = Serial.parseInt();
    taxitita = Serial.parseFloat();
    epitaxinsi = Serial.parseFloat();

    taxitita = abs(taxitita); // όχι αρνητική

    epitaxinsi = abs(epitaxinsi); //όχι αρνητική
    if (taxitita == 0) taxitita = 0.1; //όχι μηδέν
    if (epitaxinsi == 0) epitaxinsi = 0.01; // όχι μηδέν

    // εμφάνιση στην οθόνη του Η/Υ

    Serial.print("vimata\t\t=\t");
    Serial.println(vimata,DEC);
    Serial.print("taxitita\t=\t");
    Serial.println(taxitita,2);
    Serial.print("epitaxinsi\t=\t");
    Serial.println(epitaxinsi,2);
      
    stepper.setMaxSpeed(taxitita);
    stepper.setAcceleration(epitaxinsi);
    stepper.move(vimata);
     
    while(stepper.distanceToGo() != 0)
    stepper.run();
     
    Serial.println("\n\nTelos kinisis\nDose (+-)vimata,taxitita,epitaxinsi(ENTER)\n");
  }
}

No comments:

Post a Comment