Monday, March 7, 2016

L298 Motor Driver with Arduino MEGA2560

Arduino MEGA 2560

This is my first blog about the Mega2560. It has become a very useful tool for rapid prototyping and making simple project. When I say simple I mean simple but useful. 
I recently connected a DC Motor that required a potentiometer as a sensor and a motor driver (L298). 

Where to buy:
Arduino, you can buy anywhere nowadays. I usually buy mine from sparkfun $46
(https://www.sparkfun.com/products/11061)

L298 Driver: I also buy this from Sparkfun $35
(https://www.sparkfun.com/products/9670)
Connect the motor signals according to the Arduino declarations below:

//////////////////////////   Motor Driver #1 Signals  /////////////////////////////
const int MOT1_EN = 26;   
const int MOT1_IN1 = 22;  
const int MOT1_IN2 = 24;  
const int MOT1_CSA = A4;  
const int MOT1_CSB = A5;  

const int POT = A0;                          

Initialize the motor signals before using.

      pinMode(MOT1_IN1, OUTPUT);
      pinMode(MOT1_IN2, OUTPUT);
      pinMode(MOT1_EN, OUTPUT);
      pinMode(MOT1_CSA, INPUT);
      pinMode(MOT1_CSB, INPUT);

Create subroutine to move the motors Clockwise and counter clock wise.

void Motor1_CC(){
    digitalWrite(MOT1_EN, HIGH);
    delay(1);  
    digitalWrite(MOT1_IN1, HIGH);
    digitalWrite(MOT1_IN2, LOW);
}

void Motor1_CCW(){
    digitalWrite(MOT1_EN, HIGH);
    delay(1);  
    digitalWrite(MOT1_IN1, LOW);
    digitalWrite(MOT1_IN2, HIGH);
}

Write routines to break and coast motors

void Motor1_FastBrake(){
  //this routine will brake the motor A quickly
    digitalWrite(MOT1_EN, HIGH);
    delay(1);  
    digitalWrite(MOT1_IN1, LOW);
    digitalWrite(MOT1_IN2, LOW);
}

void Motor1_Coast(){
  //this routine will coast Motor A
    digitalWrite(MOT1_EN, LOW);
    delay(1);  
    digitalWrite(MOT1_IN1, LOW);   //irrelevent
    digitalWrite(MOT1_IN2, LOW);   //irrelevent
}

Now we are ready to write our code according to the project requirements. I would read the analog value for the potentiometer and move the motors using the routines as needed.
Enjoy!


No comments:

Post a Comment