How to control a stepper motor using MATALB and Arduino

Publisher:码农侠Latest update time:2022-05-18 Source: elecfansKeywords:MATALB  Arduino Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

A stepper motor is a brushless DC motor that can rotate in steps and is the best choice for many precision motion control applications. In addition, stepper motors are suitable for positioning, speed control, and applications that require high torque at low speeds.


In previous MATLAB tutorials, we have covered how to use MATLAB to control DC motors, servo motors, and home appliances. Today we will learn how to control a stepper motor using MATALB and Arduino. If you are new to MATLAB, then it is recommended that you first learn to start a simple LED blinking program using MATLAB.


Working modes of stepper motors

Before starting the stepper motor related code, you should understand the working or rotation concept of the stepper motor. Since the stator of the stepper mode is composed of different coil pairs, each coil pair can be excited in many different ways, which enables the mode to be driven in many different modes. The following is a relatively broad classification:

● Full Step Mode

In full-step excitation mode, we can achieve a full 360° rotation with the minimum number of turns (step length). But this will result in reduced inertia and the rotation will not be smooth. There are two more classifications in full-step excitation, which are one phase-on square wave stepping and two phase-on square wave stepping modes.

o4YBAF_-pxCAelKDAAOAilA-K_c596.png

1. One Phase Stepping or Wave Stepping: In this mode, only one terminal (phase) of the motor will be energized at any given time. This has a lesser number of steps, so a full 360° rotation can be achieved. Since there are fewer steps, this method also consumes very low current. The following table shows the waveform stepping sequence for a 4-phase stepper motor

Steps Phase 1 (blue) Phase 2 (Pink) Phase 3 (yellow) Phase 4 (Orange)
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

2. Two-phase stepping: As the name in this method indicates, two phases will be one step number. It has the same number of steps as the waveform stepping, but since two coils are energized at a time, it can provide better torque and speed compared to the previous method. But one disadvantage is that this method also consumes more power.

Steps Phase 1 (blue) Phase 2 (Pink) Phase 3 (yellow) Phase 4 (Orange)
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1

● Half-step mode

The half-step mode is a combination of the one-phase on mode and the two-phase on mode. This combination will help us overcome the above disadvantages of the two modes.

As you might have guessed, since we are combining two methods, we will perform 8 steps in this method to get a full rotation. The switching sequence for a 4-phase stepper motor is shown below:

Steps Phase 1 (blue) Phase 2 (Pink) Phase 3 (yellow) Phase 4 (Orange)
1 1 0 0 0
2 0 1 0 0
3 0 1 0 0
4 0 1 1 0
5 0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1

So you can choose to program the stepper motor in any mode but I prefer the two phase stepper mode. Because this method gives faster speed than single phase method and less code size due to less number of steps in two phase method compared to half mode.


Creating a MATLAB Graphical User Interface for Controlling a Stepper Motor

Then we have to build the GUI (Graphical User Interface) to control the stepper motor. To start the GUI, type the following command in the command window

guide

A popup window will open and then select New Blank GUI as shown in the image below,

pIYBAF_-py-ANIygAAD9DW7ttz4898.png

Now select two toggle buttons for rotating the stepper motor clockwise and counterclockwise as shown in the following image,

o4YBAF_-pzyAWN-hAACa1dDDfac465.png

To resize or change the shape of a button, just click on it and you can drag the corners of the button. By double clicking on the toggle button, you can change the color, string, and tag of that particular button. We customized two buttons as shown in the following image.

o4YBAF_-p0eALwcyAAGXeIdzpHo262.png

o4YBAF_-p1iAeJ5iAAA0oPtRoIs833.png

You can customize the button as per your choice. Now when you save it, a code is generated in the Editor window of MATLAB. To code the Arduino to perform any task related to the project, you always have to edit this generated code. So below we have edited the MATLAB code. You can learn more about the Command Window, Editor Window, etc. in the Getting Started with MATLAB tutorial.

MATLAB code to control stepper motor using Arduino

The complete MATLAB code to control the stepper motor is given at the end of this article. Also, we have included here the GUI file (.fig) and code file (.m) for download (right click on the link and select "Save Link As...") using which you can customize the buttons as per your requirements. Below are some adjustments we have made using two toggle buttons to rotate the stepper motor clockwise and counterclockwise.

Copy and paste the following code on line 74 of code to ensure that Arduino is communicating with MATLAB every time you run the m-file.

clear all;

global a;

a = arduino();

o4YBAF_-p2WAKrMHAABch0hEDE0602.png

As you scroll down, you will see two functions created for the two buttons in the GUI. Now write the code in both the functions according to the task you want to perform on click.

In the clockwise button function, copy and paste the following code at the end of the function to rotate the motor clockwise. To continuously rotate the stepper motor in clockwise direction, we use a while loop to repeat the two phase stepping full mode steps in clockwise direction.

while get(hObject,'Value')

global a;

