l293d motor driver with potentio meter problem

Hi there,
I have a l293d motor driver. 2 motors are attached to motor driver. I want to control speed of both motors individually through 2 potentiometer. I wrote the following code to achieve this output.

#define E1 10  // Enable Pin for motor 1
#define E2 11  // Enable Pin for motor 2
 
#define I1 8  // Control pin 1 for motor 1
#define I2 9  // Control pin 2 for motor 1
#define I3 12  // Control pin 1 for motor 2
#define I4 13  // Control pin 2 for motor 2

#define pot1 0 //Potentiometer1
#define pot2 1 //Potentiometer2
 
int val1;
int val2;

void setup() {
 
    pinMode(E1, OUTPUT);
    pinMode(E2, OUTPUT);
 
    pinMode(I1, OUTPUT);
    pinMode(I2, OUTPUT);
    pinMode(I3, OUTPUT);
    pinMode(I4, OUTPUT);
    
    digitalWrite(I1,LOW);
    digitalWrite(I2,HIGH);
    digitalWrite(I3,LOW);
    digitalWrite(I4,HIGH);
}
 
void loop() 
{
    val1 = analogRead(pot1) / 4;
    analogWrite(E1, val1);
    delay(10);
    
    val2 = analogRead(pot2) / 4;
    analogWrite(E2, val2);
    delay(10);
}

Only one motor is running at full speed. when I reduce the speed of motor1 through pot1 to 0 and again try to increase the speed of motor1 through pot1, motor2 starts spinning instead of motor1 and motor1 does not move at all. I think the problem is with the code because it is working fine with one motor. please help me to solve this problem.

Did you read:

Also, why so stingy with the symbolic constants? Their purpose is to make the code easier to read. Which is easier; your constants or:

#define ENABLEPINMOTOR1         10  // Enable Pin for motor 1
#define ENABLEPINMOTOR2         11  // Enable Pin for motor 2
 
#define CONTROLPIN1MOTOR1        8  // Control pin 1 for motor 1
#define CONTROLPIN2MOTOR1        9  // Control pin 2 for motor 1
#define CONTROLPIN1MOTOR2       12  // Control pin 1 for motor 2
#define CONTROLPIN2MOTOR2       13  // Control pin 2 for motor 2

#define POT1          0     //Potentiometer1
#define POT2          1     //Potentiometer2

Should any of the pins have the pin mode set to INPUT?

Thanks for reply econjack.
Yes I read analogWrite() - Arduino Reference and none of the pins have pin mode set to INPUT.

What I was trying to point out is that the pot pins that you are trying to read are 0 and 1, which are digital pins normally used by the Serial object. See if this change makes any difference:

#define POT1          A0     //Potentiometer1
#define POT2          A1     //Potentiometer2

which are analog pins and suggests the need for a small wiring change.

What I was trying to point out is that the pot pins that you are trying to read are 0 and 1, which are digital pins

No, the analogue pins have their own numbering - analogue pin zero can be referred to as 0 or A0

If anyone know any article about using l293d motor driver with 2 motor with speed control please post here.

@AWOL:
True, but he's still inputting on wrong pins (D0& 1).

How about the datasheet alicebj?
Block diagram on page 2. Left side is controlling a motor in 2 directions.
Right side is controlling 2 motors, with speed control.
How about posting an image of what you have connected up?

L293d motor drive.pdf (204 KB)

jcallen:
@AWOL:
True, but he’s still inputting on wrong pins (D0& 1).

? Not sure that that means?

@alivebj… this is a good example of where you should use serial prints strategically in your code so you can see what your variables are doing.

Start by adding this in setup():

Serial.begin(9600);

Then add these in loop():

void loop() 
{
    val1 = analogRead(pot1) / 4;
    Serial.println(val1); //<<<<<<<<<<<<<<< add
    analogWrite(E1, val1);
    delay(10);
    
    val2 = analogRead(pot2) / 4;
    Serial.println(val2); //<<<<<<<<<<<<<<< add
    analogWrite(E2, val2);
    delay(10);
}

You might also try as diagnostic aids:

  • Change the analogWrite()s to the Enable pins to digitalWrite() with HIGH
  • Measure the voltage on the 293’s output pins

jcallen:
@AWOL:
True, but he's still inputting on wrong pins (D0& 1).

How can you tell?
I haven't seen a schematic.

I was ass-ooming because he defined POT 1 as 1 he probably hooked his pot wiper to D1 instead of A1.

Kill me for trying to help.

I didn't hook POT1 to D0 it's hooked to A0 correctly.