# 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.

28BYJ-48 stepper motor characteristics:
Voltage: 5V
Angle of a turn: 5.625°
Spin ratio: 64
Frequency: 100HZ

## 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.

BYJ-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.

image 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.

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

## Code

```/*
*  BYJ48 Stepper motor with the ULN breakout board
*  Connect :
*  IN1 - D8
*  IN2 - D9
*  IN3 - D10
*  IN4 - D11
*  VCC - 5V
*  Gnd - gnd
*
*/

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11

int koraci = 0;
boolean smjer = true;
unsigned long vrijemeZadnje = 0;    // we will note the time
unsigned long vrijemeTrenutno = 0;  // needed to
unsigned long vrijeme = 0;          // execute the given angle
int koraciPreostalo = 4095; //this stepper has 4095 steps in total

void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
}

void loop()
{
while(koraciPreostalo > 0) // steps remaining until the full circle
{
vrijemeTrenutno = micros(); //write down the current time in microseconds
{
stepper(1); //call the stepper function (check void stepper below)
vrijeme = vrijeme + micros() - vrijemeZadnje;
koraciPreostalo--;
}
}

Serial.println(vrijeme);
Serial.println("Wait..!");
delay(2000);
smjer = !smjer; // opposite direction
koraciPreostalo = 4095;  // reset the number of steps
}

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; }
}```