本教程是的一部分: 第 107 课 28BYJ-48 步进电机 8 个项目
本视频包含 8 个项目,每个项目都有独立的代码。其他视频的链接位于本文下方。
Lesson 107-2: Controlling a 28BYJ-48 Stepper Motor via Serial Monitor
Project 2: Controlling Stepper Motor via Serial Monitor – 28BYJ-48 Stepper Motor
This article is the second installment of an 8-part tutorial on controlling the 28BYJ-48 stepper motor using Arduino. In this project, we introduce serial communication as a method of user input. Using the Arduino Serial Monitor, users can input commands to rotate the motor in either direction.
All source code and wiring diagrams related to this project are available for download at the end of this article.
📘 Introduction
This project expands on basic motor control by enabling real-time interaction via the Serial Monitor. Rather than hardcoding rotation logic into the loop(), the user enters a command through the monitor to make the motor turn forward or backward. This is ideal for debugging or creating interactive testing tools for hardware setups.
âš™ï¸ Wiring Diagram
The hardware setup remains unchanged from Project 1. The 28BYJ-48 stepper motor connects to the ULN2003 driver, which then interfaces with the Arduino as follows:
IN1 → Arduino pin 8
IN2 → Arduino pin 9
IN3 → Arduino pin 10
IN4 → Arduino pin 11
Power and GND to ULN2003 from Arduino
Caption: Project 2 wiring using ULN2003 and Arduino Uno. Same configuration as Project 1.
💻 Code Explanation
The code introduces Serial.readStringUntil() to accept user input from the Serial Monitor:
Stepper Setup:
#include <Stepper.h>
const int stepsPerRevolution = 2048;
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);2 Initialization
void setup() {
myStepper.setSpeed(10);
Serial.begin(9600);
Serial.println("Enter direction: f=forward, r=reverse");
}Main Loop with Serial Input:
void loop() {
if (Serial.available()) {
String input = Serial.readStringUntil('\n');
input.trim();
if (input == "f") {
myStepper.step(stepsPerRevolution);
} else if (input == "r") {
myStepper.step(-stepsPerRevolution);
} else {
Serial.println("Invalid input. Use 'f' or 'r'.");
}
}
}fcauses one full forward revolution.rreverses one full revolution.
This adds basic text-based control for interactive stepper testing.
🎬 Timestamps from Full Video
Reference the full tutorial video for this project at the following timestamps:
22:23 – Project 2: Introduction and code explanation
32:03 – Project 2: Demonstration using Serial Monitor
📠Download Section
The code and wiring schematic for this project can be downloaded below. These resources will help you build, test, and adapt the Serial Monitor-controlled stepper motor in your own projects.
Next up is [Project 3: Stepper Motor Control Using Push Button STPB-1].
本教程是……的一部分: 第 107 课 28BYJ-48 步进电机 8 个项目
- Lesson 107-1: Start and Stop the 28BYJ-48 Stepper Motor with Direction Set in Code
- Lesson 107-3: Controlling a 28BYJ-48 Stepper Motor Using Three Push Buttons: CW, CCW, and Stop (STPB-1)
- Lesson 107-4: Controlling a 28BYJ-48 Stepper Motor Using Two Push Buttons, CW, CCW (Keep Pressed), STPB-2
- Lesson 107-5: Send 28BYJ-48 Motor for One Revolution in CW or CCW Direction, STPB-3
- Lesson 107-6: Controlling a 28BYJ-48 Stepper Motor Using Three Push Buttons, with Angle and Speed STPB-4
- Lesson 107-7: Sending a 28BYJ-48 Stepper Motor to Any Angle with Defined STPB-5 Push Buttons
- Lesson 107-8: Controlling the Speed of a 28BYJ-48 Stepper Motor Using a Potentiometer
This code has not been parsed yet. Please return to the admin panel to parse it.
|||您可能需要的东西
-
全球速卖通Purchase 5 pcs 28BYJ-48 stepper motors from AliExpresss.click.aliexpress.com
资源与参考
-
外部Purchase 5 pcs 28BYJ-48 stepper motors from AliExpresss.click.aliexpress.com
文件📁
没有可用的文件。