Showing posts with label L293D. Show all posts
Showing posts with label L293D. Show all posts

Monday, 12 October 2015

Making A Line Following Robot

The working of an LFR , is very simple. It has two sensors placed on either side of the line. If the left sensor comes on the black line , the left motor should stop , for it to correct its position . Similarly if the right sensor comes above the black line , the right motor should stop moving.

How is the black line detected : 
Black and white colours have different optic properties. While black absorbs light of all colours incident on it , white reflects most of these back. So naturally when the sensor is above the white portion , it will receive a higher intensity of light and thus will provide a higher value.
Our task here then is to make a sensor that can convert light intensity , into a voltage reading , which the Arduino can read via its analog input pins.
We will use a photo resistor , which is a device whose resistance to current changes with the incidence of light intensity.

Making the sensor 

The sensor is a basic voltage divider circuit. The photo-resistor is connected in series with a 100 ohm resistor. On end of this combination is connected to +5v and other to ground. The junction is connected to any of the analog pins on the arduino. A resistor is added to this connection to prevent damage to the arduino due to large currents. The junction potential changes when the resistance of the photo resistor changes. This change in potential is measured by the Arduino and is taken advantage of .







Created using Fritzing
Wiring The Motors.

Check my previous post on running motors using the Arduino.
http://crazzycircuits.blogspot.com/2015/07/how-to-run-motor-using-l293d-double-h.html




The Program:

#define M1pin1 9
#define M1pin2 8
#define M2pin1 10
#define M2pin2 11
#define Sensor1 A0
#define Sensor2 A1

int sensor_value1,sensor_value2,diff;

void setup()
{

 Serial.begin (9600);
 pinMode(M1pin1, OUTPUT);
 pinMode(M1pin2, OUTPUT);
 pinMode(M2pin1, OUTPUT);
 pinMode(M2pin2, OUTPUT);

 sensor_value1 = analogRead(Sensor1);
 sensor_value2=analogRead(Sensor2);
 diff= sensor_value2-sensor_value1;  
// We account for this difference to equalise both sensor inputs values for the white portion. This   little trick eliminates the need for calibration.
}

void loop()
{

  sensor_value1 = analogRead(Sensor1)+diff;
  sensor_value2=analogRead(Sensor2);
  if(sensor_value2-sensor_value1>1)  //Sensor values differ when either sensor is on the black   line
     OFF(M2pin1,M2pin2);
  else
     ON(M2pin1,M2pin2);
   
   
 // ______________________
 
  if(sensor_value1-sensor_value2>1)
     OFF(M1pin1,M1pin2);
  else
      ON(M1pin1,M1pin2);
 
 
  //Serial.println(sensor_value1);
  //Serial.println(sensor_value2);
 
}

void ON(int Mpin1,int Mpin2) // runs motor 
{
  digitalWrite(Mpin1, HIGH);
  digitalWrite(Mpin2, LOW);
  
}
void OFF(int Mpin1,int Mpin2) // Stops motor
{
  digitalWrite(Mpin1, LOW);
  digitalWrite(Mpin2, LOW);
}
//End of Program
...................................................................................................................................................................

Thursday, 23 July 2015

How to run a motor using L293D (double H-bridge) and interfacing it with the Arduino


Motor drivers are needed because the pins of a micro-controller cannot source the required current to run a motor.
The arduino can provide a maximum 200mA per pin and can sink a total of 400mA current.

You will damage the micro - controller or pins if you try to draw more current. A motor cannot run on such small current. Thus an amplifier circuit or a relay is needed. To drive the motor in both directions , an H-bridge is needed.

Lo and behold ! Let me present to you L293D. It is a transistor implementation of both an amplifier and an H-bridge , in other words , the solution to our problem.

This is what an h-bridge looks like:

 

This is the pin layout of L293D:




















It can run two motors.
Vss needs to be connected to 5v for either h-bridge to run.
Enable1, if connected to 5v enables the left h- bridge.
Enable2, is for right H- bridge.
Gnd pins go to ground. 
Vs  is the voltage you want to give the motor.


 WIRE IT UP


Created Using Fritzing



THE PROGRAM


#define M1pin1 9  
#define M1pin2 10
void setup()
{
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
}
void loop()
{
 Clockwise();  // motor turns in clockwise direction
 delay(2000);  // Arduino sleeps for 2 seconds
 AntiClockwise(); // motor turns in anticlockwise direction
 delay(2000);
}

void Clockwise()
{
    digitalWrite(M1pin1, HIGH); // digitalWrite is a predefined function to control pin state.
    digitalWrite(M1pin2, LOW);
}  
void AntiClockwise()
{
 
    digitalWrite(M1pin2, HIGH);
    digitalWrite(M1pin1, LOW);
}



If You Are looking for speed control :
// You need to connect input wires to a pmw enabled pins on Arduino. These are identified by ~ sign on the arduino board. Notice 9 has a ~ before it on the board.


#define M1pin1 9       
#define M1pin2 10
int pmw=128;
void setup()
{
pinMode(M1pin1, OUTPUT);
pinMode(M1pin2, OUTPUT);
}
void loop()
{
 Clockwise();  // motor turns in clockwise direction
 delay(2000);  // Arduino sleeps for 2 seconds
 AntiClockwise(); // motor turns in anticlockwise direction
 delay(2000);
}

void Clockwise()
{
    analogWrite(M1pin1, pmw); // analogWrite is a predefined function to control pin state.
    digitalWrite(M1pin2, LOW);
}  
void AntiClockwise()
{
 
    analogWrite(M1pin2, pmw);
    digitalWrite(M1pin1, LOW);
}

//pmw can take values between 0 and 256. At 256, it will run at max speed. At 128, it will run at half the speed.