Controlling 2 dc-motors with l293ne

Hi,

I am trying to control 2 dc-motors with l293ne, but it wont work properly. Maybe someone here could help me out, what am i doing wrong?

Dc-motors are small cheap ones whichs takes in 1,5 to 4,5 volts.
Attached is schematics and code. Im basically just trying to get the motors running randomly in random direction. Very basic.
but doesnt work... help!

int moottori1a = 3; //pin 2 l293:d:llä
int moottori1b = 4; //pin 7 l293:d:llä
int moottori2a = 5; //pin ? l293:d:llä
int moottori2b =6; //pin ? l293:d:llä
int enablePin1 = 9; //pin 1 l293:d:llä
int enablePin2 = 9; //pin 9 l293:d:llä
int ledPin = 13; //LED 


void setup(){
pinMode(moottori1a, OUTPUT);
pinMode(moottori1b, OUTPUT);
pinMode(moottori2a, OUTPUT);
pinMode(moottori2b, OUTPUT);

digitalWrite(enablePin1, HIGH);
digitalWrite(enablePin2, HIGH);

// blink the LED 3 times. This should happen only once.
// if you see the LED blink three times, it means that the module
// reset itself,. probably because the motor caused a bownout
// or a short.
blink(ledPin, 3, 100);
}

void loop () {
  
  digitalWrite(moottori1a, LOW);
  digitalWrite(moottori1b, HIGH);
  
    analogWrite(enablePin1, random(100,255));
  
  digitalWrite(moottori2a, LOW);
  digitalWrite(moottori2b, HIGH);
  
  analogWrite(enablePin2, random(100,255));
  
 delay (10);
 
 digitalWrite(moottori1a, HIGH);
  digitalWrite(moottori1b, LOW);
  
    analogWrite(enablePin1, random(100,255));
  
  digitalWrite(moottori2a, HIGH);
  digitalWrite(moottori2b, LOW);
  
  analogWrite(enablePin2, random(100,255));
  
 delay (10);
}
void blink(int whatPin, int howManyTimes, int milliSecs) {
int i = 0;
for ( i = 0; i < howManyTimes; i++) {
digitalWrite(whatPin, HIGH);
delay(milliSecs/2);
digitalWrite(whatPin, LOW);
delay(milliSecs/2);
}
}


you are talking about l298ne but the diagram shows a l293d if the pin's are same for both the ic's then it should work ! :wink:
it would be better if you use seperate power source for the motors

Ic:s should be identical with the pins. Motors also work for sometime. Then it quits.

do you notice any overheating of any sort if it works fiast then try powerring it away from the arduino

Im basically just trying to get the motors running randomly in random direction

Motors also work for sometime

So it is working.

Are you really powering it all (motors and controller) off a single 9V battery?

That's not good.
What sort of current do the motors draw, and what is their operating voltages?
293s drop well over a volt, IIRC, so if they're running off 5V, you could have only around 3.5V at the motors.

As has been said (many, many times) always use a separate supply for motors.

Maybe I've should have been more specific. When I turn it on the motors do run for sometime, propably running the setup part of the code. The loop code doesnt execute. Dont know why. If i am right the loop code should run and turn the motos randomly all the time? Anyway this suggests that the wiring is propably ok.(?)
Motors draw from 1.5 to 4.5 volts. I dont know their current and dont have multimeter nearby now.
I power everything from 9v battery attached to arduino butI have tried powering the motors and IC from different 9v battery but it didnt change anything.
Maybe I should try that again?
Btw, thanks for all the replies this far :slight_smile:

Personally, I'd scrap the random stuff, and just try to get one motor running, either using a pot to control speed (simple), or using the serial monitor to send values to set speed (harder).
Once that is working (or not) try a second motor.
But always, use a separate power supply for the motors.

Maybe I'm blind. I just don't see where you are supplying V+ for the L293 and I also don't see where you are supplying MOTOR V+.

You are connecting those pins, right?

yeah AWOL was right why dont you try turning the motors in a specific manner so that you can know if everything is wired correct
TRY THIS!

