| A L293D motor controller chip and a DC motor. |
|
I got fed up with indecipherable pinout diagrams within datasheets, so have designed my own that I think gives more pertinent
information.
There are 3 wires connected to the arduino, 2 wires connected to the motor, and 1 wire connected to a battery.
|
A L293D motor controller chip - pinout diagram. Green - connected to arduino. Blue - connect to motor Pink - connect to a battery +ve, eg 12 volt. |
|
To use this pinout:
The left hand side deals with the first motor, the right hand side deals with a second motor.
Yes, you can run it with only one motor connected.
Arduino Connections
M1 PWM - connect this to a PWM pin on the arduino. They're labelled on the Uno, pin 3 is an example.
Output any integer between 0 and 255, where 0 will be off, 128 is half speed and 255 is max speed.
M1 direction 0/1 and M1 direction 1/0 - Connect these two to two digital arduino pins.
Output one pin as HIGH and the other pin as LOW, and the motor will spin in one direction.
Reverse the outputs to LOW and HIGH, and the motor will spin in the other direction.
The code below does not use a separate power supply (ie a battery), it uses instead the 5v power from the arduino.
Note that this would be risky without the L293D controlling it.
You should _never_ connect a motor directly to the
arduino, because when you switch a motor off you get an electrical feedback. With a small motor, this will
damage your arduino, and with a large motor, you can watch an interesting flame and sparks effect.
Wired up. L293D Pin : Arduino Pin 1 : 3 2 : 5 3 (DC motor +5V) 4 (GND) 5 (GND) 6 (DC motor GND) 7 : 4 8 (+5V) 9 : 6 10 : 8 11 (DC motor #2 +5V) 12 (GND) 13 (GND) 14 (DC motor #2 GND) 15 : 7 16 (+5V) |
|
This is the code that I used.
/************************
Exercise the motor using
the L293 chip
************************/
#define ENABLE 3
#define DIRB 4
#define DIRA 5
int i;
void setup() {
//---set pin direction
pinMode(ENABLE,OUTPUT);
pinMode(DIRA,OUTPUT);
pinMode(DIRB,OUTPUT);
Serial.begin(9600);
}
void loop() {
//---back and forth example
Serial.println("One way, then reverse");
digitalWrite(ENABLE,HIGH); // enable on
for (i=0;i<5;i++) {
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(500);
digitalWrite(DIRA,LOW); //reverse
digitalWrite(DIRB,HIGH);
delay(500);
}
digitalWrite(ENABLE,LOW); // disable
delay(4000);
Serial.println("fast Slow example");
//---fast/slow stop example
digitalWrite(ENABLE,HIGH); //enable on
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(1000);
digitalWrite(ENABLE,LOW); //slow stop
delay(3000);
digitalWrite(ENABLE,HIGH); //enable on
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(1000);
digitalWrite(DIRA,LOW); //fast stop
delay(3000);
//Serial.println("PWM full then slow");
//---PWM example, full speed then slow
digitalWrite(ENABLE,HIGH); //enable on
digitalWrite(DIRA,HIGH); //one way
digitalWrite(DIRB,LOW);
delay(2000);
analogWrite(ENABLE,128); //half speed
delay(2000);
digitalWrite(ENABLE,LOW); //all done
delay(10000);
}
|
All the above is in the public domain. It's included here to show which PIN settings work with which code.