Go Down

Topic: New Stepper Motor Example code that works (Read 172 times) previous topic - next topic

CommunityWorkshops

Code: [Select]
/*
  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 by Tom Igoe
  Modified 30 Nov. 2009 by Tom Igoe
  Modified 24 Aug. 2016 by Dave Gordon https://www.youtube.com/channel/UCw0RquafdM8U-0hfqNQz8AQ

*/

#include <Stepper.h>

const int stepsPerRevolution = 2048;  // 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,  10, 9, 11);

void setup() {
  // set the speed at 10 rpm: (15 or less for most motors and controllers)
  myStepper.setSpeed(10);
  // initialize the serial port:
  Serial.begin(9600);
}

void loop() {
  // step one revolution  in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  delay(50);

  // step one revolution in the other direction:
  Serial.println("counterclockwise");
  myStepper.step(-stepsPerRevolution);
  delay(50);
}


Changes: Changed StepsPerRevolution to be more in line with current stepper motors
             Changed initiating the Stepper class - pins changed to reflect most stepper motor driver pin out documentation
             Changed StepperSpeed to 10 most motors from kits don't work over 15 RPM
             Changed delay to 50. 500ms was too long.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy