hey, im trying to use a 28BYJ-48 stepper motor with uln2003 driver using a arduino mega however it does not work? I need to turn the Stepper a precise angle so any suggestions to fix the example or new code? I tried other examples and it worked fine but they are really complex for my small head…
#include <Stepper.h>
const int stepsPerRevolution = 4096; // 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(100);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
}
#include <Stepper.h>
const int stepsPerRevolution = 32; // 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, 3, 4, 5, 6);
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(360);
// initialize the serial port:
Serial.begin(9600);
}
void loop() {
// step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(512);
}
never mind i forgot to install the library lol, it now works but the motor spins continuously. Is there a way to move the stepper a precise amount? its for a gauge on a flight simulator. Thanks for the help!
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define motorPin1 8 // IN1 on the ULN2003 driver 1
#define motorPin2 9 // IN2 on the ULN2003 driver 1
#define motorPin3 10 // IN3 on the ULN2003 driver 1
#define motorPin4 11 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
stepper1.moveTo(20000);
}//--(end setup )---
void loop() {
//Change direction when the stepper reaches the target position
if (stepper1.distanceToGo() == 0) {
stepper1.moveTo(-stepper1.currentPosition());
}
stepper1.run();
}
trying to interface it with my flight sim now… the motor doesnt move? any ideas?
/*
This code is in the public domain
For use with "Link2fs_Multi"
Jimspage.co.nz
My thanks to the Guys that gave me snippets of code.
Here is the basic concept of my lay-outs.
You could actually use this INO as a starting point for your own project.
Deleting all the comments makes it really simple to follow once you understand the concept
*/
#include <AccelStepper.h>
#define HALFSTEP 8
// Motor pin definitions
#define motorPin1 8 // IN1 on the ULN2003 driver 1
#define motorPin2 9 // IN2 on the ULN2003 driver 1
#define motorPin3 10 // IN3 on the ULN2003 driver 1
#define motorPin4 11 // IN4 on the ULN2003 driver 1
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
String spd;
int spdi;
int CodeIn;// The normal declearations go here
void setup(){
Serial.begin(115200);
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(100.0);
stepper1.setSpeed(200);
// The normal startup stuff goes here
}
void loop() {
{OTHER();}// Check for "Other" things to do. (Non extraction stuff)
if (Serial.available()) { //Check if anything there
CodeIn = getChar(); //Get a serial read if there is.
if (CodeIn == '=') {EQUALS();} // The first identifier is "=" ,, goto void EQUALS
if (CodeIn == '<') {LESSTHAN();}// The first identifier is "<" ,, goto void LESSTHAN
if (CodeIn == '?') {QUESTION();}// The first identifier is "?" ,, goto void QUESTION
if (CodeIn == '/') {SLASH();}// The first identifier is "/" ,, goto void SLASH (Annunciators)
}
stepper1.moveTo(spdi);
stepper1.runToPosition();
}
char getChar()// Get a character from the serial buffer(Dont touch)
{
while(Serial.available() == 0);// wait for data (Dont touch)
return((char)Serial.read());// (Dont touch) Thanks Doug
}
void OTHER(){
/* In here you would put code that uses other data that
cant be put into an "extraction void" that references something else.
Also in here you would put code to do something that was not
relying on a current extraction.
(Remember - The identifier voids only trigger when it receives that identifier)
*/
}
void EQUALS(){ // The first identifier was "="
CodeIn = getChar(); // Get another character
switch(CodeIn) {// Now lets find what to do with it
case 'A'://Found the second identifier
//Do something
break;
case 'W':
//Do something
break;
case 'a':
//Do something
break;
//etc etc etc
// You only need the "Case" testing for the identifiers you expect to use.
}
}
void LESSTHAN(){ // The first identifier was "<"
//Do something (See void EQUALS)
switch(CodeIn) {// Now lets find what to do with it
case 'P':
{
spd = "";
spd += getChar();
spd += getChar();
spd += getChar();
spdi = spd.toInt();
}
}
}
void QUESTION(){ // The first identifier was "?"
//Do something (See void EQUALS)
}
void SLASH(){ // The first identifier was "/" (Annunciators)
//Do something (See void EQUALS)
}
Note that these small steppers don't always have integer ratio gear trains. If your motor is going to be turning in the same direction for many turns you may need to add a correction factor.