// Define the pins for the A4988 stepper motor driver
#define STEP_PIN 16 // Pin connected to STEP on A4988
#define DIR_PIN 17 // Pin connected to DIR on A4988
// Define the pin for the relay module that controls the LED
#define RELAY_PIN 18 // Pin connected to IN on the relay module
// Set the number of steps per revolution for your motor (usually 200 for NEMA 17)
const int stepsPerRevolution = 50;
void setup() {
// Set the stepper motor pins as OUTPUTs
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
// Set the relay pin as an OUTPUT
pinMode(RELAY_PIN, OUTPUT);
// Set the initial state of the relay to OFF
digitalWrite(RELAY_PIN, LOW);
}
void loop() {
// --- Step 1: Rotate Motor Clockwise ---
// Set the direction to clockwise
digitalWrite(DIR_PIN, HIGH);
// Spin the motor for one full revolution
for (int i = 0; i < stepsPerRevolution; i++) {
// Generate one pulse
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500); // Adjust this value to control motor speed
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500); // Adjust this value to control motor speed
}
delay(3000); // Wait for 1 second before the next action
// --- Step 2: Blink the LED ---
// Turn the relay (and LED) ON
digitalWrite(RELAY_PIN, HIGH);
delay(1000); // Keep the LED on for 1 second
// Turn the relay (and LED) OFF
digitalWrite(RELAY_PIN, LOW);
delay(1000); // Wait for 1 second
// --- Step 3: Rotate Motor Counter-Clockwise ---
// Set the direction to counter-clockwise
digitalWrite(DIR_PIN, LOW);
// Spin the motor for one full revolution
for (int i = 0; i < stepsPerRevolution; i++) {
// Generate one pulse
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(500);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(500);
}
delay(1000); // Wait for 1 second before the next action
// --- Step 4: Blink the LED Again ---
// Turn the relay (and LED) ON
digitalWrite(RELAY_PIN, HIGH);
delay(1000); // Keep the LED on for 1 second
// Turn the relay (and LED) OFF
digitalWrite(RELAY_PIN, LOW);
delay(1000); // Wait for 1 second before the loop repeats
}