Friday 11 September 2015

Speed & Direction of DC Motor - L293D Motor Driver With Arduino

Sebarang pertanyaan/pembelian/bantuan boleh dm saya dia twitter eh:
twitter.com/zakimuslim_

Maybe this post can help you how control your DC motor rotation and speed!

In this post i have write 2 basic code and circuit. First, control the motor by using delay and second, by using potientio meter.

 i use L293D (IC) motor driver, Arduino Mega 2560, small DC motor, and  variable resistor(rotary potentiometer). 


VIDEO
Watch on youtube for better quality:
Control speed of DC motor using potientio meter
https://www.youtube.com/watch?v=sW0XEpEVhjQ&feature=youtu.be






1. Okay first of all we need to know L293D specification. Lets check it.



This motor driver can drive two DC motor in two direction(bidirection). This ic widely use in robotic field. It very small and cheap! 


2. Okay now look at the these breadboard layout. I have made this wiring and i have try to run it with my Arduino. Its working properly! i have run it over than 15 minutes. Nothings wrong with it.


In my video, i just try with 1 DC motor. If you want use two DC motor you can follow this circuit.






3. This is code that i've use to control the DC motor with delay.



Its simple.

int m1 = 8;
tell arduino that i use pin 8 to control DC motor in direction1.

int m2 = 9;
tell Arduino i use pin 9 to spin DC motor in direction2.


pinMode(m1, OUTPUT);
i set pin 8 in output mode.

digitalWrite(m1, HIGH);
 Arduino send signal to motor driver to spin DC motor in direction1.

delay(3000);
DC motor will spin in direction1 in 1000milisecond (3seconds).

digitalWrite(m1, LOW);
Arduino will stop send signal to L293D ic to stop spinning.

delay(1000);
DC motor wil stop spinning for 1second.

digitalWrite(m2, HIGH);
Arduino will send signal to motor driver to spin in direction2




Arduino Code that i use.

int m1 = 8;
int m2 = 9;

void setup(){
  pinMode(m1, OUTPUT);
  pinMode(m2, OUTPUT);
}

void loop(){
  digitalWrite(m1, HIGH);
  delay(3000);
  digitalWrite(m1, LOW);
  delay(1000);
  digitalWrite(m2, HIGH);
  delay(3000);
  digitalWrite(m2, LOW);
  delay(1000);
}






Add a potentiometer.
This circuit have a little bit different. I use potentiometer to control speed of DC motor. Check it out! 



                                                                             VIDEO









This is breadboard layout. I just add potentiometer.






Arduino code that i've write. I use this code to control DC motor speed using potentiometer.





int m1 = 8;
tell arduino i have attach DC motor at pin 8.

int vrpin = A0;
tell arduino i attach variable resistor at pin A0.

Serial.begin(9600);
set baud rate to 9600.

pinMode(m1, OUTPUT);
initialize that m1 is output.

pinMode(vrpin, INPUT);
set vrpin in input mode. ready to read data.

speedd = (analogRead(A0)/4);
tell arduino that speed is equal to analog reading on A0, and divided by 4. i divide it by 4 because i want to make max analog reading equal to max pwm.
maximum pwm value = 255
maximum analog reading = 1023
so,
1023/4 = 255.75

Or

Just use map();



Serial.print("speedd: \n ");
Serial.print(speedd);

*open serial monitor to see the analog reading. press ctrl+shift+m  to open serial monitor.

analogWrite(m1, speedd);
write pwm value on pin 8(m1) according analog reading on pin A0.







Just copy and paste this code on your Arduino ide to try this.



int m1 = 8;
int vrpin = A0;
int speedd;

void setup(){
  Serial.begin(9600);
  pinMode(m1, OUTPUT);
  pinMode(vrpin, INPUT);
}

void loop(){
  speedd = (analogRead(vrpin)/4);
  Serial.print("speedd: \n ");
  Serial.print(speedd);

  analogWrite(m1, speedd);

}









1 comment:

  1. Simple Arduino Program: Speed And Direction Of Dc Motor - L293D Motor Driver With Arduino >>>>> Download Now

    >>>>> Download Full

    Simple Arduino Program: Speed And Direction Of Dc Motor - L293D Motor Driver With Arduino >>>>> Download LINK

    >>>>> Download Now

    Simple Arduino Program: Speed And Direction Of Dc Motor - L293D Motor Driver With Arduino >>>>> Download Full

    >>>>> Download LINK Kc

    ReplyDelete