|
arduino IDEArduino
|
Diy ULN2003 Motor Driver
ULN2003 driver board
The 28BYJ-48 is one of the cheapest stepper motors you can find. Although it is not super accurate or powerful, it is a great motor to use for smaller projects or if you just want to learn about stepper motors.
This motor is often used to automatically adjust the vanes of an air conditioner unit. It has a built-in gearbox, which gives it some extra torque and reduces the speed drastically.
Below you can find the specifications for both the stepper motor and driver that are used in this tutorial.
Important note: Manufacturers usually specify that the motors have a 64:1 gear reduction. Some members of the Arduino Forums noticed that this wasn’t correct and so they took some motors apart to check the actual gear ratio. They determined that the exact gear ratio is in fact 63.68395:1, which results in approximately 4076 steps per full revolution (in half step mode).
I am not sure if all manufacturers use the exact same gearbox, but you can just adjust the steps per revolution in the code, to match your model.
The Adafruit Industries Small Reduction Stepper Motor uses the same form factor as the 28BYJ-48, but does have a different gear ratio. It has a roughly 1/16 reduction gear set, which results in 513 steps per revolution (in full-step mode). You can download the datasheet for it
Basic Arduino example code to control a 28BYJ-48 stepper motor
You can upload the following example code to your Arduino using the Arduino IDE.
This example uses the Stepper.h library, which should come pre-installed with the Arduino IDE. This sketch turns the stepper motor 1 revolution in one direction, pauses, and then turns 1 revolution in the other direction.
// Include the Arduino Stepper.h library:
#include <Stepper.h>
// Define number of steps per rotation:
const int stepsPerRevolution = 2048;
// Wiring:
// Pin 8 to IN1 on the ULN2003 driver
// Pin 9 to IN2 on the ULN2003 driver
// Pin 10 to IN3 on the ULN2003 driver
// Pin 11 to IN4 on the ULN2003 driver
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
void setup() {
// Set the speed to 5 rpm:
myStepper.setSpeed(5);
// Begin Serial communication at a baud rate of 9600:
Serial.begin(9600);
}
void loop() {
// Step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// Step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
Code explanation:
The sketch starts by including the Stepper.h Arduino library. More information about this library can be found on the Arduino website.
// Include the Arduino Stepper.h library:
#include <Stepper.h>
Next, I defined how many steps the motor takes to rotate 1 revolution. In this example we will be using the motor in full-step mode. This means it takes 2048 steps to rotate 360 degrees (see motor specifications above).
// Define number of steps per rotation:
const int stepsPerRevolution = 2048;
Next, you need to create a new instance of the Stepper class, which represents a particular stepper motor connected to the Arduino. For this we use the function
Stepper(steps, pin1, pin2, pin3, pin4)
where steps is the number of steps per revolution and pin1 through pin4 are the pins to which the motor is connected. To get the correct step sequence, we need to set the pins in the following order: 8, 10, 9, 11.
// Create stepper object called 'myStepper', note the pin order:
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);
In this case I called the stepper motor ‘myStepper’ but you can use other names as well, like ‘z_motor’ or ‘liftmotor’ etc.
Stepper liftmotor = Stepper(stepsPerRevolution, 8, 10, 9, 11);
. You can create multiple stepper motor objects with different names and pins. This allows you to easily control 2 or more stepper motors at the same time.
In the setup, you can set the speed in rpm with the function
setSpeed(rpm)
. The maximum speed for a 28byj-48 stepper motor is roughly 10-15 rpm at 5 V.
void setup() {
// Set the speed to 5 rpm:
myStepper.setSpeed(5);
// Begin Serial communication at a baud rate of 9600:
Serial.begin(9600);
}
In the loop section of code, we simply call the
step(steps)
function which turns the motor a specific number of steps at a speed determined by the
setSpeed(rpm)
function. Passing a negative number to this function reverses the spinning direction of the motor.
void loop() {
// Step one revolution in one direction:
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
delay(500);
// Step one revolution in the other direction:
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
delay(500);
}
Example codes for 28BYJ-48 stepper motor with Arduino and AccelStepper library
In the following three examples I will show you how you can control both the speed, the direction and the number of steps the stepper motor should take. In these examples I will be using the AccelStepper library.
The AccelStepper library written by Mike McCauley is an awesome library to use for your project. One of the advantages is that it supports acceleration and deceleration, but it has a lot of other nice functions too.
You can download the latest version of this library AccelStepper-1.59.zip
You can install the library by going to Sketch > Include Library > Add .ZIP Library… in the Arduino IDE.
Another option is to navigate to Tools > Manage Libraries… or type Ctrl + Shift + I on Windows. The Library Manager will open and update the list of installed libraries.
You can search for ‘accelstepper‘ and look for the library by Mike McCauley. Select the latest version and then click Install.
1. Continuous rotation example code
The following sketch can be used to run one or more stepper motors continuously at a constant speed. (No acceleration or deceleration is used).
You can copy the code by clicking on the button in the top right corner of the code field.
/* Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO: continuous rotation. More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include <AccelStepper.h>
// Motor pin definitions:
#define motorPin1 8 // IN1 on the ULN2003 driver
#define motorPin2 9 // IN2 on the ULN2003 driver
#define motorPin3 10 // IN3 on the ULN2003 driver
#define motorPin4 11 // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
// Set the maximum steps per second:
stepper.setMaxSpeed(1000);
}
void loop() {
// Set the speed of the motor in steps per second:
stepper.setSpeed(500);
// Step the motor with constant speed as set by setSpeed():
stepper.runSpeed();
}
How the code works:
Again the first step is to include the library with
#include <AccelStepper.h>
.
// Include the AccelStepper library:
#include <AccelStepper.h>
The next step is to define the ULN2003 to Arduino connections.
The statement
#define
is used to give a name to a constant value. The compiler will replace any references to this constant with the defined value when the program is compiled. So everywhere you mention
motorPin1
, the compiler will replace it with the value 8 when the program is compiled.
// Motor pin definitions:
#define motorPin1 8 // IN1 on the ULN2003 driver
#define motorPin2 9 // IN2 on the ULN2003 driver
#define motorPin3 10 // IN3 on the ULN2003 driver
#define motorPin4 11 // IN4 on the ULN2003 driver
The next step is to specify the motor interface type for the AccelStepper library. In this case we will be driving a 4 wire stepper motor in half step mode, so we set the interface type to ‘8’. You can find the other interface types here. If you want to run the motor in full-step mode (fewer steps per revolution), just change the 8 to 4.
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
Next, you need to create a new instance of the AccelStepper class with the appropriate motor interface type and connections. To get the correct step sequence, we need to set the pins in the following order: motorPin1, motorPin3, motorPin2, motorPin4.
In this case I called the stepper motor ‘stepper’ but you can use other names as well, like ‘z_motor’ or ‘liftmotor’ etc.
AccelStepper liftmotor = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
. You can create multiple stepper motor objects with different names and pins. This allows you to easily control 2 or more stepper motors at the same time.
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
In the setup section of the code, we define the maximum speed in steps/second with the function
setMaxSpeed()
. Speeds of more than 1000 steps per second can be unreliable, so I set this as the maximum. Note that I specify the name of the stepper motor (‘stepper’), for which I want to define the maximum speed. If you have multiple stepper motors connected, you can specify a different speed for each motor:
stepper2.setMaxSpeed(500);
.
void setup() {
// Set the maximum steps per second:
stepper.setMaxSpeed(1000);
}
In the loop, we first set the speed that we want the motor to run at with the function
setSpeed()
. (you can also place this in the setup section of the code).
stepper.runSpeed()
polls the motor and when a step is due it executes 1 step. This depends on the set speed and the time since the last step. If you want to change the direction of the motor, you can set a negative speed:
stepper.setSpeed(-400);
turns the motor the other way.
void loop() {
// Set the speed of the motor in steps per second:
stepper.setSpeed(500);
// Step the motor with constant speed as set by setSpeed():
stepper.runSpeed();
}
In half step mode, one revolution takes 4096 steps, so 500 steps/sec results in roughly 7 rpm.
2. Sketch to control number of steps or revolutions
With the following sketch you can control both the speed, direction and the number of steps/revolutions.
In this case, the stepper motor turns 1 revolution clockwise with 500 steps/sec, then turns 1 revolution counterclockwise at 1000 steps/sec, and lastly turns 2 revolutions clockwise at 1000 steps/sec.
/* Example sketch to control a 28BYJ-48 stepper motor with ULN2003 driver board, AccelStepper and Arduino UNO: number of steps/revolutions. More info: https://www.makerguides.com */
// Include the AccelStepper library:
#include <AccelStepper.h>
// Motor pin definitions:
#define motorPin1 8 // IN1 on the ULN2003 driver
#define motorPin2 9 // IN2 on the ULN2003 driver
#define motorPin3 10 // IN3 on the ULN2003 driver
#define motorPin4 11 // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper = AccelStepper(MotorInterfaceType, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
// Set the maximum steps per second:
stepper.setMaxSpeed(1000);
}
void loop() {
// Set the current position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 500 steps/second until the motor reaches 4096 steps (1 revolution):
while (stepper.currentPosition() != 4096) {
stepper.setSpeed(500);
stepper.runSpeed();
}
delay(1000);
// Reset the position to 0:
stepper.setCurrentPosition(0);
// Run the motor backwards at 1000 steps/second until the motor reaches -4096 steps (1 revolution):
while (stepper.currentPosition() != -4096) {
stepper.setSpeed(-1000);
stepper.runSpeed();
}
delay(1000);
// Reset the position to 0:
stepper.setCurrentPosition(0);
// Run the motor forward at 1000 steps/second until the motor reaches 8192 steps (2 revolutions):
while (stepper.currentPosition() != 8192) {
stepper.setSpeed(1000);
stepper.runSpeed();
}
delay(3000);
}
Diy ULN2003 Motor Driver
*PCBWay community is a sharing platform. We are not responsible for any design issues and parameter issues (board thickness, surface finish, etc.) you choose.
- Comments(0)
- Likes(0)
- 0 USER VOTES
- YOUR VOTE 0.00 0.00
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
More by Sreeram.zeno
- Esp12-F Cluster V1.0 The ESP8266 is a low-cost Wi-Fi microchip, with built-in TCP/IP networking software, and microcontro...
- TB6612FNG Motor Driver The TB6612FNG Motor Driver can control up to two DC motors at a constant current of 1.2A (3.2A peak)...
- Sunny Buddy Solar Charger v1.0 This is the Sunny Buddy, a maximum power point tracking (MPPT) solar charger for single-cell LiPo ba...
- Diy 74HC4051 8 Channel Mux Breakout Pcb The 74HC4051; 74HCT4051 is a single-pole octal-throw analog switch (SP8T) suitable for use in analog...
- Diy RFM97CW Breakout Pcb IntroductionLoRa? (standing for Long Range) is a LPWAN technology, characterized by a long range ass...
- ProMicro-RP2040 Pcb The RP2040 is a 32-bit dual ARM Cortex-M0+ microcontroller integrated circuit by Raspberry Pi Founda...
- Serial Basic CH340G Pcb A USB adapter is a type of protocol converter that is used for converting USB data signals to and fr...
- Mp3 Shield For Arduino Hardware OverviewThe centerpiece of the MP3 Player Shield is a VS1053B Audio Codec IC. The VS1053B i...
- MRK CAN Shield Arduino The CAN-BUS Shield provides your Arduino or Redboard with CAN-BUS capabilities and allows you to hac...
- AVR ISP Programmer AVR is a family of microcontrollers developed since 1996 by Atmel, acquired by Microchip Technology ...
- Diy Arduino mega Pcb The Arduino Mega 2560 is a microcontroller board based on the ATmega2560. It has 54 digital input/ou...
- Max3232 Breakout Board MAX3232 IC is extensively used for serial communication in between Microcontroller and a computer fo...
- Line Follower Pcb The Line Follower Array is a long board consisting of eight IR sensors that have been configured to ...
- HMC6343 Accelerometer Module The HMC6343 is a solid-state compass module with tilt compensation from Honeywell. The HMC6343 has t...
- RTK2 GPS Module For Arduino USBThe USB C connector makes it easy to connect the ZED-F9P to u-center for configuration and quick ...
- Arduino Explora Pcb The Arduino Esplora is a microcontroller board derived from the Arduino Leonardo. The Esplora differ...
- Diy Stepper Motor Easy Driver A motor controller is a device or group of devices that can coordinate in a predetermined manner the...
- Diy Arduino Pro Mini The Arduino Pro Mini is a microcontroller board based on the ATmega168 . It has 14 digital input/out...
-
-
Helium IoT Network Sensor Development board | H2S-Dev V1.2
80 0 0 -
-
-
-
-
-
3D printed Enclosure Backplate for Riden RD60xx power supplies
175 1 1