#define m1p1 8 
#define m1p2 9
#define m2p1 10
#define m2p2 11
void setup()
{
  pinMode (m1p1,OUTPUT);
  pinMode (m1p2,OUTPUT);
  pinMode (m2p1,OUTPUT);
  pinMode (m2p2,OUTPUT);
}
void loop()
{
  digitalWrite(m1p1,HIGH);
  digitalWrite(m1p2,LOW);
  digitalWrite(m2p1,HIGH);
  digitalWrite(m2p2,LOW);
  // drive motors forward set  delay
  delay(10000);
  //now drive motors reverse both 
  digitalWrite(m1p1,LOW);
  digitalWrite(m1p2,HIGH);
  digitalWrite(m2p1,LOW);
  digitalWrite(m2p2,HIGH);
  delay(10000);
  digitalWrite(m1p1,LOW);
  digitalWrite(m1p2,LOW);
  digitalWrite(m2p1,HIGH);
  digitalWrite(m2p2,LOW);
  delay(2000);
  //only one motor on
  digitalWrite(m1p1,HIGH);
  digitalWrite(m1p2,LOW);
  digitalWrite(m2p1,LOW);
  digitalWrite(m2p2,LOW);
  //  //onemotor in one the direction and another in the reverse  
  digitalWrite(m1p1,LOW);
  digitalWrite(m1p2,HIGH);
  digitalWrite(m2p1,HIGH);
  digitalWrite(m2p2,LOW);
  //onemotor in one the direction and another in the reverse   digitalWrite(m1p1,HIGH);
  digitalWrite(m1p2,LOW);
  digitalWrite(m2p1,LOW);
  digitalWrite(m2p2,HIGH);
}

noe as the output is aldreadyy known so you can actually see what is wrong
even i had a similar experience i wired things in a worng manner and only one part of the code worked
GUD LUCK :wink:

pwillard: you have a very good vision, so youre not blind! I forgot to put those in scheme. But yes I am connecting those pins. They go to +5v from arduino.
Im a bit puzzled about the the powering problem. I have checked quite many tutorials and in those the +5volts from arduino (when powered from ext. power source) is enough to drive a motor. But still it is suggested to drive them from different power source. I would like to make my design as simple and light as possible, so do you guys think its possible to power them from the arduinos +5v?

Anyway, I'll try again to power the motors from different 9volt battery.

I have tried the motors with different code. Controlling with pot worked for few seconds, then it stopped. Alsothe lower speeds didnt turn the motor, it just stopped with some electric noise buzzing, so I could actually hear it was getting differetn amount of current, but the motor just didnt turn..

newbie: in your code, theres no pins for enabling the l293? But thanks for the code and trouble!

just tie the things to the +ve terminals on the battery !
i posted this code so that you can check where the fault is

Anyway, I'll try again to power the motors from different 9volt battery

I thought you said they were 4.5V motors.

Great, thanks. Will try that tomorrow!

I power everything from 9v battery attached to arduino butI have tried powering the motors and IC from different 9v battery but it didnt change anything.
Maybe I should try that again?

I think you should over and over again so you can get the same bad results over and over again.

Anyway, I'll try again to power the motors from different 9volt battery.

9v batterys are made for powering low current electronics and not high current motors. "worked for few seconds, then it stopped" could be a clue. You can power the arduino with a 9v battery for a while, but motors will need their own larger power supply. Also you might expect a ~2v voltage drop across the l293 chip (check the data sheet), so you may need a 6v battery to power a 4v motor.

Sorry for the stupid question, but why should I use 6v source instead of 9v? I have understood that l293 should be able to take it. That also leaves some space for voltage drops if using 2motors?

Well that made it lot clearer. It's like old people yelling to foreigners if tvey dont understand the language; maybe if I yell they get it... ::slight_smile:

I have used 9volt batteries with good results in many arduino projects. They are cheap and can be found everywhere. So I wouldnt say they are useless.
But ok, with dc-motors im total newbie and didnt understand that they take that much current.

But ok, with dc-motors im total newbie and didnt understand that they take that much current.

I suggest you get a multimeter (even a $4 harbor freight will do) so you can take measurments to see what is going on.

A voltmeter is how I would debug it, too. Write a sketch that should make your motors turn full speed. While the sketch is running, check everything: the outputs of the Arduino, the inputs to the l293, and the outputs to the motor. It's pretty easy to debug this way and picks up many dumb mistakes.

Thanks to all the replies! Yesterday I managed to fix the problem. And YES the problem was the 9volt battery powering the motors. I changed that to better powersource and put capacitor to smooth the values and then everything worked :slight_smile:
And yes I have now different powersources for arduino and motors. Theres 9volt battery powering arduino and IC and AA-batteries for the motors.
code works perfectly but I refined it a little, but is still very simple and a work in process. It is meant to be very simple because I'll use it on some courses, but for my own amusement and interest I plan to work on it more...
if anyones interested, it's here

/* "RANDOM SWITCH CASE DRAWING ROBOT"
 2010 Tomi Dufva
 Uses l293ne IC-chip (H-bridge, for info look:http://www.ecs.umass.edu/ece/m5/tutorials/H-Bridge_tutorial.html
 and http://luckylarry.co.uk/arduino-projects/control-a-dc-motor-with-arduino-and-l293d-chip/)
 for driving 2 simple dc-motors on random directions. */

