UniPolar and Bipolar Stepper Motors Speed and Position Control programming
Description:
Stepper Motors are used for precise positioning and that’s why they are used in Printers, CNC machines, CD ROMs, Robotics, Telescopes, toys and so on. There are mainly two types of Stepper motors
- UniPolar stepper Motors
- Bipolar stepper motors.
Unipolar motors are further divided into two groups
- 5 wires UniPolar stepper motors
- 6 wires UniPolar stepper motors.
Bipolar motors come with 4 wires.
Regardless of the stepper motors type and size, the basic working concept of all stepper motors is exactly the same. In this guide, I will teach you how to use the same programs to power all three stepper motors.
This post covers
- Constant speed and constant limits
- Variable speed and constant limits
- Precise manual positioning
The Amazon Purchase links are given below:
L298N motor driver: https://amzn.to/2Qprz68
12v UniPolar stepper motor: https://amzn.to/35aA8qB
CD ROM bipolar stepper motor: https://amzn.to/2Qa6lKN
24BYJ48 5V DC unipolar stepper motor: https://amzn.to/2Ffoel7
Arduino Uno: https://amzn.to/39aq6ZT
Mega 2560: https://amzn.to/2SszMsI
lm7805 Voltage Regulator: https://amzn.to/2ERYoTJ
330-ohm resistors pack: https://amzn.to/2Qj1Igg
female DC power jack socket: https://amzn.to/377Pg9M
470uf capacitors: https://amzn.to/2MrN3xR
5×7 cm Vero board: https://amzn.to/37b7KWO
female headers: https://amzn.to/350w6RE
connection wires: https://amzn.to/2MvOJXd
Super Starter kit for Beginners: https://amzn.to/398dliF
Jumper Wires: https://amzn.to/2SrnBwo
Bread Board: https://amzn.to/2MxV5FM
12v Adaptor: https://amzn.to/2MuOlZk
PCB plate: https://amzn.to/2MuwNMB
Variable Supply: https://amzn.to/39d0KdP
Digital Multimeter: https://amzn.to/34WbVoa
Vero Board / stripboard: https://amzn.to/39nL9Zg
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2PVwexF
Solder wire: https://amzn.to/2QlOvTS
Wire Stripper: https://amzn.to/353tYJa
wirecutter: https://amzn.to/2tV2lFj
PCB small portable drill machine: https://amzn.to/2MvQqnx
DISCLAIMER:
Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Circuit Diagram:
This is the full circuit diagram, built in the 9.1.0 edition of cadsoft eagle. As I said earlier, the basic working concept of all stepper motors is exactly the same regardless of the type and size of the motors. As you can see on the screen, the circuit diagram will remain the same for all 3 stepper motors. Like the stepper motor,
I use less than 4amps, so that’s why I chose to use the L298N motor engine. The L298N motor driver is essentially designed to control simple dc motors; I used this motor driver to power the robot in my previous tutorials … Since the L298N motor driver has the ability to control 2 dc motors at the same time, and since you know that a stepper motor has 4 wires, this means that the l298N motor diver can control the unipolar and bipolar stepper motor. Okay, there’s no connection between the through wires. The unipolar connection is the same as the l298 engine as the bipolar connection As you can see in the circuit diagram, the stepper motor’s four wires are connected to the out1, out2, out3 and out4. While the motor driver’s input pins are attached to Arduino’s Pin number 8, 9, 10, and 11.
By definition, the l298n motor driver pins come with the jumper caps, so make sure you remove the jumper caps and attach the allow pins to pin number 6 and pin number 7 of the Arduino.
J1 is the DC female power jack, where we attach the 12v or 5v external power supply and eventually link the L298N motor driver’s VCC to the Arduino’s 5volts.
Let me tell you again if you use stepper motors with current ratings in excess of 4amps then never use the motor driver l298N. Now let’s look at the L298N motor driver’s pinout.
L2989N Motor Driver Pinout:
This is the dual H bridge operator of the L298N. With a peak current of up to 2amps, it motor driver can be used to power Dc motors with voltages between 5 and 35volts. Since this is a dual HBridge motor driver, the speed and direction of two DC motors can be controlled simultaneously.
Since you have 4 wires in a stepper motor, this means that we can use the L298N motor driver to power one stepper motor. The motor coil wires of UniPolar and BiPolar Stepper are attached to the OUT1, OUT2, OUT3, and OUT4.
Let’s look at the L298N module pinout now, this module has three terminal blocks, two-terminal blocks are marked with OUT1, OUT2, OUT3, and OUT4.
This is where the Stepper Motor wires are connected.
It block is labelled with + 12v, GND, and + 5v. The external power supply is connected to the contacts of + 12v and GND. This doesn’t matter if we always connect 12 volts, it depends on the Stepper motor’s voltage level.
You can attach a 5 volt power supply to the + 12v and GND contacts if you are using a 5v Stepper motor. I will be using a 12-volt power supply and a 5-volt power supply in this article. The touch of + 5 volts is connected to the 5 volts of the Arduino.
As you can see, this motor driver also has some male headers and ENA, IN1, IN2, IN3, IN4, and ENB are clearly labeled. To allow both the engines, the ENA and the ENB are used. Jumper caps ensures all engines are permitted by default and the engines spin at full speed.
If the jumper caps are removed and the ENA and ENB pins connected to the Arduino PWM pins, the frequency of the motors can be controlled by modulation of the pulse width.
But we don’t use Pulse Width Modulation in this tutorial. We will only use the appropriate pins to turn the stepper motors on and off. In the circuit diagram, the relations are already clarified.
Stepper Motor Programming:
Stepper Motors Program 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
// constant speed and constant limits. #include <Stepper.h> int ENA = 6; int ENB = 7; int steps = 16; // you can set to different values, 4, 8, 12,16,20,24,28 and 32 Stepper myStepper(steps, 8, 9, 10, 11); void setup() { myStepper.setSpeed(50); Serial.begin(9600); digitalWrite(ENA , HIGH); digitalWrite(ENB , HIGH); } void loop() { // step one for one direction: Serial.println("clockwise"); myStepper.setSpeed(100); myStepper.step(50); delay(500); // step one for other direction: Serial.println("counterclockwise"); myStepper.step(-50); delay(500); } |
Stepper Motors Program number 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// varialbe speed and constant limits. #include <Stepper.h> int vres = A0; int data ; int ENA = 6; int ENB = 7; int steps = 32; // you can set to different values, 4, 8, 12,16,20,24,28 and 32 Stepper myStepper(steps, 8, 9, 10, 11); void setup() { myStepper.setSpeed(50); Serial.begin(9600); pinMode(vres, INPUT); digitalWrite(ENA , HIGH); digitalWrite(ENB , HIGH); pinMode(5,OUTPUT); digitalWrite(5,HIGH); } void loop() { data = analogRead(vres); data = map(data,0,1023,1,600); Serial.println(data); myStepper.setSpeed(data); // step one for one direction: Serial.println("clockwise"); myStepper.step(100); delay(100); // step one for other direction: Serial.println("counterclockwise"); myStepper.step(-100); delay(100); } |
Stepper Motors Program 3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
// Manual positioning #include <Stepper.h> int vres = A0; int data ; int ENA = 6; int ENB = 7; int cvalue = 0; // current value int pvalue = 0 ; int steps = 4; // you can set to different values, 4, 8, 12,16,20,24,28 and 32 Stepper myStepper(steps, 8, 9, 10, 11); void setup() { myStepper.setSpeed(500); Serial.begin(9600); pinMode(vres, INPUT); digitalWrite(ENA , HIGH); digitalWrite(ENB , HIGH); pinMode(5,OUTPUT); digitalWrite(5,HIGH); } void loop() { data = analogRead(vres); data = map(data,1,1023,1,50); Serial.println(data); cvalue = data ; if ( cvalue > pvalue ) { digitalWrite(ENA , HIGH); digitalWrite(ENB , HIGH); int fahad = cvalue - pvalue; Serial.println("fahad"); Serial.println(fahad); pvalue = cvalue; myStepper.step(fahad); } if ( cvalue < pvalue ) { digitalWrite(ENA , HIGH); digitalWrite(ENB , HIGH); int fahadc = pvalue - cvalue; Serial.println("fahad"); Serial.println(fahadc); pvalue = cvalue; myStepper.step(-fahadc); } if(cvalue == pvalue) { pvalue = cvalue; digitalWrite(ENA , LOW); digitalWrite(ENB , LOW); } } |