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.
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,
Now select two toggle buttons for rotating the stepper motor clockwise and counterclockwise as shown in the following image,
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.
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();
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
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
Required Materials
● Install MATLAB laptop
● Arduino UNO development board
● Stepper motor (28BYJ-48, 5VDC)
● ULN2003 - Stepper Motor Driver
Circuit Schematic
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
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.
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications