Arduino Stepper Motor
In this tutorial, I’ll show you how to connect the motor to an Arduino Nano expansion board, add an HC-SR04 ultrasonic sensor, and use Arduino code to automatically activate the stepper motor when an object is detected.
This is the same basic setup I used in my 3D Printed Smart Display, where the ultrasonic sensor detects someone approaching and automatically rotates the platform.d.
Arduino Nano
Arduino Nano Expansion Board
28BYJ-48 5V Stepper Motor
ULN2003 Driver Board
HC-SR04 Ultrasonic Sensor
Jumper wires
power supply / USB connection
The Nano expansion board is optional, but it makes the wiring much easier by giving better access to the digital pins, 5V and ground connections.
Connect the ultrasonic sensor to the Arduino Nano:
VCC → 5V
GND → GND
TRIG → D2
ECHO → D3
The HC-SR04 measures the distance in front of the project. When an object enters the detection range defined in the Arduino code, the stepper motor is activated.
The 28BYJ-48 plugs directly into the connector on the ULN2003 driver board.
Connect the ULN2003 inputs to the Arduino Nano:
IN1 → D4
IN2 → D5
IN3 → D6
IN4 → D7
Also connect:
ULN2003 VCC → 5V
ULN2003 GND → GND
The ULN2003 is required because the Arduino pins should not drive the stepper motor coils directly.
The system works in a simple sequence:
HC-SR04 → Arduino Nano → ULN2003 → 28BYJ-48 Stepper Motor
The Arduino continuously measures the distance from the ultrasonic sensor.
When an object is detected inside the chosen range, the Arduino sends the stepping sequence to the ULN2003 driver.
The driver controls the motor coils, causing the stepper motor to rotate by the amount defined in the code.
#include <Stepper.h>
// ============================================================
// Alex3DWorks
// Arduino Nano + HC-SR04 + 28BYJ-48 + ULN2003
// Ultrasonic Activated Stepper Motor
// ============================================================
// ---------------- ULTRASONIC SENSOR ----------------
const int TRIG_PIN = 2;
const int ECHO_PIN = 3;
// ---------------- STEPPER MOTOR ----------------
// 28BYJ-48 is commonly used as approximately 2048 steps/revolution
const int STEPS_PER_REVOLUTION = 2048;
// ULN2003 connections:
// IN1 -> D4
// IN2 -> D5
// IN3 -> D6
// IN4 -> D7
//
// With the Arduino Stepper library, this order works correctly
// with the 28BYJ-48 + ULN2003 combination.
Stepper stepperMotor(
STEPS_PER_REVOLUTION,
4, // IN1
6, // IN3
5, // IN2
7 // IN4
);
// ---------------- SETTINGS ----------------
const int MOTOR_SPEED = 10; // RPM
const int TRIGGER_DISTANCE = 30; // Activate below 30 cm
const int RESET_DISTANCE = 45; // Rearm above 45 cm
// Number of motor steps for each activation
// 2048 = approximately one complete revolution
const int ROTATION_STEPS = 2048;
// Prevent repeated activation while someone remains nearby
bool systemArmed = true;
// ============================================================
// SETUP
// ============================================================
void setup() {
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
stepperMotor.setSpeed(MOTOR_SPEED);
Serial.println("Alex3DWorks Stepper Motor Tutorial");
Serial.println("System ready.");
}
// ============================================================
// MAIN LOOP
// ============================================================
void loop() {
float distance = measureDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// ----------------------------------------------------------
// ACTIVATE MOTOR
// ----------------------------------------------------------
if (
systemArmed &&
distance > 0 &&
distance < TRIGGER_DISTANCE
) {
Serial.println("Object detected!");
Serial.println("Rotating stepper motor...");
stepperMotor.step(ROTATION_STEPS);
Serial.println("Rotation complete.");
// Prevent immediate re-triggering
systemArmed = false;
}
// ----------------------------------------------------------
// RESET / REARM
// ----------------------------------------------------------
if (
!systemArmed &&
distance > RESET_DISTANCE
) {
systemArmed = true;
Serial.println("System rearmed.");
}
delay(100);
}
// ============================================================
// ULTRASONIC DISTANCE FUNCTION
// ============================================================
float measureDistance() {
// Make sure trigger starts LOW
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Send 10 microsecond ultrasonic trigger pulse
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Measure echo pulse
// Timeout after 30 milliseconds
unsigned long duration =
pulseIn(ECHO_PIN, HIGH, 30000);
// If no echo is received
if (duration == 0) {
return -1;
}
// Convert echo time into distance in centimetres
float distance = duration * 0.0343 / 2.0;
return distance;
}