-1

I have the following code which I use to run some DC motors on the Raspberry Pi with an L293D H-Bridge. I should be able to control which motor is driven and in which direction.

#include <stdio.h>
#include <pigpio.h>

#define MOTOR1_FWD 16
#define MOTOR1_BCK 20
#define MOTOR2_FWD 26
#define MOTOR2_BCK 19
#define MAX 1
#define MIN 0

int main(int argc, char *argv[])
{
   double start;

   if (gpioInitialise() < 0)
   {
      fprintf(stderr, "pigpio initialisation failed\n");
      return 1;
   }

   /* Set GPIO modes */
   gpioSetMode(MOTOR1_FWD, PI_OUTPUT);
   gpioSetMode(MOTOR1_BCK, PI_OUTPUT);
   gpioSetMode(MOTOR2_FWD, PI_OUTPUT);
   gpioSetMode(MOTOR2_BCK, PI_OUTPUT);

   gpioWrite(MOTOR1_FWD, MIN);
   gpioWrite(MOTOR1_BCK, MIN);
   gpioWrite(MOTOR2_FWD, MIN);
   gpioWrite(MOTOR2_BCK, MIN);

   if (argc <= 1)
   {
      printf("STOP\n");
   } else {
      int motor1 = MOTOR1_BCK;
      int motor2 = MOTOR2_BCK;
      if (strcmp(argv[2], "f") == 0) {
         printf("FORWARD\n");
         motor1 = MOTOR1_FWD;
         motor2 = MOTOR2_FWD;
      } else {
         printf("BACKWARD\n");
      }
      if (strcmp(argv[1], "l") == 0 || strcmp(argv[1], "lr") == 0) 
      {
         printf("LEFT ON\n");
         gpioWrite(motor1, MAX);
      }
      if (strcmp(argv[1], "r") == 0 || strcmp(argv[1], "lr") == 0)
      {
         printf("RIGHT ON\n");
         gpioWrite(motor2, MAX);
      }
   }
   gpioTerminate();
   return 0;
}

Below is my wiring diagram:

Not_Working

Unfortunately, this doesn't work. Even though it seems to be replicated in many tutorials and blogs. However if I take the GROUND from the 6v and attach it to the 5v of the Pi, my creation can't go fast enough:

Working

Now I have noticed the L293D H-Bridge getting quite warm when run like this, but not alarmingly warm. I want to know why this is happening and if I have not perhaps missed something important. I have tried having a separate pin on the Pi for pin 1 on the L293D (1,2 Enable), but the problem is the same. It's odd to that connecting a ground to live should 'fix' the issue.

Please be aware I have checked my battery, it's the right way around.

EDIT I fixed a mistake with power and ground on the right side of the chip.

Voltages:

  • Pi 5v: 5.1v
  • Battery (VCC 2): 9.87v (I upped to a 9v battery to ensure that vvc2 > vcc1)
  • VCC 1: 5.09v
  • Input 1: 0v
  • Input 2: 3.31v
  • Output 1: 0.785
  • Output 2: 2.81
Quintin Balsdon
  • 681
  • 5
  • 19
  • 1
    This looks like a question you could ask on Electronics.SE, but note that you'd be expected to provide a schematic (rather a wiring diagram) when asking there. – Dmitry Grigoryev Aug 11 '17 at 10:39
  • Thanks - I'll try to see if I can make one... I have a suspicion I have been learning this stuff from the wrong people and they don't actually know what is going on either... Although from the datasheet it does look correct – Quintin Balsdon Aug 11 '17 at 10:45

1 Answers1

1

It looks like you connected the L293D pin 16 to ground. It should connect to +5.

mr2chips
  • 19
  • 1
  • My sincere apologies, that was just a diagramming mistake on my part. I am so sorry I tried to be so careful. I have corrected and double checked – Quintin Balsdon Aug 12 '17 at 10:25