Saturday, 14 February 2015

Arduino DC Motor Driving

Arduino Motor Driving

Arduino and L293B

       We will make another simple project with Arduino. This project's name is Arduino motor driving! We will learn how to control a motor with Arduino. Firstly we will control the motor's speed by a potentiometer. Then we will learn what is the PWM and how to change the motor's speed by the PWM.

We need,
  • Potentiometer
  • An Arduino board
  • L293D Motor Driver
  • 9V Battery
to make this project. I used L293D motor driver and 5K potentiometer but you can use L293B motor driver and any other potentiometer. This is the sketch of the circuit.


The difference between L293D and L293B is the current limit. L293D's current limit is 0.5 A and L293B's current limit is 1A. Now, we can see the code.



void setup() {
//we arrange pins, which we will use, as output 
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(11, OUTPUT);
}

//loop function will repeat while Arduino is powered on

void loop() {

/* 8th and 9th pins are input pin to motor driver.
 we arrange 8th pin as HIGH to get 5V output and 9th LOW to get 0V.
 if we arrange both of them as HIGH or LOW, motor will not work. 11th pin is PWM pin. 
 we arranged it as HIGH to make it disabled. */

digitalWrite(8, HIGH); 
digitalWrite(9, LOW);
digitalWrite(11, HIGH);

}

       When we load this code to Arduino, we will arrange the speed of the motor with potentiometer.

       Now, we will change the motor direction with code. Do not use the potentiometer for this time. Directly connect the motor to the motor driver's output pin. Look at the sketch.





void setup() {
//we arrange pins, which we will use, as output 
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(11, OUTPUT);
}

//loop function will repeat while Arduino is powered on

void loop() {

/* 8th and 9th pins are input pin to motor driver.
 we arrange 8th pin as HIGH to get 5V output and 9th LOW to get 0V.
 if we arrange both of them as HIGH or LOW, motor will not work.

 11th pin is PWM pin. 
 we arranged it as HIGH to make it disabled. */

digitalWrite(8, HIGH); 
digitalWrite(9, LOW);
digitalWrite(11, HIGH);

// motor will work for 3 seconds 

delay(3000);

//we should let the motor to stop
digitalWrite(11, LOW);
delay(1000);


//we will change the 8th and 9th pins' values. then motor will change its direction 

digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(11,HIGH);

delay(3000);

}


Now, we will change the motor speed by changing the PWM value in the code. We will use a special function for this: " analogWrite() ". Let's see the code


void setup() {
//we arrange pins, which we will use, as output 
 
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(11, OUTPUT);
}

//loop function will repeat while Arduino is powered on

void loop() {

/* 8th and 9th pins are input pin to motor driver.
 we arrange 8th pin as HIGH to get 5V output and 9th LOW to get 0V.
 if we arrange both of them as HIGH or LOW, motor will not work.

 11th pin is PWM pin. 
 We will use analogWrite() function to arrange the motor's speed. */

digitalWrite(8, HIGH); 
digitalWrite(9, LOW);
analogWrite(11, 255);  //This function has a range from 0 to 255


}


Let's watch the video to see how it works. We finished that project. As an idea, you can control the motor with a button. We will make an other project in a short time. Keep in touch!

If you liked this post please subscribe it to reach more people! Thank you :)


1 comment :

Thanks for your comments :)