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:
28BYJ-48:
Here is the reference table from the previous tutorial:
Official WeMos D1 Board Label | Knockoff WeMos D1 Board Label | Digital Port | Arduino Software |
RX | RX<-d0< td=""> | D0 | 3 |
TX | TX->D0 | D1 | 1 |
D0 | D2 | D2 | 16 |
SCL/D1 | D15/SCL/D3 | D3 | 5 |
SDA/D2 | D14/SDA/D4 | D4 | 4 |
D3 | D13/SCK/D5 | D5 | 0 |
D4 | D12/MISO/D6 | D6 | 2 |
D5 | D11/MOSI/D7 | D7 | 14 |
D6 | D8 | D8 | 12 |
D7 | TX1/D9 | D9 | 13 |
SS/D8 | D10/SS | D10 | 14 |
MOSI/D7 | D11/MOSI | D11 | 13 |
MISO/D6 | D12/MISO | D12 | 12 |
SCK/D5 | D13/SCK | D13 | 14 |
SDA/D2 | D14/SDA | D14 | 4 |
SCL/D1 | D15/SCL | D15 | 5 |
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 |
D2 | ENA |
D15/SCL/D3 | IN1 |
D14/SDA/D4 | IN2 |
D13/SCK/D5 | IN3 |
D12/MISO/D6 | IN4 |
D11/MOSI/D7 | ENB |
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 Controller | 28BYJ-48 Stepper Motor |
OUT1 | 1. Blue |
OUT2 | 3. Yellow |
OUT3 | 4. Pink |
OUT4 | 5. Orange |
Motor Controller to Power
We need to supply the motor controller with power. You can use any DC (7 - 35V).
- 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.
- Plug the power supply's hot end into +12V on the motor controller.
- 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.
Comments