A stepper motor is an electromechanical device that converts electrical pulses into discrete mechanical movements. The shaft of a stepper motor rotates in discrete increments when electrical control pulses are applied to it in the correct sequence.
connection diagram
For this exercise, the first thing we have done is to make the connection diagram using the CirkitDesigne, online simulator which will allow us to load the programming code into the Arduino IDE, as well as simulate it.
simulation carried out in circuit designer
This is the code I used:
// Pines XIAO ESP32C3 -> Driver ULN2003 (IN1 a 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
// Configuración motor
int velocidadMotor = 1000; // Microsegundos entre pasos (1000 = suave)
int pasosPorVuelta = 4076; // Pasos para 360° (28BYJ-48)
// Secuencia media fase (8 pasos) - Mayor precisión
const int tablaPasos[8] = {
B1000, // Paso 1: IN1 activo
B1100, // Paso 2: IN1+IN2
B0100, // Paso 3: IN2 activo
B0110, // Paso 4: IN2+IN3
B0010, // Paso 5: IN3 activo
B0011, // Paso 6: IN3+IN4
B0001, // Paso 7: IN4 activo
B1001 // Paso 8: IN4+IN1
};
void setup() {
// Configurar pines como salidas
pinMode(pinMotor1, OUTPUT);
pinMode(pinMotor2, OUTPUT);
pinMode(pinMotor3, OUTPUT);
pinMode(pinMotor4, OUTPUT);
Serial.begin(115200);
Serial.println("IN1,IN2,IN3,IN4"); // Encabezados para Serial Plotter
}
void loop() {
// Giro horario (2 vueltas)
for (int i = 0; i < pasosPorVuelta * 2; i++) {
ejecutarPaso(i % 8, true); // true = horario
delayMicroseconds(velocidadMotor);
}
delay(1000);
// Giro antihorario (2 vueltas)
for (int i = 0; i < pasosPorVuelta * 2; i++) {
ejecutarPaso(i % 8, false); // false = antihorario
delayMicroseconds(velocidadMotor);
}
delay(1000);
}
void ejecutarPaso(int paso, bool horario) {
int pasoActual = horario ? paso : 7 - paso; // Invierte dirección
// Escribe señales en los pines
digitalWrite(pinMotor1, bitRead(tablaPasos[pasoActual], 0));
digitalWrite(pinMotor2, bitRead(tablaPasos[pasoActual], 1));
digitalWrite(pinMotor3, bitRead(tablaPasos[pasoActual], 2));
digitalWrite(pinMotor4, bitRead(tablaPasos[pasoActual], 3));
// Envía datos al Serial Plotter (1 = HIGH, 0 = LOW)
Serial.print(bitRead(tablaPasos[pasoActual], 0));
Serial.print(",");
Serial.print(bitRead(tablaPasos[pasoActual], 1));
Serial.print(",");
Serial.print(bitRead(tablaPasos[pasoActual], 2));
Serial.print(",");
Serial.println(bitRead(tablaPasos[pasoActual], 3));
}