Sunday 3 January 2016

Motors Interfacing With Arduino

DC MOTORS




While working with ardiuno passionates have three choices to run motors.


1) Using redimade adafruit motor shield and burn the code-----cost around Rs 350 (run 4 DC motors & 2-servo motors)

2) Using L293D IC------ chepest cost around Rs 30/-and max Rs 50/-

3) Using H-Brige Motor Shield  -----cost around Rs 250/- to 300/-

Moreover while using 2nd Option if you want to take supply more power to DC motor then you can take that power from Vcc pin of arduino . Obviously, accordingly you have to supply voltage to arduino via DC jack. Read Datasheet of arduino for warnings.

I personally suggest 2nd Method as far as my experience is concern.
I had buyied adafruit mot
Now,Anyway Let uor shield but it not run all 4-motors with my clone arduino
s Discuss 1st Method


Using Adafruit Motor Shield






Don't Forget to Download Library or Adafruit into your Arduino IDE

In this shield to test motor there is some code.to get that code u can click Here . or u can just see that code given below

Program



// Adafruit Motor shield library
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!

#include <AFMotor.h>


AF_DCMotor motor(4);


void setup() {

  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");

  // turn on motor

  motor.setSpeed(200);

  motor.run(RELEASE);

}

void loop() {

  uint8_t i;

  Serial.print("tick");

  motor.run(FORWARD);
  for (i=0; i<255; i++) {
    motor.setSpeed(i);
    delay(10);
 }

  for (i=255; i!=0; i--) {

    motor.setSpeed(i);
    delay(10);
 }

  Serial.print("tock");

  motor.run(BACKWARD);

  for (i=0; i<255; i++) {
    motor.setSpeed(i);
    delay(10);
 }

  for (i=255; i!=0; i--) {

    motor.setSpeed(i);
    delay(10);
 }


  Serial.print("tech");

  motor.run(RELEASE);
  delay(1000);
}


-------------------------------------------------------------------------------------------------

After Ada fruit there come 2nd method

i,e Using L293D IC

the pin out diagram for the IC is shown below


in this you don't need to download any library into your arduino .here u just have to write a dedicated code into your IDE

To copy paste that code you can click Here

or you can just see the code on screen
Program
// Use this code to test your motor with the Arduino board:

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// --------------------------------------------------------------------------- Motors
int motor_left[] = {3,8};
int motor_right[] = {12, 7};

// --------------------------------------------------------------------------- Setup
void setup() {
Serial.begin(9600);

// Setup motors
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

}

// --------------------------------------------------------------------------- Loop
void loop() {

drive_forward();
delay(1000);
motor_stop();
Serial.println("1");

drive_backward();
delay(1000);
motor_stop();
Serial.println("2");

turn_left();
delay(1000);
motor_stop();
Serial.println("3");

turn_right();
delay(1000);
motor_stop();
Serial.println("4");

motor_stop();
delay(1000);
motor_stop();
Serial.println("5");
}

// --------------------------------------------------------------------------- Drive

void motor_stop(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void drive_backward(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

void turn_left(){
digitalWrite(motor_left[0], LOW);
digitalWrite(motor_left[1], HIGH);

digitalWrite(motor_right[0], HIGH);
digitalWrite(motor_right[1], LOW);
}

void turn_right(){
digitalWrite(motor_left[0], HIGH);
digitalWrite(motor_left[1], LOW);

digitalWrite(motor_right[0], LOW);
digitalWrite(motor_right[1], HIGH);
}

I have did this and have a video you can check the video on the link given below




----------------------------------------------------------------------


Now the 3rd method is H-Bridge


you can study how h brige works by sliding the pics given on this link
here also you dont need to download any library only dedicated code is needed

To copy paste that code you can click Here


Program

#define ENA 5;
#define ENB 3;
#define IN1 2;
#define IN2 4;
#define IN3 6;
#define IN4 7;

void setup() {
 pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
}

void loop()
{
motorA(1,15);
delay(5000);
motorA(3,100);
motorB(1,15);
delay(5000);
motorB(3,50);
motorA(0,100);
delay(5000);

}
void motorA(int mode,int percent)
{
  int duty=map(percent,0,100,0,255);
  switch(mode)
  {
    case 0:
    digitalWrite(ENA,LOW);
    break;
   
    case 1:
    digitalWrite(IN1,HIGH);
    digitalWrite(IN2,LOW);
    analogWrite(ENA,duty);
    break;
   
    case 2:
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,HIGH);
    analogWrite(ENA,duty);
    break;
   
    case 3:
    digitalWrite(IN1,LOW);
    digitalWrite(IN2,LOW);
    analogWrite(ENA,duty);
    break;
  }
}


void motorB(int mode,int percent)
{
  int duty=map(percent,0,100,0,255);
  switch(mode)
  {
    case 0:
    digitalWrite(ENB,LOW);
    break;
   
    case 1:
    digitalWrite(IN3,HIGH);
    digitalWrite(IN4,LOW);
    analogWrite(ENB,duty);
    break;
   
    case 2:
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,HIGH);
    analogWrite(ENB,duty);
    break;
   
    case 3:
    digitalWrite(IN3,LOW);
    digitalWrite(IN4,LOW);
    analogWrite(ENB,duty);
    break;
  }
}


Servo Motors





one thing is missing in above diagram and that is potentiometer driver.Servo Motor Can be controlled by Arduino Voltage itself .as it has 3 wires as shown below

where + you can connect to arduino 5V or VCC pin in case you want high voltage
and signal pin to your output selected pin of arduino and
- to ground
you can get the program for servo motor in file-->example section of your arduino IDE itself.

Stepper Motor

these motors are gernally used when we want precission in rotations as far as i know there are two kinds of steeper motor motor one is unipolar and second one is bipolar .for more imformation about stepper motor and wire detections of stepper motor  you have to visit my blog .you can get the program for stepper motor in file-->example section of your arduino IDE itself.