int moottori1a = 3; //pin 2 on l293
int moottori1b = 4; //pin 7 on l293:
int moottori2a = 5; //pin 10 on l293
int moottori2b =6; //pin 15 on l293:
int enablePin1 = 9; //pin 1 onl293
int enablePin2 = 9; //pin 9 on l293
int ledPin = 13; //LED which blinks when arduino resets, great visual to see if theres some short or other trouble
int ledPin2 = 12; // Led which blinks whenever Switch switches to new case
int liike = 0; //Variable for determining which way robot should go. ie. Which case it takes.



void setup(){ 
  Serial.begin(9600); // debugging
  pinMode(moottori1a, OUTPUT);
  pinMode(moottori1b, OUTPUT);
  pinMode(moottori2a, OUTPUT);
  pinMode(moottori2b, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);

  digitalWrite(enablePin1, HIGH); // Putting IC on and motors running
  digitalWrite(enablePin2, HIGH);// Putting IC on and motors running

  // blink the LED 3 times. This should happen only once.
  // if you see the LED blink three times, it means that the module
  // reset itself,. probably because the motor caused a brownout
  // or a short.
  blink(ledPin, 7, 100);
}

void loop () {

  int liike = random(1,7); // Assigns randomw value from 1-7 to liike variable
  Serial.println(liike); // Prints assigned number to serial to see if it works

  switch (liike) {
  case 1: // Both motors forward

    blink (ledPin2,2,100);

    digitalWrite(moottori1a, LOW);
    digitalWrite(moottori1b, HIGH);
    analogWrite(enablePin1, 100);
    digitalWrite(moottori2a, LOW);
    digitalWrite(moottori2b, HIGH);


    analogWrite(enablePin2, 100); // Assigning the speed of the motor

    delay (1000);
    break;

  case 2: //Right motor forward left backward

    blink (ledPin2,2,100);
    digitalWrite(moottori1a, LOW);
    digitalWrite(moottori1b, HIGH);

    analogWrite(enablePin1, 100); // Assigning the speed of the motor

    digitalWrite(moottori2a, HIGH);
    digitalWrite(moottori2b, LOW);

    analogWrite(enablePin2, 100); // Assigning the speed of the motor

    delay (1000);
    break;
  case 3: //Right motor forward, left stop

    blink (ledPin2,2,100);
    digitalWrite(moottori1a, LOW);
    digitalWrite(moottori1b, HIGH);

    analogWrite(enablePin1, 100); // Assigning the speed of the motor

    digitalWrite(moottori2a, LOW);
    digitalWrite(moottori2b, LOW);

    analogWrite(enablePin2, 100); // Assigning the speed of the motor

    delay (1000);
    break;

  case 4: //Right motor backward, left forward

    blink (ledPin2,2,100);

    digitalWrite(moottori1a, HIGH);
    digitalWrite(moottori1b, LOW);

    analogWrite(enablePin1, 100); // Assigning the speed of the motor

    digitalWrite(moottori2a, LOW);
    digitalWrite(moottori2b, HIGH);

    analogWrite(enablePin2, 100); // Assigning the speed of the motor

    delay (1000);
    break;

  case 5: //Right motor backward, left stop

    blink (ledPin2,2,100);
    digitalWrite(moottori1a, HIGH);
    digitalWrite(moottori1b, LOW);

    analogWrite(enablePin1, 100); // Assigning the speed of the motor

    digitalWrite(moottori2a, LOW);
    digitalWrite(moottori2b, LOW);

    analogWrite(enablePin2, 100);

    delay (1000);
    break;

  case 6: //Right motor stop left forward

    blink (ledPin2,2,100);
    digitalWrite(moottori1a, LOW);
    digitalWrite(moottori1b, LOW);

    analogWrite(enablePin1, 100); // Assigning the speed of the motor

    digitalWrite(moottori2a, LOW);
    digitalWrite(moottori2b, HIGH);

    analogWrite(enablePin2, 100); // Assigning the speed of the motor

    delay (1000);
    break;

  case 7: //Right motor stop, left backward

    blink (ledPin2,2,100);
    digitalWrite(moottori1a, LOW);
    digitalWrite(moottori1b, LOW);

    analogWrite(enablePin1, 100); // Assigning the speed of the motor

    digitalWrite(moottori2a, HIGH);
    digitalWrite(moottori2b, LOW);

    analogWrite(enablePin2, 100); // Assigning the speed of the motor
    delay (1000);
    break;
  }

}
void blink(int whatPin, int howManyTimes, int milliSecs) {
  int i = 0;
  for ( i = 0; i < howManyTimes; i++) {
    digitalWrite(whatPin, HIGH);
    delay(milliSecs/2);
    digitalWrite(whatPin, LOW);
    delay(milliSecs/2);
  }
}