This circuit is designed to control two 28BYJ-48 stepper motors using an Arduino UNO microcontroller and two ULN2003A breakout boards. The Arduino UNO is programmed to receive commands via its serial interface to control the direction and state (running or stopped) of the stepper motors. The ULN2003A breakout boards serve as the driving interface between the low-power Arduino digital outputs and the higher-power requirements of the stepper motors.
#include <AccelStepper.h>
// Define the pins for motor 1
AccelStepper motor1(AccelStepper::HALF4WIRE, 8, 10, 9, 11);
// Define the pins for motor 2
AccelStepper motor2(AccelStepper::HALF4WIRE, 4, 6, 5, 7);
// Set initial rotation speed
const int rotationSpeed = 500;
void setup() {
// Set up the serial communication for Bluetooth
Serial.begin(9600);
motor1.setMaxSpeed(rotationSpeed);
motor1.setAcceleration(500);
motor1.setCurrentPosition(0);
motor2.setMaxSpeed(rotationSpeed);
motor2.setAcceleration(500);
motor2.setCurrentPosition(0);
}
void loop() {
// Check if data is available from the Bluetooth app
if (Serial.available() > 0) {
char command = Serial.read();
// Write commands for the app
switch (command) {
case 'F': // Start both motors rotating forward
motor1.setSpeed(rotationSpeed);
motor2.setSpeed(rotationSpeed);
motor1.enableOutputs();
motor2.enableOutputs();
break;
case 'B': // Start both motors rotating backward
motor1.setSpeed(-rotationSpeed);
motor2.setSpeed(-rotationSpeed);
motor1.enableOutputs();
motor2.enableOutputs();
break;
case 'S': // Stop and disconnect both motors
motor1.setSpeed(0); // Set speed to 0 to stop immediately
motor2.setSpeed(0); // Set speed to 0 to stop immediately
motor1.disableOutputs(); // Disable outputs to disconnect motors
motor2.disableOutputs(); // Disable outputs to disconnect motors
break;
default:
// Ignore any other commands
break;
}
}
// Run motors
motor1.runSpeed();
motor2.runSpeed();
}
Filename: sketch.ino
Description: This code snippet is the main program for the Arduino UNO. It initializes two AccelStepper
objects for controlling the stepper motors. The setup function configures the serial communication and sets the maximum speed and acceleration for both motors. The loop function listens for serial commands to control the motors' speed and direction.