writeDigitalPin(a, 'D8', 1);

writeDigitalPin(a, 'D9', 0);

writeDigitalPin(a, 'D10', 0);

writeDigitalPin(a, 'D11', 1);

pause(0.0002);

writeDigitalPin(a, 'D8', 0);

writeDigitalPin(a, 'D9', 0);

writeDigitalPin(a, 'D10', 1);

writeDigitalPin(a, 'D11', 1);

pause(0.0002);

writeDigitalPin(a, 'D8', 0);

writeDigitalPin(a, 'D9', 1);

writeDigitalPin(a, 'D10', 1);

writeDigitalPin(a, 'D11', 0);

pause(0.0002);

writeDigitalPin(a, 'D8', 1);

writeDigitalPin(a, 'D9', 1);

writeDigitalPin(a, 'D10', 0);

writeDigitalPin(a, 'D11', 0);

pause(0.0002);

end

pIYBAF_-p3WAFcHKAAIydZ6EPg4171.png

Now in the function of the counterclockwise button, paste the following code at the location of the function to rotate the motor in counterclockwise direction. To rotate the stepper motor continuously in counterclockwise direction, we use a while loop to repeat the two phase stepping full mode steps in counterclockwise direction.

while get(hObject,'Value')

global a;

writeDigitalPin(a, 'D8', 1);

writeDigitalPin(a, 'D9', 1);

writeDigitalPin(a, 'D10', 0);

writeDigitalPin(a, 'D11', 0);

pause(0.0002);

writeDigitalPin(a, 'D8', 0);

writeDigitalPin(a, 'D9', 1);

writeDigitalPin(a, 'D10', 1);

writeDigitalPin(a, 'D11', 0);

pause(0.0002);

writeDigitalPin(a, 'D8', 0);

writeDigitalPin(a, 'D9', 0);

writeDigitalPin(a, 'D10', 1);

writeDigitalPin(a, 'D11', 1);

pause(0.0002);

writeDigitalPin(a, 'D8', 1);

writeDigitalPin(a, 'D9', 0);

writeDigitalPin(a, 'D10', 0);

writeDigitalPin(a, 'D11', 1);

pause(0.0002);

end

pIYBAF_-p4OAEj63AALYo9rEUt4132.png

Required Materials

● Install MATLAB laptop

● Arduino UNO development board

● Stepper motor (28BYJ-48, 5VDC)

● ULN2003 - Stepper Motor Driver

Circuit Schematic

pIYBAF_-p5aAZK_9AAFOUdZTyz0506.png

o4YBAF_-p6mAQPvRAAXOfB5SOYU505.png

Controlling stepper motors with MATLAB

After setting up the hardware according to the circuit diagram, just click the run button to run the edited code in the .m file

o4YBAF_-p7iAGC_IAAC-c7UwSZI053.png

MATLAB may take a few seconds to respond, do not click any GUI buttons until MATLAB displays a busy message in the bottom left corner as shown below,


After everything is ready, press the clockwise or counterclockwise direction button to rotate the motor. When we use the toggle button, the stepper motor will move continuously in the clockwise direction until we press the button again. Similarly, press the counterclockwise toggle button and the motor starts rotating counterclockwise until we press the button again.

o4YBAF_-p_OARb80AATBpbW9e7k489.png


Keywords:MATALB  Arduino Reference address:How to control a stepper motor using MATALB and Arduino

Previous article:PLC motor forward and reverse control circuit diagram ladder diagram program
Next article:Maintenance and Fault Diagnosis of Low Voltage Inverter

Recommended ReadingLatest update time:2024-11-16 12:03

Build your own Arduino on a breadboard using the ATmega8L-8PU and flash an LED
Step 1: Hardware Preparation 1.ATMEGA8L-8PU avr microcontroller (Figure 1) figure 1 2. USBasp (a tool for burning bootloader) (Figure 2, top) 3. USB to TTL serial cable (I use the FT232 chip, which has better stability and is used to load programs under Arduino) (Figure 2 below) figure 2 4. 16M crystal, br
[Microcontroller]
Build your own Arduino on a breadboard using the ATmega8L-8PU and flash an LED
Design of Cellular Modem Module for ARM Based Arduino Processor Board
By leveraging the cellular infrastructure that carriers have already established, designers can use this network instead of Wi-Fi or other network interfaces to collect data or provide remote management. Cellular networks are ideal for small to medium-sized data packets and can reduce the hardware budget of large dist
[Microcontroller]
Design of Cellular Modem Module for ARM Based Arduino Processor Board
Design of ultrasonic ranging system based on nRF24L01+ and Arduino
Distance is an important part of describing the plane structure of a building. Traditional methods of measuring building dimensions require personnel to use tools on site, but when facing dangerous buildings, traditional measurement methods are bound to increase the chance of casualties. With the development of science
[Test Measurement]
Design of ultrasonic ranging system based on nRF24L01+ and Arduino
Latest Embedded Articles
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号