Controlling a DC motor using L293

Post Reply
User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Controlling a DC motor using L293

Post by SevenZero » Sat Aug 18, 2012 1:38 pm

Here is the circuit diagram which you need to use to connect the motor to L293.
l293d.gif
l293d.gif (9.48 KiB) Viewed 2872 times
Simple Arduino wiring diagram
dcmotor.png
dcmotor.png (167.95 KiB) Viewed 2872 times
Sample source code:

Code: Select all

int switchPin = 2; // switch input
int motor1Pin1 = 3; // pin 2 on L293D
int motor1Pin2 = 4; // pin 7 on L293D
int enablePin = 9; // pin 1 on L293D

void setup() {
    // set the switch as an input:
    pinMode(switchPin, INPUT);

    // set all the other pins you're using as outputs:
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
}

void loop() {
    // if the switch is high, motor will turn on one direction:
    if (digitalRead(switchPin) == HIGH) {
        digitalWrite(motor1Pin1, LOW); // set pin 2 on L293D low
        digitalWrite(motor1Pin2, HIGH); // set pin 7 on L293D high
    }
    // if the switch is low, motor will turn in the opposite direction:
    else {
        digitalWrite(motor1Pin1, HIGH); // set pin 2 on L293D high
        digitalWrite(motor1Pin2, LOW); // set pin 7 on L293D low
    }
}
Post Reply

Return to “Arduino”