Arduino Stepper Motor Serial Control Codes

Posted on  by
  1. In this example, the shaft does a full rotation clockwise, doing the number of stepper motor using the Arduino Stepper Library. The stepper is controlled by with digital pins 8, 9, 10, and 11 for either unipolar or bipolar motors.
  2. The stepper motor will behave odd with only USB power, so remember to hook up your Arduino with an external 12v power supply. The code Once the hardware is hooked up correctly, open your Arduino IDE and copy/paste one of the following code snippets to see the magic happen.
  3. In this section of the tutorial Stepper Motor Speed Control using Arduino, I am going to elaborate you about the Arduino source. I have made two different functions for increasing (accelerating) the speed of the stepper motor and for decreasing (deaccelerating) the speed of the stepper motor respectively.

Stepper motors are increasingly taking its position in the world of the electronics. Starting from a normal Surveillance camera to a complicated CNC machines/Robot these Stepper Motors are used everywhere as actuators since they provide accurate controlling. In this tutorial we will learn about the most commonly/cheaply available stepper motor 28-BYJ48 and how to interface it with Arduino using ULN2003 stepper module.

Learn how to use inexpensive L298N motor control modules to drive DC and stepper motors with Arduino. This is chapter fifty-nine of our huge Arduino tutorial series. Tutorial – L298N Dual Motor Controller Modules and Arduino Learn how to use inexpensive L298N motor control modules to drive DC and stepper motors with Arduino. This is chapter fifty-nine of our huge Arduino tutorial series. In this example, a potentiometer (or other sensor) on analog input 0 is used to control the rotational speed of a stepper motor using the Arduino Stepper Library. The stepper is controlled by with digital pins 8, 9, 10, and 11 for either unipolar or bipolar motors.

Arduino Stepper Motor Serial Control Codes

Stepper Motors:

Let us take a look at this 28-BYJ48 Stepper motor.

Okay, so unlike a normal DC motor this one has five wires of all fancy colors coming out of it and why is it so? To understand this we should first know how a stepper works and what its specialty is. First of all steppers motors do not rotate, they step and so they also known as step motors. Meaning, they will move only one step at a time. These motors have a sequence of coils present in them and these coils have to be energized in a particular fashion to make the motor rotate. When each coil is being energized the motor takes a step and a sequence of energization will make the motor take continuous steps, thus making it to rotate. Let us take a look at the coils present inside the motor to know exactly know from where these wires come from.

As you can see the motor has Unipolar 5-lead coil arrangement. There are four coils which have to be energized in a particular sequence. The Red wires will be supplied with +5V and the remaining four wires will be pulled to ground for triggering the respective coil. We use a microcontroller like Arduino energize these coils in a particular sequence and make the motor perform the required number of steps.

So now, why is this motor called the 28-BYJ48? Seriously!!! I don’t know. There is no technical reason for this motor for being named so; maybe we should dive much deeper into it. Let us look at some of the important technical data obtained from the datasheet of this motor in the picture below.

That is a head full of information, but we need to look at few important ones to know what type of stepper we are using so that we can program it efficiently. First we know that it is a 5V Stepper motor since we energize the Red wire with 5V. Then, we also know that it is a four phase stepper motor since it had four coils in it. Now, the gear ratio is given to be 1:64. This means the shaft that you see outside will make one complete rotation only if the motor inside rotates for 64 times. This is because of the gears that are connected between the motor and output shaft, these gears help in increasing the torque.

Another important data to notice is the Stride Angle: 5.625°/64. This means that the motor when operates in 8-step sequence will move 5.625 degree for each step and it will take 64 steps (5.625*64=360) to complete one full rotation.

Calculating the Steps per Revolution for Stepper Motor:

It is important to know how to calculate the steps per Revolution for your stepper motor because only then you can program it effectively.

In Arduino we will be operating the motor in 4-step sequence so the stride angle will be 11.25° since it is 5.625°(given in datasheet) for 8 step sequence it will be 11.25° (5.625*2=11.25).

Steps per revolution = 360/step angle

Here, 360/11.25 = 32 steps per revolution.

Why so we need Driver modules for Stepper motors?

Most stepper motors will operate only with the help of a driver module. This is because the controller module (In our case Arduino) will not be able to provide enough current from its I/O pins for the motor to operate. So we will use an external module like ULN2003 module as stepper motor driver. There are a many types of driver module and the rating of one will change based on the type of motor used. The primary principle for all driver modules will be to source/sink enough current for the motor to operate.

Arduino Stepper Motor Control Circuit Diagram and Explanation:

The circuit Diagram for the arduino stepper motor control project is shown above. We have used the 28BYJ-48 Stepper motor and the ULN2003 Driver module. To energise the four coils of the stepper motor we are using the digital pins 8,9,10 and 11. The driver module is powered by the 5V pin of the Arduino Board.

But, power the driver with External Power supply when you are connecting some load to the steppe motor. Since I am just using the motor for demonstration purpose I have used the +5V rail of the Arduino Board. Also remember to connect the Ground of the Arduino with the ground of the Diver module.

