Motor Reversing

Overview:

In this lesson, you will learn how to control both the direction and speed of a small DC motor using an Arduino and the L293D motor driver chip.

Materials Needed:

  • (1) Small 6V DC Motor
  • (1) L293D IC
  • (1) 10 kΩ variable resistor (pot)
  • (1) Push button
  • (1) Half-size breadboard
  • (1) Arduino Uno R3
  • Jumper wires

Code:

int enablePin = 11;

int in1Pin = 10;

int in2Pin = 9;

int switchPin = 7;

int potPin = 0;

 

void setup()

{

  pinMode(in1Pin, OUTPUT);

  pinMode(in2Pin, OUTPUT);

  pinMode(enablePin, OUTPUT);

  pinMode(switchPin, INPUT_PULLUP);

}

 

void loop()

{

  int speed = analogRead(potPin) / 4;

  boolean reverse = digitalRead(switchPin);

  setMotor(speed, reverse);

}

 

void setMotor(int speed, boolean reverse)

{

  analogWrite(enablePin, speed);

  digitalWrite(in1Pin, ! reverse);

  digitalWrite(in2Pin, reverse);

}

Layout: