In the previous tutorial, we set up our WeMos D1 board.

Now, we are going to use it to control a stepper motor. Specifically, the L298N Dual H Bridge DC Stepper Motor Controller and the 28BYJ-48 5V DC Stepper Motor.

L298N:

Image courtesy of: http://www.instructables.com/id/Arduino-Modules-L298N-Dual-H-Bridge-Motor-Controll/?ALLSTEPS 

28BYJ-48:

Image courtesy of: https://www.aimagin.com


Here is the reference table from the previous tutorial:

Official WeMos D1 Board Label Knockoff WeMos D1 Board Label Digital Port Arduino Software
RXRX<-d0< td="">D03
TXTX->D0D11
D0D2D216
SCL/D1D15/SCL/D3D35
SDA/D2D14/SDA/D4D44
D3D13/SCK/D5D50
D4D12/MISO/D6D62
D5D11/MOSI/D7D714
D6D8D812
D7TX1/D9D913
SS/D8D10/SSD1014
MOSI/D7D11/MOSID1113
MISO/D6D12/MISOD1212
SCK/D5D13/SCKD1314
SDA/D2D14/SDAD144
SCL/D1D15/SCLD155

Wiring

Motor Controller to Board

Let's start by connecting the motor controller (L298N) to the board (WeMos D1).

Using pliers, pull the caps off ENA and ENB (I have no idea what they're for):

Using male to female DuPont jumper wires, make the following connections:

WeMos D1 Board L298N Motor Controller
D2ENA
D15/SCL/D3IN1
D14/SDA/D4IN2
D13/SCK/D5IN3
D12/MISO/D6IN4
D11/MOSI/D7ENB

Motor Controller to Motor

Here's a diagram of the 28BYJ-48 stepper motor:

If you compare this diagram to the actual motor, the colours of the wires connected to the motor correspond. Remember this when connecting the motor.

Using male to male DuPont jumper wires, make the following connections:

L298N Motor Controller28BYJ-48 Stepper Motor
OUT11. Blue
OUT23. Yellow
OUT34. Pink
OUT45. Orange

Motor Controller to Power

We need to supply the motor controller with power. You can use any DC (7 - 35V).

  1. If your power supply doesn't have two wires coming from it (hot and ground), you'll have to cut it and strip the two ends.
  2. Plug the power supply's hot end into +12V on the motor controller.
  3. I recommend a breadboard for this part. Connect the GND on the motor controller (with a male to male jumper), the motor's red wire, one of the GND's on the board, and the power supply's ground together. This is called tying the grounds together.

Code

If you read about bipolar and unipolar stepper motors, you will understand the following code. Basically, the magnets in the motor must turn on in a sequence to rotate the motor's head.

Remember the pins may look random, but correspond to the digital I/O ports as seen in the table at the top of this tutorial.

/*
 * Driving a 5V stepper motor using Keyes L298N Dual Motor Driver;
 * Chienline @2015;
 */
const int ENA = 7;
const int IN1 = 6;
const int IN2 = 5;
const int ENB = 8;
const int IN4 = 9;
const int IN3 = 10;
const int ledPin = 13;
void setup()
{
  pinMode(ENA,OUTPUT);
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(ENB,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  pinMode(ledPin,OUTPUT);
  digitalWrite(ledPin, LOW);
  //delay is used to control the speed, the lower the faster.
  //reverse(step,delay);
  reverse(80,20);
  //forward(step,delay);
  forward(80,20);
}
void loop()
{
}
void reverse(int i, int j) {
  // set both motors ON
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  while (1)   {
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(j);
    i--;
    if (i < 1) break; 
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(j);  
    i--;
    if (i < 1) break;
    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(j);
    i--;
    if (i < 1) break;
    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(j);  
    i--;
    if (i < 1) break;
  }
  // set both motors OFF
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
}  // end reverse()
void forward(int i, int j) {
  // Set both motors ON
  digitalWrite(ENA, HIGH);
  digitalWrite(ENB, HIGH);
  while (1)   {
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(j);  
    i--;
    if (i < 1) break;
    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 0);
    digitalWrite(IN4, 1);
    delay(j);
    i--;
    if (i < 1) break;
    digitalWrite(IN1, 1);
    digitalWrite(IN2, 0);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(j);  
    i--;
    if (i < 1) break;
    digitalWrite(IN1, 0);
    digitalWrite(IN2, 1);
    digitalWrite(IN3, 1);
    digitalWrite(IN4, 0);
    delay(j);
    i--;
    if (i < 1) break;
  }
  // set both motors OFF
  digitalWrite(ENA, LOW);
  digitalWrite(ENB, LOW);
}  // end forward()

Code courtesy of: http://www.instructables.com/id/Driving-Bi-Polar-Stepper-Motor-with-Keyes-L298N/?ALLSTEPS

Final Words

Once again, I will not be talking about the WeMos D1/ ESP8266's WIFI since there are many other tutorials out there for that.

I now have to decide what I'm going to do with this WIFI-capable stepper motor. I was originally going to make some blinds, but I don't have enough blinds to make it worth it.

Hopefully, you've enjoyed this tutorial, if you have any questions or comments, please get in touch with me.