Code for Arduino Board:

Before we start programming with our Arduino, let us understand what should actually happen inside the program. As said earlier we will be using 4-step sequence method so we will have four steps to perform for making one complete rotation.

Step

Pin Energized

Coils Energized

Step 1

8 and 9

A and B

Step 2

9 and 10

B and C

Step 3

PDF Architect 5 Pro PDF Architect 5 Full Crack is a powerful modular PDF toolkit that allows you to read, create, convert, edit, review, insert elements into, create and fill forms within, and secure and sign PDF documents. Pdf architect activation key.

10 and 11

C and D

Step 4

11 and 8

D and A

The Driver module will have four LED using which we can check which coil is being energised at any given time. The video which shows the sequence of energization can be found at the end of this tutorial.

In this tutorial we are going to write the arduino stepper motor code and for that we will program the Arduino in such a way that we can enter the number of steps to be taken by the stepper motor through the serial monitor of the Arduino. The complete program can be found at the end of the tutorial few important lines are explained below.

The number of steps per revolution for our stepper motor was calculated to be 32; hence we enter that as shown in the line below

Next you have to create instances in which we specify the pins to which we have connected the Stepper motor.

Note: The pins number are disordered as 8,10,9,11 on purpose. You have to follow the same pattern even if you change the pins to which your motor is connected.

Since we are using the Arduino stepper library, we can set the speed of the motor using the below line. The speed can range between 0 to 200 for 28-BYJ48 stepper motors.

Now, to make the motor move one step we can use the following line.

The number of steps to be moved will be provided by the variable “val”. Since we have 32 steps and 64 as the gear ratio we need to move 2048 (32*64=2048), to make one complete rotation.

The value of the variable “val” can be entered by the user using the serial monitor.

Working of Stepper Motor with Arduino:

Once the connection is made the hardware should look something like this in the picture below.

Now, upload the below program in your Arduino UNO and open the serial monitor. As discussed earlier we will have to make 2048 steps to make one complete rotation, so when we enter 2048 the motor will make one complete rotation in clockwise direction by making 2048 steps. To rotate in anti-clockwise just enter the number with “–“negative sign. So, entering -1024 will make the motor to rotate half the way in anti-clock wise direction. You can enter any desired values, like entering 1will make the motor to take only one step.

Hope you understood the project and enjoyed building it. The complete working of the project is shown in the video below. If you have any doubts post them on the comment section below our on our forums.

// Arduino stepper motor control code

#include <Stepper.h> // Include the header file

// change this to the number of steps on your motor
#define STEPS 32

// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);

int val = 0;

void setup() {
Serial.begin(9600);
stepper.setSpeed(200);
}

void loop() {

if (Serial.available()>0)
{
val = Serial.parseInt();
stepper.step(val);
Serial.println(val); //for debugging
}

}

Active1 year, 3 months ago

I use Arduino and I'm beginner. I use the code below, but I don't understand what stepper.step(num) and STEPS in Stepper stepper(STEPS, 2,3,4,5) are, and I want to use stepper for specific degree and I don't know what I can do. for example if I want to use stepper for 40 degree what are STEP and num?

-----------------------------------------EDIT #2-----------------------------my stepper is the first one in this link:https://grahamwideman.wikispaces.com/file/view/28byj48_models_table.jpg/538898220/604x265/28byj48_models_table.jpg( I think! )and I don't know what it means?------------------------------------------EDIT #3----------------------------Thank you very much, I saw the link.

Step Angle (8-Step sequence: Internal Motor alone): 5.625° (64 steps per revolution)

and I run this code for 45deg :

because I understand each turn has 2048 steps, for example if I want to have 1/2 turn, I have to have num=1024 ( 2048/2). ok, So, for 45deg, it's 1/8 turn so I have to set num=2048/8. but for example for 33 deg what should I do? I thought num= 33*5.61 so for 45deg I use num=45*5.61you know we have a lot of floating point and I'm not sure we do it correct. right? I'm a little confused.

Arduino Control Stepper Driver

ReD
ReDReD

1 Answer

stepper.step(num)is used to rotate your motor 'num'-step. i.e. num=25, then calling stepper.step will cause your motor do 25-step.

STEPS is number of steps per revolution for your motor. It will be depending on the motor you used. For more information, you can check here.

You have to check for your motor stepper resolution. Most common motor stepper has 200 steps per revolution, which means 1 step is 360/200 = 1.8 degree. If you try Stepper.step(1), your motor will rotate 1.8 degree.

So in your case, if you already found out the resolution of your motor stepper, just do a simple conversion task:

This can be use by calling

Stepper Motor With Arduino

Motor

--EDIT--
You can check this link, since it (perhaps) using same motor stepper (24BYJ48)

Based on 24BYJ48 datasheet, it has 5.625 degree resolution / 64 steps per revolution.

Arduino Stepper Motor Serial Control Code Pdf

dhimaspwdhimaspw

Arduino Stepper Motor Speed Control

Not the answer you're looking for? Browse other questions tagged stepper or ask your own question.