DC Motors

DC motors are all around you. They allow your cellphone to vibrate, they run laptop fans and they run the dvd drives.

Parameters of motors


To drive a motor, apply a voltage.
The higher the voltage, the faster the spinning.

Just as voltage causes rotation, rotation causes voltage.

The circuit for motors is more complex than prior circuits you have worked with. In addition to using familiar components, you'll also be using a DC Motor, diode , and a transistor.


Diodes allow current to flow only in one direction. Motors often create current spikes as they turn on and off. These spikes can damage the transistor, so you use a diode to protect the transistor from the spikes. Diodes have polarity, so the direction you install them matters. The bar in the schematic diagram corresponds to a white stripe on the actual diode. Current can only flow in the direction of the triangle.
Since motors can act like generators, need to prevent them from generating kickback into the circuit

Once a motor starts spinning, its inertia keeps it spinning, this turns it into a generator and thus can generate a kickback voltage. The kickback diode routes that voltage harmlessly back into the motor so it can't damage the rest of the circuit.
Kickback is also called back EMF (EMF == electromotive force == voltage)


Transistors have three terminals: The base is the input that you use to open and close the switch across the collector and emitter.

On this type of transistor (called an NPN, you need to make sure the collector is always more positive than the emitter. Generally you do this by connecting the emitter to ground.

Because the motor requires much more power than the Arduino is capable of providing, you use batteries to power the motor. The transistor allows you to control the amount of current that flows from the battery through the motor.

Transistors are used to run loads larger than 5V.


Typical Tip120 circuit

Controlling Motors

You can control speed of motor with analogWrite() just like controlling brightness of LED.

If you use a small motor, you can wire it up on the same breadboard:
In Arduino add this code:
int motor=___;
char val;

void setup(){
Serial.println("enter speed number 0-9");
void loop(){
if(val>='0' &&val<='9'){
    val=val-'0'; //converts from char to num
    val=28*val; //converts num to 0-255
    Serial.println("enter speed number 0-9");

Bigger Motor

Wire up a DC Motor with a pot



The SN754410 is a handy IC that allows you to control the speed and direction of a DC motor with only one PWM output and two digital outputs from your Arduino board

The first half of the 270° degree turning angle of the potentiometer will make the motor run forward. The motor's speed will decrease the more the knob is turned toward the potentiometer's center position. The second half of the turning angle will make the motor run in reverse, with increasing speed toward the 270° mark. the percentages in the following picture are related to the motor's speed.

* Arduino code for SN754410 H-bridge
 * motor driver control. A potentiometer's
 * 270 degree rotational angle is used to
 * control a DC motor in the following way:
 * 0-135 degrees - motor runs forward, speed
 * getting slower the higher the angle, motor
 * stops at 135 degrees.
 * 135-270 degrees: motor runs backward,
 * speed is getting faster the higher the
 * angle, motor speed is 0 at 135 degrees.
 * copyleft Feb. 2008, Fabian Winkler
int potPin = 0;        // analog input pin 0 for the potentiometer 
int speedPin = 3;      // H-bridge enable pin for speed control 
int motor1Pin = 6;     // H-bridge leg 1  
int motor2Pin = 7;     // H-bridge leg 2  
int ledPin = 13;       // status LED 
int val = 0;           // variable to store the value coming from the 

void setup() { 
  // set digital i/o pins as outputs: 
  pinMode(speedPin, OUTPUT); 
  pinMode(motor1Pin, OUTPUT);  
  pinMode(motor2Pin, OUTPUT);  
  pinMode(ledPin, OUTPUT); 
void loop() { 
  digitalWrite(ledPin, HIGH);  // status LED is always on 
  val = analogRead(potPin);    // read the value from the potentiometer 
  val = val/4;                 // convert 0-1023 range to 0-255 range  
  if (val <= 127) { 
    // put motor in forward motion 
    digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low 
    digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high 
    // control speed based on angle of potentiometer 
    analogWrite(speedPin, 254-(val*2));  // output speed as PWM value 
    // this value needs to go from 254 to 0 for input values  
    //from 0 to 127 
  // put motor in backward motion  
  else { 
    digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high 
    digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low 
     // control speed based on angle of potentiometer 
    analogWrite(speedPin, (val*2)-256);  // output speed as PWM value 
    // this value needs to go from 0 to 254 for input values  
    // from 128 to 255 
Here is another H-Bridge program:
/* simpleMotor using th HBridge
 * ---------------- 

//the H bridge takes two outputs from the Arduino to control the motor.
int motor1Pin = 4; 
int motor2Pin = 3; 

// there is one switch 
int switchPin = 1;

//declare the state variable
int state = 0;

void setup() {
  //the motor control wires are outputs
  pinMode(motor1Pin, OUTPUT);
  pinMode(motor2Pin, OUTPUT);
  //the switch is an input
  pinMode(switchPin, INPUT);

void loop() {
  //read the switch
  state = digitalRead(switchPin);
  //based on the state of the switch alternate the control pins to change he direction of the motor.
  switch (state){
  case 0:
    digitalWrite(motor1Pin, HIGH);
    digitalWrite(motor2Pin, LOW);
  case 1:
    digitalWrite(motor1Pin, LOW);
    digitalWrite(motor2Pin, HIGH);