# Understanding Stepper Motors

#### scuzzo500

Joined Jul 16, 2021
2
I'm trying to learn as much as I can about stepper motors. I've been reading tutorials and watching youtube videos and I'm just stuck. I've gotten to that tipping point where I know enough to be dangerous but not enough to understand. So before I let the smoke out of something or burn down my house I figured I would ask as many questions as I can get answered. I'm just trying to get the basics of moving a stepper motor left and right.
I've got an arduino mega running a xy-160d motor controller. I'm running 18v to the motor controller. I'm running this code:
Code:
const int IN1=5;
const int IN2=4;
const int ENA=6;
const int IN3=8;
const int IN4=7;
const int ENB=9;
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(ENB, OUTPUT);
}
void loop() {
Motor1_Brake();
Motor2_Brake();
delay(100);
Motor1_Forward(200);
Motor2_Forward(200);
delay(1000);
Motor1_Brake();
Motor2_Brake();
delay(100);
Motor1_Backward(200);
Motor2_Backward(200);
delay(1000);
}
void Motor1_Forward(int Speed)
{
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
analogWrite(ENA,Speed);
}
void Motor1_Backward(int Speed)
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
analogWrite(ENA,Speed);
}
void Motor1_Brake()
{
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
}
void Motor2_Forward(int Speed)
{
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
analogWrite(ENB,Speed);
}
void Motor2_Backward(int Speed)
{
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
analogWrite(ENB,Speed);
}
void Motor2_Brake()
{
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
I've got everything wired correctly and I can get the motor to buzz and vibrate. Of course the problem is I want to rotate it. The motors I'm using are 2BYJ-48 and PK244-02AA-C8.
I have also used this code:
Code:
/* Example sketch to control a stepper motor with L298N motor driver, Arduino UNO and Stepper.h library. More info: https://www.makerguides.com */ // Include the Stepper library:
include <Stepper.h>
// Define number of steps per revolution:
const int stepsPerRevolution = 200;
// Initialize the stepper library on pins 8 through 11:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 9, 10, 11);
void setup() {   // Set the motor speed (RPMs):
myStepper.setSpeed(100); }
void loop() {   // Step one revolution in one direction:
myStepper.step(stepsPerRevolution);    delay(2000);
// Step on revolution in the other direction:
myStepper.step(-stepsPerRevolution);    delay(2000); }
Both of them create the same situation with the motor.
What am I missing to get these motors to go through a full rotation?

Here is how it is wired. When trying either of the above programs I do edit the pins and I just copied the codes above off the websites I found them at. There are two wires missing from the photo because the second program is the one I was trying when I took the picture.

#### MrChips

Joined Oct 2, 2009
26,776
I have a number of suggestions and/or questions.

1) How is the stepper motor wired? Can you show a photo of the motor and wiring? What is the part number of the motor?

2) Can you slow the steps to a dead crawl, such as one step per second and see if it works?

#### scuzzo500

Joined Jul 16, 2021
2
I have a number of suggestions and/or questions.

1) How is the stepper motor wired? Can you show a photo of the motor and wiring? What is the part number of the motor?

2) Can you slow the steps to a dead crawl, such as one step per second and see if it works?

The two wires coming off the motor control go to one coil of the motor. I'm just trying to establish that I can get the motor to turn a full rotation in any direction.

I've gone as low as 50 steps/revolution and as high as 400. It only changes the frequency of the vibration.

#### MrChips

Joined Oct 2, 2009
26,776
A stepper motor has to have more than two wires connected, usually 4, 5, or 6 wires.
Show us the motor and the model number.

Joined Jul 18, 2013
25,623
Those motors are quite different one is a common unipolar motor. the other a Bi-polar, what is the drive controllers you are using?

#### MrSalts

Joined Apr 2, 2020
2,039
Visit pololu.com and you'll be able to get a board that can take care of all the switching to each coil in the stepper. All you need is to supply the board with the direction pin, and pulse another pin to control the speed (steps).

https://www.pololu.com/product-info-merged/2879

#### atferrari

Joined Jan 6, 2004
4,647
My personal experience went like this: after reading on steppers in Jones' site I managed to have one stepper moving back and forward by touching the terminals in proper sequences with a wire to my PSU.
Next was a PIC 16F84A generating a slow sequence following the required direction by pushing one of two buttons.

More or less in line with MrChips' point 2.

#### KeepItSimpleStupid

Joined Mar 4, 2014
5,088
This is the datasheet of the driver: https://www.handsontec.com/dataspecs/module/7A-160W motor control.pdf

Steppers come in two major configurations. Unipolar and bi-polar.

The driver kinda mentions using PWM on the enable input. For now don't. You "could" use this for micostepping which we don't want to get into.

You should really wire a couple of bi-polar LEDs where the windings go and slow the speed down to a crawl. Even less that 1 pps.

here http://www.piclist.com/Techref/piclist/jal/drivingbipolarsteppermotors.htm, the Full step sequence table is what you want to see.

You can see it very easy if you put resistor, a led and a diode or a bipolr LED where the windings go.

You want to slow down the process so you can see that sequence.

You also have problems with connections, once you get the sequencw right.

The motor spec, should have phase marks and the coils identified.

if you have 5 or 6 wires, you can use a bi-polar driver, but you would not use two motor wires and the voltage you would need would double.

I used stuff from these https://www.rorze.com/en/products_category/stepping-motor-driver/ guys. They had a lot more stuff and they still might. I thought for the most part they had their act together. They even had a language within their controllers. I did need something that wasn't implemented.

They had some really neat stuff. The implemented limits, org, s-curves and all sorts of stuff.

#### shortbus

Joined Sep 30, 2009
9,525
I've got an arduino mega running a xy-160d motor controller.
As some one who doesn't understand micro controllers at all but has some experience with steppers, I don't know why you would be using that motor driver. That type of driver isn't a stepper driver module but one to drive two PMDC motors.

My reason for saying that is there are so many dedicated stepper drivers at a way smaller price tag now that it doesn't make sense not to use them. They include all of the step and direction parts built into them, so all the Arduino or other micro controller would need to give is a pulse/frequency signal and a high or low for direction.

#### KeepItSimpleStupid

Joined Mar 4, 2014
5,088
I learned the importanc eof backups. I had the project on a Syquest drive and lost it. I started working on a bunch of LabView vi's to drive a stepper motor. They were fancy. If I would have completed the project, I would have needed an ORG sensor. The limit sensors were already there. ORG is just more accurate.