// XIAO ESP32C3 Pins -> ULN2003 Driver (IN1 to IN4) const int pinMotor1 = 2; // GPIO2 -> IN1 const int pinMotor2 = 3; // GPIO3 -> IN2 const int pinMotor3 = 4; // GPIO4 -> IN3 const int pinMotor4 = 5; // GPIO5 -> IN4 // Motor configuration int motorSpeed = 1000; // Microseconds between steps (1000 = smooth) int stepsPerRevolution = 4076; // Steps for 360° (28BYJ-48 motor) // Half-step sequence (8 steps) - More precision const int stepTable[8] = { B1000, // Step 1: IN1 active B1100, // Step 2: IN1 + IN2 B0100, // Step 3: IN2 active B0110, // Step 4: IN2 + IN3 B0010, // Step 5: IN3 active B0011, // Step 6: IN3 + IN4 B0001, // Step 7: IN4 active B1001 // Step 8: IN4 + IN1 }; void setup() { // Set pins as outputs pinMode(pinMotor1, OUTPUT); pinMode(pinMotor2, OUTPUT); pinMode(pinMotor3, OUTPUT); pinMode(pinMotor4, OUTPUT); Serial.begin(115200); Serial.println("IN1,IN2,IN3,IN4"); // Headers for Serial Plotter } void loop() { // Clockwise rotation (2 revolutions) for (int i = 0; i < stepsPerRevolution * 2; i++) { runStep(i % 8, true); // true = clockwise delayMicroseconds(motorSpeed); } delay(1000); // Counter-clockwise rotation (2 revolutions) for (int i = 0; i < stepsPerRevolution * 2; i++) { runStep(i % 8, false); // false = counter-clockwise delayMicroseconds(motorSpeed); } delay(1000); } void runStep(int stepIndex, bool clockwise) { int currentStep = clockwise ? stepIndex : 7 - stepIndex; // Reverse direction if needed // Write signals to motor driver inputs digitalWrite(pinMotor1, bitRead(stepTable[currentStep], 0)); digitalWrite(pinMotor2, bitRead(stepTable[currentStep], 1)); digitalWrite(pinMotor3, bitRead(stepTable[currentStep], 2)); digitalWrite(pinMotor4, bitRead(stepTable[currentStep], 3)); // Send pin states to Serial Plotter (1 = HIGH, 0 = LOW) Serial.print(bitRead(stepTable[currentStep], 0)); Serial.print(","); Serial.print(bitRead(stepTable[currentStep], 1)); Serial.print(","); Serial.print(bitRead(stepTable[cur|rentStep], 2)); Serial.print(","); Serial.println(bitRead(stepTable[currentStep], 3)); }