Stepper motor controller

<br />


Basic example to control a stepper motor using built-in L293Chip of

SensorAktor-Shield.


/* stepper controller

 *
 * control stepper motor with poti in half step mode (smoother than full step)
 *

 * SensorAktor Workshop
 * Lab3 2008
 * Kunsthochschule fuer Medien Koeln
 * Academy of Media Arts Cologne
 * http://interface.khm.de

 * pin mapping SensorAktor Shield
 * import SensorAktor.h to get already defined mapping
 *
 * analog inputs: analog0 = pin0, analog1 = pin1, analog2 = pin2,
 * analog3 = pin3, analog4 = pin4, analog5 = pin5
 * built-in poti: poti = pin5
 * amplified input: mic = pin0
 * switches: switch1 = pin1, switch2 = pin2, switch3 = pin3
 * leds: led1 = pin3, led2 = pin5, led3 = pin6
 * power outs: out1 = pin3, out2 = pin5, out3 = pin6
 * motor outs: motor1_1 = pin8, motor1_2 = pin13, motor2_1 = pin11, motor2_2 = pin12
 * motor pwms: pwmMotor1 = pin10, pwmMotor2 = pin9
 * servo outs: servo1 = pin2, servo2 = pin4, servo3 = pin7

 */

// import sensor aktor library for pin mapping
#include <SensorAktor.h> 

// import stepper library
#include <StepperL293.h>
// create instance for stepper
StepperL293 myStepper;

// poti value
int potiValue = 0;

// this block is executed one time when programm starts
void setup(){

  // attach pins to stepper (motor pin1, motor pin2, motor pin3, motor pin4, number of steps of motor)
  myStepper.attach(motor1_1, motor1_2, motor2_1, motor2_2, 48);

  // set mode (0 = half step moede, 1 = full step mode)
  myStepper.setMode(0);

  // set power (value between 0 and 100)
  myStepper.setPower(100);

  // set speed (values between 1 and 100)
  myStepper.setSpeed(50);

}

// this block is executed in a loop after setup is called one time
void loop(){

  // read poti (value between 0 and 1023)
  potiValue = analogRead(poti);

  // set step of stepper using poti value
  myStepper.setStep(potiValue);  

  // drive stepper
  myStepper.drive();

}


We developed a library to control a stepper motor using the L293 chip. Include it in the header of your code and define a variable for the stepper motor.


// import stepper library
#include <StepperL293.h>
// create instance for stepper
StepperL293 myStepper;


Then attach the pins to the motor and define the number of steps your stepper motor has.


// attach pins to stepper (motor pin1, motor pin2, motor pin3, motor pin4, number of steps of motor)
myStepper.attach(motor1_1, motor1_2, motor2_1, motor2_2, 48);


Set mode, power and speed of stepper. In full step mode the stepper has 48 steps and in half step mode 96 steps for a complete rotation.


// set mode (0 = half step moede, 1 = full step mode)
myStepper.setMode(0);

// set power (value between 0 and 100)
myStepper.setPower(100);

// set speed (values between 1 and 100)
myStepper.setSpeed(50);


To set the step use the setStep() function.


// set step of stepper using poti value
myStepper.setStep(potiValue);


The drive() function has to be called within the loop() function to refresh the stepper motor.


// drive stepper
myStepper.drive();


You can have a view at the “StepperL293.cpp” file in the library to get an idea, how the stepper motor is controlled. We use the PWM to set the power of the motor. To get a better result the PWM frequency is increased by setting its prescaler to 1.


// Timer 1 prescaler to 1
cbi(TCCR1B, CS12);
cbi(TCCR1B, CS11);
sbi(TCCR1B, CS10);


Here is short description of the sequence for the control signals for each step.


Full Step Mode:


Step Pin1 Pin2 Pin3 Pin4
1    0    0    0    1
2    0    1    0    0
3    0    0    1    0
4    1    0    0    0


Half Step Mode:


Step Pin1 Pin2 Pin3 Pin4
1    0    0    0    1
2    0    1    0    1
3    0    1    0    0
4    0    1    1    0
5    0    0    1    0
6    1    0    1    0
7    1    0    0    0
8    1    0    0    1


Have a look at the drive() function in the “StepperL293.cpp” file to get a better understanding.



Files needed

Since Arduino 0015 there are some problems with the old “SensorAktor.h” file.

Use this one instead.


SensorAktor.zip

StepperL293.zip

steppercontroller.zip