I just wrote a program and rigged up my circuit to control a DC motor via the L293D chip.
Here's my program:
int pwmPin = 9; //Motor's PWM pin
int dPin = 7; // Digital Pin to turn the motor on/off
int ledPin = 13; //LED pin
void setup()
{
pinMode(dPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // turn on LED
digitalWrite(dPin, HIGH); //turn on the motor
//Apply PWM to the motor
for(int i=0; i<=255; i++)
{
analogWrite(pwmPin, i);
delay(50);
}
delay(1000);
for(int i=255; i>=0; i--)
{
analogWrite(pwmPin, i);
delay(50);
}
digitalWrite(ledPin, LOW); // turn off motor
digitalWrite(dPin, LOW); //turn off LED
delay(2000); //delay for 2 seconds.
}
Here's my pin connection:
Are they right? I ran this and my Led works fine. but the motor doesn't work.
Well..
To run L293D chip you should apply PWM signal to the first pin, an 2 digital arduino outputs to the 2-d and 7-th if they both LOW or both HIGH motor stops and for 2(LOW), 7(HIGH) it rotates one direction and for 2(HIGH), 7(LOW) - another direction
Then...
pins 4 and 5 should be grounded and motor should be connected to the 3-rd and 6-th pin.
oh! so I got the pin connections wrong on the L293D chip. k.
Also, I didn't use pin 6,7 on the L293D chip since I wasnt trying to control the motor in two directions. Just one direction. on and off.
So if this use case is considered, is it right?
That article is in German. but the blog blog post contains only one line of text. anyway, I'll now try changing the digital pins like what you said.
Is the code right?
I changed my code and connections like you said. I now have 2 digital pins(2,7) from the chip connected to the Arduino. And using pin-1 on the chip connected to pin-9 on the Arduino for PWM. Connected pins(4,5) on chip to ground. Connected the logic power pin-16 on the chip to 5v pin on the Arduino. And connected +ve of a 1.5v battery to pin-8 on the chip and -ve to ground.
and here's the modified code:
int pwmPin = 9; //Motor's PWM pin
int dPin_1 = 6;// Digital Pin to turn the motor on/off
int dPin_2 = 7;
int ledPin = 13; //LED pin
void setup()
{
pinMode(dPin_1, OUTPUT);
pinMode(dPin_2, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
digitalWrite(ledPin, HIGH); // turn on LED
digitalWrite(dPin_1, HIGH); //turn on the motor
digitalWrite(dPin_2, LOW);
//Apply PWM to the motor
for(int i=0; i<=255; i++)
{
analogWrite(pwmPin, i);
delay(50);
}
delay(1000);
for(int i=255; i>=0; i--)
{
analogWrite(pwmPin, i);
delay(50);
}
digitalWrite(ledPin, LOW); // turn off LED
digitalWrite(dPin_1, LOW); //turn off motor
delay(2000); //delay for 2 seconds.
}
I'm not sure, but I think that 1.5V may be too small to L293D (in datasheet it is written that supply voltage should be greater than logic supply voltage (4.5V)). But I used 3V supply and the motor worked well...
ya. they all have a common ground connected to the GnD pin on the arduino. I found 2 GnD pins on the arduino. one on the Digital pins side. and another along the analog pins. I connected them all to the GnD pin on the analog pin side.
and I've also modified my program again to make it simple. I removed the PWM. I refered the program here:
int motor1Pin = 6; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 7; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 9; // H-bridge enable pin
int ledPin = 13; // LED
void setup() {
// set the switch as an input:
//pinMode(switchPin, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(ledPin, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin, HIGH);
blink(ledPin, 3, 100);
}
void loop() {
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
delay(2000);
blink(ledPin, 6, 100);
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
}
/*
blinks an LED
*/
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);
}
}
That program uses a switch. I removed the switch and just put in a few LED blinks when the motor stops to change direction.
The LED still works fine. i blinks well (the 6 blinks in between switching the motor's direction). But the motor is silent :o
I spent a day on this and just to check the connections I applied a bit of pressure on the chip connected to the bread board and it worked. The chip was a connected loosely to the breadboard.
3V motors often draw more current than the 600mA the L293D is rated for. Check the specs on your motor.
At high currents, the L293 can lose a couple of volts to the Darlington transistors it uses on both the high side and low side of the motor drive. You might need to need to increase the motor supply to as much as 5V.