Control stepper motor with Arduino

One of the best solutions if you want to control a motor precisely is to use a stepper motor. They provide you with exact control over the position, speed, acceleration, and direction of the motor shaft.


In this tutorial we will use the Arduino and motor driver to control the stepper motor. We will make it move with a specific direction and acceleration.


Overview


There are many different kinds of motors, each of which has a variety of uses. One type is the stepper motor, which got its name because it moves in precise steps and allows you to control the movement speed and direction with high precision. This type of motor is used in many devices that you may use in your daily life, such as 3D scanners, 3D printers, and even robots.

Getting the items


For this project you will need the following components you can buy them form our store.

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat 5V DC Stepper Motor & Drive Module
25 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR

Wiring it up


To set up the stepper motor in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the stepper motor and the Arduino. Once the stepper motor, the driver and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.


Connections from the motor driver to the Arduino:
• Motor driver GND pin (- pin) → Arduino GND pin
• Motor driver VCC pin (+ pin) → Arduino 5V pin
• Motor driver IN1 pin → Arduino pin 11
• Motor driver IN2 pin → Arduino pin 10
• Motor driver IN3 pin → Arduino pin 9
• Motor driver IN4 pin → Arduino pin 8

Make sure the jumper pin is connected to the driver to switch it on, and then connect the motor cable.

Coding


This sketch function is to control the stepper motor to move in one direction at a speed of 1000 steps per second and an acceleration of 200 steps per second square. It will move for nine complete revolutions, then reverse its direction and repeat the movement.


In order for the code to work correctly, you need to download the library. Libraries are files that you can download and copy to the Arduino IDE software files so the Arduino can recognize different sensors. You can download the library files from the resources section and then install it by following this tutorial.


Stepper library

AccelStepper library


You can change the code parameters yourself and test the motor behavior.


What do you think about the best use of this code?


/*
Voltaat learn (http://learn.voltaat.com)
Link for full tutorial: https://bit.ly/3DpHV7o
Stepper library: https://bit.ly/3fmyAoG
AccelStepper library: https://bit.ly/3UeaR97

Tutorial: Control stepper motor with Arduino

This sketch function is to control the stepper motor
to move in one direction at a speed of 1000 steps per second
and an acceleration of 200 steps per second square for 9 revolutions,
then reverse its direction and repeat the movement

Connections from the motor driver to the Arduino:
• Motor driver GND pin (- pin) → Arduino GND pin
• Motor driver VCC pin (+ pin) → Arduino 5V pin
• Motor driver IN1 pin → Arduino pin 11
• Motor driver IN2 pin → Arduino pin 10
• Motor driver IN3 pin → Arduino pin 9
• Motor driver IN4 pin → Arduino pin 8

*/


//Include the Arduino Stepper.h library
#include < Stepper.h >
//Include the AccelStepper library
#include < AccelStepper.h >

//Define the motor pins
//IN1 on the motor driver
#define MP1 11
//IN2 on the motor driver
#define MP2 10
//IN3 on the motor driver
#define MP3 9
//IN4 on the motor driver
#define MP4 8

//Define the interface type
#define MotorInterfaceType 8
//Define the pin sequence (IN1-IN3-IN2-IN4)
AccelStepper stepper =AccelStepper(MotorInterfaceType, MP1, MP3, MP2, MP4);/
const int SPR =2048;//Steps per revolution

//Commands inside void setup run once
void setup(){
//Set the maximum motor speed in steps per second
stepper.setMaxSpeed(1000);
//Set the maximum acceleration in steps per second^2
stepper.setAcceleration(200);
}

//Commands inside void loop run forever
void loop(){
//Set the target motor position (9 full revolutions)
stepper.moveTo(9* SPR);
//Run the motor to the target position
stepper.runToPosition();
//delay for 1 sec
delay(1000);
//Set the target motor position (9 full revolutions but in opposite direction)
stepper.moveTo(-9* SPR);
// Run the motor to the target position
stepper.runToPosition();
//delay for 1 sec
delay(1000);
}

Testing it out



After uploading the code to your Arduino board, you will observe the stepper motor rotating slowly and making nine revolutions before changing direction and repeating the action. The driver LEDs will also flash in response to the signals transmitted from the driver to the motor.


You can change the number of revolutions, acceleration and speed in the code with the specific value that you need.


Resources 


Arduino Code

Stepper Library
AccelStepper Library

Fritzing Wiring file

Related Tutorials


A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.


In this tutorial, we will learn how to use a PNP transistor to operate a DC motor. It will provide the motor with the necessary power.


In this tutorial, we will learn more about bush buttons and how to use them. We'll use the push button in a simple project to control the Arduino's internal LED and print the current state of the button directly on your commuter.