L298N + arduino mega 2560 + stepper motor / DC motors

Code
Anonymous (not verified) Wed, 01/08/2012 - 11:14

L298N Stepper Motor Driver Controller Board for Arduino, arduino mega 2560 and stepper MINEBEA EM-257.
Disclaimer: I won't take resposibility for any damages, it worked for me without smoke, but do it at your own risk!

 

 
Now I tuned the relation between analog input and stepper movement:
 
Then tried with a Mitsumi m42sp_5 stepper, this time with a just turn example from arduino. It was rated DC 24 V, but it seemed to work fine (without load) at 13 volts:
Then, a DVD tray stepper motor:
 
 
And finally controlling two DC motors:
 

Code example for this last video:

/*              PRUEBA MOTORES              */

int Aon=22; // Define output for activation of motor A
int Bon=23; // Define output for activation of motor B
  
void setup()
{  
  pinMode(Aon,OUTPUT);  
  pinMode(Bon,OUTPUT);  
  digitalWrite(Aon,HIGH);  //In case activation is used, activate motor A
  digitalWrite(Bon,HIGH);  //In case activation is used, activate motor B
}

void loop()
{
  int i;
  for (i = 0; i < 255; i++)  
  {
  analogWrite(2,LOW);  //spin in one direction A
  analogWrite(3,i);
  analogWrite(4,LOW);  //spin in one direction B
  analogWrite(5,i);
  delay(10);
  }
  delay(3000);
  
  for (i = 0; i < 255; i++)  
  {
  analogWrite(2,i);  //spin in the other direction A
  analogWrite(3,LOW);
  analogWrite(4,i);  //spin in the other direction B
  analogWrite(5,LOW);
  delay(10);
  }
  delay(3000);

  analogWrite(2,LOW);  //stops A
  analogWrite(3,LOW);
  analogWrite(4,LOW);  //stops B
  analogWrite(5,LOW);
  delay(5000);
  
}

Code examples for stepper motor, one step at a time:

/* 
 Stepper Motor Control - one step at a time

 This program drives a unipolar or bipolar stepper motor. 
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor will step one step at a time, very slowly.  You can use this to
 test that you've got the four wires of your stepper wired to the correct
 pins. If wired correctly, all steps should be in the same direction.

 Use this also to count the number of steps per revolution of your motor,
 if you don't know it.  Then plug that number into the oneRevolution
 example to see if you got it right.

 Created 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);            

int stepCount = 0;         // number of steps the motor has taken

void setup() {
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one step:
  myStepper.step(1);
  Serial.print("steps:" );
  Serial.println(stepCount);
  stepCount++;
  delay(100);
}

-          Full rev.:
/*
 Stepper Motor Control - one revolution

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.

 The motor should revolve one revolution in one direction, then
 one revolution in the other direction. 

 
 Created 11 Mar. 2007
 Modified 30 Nov. 2009
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
                                     // for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8,9,10,11);           

void setup() {
  // set the speed at 60 rpm:
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
   Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(500);
 
   // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(500);
}

-          Knob control:
/*
 * MotorKnob
 *
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 *
 * http://www.arduino.cc/en/Reference/Stepper
 * This example code is in the public domain.
 */

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 200

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int previous = 0;

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(100);
    // initialize the serial port:
  Serial.begin(9600);
}

void loop()
{
  // get the sensor value
  //int val0 = analogRead(0);
  int val0 = analogRead(0);
  int val = (val0*0.2);
  Serial.print("Analog reading:" );
  Serial.println(val0);
  //Serial.println(val);
  // move a number of steps equal to the change in the
  // sensor reading
  if (abs(val-previous) < 100)
    {stepper.step(val - previous);
    // remember the previous value of the sensor
    previous = val;
    Serial.println(previous);
  //Serial.print("steps:" );
  //Serial.println(val);
    }
    else
    {
      Serial.print("ERROR DE LECTURA??" );
      previous = val0;
    }
}

-          Knob control and DVD tray stepper:
/*
 * MotorKnob
 *
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 *
 * http://www.arduino.cc/en/Reference/Stepper
 * This example code is in the public domain.
 */

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 25

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);

// the previous reading from the analog input
int previous = 0;

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper.setSpeed(90);
    // initialize the serial port:
  Serial.begin(9600);
}

void loop()
{
  // get the sensor value
  //int val0 = analogRead(0);
  int val0 = analogRead(0);
  int val = (val0*0.12);
  Serial.print("Analog reading:" );
  Serial.println(val0);
  //Serial.println(val);
  // move a number of steps equal to the change in the
  // sensor reading
  if (abs(val-previous) < 100)
    {stepper.step(val - previous);
    // remember the previous value of the sensor
    previous = val;
    Serial.println(previous);
  //Serial.print("steps:" );
  //Serial.println(val);
    }
    else
    {
      Serial.print("ERROR DE LECTURA??" );
      previous = val0;
    }
}

NOTE: All this code is not optimized, and was set up just to make things run. A neater approach like using mapping functions and other stuff should be used when translating analog input into movement, but all this may be useful to someone.