Experiment 7: Stepper Motor Control via Microcontroller: Difference between revisions
No edit summary |
No edit summary |
||
| (20 intermediate revisions by 2 users not shown) | |||
| Line 7: | Line 7: | ||
# Connect stepper motor and driver to Arduino. | # Connect stepper motor and driver to Arduino. | ||
# Write code to rotate motor clockwise and counter-clockwise. | # Write code to rotate motor clockwise and counter-clockwise. | ||
# Vary delay to change motor speed. | # Vary delay to change motor speed. | ||
=== Circuit Diagram === | <span style="background-color: #ffcccc; display: inline-block; padding: 5px; border-radius: 5px; font-weight: bold;"> | ||
⚠️ Disconnect your Arduino from the computer while making or removing component connections. | |||
</span> | |||
<span style="background-color: #ffff99; display: inline-block; padding: 5px; border-radius: 5px;"> | |||
<strong>⚠️ Important Note:</strong><br> | |||
1. Connect only the Arduino Uno (without any component connections) to the computer via USB.<br> | |||
2. Upload the code to the Arduino.<br> | |||
3. Disconnect the Arduino from the computer and remove the USB cable.<br> | |||
4. Make all component connections using patch cords as per the circuit diagram.<br> | |||
5. Then connect the 9V adapter to provide external power supply to the Arduino. | |||
</span> | |||
=== Circuit Diagram (without LCD) === | |||
[[File:Eecit-kit-07-circuit.png|center|thumb|400px|Circuit Diagram]] | [[File:Eecit-kit-07-circuit.png|center|thumb|400px|Circuit Diagram]] | ||
=== Board Connections === | === Circuit Diagram (with LCD) === | ||
[[File:Stepper-circuit-diagram.png|center|thumb|400px|Circuit Diagram]] | |||
=== Board Connections (without LCD) === | |||
[[File:Eecit-kit-07-board-connection.png|center|thumb|400px|Board Connections]] | [[File:Eecit-kit-07-board-connection.png|center|thumb|400px|Board Connections]] | ||
== Connections == | === Board Connections (with LCD) === | ||
[[File:Stepper-board-diagram.png|center|thumb|400px|Board Connections]] | |||
== Connections (without LCD) == | |||
{| class="wikitable" style="text-align:center;" | {| class="wikitable" style="text-align:center;" | ||
! Component !! Arduino Pin !! Description | ! Component !! Arduino Pin !! Description | ||
| Line 21: | Line 41: | ||
| Stepper Motor IN1 || D8 || Control input 1 | | Stepper Motor IN1 || D8 || Control input 1 | ||
|- | |- | ||
| Stepper Motor IN2 || | | Stepper Motor IN2 || D9 || Control input 2 | ||
|- | |- | ||
| Stepper Motor IN3 || | | Stepper Motor IN3 || D10 || Control input 3 | ||
|- | |- | ||
| Stepper Motor IN4 || D11 || Control input 4 | | Stepper Motor IN4 || D11 || Control input 4 | ||
| Line 46: | Line 66: | ||
// Create a Stepper object named myStepper | // Create a Stepper object named myStepper | ||
// Connected to digital pins 8, 10 | // Connected to digital pins 8, 9, 10, 11 (sequence matters!) | ||
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); | Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11); | ||
| Line 57: | Line 77: | ||
void loop() { | void loop() { | ||
// Rotate clockwise (CW) at increasing speeds | // Rotate clockwise (CW) at increasing speeds | ||
for (int spd = 5; spd <= | for (int spd = 5; spd <= 10; spd += 5) { | ||
Serial.print("Rotating CW at "); | Serial.print("Rotating CW at "); | ||
Serial.print(spd); | Serial.print(spd); | ||
| Line 68: | Line 88: | ||
// Rotate counterclockwise (CCW) at decreasing speeds | // Rotate counterclockwise (CCW) at decreasing speeds | ||
for (int spd = | for (int spd = 10; spd >= 5; spd -= 5) { | ||
Serial.print("Rotating CCW at "); | Serial.print("Rotating CCW at "); | ||
Serial.print(spd); | Serial.print(spd); | ||
| Line 78: | Line 98: | ||
} | } | ||
} | } | ||
</pre> | |||
== Connections (with LCD) == | |||
{| class="wikitable" style="text-align:center;" | |||
! Component !! Arduino Pin !! Description | |||
|- | |||
| Stepper Motor IN1 || D8 || Control input 1 | |||
|- | |||
| Stepper Motor IN2 || D9 || Control input 2 | |||
|- | |||
| Stepper Motor IN3 || D10 || Control input 3 | |||
|- | |||
| Stepper Motor IN4 || D11 || Control input 4 | |||
|- | |||
| ULN2003 VCC || 5V || Power supply for driver | |||
|- | |||
| ULN2003 GND || GND || Common ground | |||
|- | |||
| LCD VCC || 5V || Power supply for LCD | |||
|- | |||
| LCD GND || GND || Common Ground | |||
|- | |||
| LCD SDA || A4 || Serial Data | |||
|- | |||
| LCD SCL || A5 || Serial Clock | |||
|} | |||
=== Important === | |||
Before uploading your Arduino code, make sure you have installed the following libraries in the Arduino IDE: | |||
1. Wire.h – For I²C communication between Arduino and peripheral devices. | |||
2. LiquidCrystal_I2C.h – For controlling LCD displays using the I²C interface. | |||
<span style="background-color: #E7DDFF; display: inline-block; padding: 5px; border-radius: 5px; font-weight: bold;"> | |||
✅ Install via (ZIP Method): | |||
</span> | |||
<span style="background-color: #E7DDFF; display: inline-block; padding: 5px; border-radius: 5px; font-weight: bold;"> | |||
Sketch → Include Library → Add .ZIP Library → Select Downloaded .zip File → Open → Wait for “Library Installed” message | |||
</span> | |||
<span style="background-color: #FFECA1; display: inline-block; padding: 5px; border-radius: 5px; font-weight: bold;"> | |||
✅ Verify : | |||
</span> | |||
<span style="background-color: #FFECA1; display: inline-block; padding: 5px; border-radius: 5px; font-weight: bold;"> | |||
Sketch → Include Library → The included library name should be present | |||
</span> | |||
== Code == | |||
<pre> | |||
// © 2025, Cognifront. Member of The IoT Kits Family. | |||
// Experiment 7: Stepper Motor Control via Microcontroller | |||
// Stepper with I2C 16x2 LCD showing Speed and Direction | |||
#include <Wire.h> | |||
#include <Stepper.h> | |||
#include <LiquidCrystal_I2C.h> | |||
// ============================== | |||
// LCD I2C CONNECTIONS | |||
// ------------------------------ | |||
// LCD VCC → Arduino 5V | |||
// LCD GND → Arduino GND | |||
// LCD SDA → Arduino A4 | |||
// LCD SCL → Arduino A5 | |||
// ============================== | |||
// Initialize the LCD with I2C address 0x27 and size 16x2 | |||
LiquidCrystal_I2C lcd(0x27, 16, 2); | |||
// ============================== | |||
// STEPPER MOTOR CONNECTIONS (via ULN2003 driver board) | |||
// ------------------------------ | |||
// IN1 → Arduino D8 | |||
// IN2 → Arduino D9 | |||
// IN3 → Arduino D10 | |||
// IN4 → Arduino D11 | |||
// ============================== | |||
// Define motor pins using #define | |||
#define IN1 8 | |||
#define IN2 9 | |||
#define IN3 10 | |||
#define IN4 11 | |||
// Define number of steps per revolution for 28BYJ-48 stepper | |||
const int stepsPerRevolution = 2048; | |||
// Create Stepper object with defined pins (same sequence) | |||
Stepper myStepper(stepsPerRevolution, IN1, IN2, IN3, IN4); | |||
void setup() { | |||
Serial.begin(9600); | |||
// Initialize LCD | |||
lcd.init(); // Initialize the LCD | |||
lcd.backlight(); // Turn on backlight | |||
lcd.clear(); | |||
lcd.setCursor(0, 0); | |||
lcd.print("Stepper Test"); | |||
delay(1000); | |||
lcd.clear(); | |||
Serial.println("Stepper Motor Speed Test with I2C LCD"); | |||
} | |||
void loop() { | |||
// Rotate Counterclockwise (CCW) at 5 and 10 RPM | |||
for (int spd = 5; spd <= 10; spd += 5) { | |||
myStepper.setSpeed(spd); | |||
Serial.print("Rotating CCW at "); | |||
Serial.print(spd); | |||
Serial.println(" RPM"); | |||
lcd.clear(); | |||
lcd.setCursor(0, 0); | |||
lcd.print("Direction: CCW"); | |||
lcd.setCursor(0, 1); | |||
lcd.print("Speed: "); | |||
lcd.print(spd); | |||
lcd.print(" RPM"); | |||
myStepper.step(stepsPerRevolution / 2); // Half revolution CCW | |||
delay(500); | |||
} | |||
// Rotate Clockwise (CW) at 10 and 5 RPM | |||
for (int spd = 10; spd >= 5; spd -= 5) { | |||
myStepper.setSpeed(spd); | |||
Serial.print("Rotating CW at "); | |||
Serial.print(spd); | |||
Serial.println(" RPM"); | |||
lcd.clear(); | |||
lcd.setCursor(0, 0); | |||
lcd.print("Direction: CW"); | |||
lcd.setCursor(0, 1); | |||
lcd.print("Speed: "); | |||
lcd.print(spd); | |||
lcd.print(" RPM"); | |||
myStepper.step(-stepsPerRevolution / 2); // Half revolution CW | |||
delay(500); | |||
} | |||
} | |||
</pre> | </pre> | ||
| Line 87: | Line 258: | ||
== Conclusion == | == Conclusion == | ||
The experiment successfully demonstrates control of a stepper motor’s speed and direction using Arduino and a ULN2003 driver. This forms the basis for applications like robotic motion control, CNC systems, and precise positioning mechanisms. | The experiment successfully demonstrates control of a stepper motor’s speed and direction using Arduino and a ULN2003 driver. This forms the basis for applications like robotic motion control, CNC systems, and precise positioning mechanisms. | ||
=== Real Life Example === | |||
CNC Machine Feed Drive: Stepper motors provide precise and repeatable control of tool or table movement along X, Y, and Z axes, ensuring accurate machining operations in computer-controlled manufacturing. | |||
Latest revision as of 05:01, 10 October 2025
Experiment 7: Stepper Motor Control via Microcontroller
Objective
To control the rotation and direction of a stepper motor using Arduino and a ULN2003 driver.
Task
- Connect stepper motor and driver to Arduino.
- Write code to rotate motor clockwise and counter-clockwise.
- Vary delay to change motor speed.
⚠️ Disconnect your Arduino from the computer while making or removing component connections.
⚠️ Important Note:
1. Connect only the Arduino Uno (without any component connections) to the computer via USB.
2. Upload the code to the Arduino.
3. Disconnect the Arduino from the computer and remove the USB cable.
4. Make all component connections using patch cords as per the circuit diagram.
5. Then connect the 9V adapter to provide external power supply to the Arduino.
Circuit Diagram (without LCD)

Circuit Diagram (with LCD)

Board Connections (without LCD)

Board Connections (with LCD)

Connections (without LCD)
| Component | Arduino Pin | Description |
|---|---|---|
| Stepper Motor IN1 | D8 | Control input 1 |
| Stepper Motor IN2 | D9 | Control input 2 |
| Stepper Motor IN3 | D10 | Control input 3 |
| Stepper Motor IN4 | D11 | Control input 4 |
| ULN2003 VCC | 5V | Power supply for driver |
| ULN2003 GND | GND | Common ground |
Code
// © 2025, Cognifront. Member of The IoT Kits Family.
//Experiment 7: Stepper Motor Control via Microcontroller
// Include the Stepper motor library
#include <Stepper.h>
// Define the number of steps per revolution for the stepper motor
// For a 28BYJ-48 stepper, it's typically 2048 steps per revolution (with gear reduction)
const int stepsPerRevolution = 2048;
// Create a Stepper object named myStepper
// Connected to digital pins 8, 9, 10, 11 (sequence matters!)
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// Start serial communication for debugging and monitoring
Serial.begin(9600);
Serial.println("Stepper Motor Speed Test"); // Initial message
}
void loop() {
// Rotate clockwise (CW) at increasing speeds
for (int spd = 5; spd <= 10; spd += 5) {
Serial.print("Rotating CW at ");
Serial.print(spd);
Serial.println(" RPM");
myStepper.setSpeed(spd); // Set stepper speed in RPM
myStepper.step(stepsPerRevolution / 2); // Rotate 1/2 revolution in CW direction
delay(500); // Short pause between speed changes
}
// Rotate counterclockwise (CCW) at decreasing speeds
for (int spd = 10; spd >= 5; spd -= 5) {
Serial.print("Rotating CCW at ");
Serial.print(spd);
Serial.println(" RPM");
myStepper.setSpeed(spd); // Set stepper speed in RPM
myStepper.step(-stepsPerRevolution / 2); // Rotate 1/2 revolution in CCW direction
delay(500); // Short pause between speed changes
}
}
Connections (with LCD)
| Component | Arduino Pin | Description |
|---|---|---|
| Stepper Motor IN1 | D8 | Control input 1 |
| Stepper Motor IN2 | D9 | Control input 2 |
| Stepper Motor IN3 | D10 | Control input 3 |
| Stepper Motor IN4 | D11 | Control input 4 |
| ULN2003 VCC | 5V | Power supply for driver |
| ULN2003 GND | GND | Common ground |
| LCD VCC | 5V | Power supply for LCD |
| LCD GND | GND | Common Ground |
| LCD SDA | A4 | Serial Data |
| LCD SCL | A5 | Serial Clock |
Important
Before uploading your Arduino code, make sure you have installed the following libraries in the Arduino IDE: 1. Wire.h – For I²C communication between Arduino and peripheral devices. 2. LiquidCrystal_I2C.h – For controlling LCD displays using the I²C interface.
✅ Install via (ZIP Method):
Sketch → Include Library → Add .ZIP Library → Select Downloaded .zip File → Open → Wait for “Library Installed” message
✅ Verify :
Sketch → Include Library → The included library name should be present
Code
// © 2025, Cognifront. Member of The IoT Kits Family.
// Experiment 7: Stepper Motor Control via Microcontroller
// Stepper with I2C 16x2 LCD showing Speed and Direction
#include <Wire.h>
#include <Stepper.h>
#include <LiquidCrystal_I2C.h>
// ==============================
// LCD I2C CONNECTIONS
// ------------------------------
// LCD VCC → Arduino 5V
// LCD GND → Arduino GND
// LCD SDA → Arduino A4
// LCD SCL → Arduino A5
// ==============================
// Initialize the LCD with I2C address 0x27 and size 16x2
LiquidCrystal_I2C lcd(0x27, 16, 2);
// ==============================
// STEPPER MOTOR CONNECTIONS (via ULN2003 driver board)
// ------------------------------
// IN1 → Arduino D8
// IN2 → Arduino D9
// IN3 → Arduino D10
// IN4 → Arduino D11
// ==============================
// Define motor pins using #define
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
// Define number of steps per revolution for 28BYJ-48 stepper
const int stepsPerRevolution = 2048;
// Create Stepper object with defined pins (same sequence)
Stepper myStepper(stepsPerRevolution, IN1, IN2, IN3, IN4);
void setup() {
Serial.begin(9600);
// Initialize LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on backlight
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Stepper Test");
delay(1000);
lcd.clear();
Serial.println("Stepper Motor Speed Test with I2C LCD");
}
void loop() {
// Rotate Counterclockwise (CCW) at 5 and 10 RPM
for (int spd = 5; spd <= 10; spd += 5) {
myStepper.setSpeed(spd);
Serial.print("Rotating CCW at ");
Serial.print(spd);
Serial.println(" RPM");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction: CCW");
lcd.setCursor(0, 1);
lcd.print("Speed: ");
lcd.print(spd);
lcd.print(" RPM");
myStepper.step(stepsPerRevolution / 2); // Half revolution CCW
delay(500);
}
// Rotate Clockwise (CW) at 10 and 5 RPM
for (int spd = 10; spd >= 5; spd -= 5) {
myStepper.setSpeed(spd);
Serial.print("Rotating CW at ");
Serial.print(spd);
Serial.println(" RPM");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Direction: CW");
lcd.setCursor(0, 1);
lcd.print("Speed: ");
lcd.print(spd);
lcd.print(" RPM");
myStepper.step(-stepsPerRevolution / 2); // Half revolution CW
delay(500);
}
}
Observation
- The stepper motor rotates in both clockwise and counterclockwise directions.
- The motor speed changes as per the programmed RPM values.
- Smooth operation is observed with correct wiring sequence.
Conclusion
The experiment successfully demonstrates control of a stepper motor’s speed and direction using Arduino and a ULN2003 driver. This forms the basis for applications like robotic motion control, CNC systems, and precise positioning mechanisms.
Real Life Example
CNC Machine Feed Drive: Stepper motors provide precise and repeatable control of tool or table movement along X, Y, and Z axes, ensuring accurate machining operations in computer-controlled manufacturing.