Stepper Motors - johnosbb/Automation GitHub Wiki
- IN1 – IN4 are motor control input pins. Connect them to the Arduino’s digital output pins.
- GND is the ground pin.
- VCC pin powers the motor. Because the motor consumes a significant amount of power, it is preferable to use an external 5V power supply rather than from the Arduino.
//Includes the Arduino Stepper Library
#include <Stepper.h>
// Defines the number of steps per rotation
const int stepsPerRevolution = 2038;
// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// Nothing to do (Stepper Library sets pins as outputs)
}
void loop() {
// Rotate CW slowly at 5 RPM
myStepper.setSpeed(5);
myStepper.step(stepsPerRevolution);
delay(1000);
// Rotate CCW quickly at 10 RPM
myStepper.setSpeed(10);
myStepper.step(-stepsPerRevolution);
delay(1000);
}
Note: step() is a blocking function. This means that the Arduino will wait until the motor stops running before proceeding to the next line in your sketch. For example, on a 100-step motor, if you set the speed to 1 RPM and call step(100), this function will take a full minute to finish.
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-stepper-motor-28byj-48-uln2003/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
Based on Stepper Motor Control - one revolution by Tom Igoe
*/
#include <AccelStepper.h>
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// ULN2003 Motor Driver Pins
#define IN1 5 // D1
#define IN2 4 // D2
#define IN3 14 // D5
#define IN4 12 // D6
// initialize the stepper library
AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);
void setup() {
// initialize the serial port
Serial.begin(115200);
// set the speed and acceleration
stepper.setMaxSpeed(500);
stepper.setAcceleration(100);
// set target position
stepper.moveTo(stepsPerRevolution);
}
void loop() {
// check current stepper motor position to invert direction
if (stepper.distanceToGo() == 0){
stepper.moveTo(-stepper.currentPosition());
Serial.println("Changing direction");
}
// move the stepper motor (one step at a time)
stepper.run();
}