# Using L293D Motor Driver Shield without any library

**URL:** <https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [December 4, 2020, 11:32am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601 "2020-12-04T11:32:23Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 4, 2020, 11:32am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/1 "2020-12-04T11:32:23Z")

</div>

Dear Arduino Forum members,

I have newly started learning Arduino for making some personal projects.  
I have some computer programming background.  
First, I decided to learn controlling motors using Arduino  
as I have two bipolar motors from **HP Scanjet 3800s**.  
After searching about these motors I have figured out that  
they are **Epoch T3508** and their data sheet exists [here](http://www.epochelectronics.com/pdf/T3508_e.PDF).  
In the internet, I have heard about **L293D Motor Driver Shield** and purchased one  
thinking that I can drive my Epoch T3508 motors with them.

 ![](https://europe1.discourse-cdn.com/arduino/original/4X/8/c/e/8ce1409b9cbf52aae99033291f5aa08275603f5e.jpeg)

I have coupled this with my Arduino Uno,  
and connected a 6V battery to my L293D Shield (and removed the power jumper).  
I also made proper connections for my **Epoch1** motor to M1+M2 slots of the L293D Shield.  
I can run these motors with **Stepper Test** example supplied by **Adafruit Motor shield library**.  
However, I would like to learn how to give a single step to my motor without using the library.  
Which pins should I use to give output for M1+M2 (also if possible M3+M4)?  
How can I make a full turn for my Epoch bipolar motor?

I believe this will be a good lesson for me to start with.

Best regards.  
bkarpuz

---

<div class="post-metadata">

**Author:** ![johndg](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@johndg](https://forum.arduino.cc/u/johndg)\
**Post date:** [December 4, 2020, 12:37pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/2 "2020-12-04T12:37:36Z")

</div>

If you go to the Adafruit [downloads page for the motor shield,](https://learn.adafruit.com/adafruit-motor-shield/downloads) you can get another link to access the schematic for the shield from github.  
You should be able to find all the information there (altho' I confess I haven't looked).

---

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 5, 2020, 5:58am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/3 "2020-12-05T05:58:28Z")

</div>

**johndg** , thank you ver much for giving me a direction. I am not an electronics guy but as far as I figured out from the [schematics](https://github.com/adafruit/Adafruit_Motor-Shield-v1/blob/master/mshieldv12schem.png) of the shield Driver #1 has to inputs from Arduino called as PWM2A and PWM2B, which are the outputs D11 and D3, respectively.  
For Driver #2, I see that they are PWM0A and PWM0B, i.e., D6 and D5, respectively.  
Do you think activating these pins will work for me? Do I need to activate other pins such as DIR\_EN?  
Please teach me which code files in the library I should read to figure this out.

Thank you.  
bkarpuz

---

<div class="post-metadata">

**Author:** ![johndg](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@johndg](https://forum.arduino.cc/u/johndg)\
**Post date:** [December 5, 2020, 11:17am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/4 "2020-12-05T11:17:32Z")

</div>

This is quite a complex project, if you are determined to do your own thing, without a library. The IC is quite straightforward, but working out how the shield works is another thing!  
You should learn all you can about the L293D, from the datasheet ([which you'll find here](https://www.st.com/en/motor-drivers/l293d.html)) and resources like[Adafruit themselves](https://learn.adafruit.com/adafruit-arduino-lesson-15-dc-motor-reversing/lm293d), and [this application note](https://www.st.com/resource/en/application_note/cd00003776-applications-of-monolithic-bridge-drivers-stmicroelectronics.pdf). I don't like the schematic - I find it very hard to follow.  
You can also look at the Adafruit library. The way to find it is  
(top of page) DOCUMENTATION-\>REFERENCE-\>(left of page) LIBRARIES-\>Device Control-\>Adafruit Motor Shield Library-\>Read the documentation. This will take you to github. The library code is in AFMotor.cpp. It's quite complicated!  
Good luck.

---

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 5, 2020, 11:51am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/5 "2020-12-05T11:51:51Z")

</div>

**johndg** , thank you once again. I extracted one of my **L293D** from the L293D motor driver shield, and could run my 4 wire stepper forward and backward after placing the L293D on my breadboard, which I believe is a good step for this study. I had lots of cables on the breadboard to have it run. I believe the shield itself has those connections on its circuit. I just need to figure out how the **74HCT595N** operates here... So that I can score my goal.

**Circuit scheme** :  
As I have used (4 wire) bipolar stepper motor in my project,  
I have connected two joint wires of the bipolar motor to slots of Motor A,  
and the other two joint ones to slots of Motor B.  
 ![](https://europe1.discourse-cdn.com/arduino/original/4X/1/e/4/1e4151c137f29215e78347b85ae00693b44a34bb.jpeg)

**Code for running BP Motor forward** :

```cpp
const int motorPin1 = 5; // Pin 14 of L293D
const int motorPin2 = 6; // Pin 10 of L293D
const int motorPin3 = 10; // Pin 7 of L293D
const int motorPin4 = 9; // Pin 2 of L293D

void setup(){

    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(motorPin4, OUTPUT);

}

void loop(){

    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(5);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
    delay(5);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(5);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
    delay(5);

}

```

**Code for running BP Motor backward** :

```cpp
const int motorPin1 = 5; // Pin 14 of L293D
const int motorPin2 = 6; // Pin 10 of L293D
const int motorPin3 = 10; // Pin 7 of L293D
const int motorPin4 = 9; // Pin 2 of L293D

void setup(){

    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(motorPin4, OUTPUT);

}

void loop(){

    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
    delay(5);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(5);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
    delay(5);
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
    delay(5);

}

```

---

<div class="post-metadata">

**Author:** ![johndg](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@johndg](https://forum.arduino.cc/u/johndg)\
**Post date:** [December 5, 2020, 12:17pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/6 "2020-12-05T12:17:02Z")

</div>

> bkarpuz:  
> I just need to figure out how the 74HCT595N operates here... so that I can score my goal.

Well done, that's great progress! They use the shift register to reduce the number of outputs needed from the Arduino.  
[Here's a library](https://www.arduino.cc/reference/en/libraries/shift7seg/) which may help work out how to drive it.

---

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 6, 2020, 4:12pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/7 "2020-12-06T16:12:42Z")

</div>

I was so sure that the following code would work. But it did **not**!

I have placed L293D Motor Driver Shield over my Arduino Uno,  
and connected my bipolar motor to M1&M2 slots properly.

I would be very glad if one can show me a direction.

```cpp
 // Bit positions in the Arduino UNO micro controller output

//#define PWM2B 3
#define DIR_CLK 4 //SHCP
//#define PWM0B 5
//#define PWM0A 6
//#define DIR_EN 7 //OE
#define DIR_SER 8 //DS
//#define PWM1A 9
//#define PWM1B 10
//#define PWM2A 11
#define DIR_LATCH 12 //STCP

// Bit positions in the 74HCT595 shift register output

#define MOTOR3_A 15 //Q0
#define MOTOR2_A 1 //Q1
#define MOTOR1_A 2 //Q2
#define MOTOR1_B 3 //Q3
#define MOTOR2_B 4 //Q4
#define MOTOR4_A 5 //Q5
#define MOTOR3_B 6 //Q6
#define MOTOR4_B 7 //Q7

//Step BPMotor1 connected to M1&M2 by setting the following entries to HIGH, respectively: Q2, Q1, Q3, Q4
//Step BPMotor2 connected to M3&M4 by setting the following entries to HIGH, respectively: Q0, Q5, Q6, Q7

boolean QSeq[8] = {LOW, LOW, LOW, LOW, LOW, LOW, LOW, LOW};
int counter = 0;

void PostSeq() {
  digitalWrite(DIR_LATCH, LOW);

  for (int i=7; i>=0; i--)
  {
    digitalWrite(DIR_CLK, LOW);
    digitalWrite(DIR_SER, QSeq[i]);
    digitalWrite(DIR_CLK, HIGH);
  }
  //digitalWrite(DIR_CLK, LOW);
  digitalWrite(DIR_LATCH, HIGH);
}

void setup() {

  pinMode(DIR_SER, OUTPUT);
  pinMode(DIR_LATCH, OUTPUT);
  pinMode(DIR_CLK, OUTPUT);
  //pinMode(PWM2A, OUTPUT);
  //pinMode(PWM2B, OUTPUT);
  //pinMode(DIR_EN, OUTPUT);

}

void loop() {

  Serial.begin(9600);
  Serial.print(counter);
  switch (counter) {
    case 0:
      {
        QSeq[0]=LOW; //HIGH
        QSeq[1]=LOW;
        QSeq[2]=HIGH;
        QSeq[3]=LOW;
        QSeq[4]=LOW;
        QSeq[5]=LOW;
        QSeq[6]=LOW;
        QSeq[7]=LOW;
      }
      break;
    case 1:
      {
        QSeq[0]=LOW;
        QSeq[1]=HIGH;
        QSeq[2]=LOW;
        QSeq[3]=LOW;
        QSeq[4]=LOW;
        QSeq[5]=LOW; //HIGH
        QSeq[6]=LOW;
        QSeq[7]=LOW;
      }
      break;
    case 2:
      {
        QSeq[0]=LOW;
        QSeq[1]=LOW;
        QSeq[2]=LOW;
        QSeq[3]=HIGH;
        QSeq[4]=LOW;
        QSeq[5]=LOW;
        QSeq[6]=LOW; //HIGH
        QSeq[7]=LOW;
      }
      break;
    case 3:
      {
        QSeq[0]=LOW;
        QSeq[1]=LOW;
        QSeq[2]=LOW;
        QSeq[3]=LOW;
        QSeq[4]=HIGH;
        QSeq[5]=LOW;
        QSeq[6]=LOW;
        QSeq[7]=LOW; //HIGH
      }
      break;
  }
  PostSeq();
  delay(300);
  counter = (counter + 1) % 4;

}

```

---

<div class="post-metadata">

**Author:** ![johndg](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@johndg](https://forum.arduino.cc/u/johndg)\
**Post date:** [December 6, 2020, 6:22pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/8 "2020-12-06T18:22:01Z")

</div>

> bkarpuz:  
> As I have used bipolar stepper motor in my project,  
> I have connected two joint wires of the bipolar motor to slots of Motor A,  
> and the other two joint ones to slots of Motor B.
> 
> ![](https://europe1.discourse-cdn.com/arduino/original/4X/d/a/d/dad4b54248c4db85c5cf5c0b82d32dd9cafacdd3.png)

Think you need a re-think! Don't know why I didn't spot it before but the motors you are using a just DC motors, not stepper motors. You don't get 2-wire stepper motors, unipolar or bipolar (except maybe the tiny ones used in clocks, which are very low torque and designed to run at a single speed). Your 2-wire motors are just DC motors.  
One L293D can either drive 4 DC motors unidirectionally, two DC motors bidirectionally, or 1 stepper motor. See the application note I linked in #3.

---

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 6, 2020, 7:09pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/9 "2020-12-06T19:09:18Z")

</div>

**johndg** , I don't know how to draw this scheme on my Linux computer and I got it from somewhere in the internet. And I noted that I did a correct connection with my 4 wire Stepper Motor (wich I call as Bipolar Motor) to the slots of the DC Motors (Motor A and Motor B). There is no problem with my code and circuit setting as they work perfectly. I am working on the stepper motor ( **Epoch T3508** ) I have mentioned in my post #1. My problem appears in my last post, where the L293D Motor Diver Shield comes in the play.

When I will learn how to draw this scheme, I will update it in my post #3.

Thank you.  
bkarpuz

---

<div class="post-metadata">

**Author:** ![johndg](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@johndg](https://forum.arduino.cc/u/johndg)\
**Post date:** [December 6, 2020, 7:30pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/10 "2020-12-06T19:30:12Z")

</div>

Ah! I thought the circuit drawing was what you actually had used to wire the motor(s). I'll try to have a proper look at your code later. Did anything happen when you ran it?

---

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 7, 2020, 9:56am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/11 "2020-12-07T09:56:56Z")

</div>

**johndg** ,

I have updated the scheme in my post #4.

> johndg:  
> ... Did anything happen when you ran it?

About your question, nothing happened.

Thank you.  
**bkarpuz**

---

<div class="post-metadata">

**Author:** ![johndg](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@johndg](https://forum.arduino.cc/u/johndg)\
**Post date:** [December 7, 2020, 11:48pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/12 "2020-12-07T23:48:32Z")

</div>

Afraid I've been tied up on other things. You may glean something from the video below, at least it's something to look at for a while:

[74HC595 & 74HC165 Shift Registers with Arduino - YouTube](https://www.youtube.com/watch?v=Ys2fu4NINrA&t=1s).

---

<div class="post-metadata">

**Author:** ![bkarpuz](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bkarpuz](https://forum.arduino.cc/u/bkarpuz)\
**Post date:** [December 8, 2020, 7:56pm UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/13 "2020-12-08T19:56:30Z")

</div>

Finally, I could make it work, even for two stepper motors. After reading the _AFMotor.h_ file, I saw that I had to activate the pin DIR\_EN for the 74HC595, and the pins PWM0A, PWM0B (these 2 for BPM2=M3&M4), PWM2A and PWM2B (these 2 for BPM1=M1&M2). I am sharing my code for those who may need it.

**johndg** , thank you very much for your time and and your guidance for directing me to the correct files,  
which eventually lead me to the solution.

Here is a picture of the circuit:

[https://kisi.deu.edu.tr/basak.karpuz/arduino/l293d-mds.jpg(image larger than 4 MB)](https://kisi.deu.edu.tr/basak.karpuz/arduino/l293d-mds.jpg)
  
Here is the link for a video:

[![](https://europe1.discourse-cdn.com/arduino/original/4X/d/3/c/d3c945d105bd4906af06d357676e49035a6bef58.jpeg "Driving Bipolar Motors with L293D Motor Shield without Any Library") ](https://www.youtube.com/watch?v=xc0qZcfvuK8)

```cpp
//Bit positions in the Arduino UNO micro controller output

#define PWM2B 3 //For slots M1&M2 on the L293D Motor Shield
#define DIR_CLK 4 //SHCP of the Shift Register 74HC595
#define PWM0B 5 //For slots M3&M4 on the L293D Motor Shield
#define PWM0A 6 //For slots M3&M4 on the L293D Motor Shield
#define DIR_EN 7 //OE of the Shift Register 74HC595
#define DIR_SER 8 //DS of the Shift Register 74HC595
#define PWM1A 9 //For slots SERVO1&SERVO3 on the L293D Motor Shield
#define PWM1B 10 //For slots SERVO1&SERVO3 on the L293D Motor Shield
#define PWM2A 11 //For slots M1&M2 on the L293D Motor Shield
#define DIR_LATCH 12 //STCP of the Shift Register 74HC595

//Bit positions in the 74HCT595 shift register output

#define MOTOR3_A 15 //Q0
#define MOTOR2_A 1 //Q1
#define MOTOR1_A 2 //Q2
#define MOTOR1_B 3 //Q3
#define MOTOR2_B 4 //Q4
#define MOTOR4_A 5 //Q5
#define MOTOR3_B 6 //Q6
#define MOTOR4_B 7 //Q7

//Step BPMotor1 connected to M1&M2 by setting the following entries to HIGH, respectively: Q2=4, Q1=2, Q3=8, Q4=16
//Step BPMotor2 connected to M3&M4 by setting the following entries to HIGH, respectively: Q0=1, Q5=32, Q6=64, Q7=128

int BPMotor1Seq[4]={4,2,8,16}; //Sequence for BPM1 for forward 4 steps
int BPMotor2Seq[4]={1,32,64,128}; //Sequence for BPM2 for forward 4 steps
int stepperval;
int stepper1val;
int stepper2val;
int qcounter = 0;
int hcounter = 0;
int hmax=1500;

void setup() {

  pinMode(DIR_EN, OUTPUT);
  pinMode(DIR_SER, OUTPUT);
  pinMode(DIR_LATCH, OUTPUT);
  pinMode(DIR_CLK, OUTPUT);
  pinMode(PWM0A, OUTPUT);
  pinMode(PWM0B, OUTPUT);
  digitalWrite(PWM0A, HIGH);
  digitalWrite(PWM0B, HIGH);
  pinMode(PWM2A, OUTPUT);
  pinMode(PWM2B, OUTPUT);
  digitalWrite(PWM2A, HIGH);
  digitalWrite(PWM2B, HIGH);

}

void loop() {

  if (hcounter<(hmax/4)) {
    SetBPMotor1CW();
    SetBPMotor2CW();
  } else if (hcounter<(hmax/2)) {
    SetBPMotor1CCW();
    SetBPMotor2CW();
  } else if (hcounter<(3*hmax/4)) {
    SetBPMotor1CCW();
    SetBPMotor2CCW();
  } else {
    SetBPMotor1CW();
    SetBPMotor2CCW();
  }

  stepperval=stepper1val+stepper2val;
  digitalWrite(DIR_LATCH, LOW);
  shiftOut(DIR_SER, DIR_CLK, MSBFIRST, stepperval);
  digitalWrite(DIR_LATCH, HIGH);
  delay(20);
  qcounter = (qcounter + 1) % 4;
  hcounter = (hcounter + 1) % hmax;

}

void SetBPMotor1CW() {

  stepper1val=BPMotor1Seq[qcounter];

}

void SetBPMotor1CCW() {

  stepper1val=BPMotor1Seq[3-qcounter];

}

void SetBPMotor2CW() {

  stepper2val=BPMotor2Seq[qcounter];

}

void SetBPMotor2CCW() {

  stepper2val=BPMotor2Seq[3-qcounter];

}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 9:54am UTC](https://forum.arduino.cc/t/using-l293d-motor-driver-shield-without-any-library/685601/14 "2021-05-06T09:54:16Z")

</div>


