#include "pico/stdlib.h"
// Define pins
#define DIR_PIN 2 // Direction pin connected to A4988
#define STEP_PIN 3 // Step pin connected to A4988
#define STEPS_PER_REV 200 // 1.8° stepper motor
#define HALF_TURN (STEPS_PER_REV / 2) // 180° = 100 steps
// Stepper motor function
void stepMotor(int steps, bool direction, int delay_us) {
gpio_put(DIR_PIN, direction); // 0 = CW, 1 = CCW
for (int i = 0; i < steps; i++) {
gpio_put(STEP_PIN, 1);
sleep_us(delay_us);
gpio_put(STEP_PIN, 0);
sleep_us(delay_us);
}
}
int main() {
stdio_init_all();
// Initialize pins
gpio_init(DIR_PIN);
gpio_set_dir(DIR_PIN, true);
gpio_init(STEP_PIN);
gpio_set_dir(STEP_PIN, true);
while (true) {
// Rotate 180° clockwise
stepMotor(HALF_TURN, 0, 1000);
sleep_ms(3000); // wait 3 seconds
// Rotate 180° counter-clockwise
stepMotor(HALF_TURN, 1, 1000);
sleep_ms(3000); // wait 3 seconds
}
}