搜索代码

Lesson 107-2: Controlling a 28BYJ-48 Stepper Motor via Serial Monitor

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:

  1. 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");
}


  1. 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'.");
    }
  }
}
  • f causes one full forward revolution.

  • r reverses 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].




582-Project 107-2: Controlling a 28BYJ-48 Stepper Motor from Serial Monitor
语言: C++
/*
* Lesson 107-2: Controlling a stepper motor from the Serial Monitor
In this lesson, we learn how to use a mini stepper motor 28BYJ-48 for our project. I am presenting eight projects 
  so you can use it in almost any application. In the video, I have explained the code, shown the full wiring diagram, and 
  how to connect the wires, push buttons, and breadboard. 

  Project 2: We will push different keys on the keyboard and control the motor
 
 
  Project 1: Running the motor
  Project 2: Controlling a stepper motor from the Serial Monitor (this code)
  Project 3:  Controlling a stepper motor using push button STPB-1
  Project 4: Controlling using push button STPB-2  keep pressing
  Project 5: One Revolution using push button STPB-3
  Project 6: Push button for any angle and speed STPB-4
  Project 7: Using multiple buttons for any angle STPB-5 angle, speed, and direction
  Project 8: Controlling a stepper motor using a potentiometer

  * Watch video instructions for this code: https://youtu.be/TQ7R2bY-MWU
  * 
  * This code is part of the Arduino Step by Step Course, which starts here:  https://youtu.be/-6qSrDUA5a8
  * 
  * For the library for this code, visit http://robojax.com/
  * 
 If you found this tutorial helpful, please support me so I can continue creating 
 content like this. Make a donation using PayPal or a credit card: https://bit.ly/donate-robojax 
 

 
  *  * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
  * This code has been downloaded from Robojax.com
     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation, either version 3 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
 
     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <https://www.gnu.org/licenses/>.

 */

int Pin1 = 10;//IN1 is connected to 10 
int Pin2 = 11;//IN2 is connected to 11  
int Pin3 = 12;//IN3 is connected to 12  
int Pin4 = 13;//IN4 is connected to 13  
int pole1[] ={0,0,0,0, 0,1,1,1, 0};//pole1, 8 step values
int pole2[] ={0,0,0,1, 1,1,0,0, 0};//pole2, 8 step values
int pole3[] ={0,1,1,1, 0,0,0,0, 0};//pole3, 8 step values
int pole4[] ={1,1,0,0, 0,0,0,1, 0};//pole4, 8 step values

int commandLeft  =106;// 106 is ASCII value for lowercase L (l) rotate CW
int commandStop  =107;// 107 is ASCII value for lowercase K (k) stops
int commandRight =108;// 108 is ASCII value for lowercase J (j) rotate CCW

int poleStep = 0; 
int  dirStatus = 3;// stores direction status 3= stop (do not change)
int directionCommand = commandStop;
void setup() 
{ 
 pinMode(Pin1, OUTPUT);  
 pinMode(Pin2, OUTPUT);  
 pinMode(Pin3, OUTPUT);  
 pinMode(Pin4, OUTPUT);  
  Serial.begin(9600);
  Serial.println("Robojax 28BYJ-48 Stepper"); 
} 
 void loop() 
{ 
  directionCommand =getCommand();
  if(directionCommand ==commandRight) 
  {
    dirStatus =1;
  }else if(directionCommand ==commandLeft)
  {
   dirStatus  = 2;  
  }else if(directionCommand ==commandStop)
  {
    dirStatus =3; 
  }
 if(dirStatus ==1){ 
   poleStep++; 
    driveStepper(poleStep);    
 }else if(dirStatus ==2){ 
   poleStep--; 
    driveStepper(poleStep);    
 }else{
  driveStepper(8);   
 }
 if(poleStep>7){ 
   poleStep=0; 
 } 
 if(poleStep<0){ 
   poleStep=7; 
 } 
 delay(1); 

}// loop

void driveStepper(int c)
{
     digitalWrite(Pin1, pole1[c]);  
     digitalWrite(Pin2, pole2[c]); 
     digitalWrite(Pin3, pole3[c]); 
     digitalWrite(Pin4, pole4[c]);   
}

/*
 * 
 * @brief Turns gets command from Serial monitor to control  
 * direction of stepper motor
 * @param none
 * @return integer value of ASCII character
 */
int getCommand()
{
  int command =0;
        if (Serial.available() > 0) {
                command =Serial.read()  ;
        } 
  //Serial.println(command);
 if(command >= commandLeft &&   command <= commandRight){   
  return command; 
 }else{
  return 1000;//nothing was received
 }
}//getCommand() end

|||您可能需要的东西

文件📁

没有可用的文件。