Using an Arduino to control the speed and direction of a DC Motor.

Complete Circuit
Completed Circuit

Parts List:

  • Arduino
  • Solderless Breadboard
  • 5v DPDT Relay
  • DC Motor
  • Connector Block (optional)
  • 100k Pot
  • 1k Resistor (2)
  • Zener Diode
  • TIP120 or similar transistor
  • Solid hook-up wire

DigitalGroundAnalogOther
Green WireBlack WireYellow WireYellow Wire

__________________________________________________________________________

Getting Started:

Because the Arduino can't handle more than 40ma of current, you must have an external power source to power it. This circuit enables you to use a motor that is too big for the Arduino to handle, control direction of the motor with a relay, and control the speed of the motor with a potentiometer.

Note: The pot is not directly controlling the motor, but the Arduino is getting the value of the pot and using PWM to control the motor speed based off of that value.

Warning: the schematic for this project is incorrect. Ask about it in the forum before attempting to build!

Program:

/*
 * Switch test program
 */

int pot = 0;
int relay = 3;
int motorPin = 11;              

void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
  pinMode(pot, INPUT); 
  pinMode(relay, OUTPUT);
  pinMode(motorPin, OUTPUT);
}

int getPot() {
  int v;
  v = analogRead(pot);
  v /= 4;
  v = max(v, 90);
  v = min(v, 255);
  return v;
}

int motorFoward() {
  analogWrite(motorPin, getPot());
  delay(1000);
  digitalWrite(motorPin, LOW);
  delay(1000);
  digitalWrite(relay, HIGH);
  Serial.println(getIR());
  delay(1000);
}

int motorBackward() {
  analogWrite(motorPin, getPot());
  delay(1000);
  digitalWrite(motorPin, LOW);
  delay(1000);
  digitalWrite(relay, LOW);
  Serial.println(getIR());
  delay(1000);
}

void loop()                     // run over and over again
{
  motorFoward();
  motorBackward();
}

Share