• Worldwide Shipping

  • 24/7 Customer Support

  • FAQ
  • Worldwide Shipping

  • 24/7 Customer Support

  • FAQ

How to use: Stepper motor and ULN2003 driver

Are you a starter with Croduino? Or a newbie when it comes to electronics? You fancy a specific module, but you don't know how to use it? Don't worry, we have our How To Use series!

How to Use is a series of blog tutorials by e-radionica where you will find everything you need to begin working with your favorite modules. Tutorials include technical characteristics, operating principles, instructions on how to connect the module with Croduino and basic coding. Everything else is up to you and your imagination.

Basic characteristics

Let's face it, motors can be found in various devices that we use every day. Printers, laptops, washing machines, electric shavers, quadcopters and many more. Still, there's a really small human percentage who know how to work with them. Best and simplest way to learn something is when we actually have a real example. That's why we love Croduino and "How to use" tutorials.

DSC_4679
28BYJ-48 stepper motor characteristics:
Voltage: 5V
Angle of a turn: 5.625°
Spin ratio: 64
Frequency: 100HZ
For more info, check out the datasheet

What and how does a stepper motor work?

The working principle of the stepper motor will be discussed in the example of the 28-BYJ48 unipolar stepper motor.

858-01BYJ-48 stepper motor from inside.
Stepper motor rotates the moving part with precise steps at certain time intervals. The pulse is sent to the coil series (our motor has 4 coils) that make the ring around the rotor, but those are static so that part of the motor we call the stator. Engine precision is expressed by the number of steps the engine is doing to make a full circle, 360 °. We can calculate it from the formula: a number of steps = (360 ° / degree of a single spin) * spin ratio. The example of our stepper is: (360 ° / 5.625 °) * 64 = 4096 steps.

StepperMotorimage source wikipedia.
The working principle can be seen at .gif above. Each stepper motor works in such a way that the coil series in the stator "turn on" alternately in the direction in which we want to rotate the motor, or they are not changing their condition if we want the engine to idle.

How to connect module with Croduino?

We will connect the stepper motor to the ULN driver, which we'll connect to Croduino. The picture shows the parts of the ULN2003 module. For more info, check out the datasheet.

DSC_8619
Stepper motor, ULN2003 breakout boar and Croduino we will connect as shown below.

28BYJ-48_bb

Code

/*
* BYJ48 Koracni motor s ULN breakout boardom
* Spojite :
* IN1 - D8
* IN2 - D9
* IN3 - D10
* IN4 - D11
* VCC - 5V
* Gnd - gnd
*
* e-radionica.com
*/
#define IN1 8
#define IN2 9
#define IN3 10
#define IN4 11
int koraci = 0;
boolean smjer = true;
unsigned long vrijemeZadnje = 0; // biljeziti cemo vrijeme
unsigned long vrijemeTrenutno = 0; // koje je potrebno da se
unsigned long vrijeme = 0; // se izvrsi zadani kut
int koraciPreostalo = 4095; //ovaj koracni ukupno ima 4095 koraka
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}
void loop()
{
while(koraciPreostalo > 0) // dokle je preostalo koraka do punog okreta
{
vrijemeTrenutno = micros(); //zapisi trenutno vrijeme u micro sek
if(vrijemeTrenutno - vrijemeZadnje >= 1000)
{
stepper(1); //pozovi funkciju stepper (vidi dolje void stepper)
vrijeme = vrijeme + micros() - vrijemeZadnje;
vrijemeZadnje = micros();
koraciPreostalo--;
}
}
Serial.println(vrijeme);
Serial.println("Pricekaj..!");
delay(2000);
smjer = !smjer; // obrni smjer
koraciPreostalo = 4095; // resetiraj broj koraka
}
void stepper(int brojKoraka)
{
for (int x=0; x < brojKoraka; x++) { switch(koraci) { case 0: digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); break; case 1: digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, HIGH); break; case 2: digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); break; case 3: digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, HIGH); digitalWrite(IN4, LOW); break; case 4: digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); break; case 5: digitalWrite(IN1, HIGH); digitalWrite(IN2, HIGH); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); break; case 6: digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); break; case 7: digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, HIGH); break; default: digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); digitalWrite(IN3, LOW); digitalWrite(IN4, LOW); break; } postaviSmjer(); } } void postaviSmjer() { if(smjer==1){ koraci++; } if(smjer==0){ koraci--; } if(koraci>7){ koraci=0; }
if(koraci<0){ koraci=7; }
}
Leave a Reply