Introduction: Infrared Sensor Guided Arduino Controlled L293D Robot ( Part 2 )
Here is part 2 of the Arduino controlled L293D Robot.
Part 2 includes Infrared Sensors.This is part of a series of instructables leading to a Line follower Robot.
QRD1114 Infrared Sensor connected to Arduino
Step 1: Wiring the Infrared Sensor to the Arduino
Negative from the IR Sensor connects to Negative on the Breadboard.
Middle Pin ( Positive ) from the IR Sensor connects to Positive on the Breadboard.
Last Pin ( Input ) from the IR Sensor connects to Analog input on Arduino Board.
In this case I had the Input from the IR Sensor connecting to Pin 3 on the Analog Input on the Arduino.
Step 2: Basic Code to Read the IR Sensor
int analogPin = 3; // Infrared Sensor (Right lead) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup()
{
Serial.begin(9600); // setup serial
}
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
Step 3: Code to Drive Forward When IR Sensor Value Is Less Than 100
// This is the basic code which runs a loop that makes the Robot go straight forward when the Infrared value is below 100.
int analogPin = 3; // Infrared Sensor (Right lead) connected to analog pin 3
// Infrared Sensor(middle Lead) connected to +5V
// Infrared Sensor(Left Lead) connected to ground
int val = 0; // variable to store the value read
int LeftMotorForward = 10; // Pin 10 has Left Motor connected on Arduino boards.
int LeftMotorReverse = 9; // Pin 9 has Left Motor connected on Arduino boards.
int RightMotorForward = 12; // Pin 12 has Right Motor connected on Arduino boards.
int RightMotorReverse = 13; // Pin 13 has Right Motor connected on Arduino boards.
//------------------------------------------------------------------------------------------------
void setup()
{
pinMode(LeftMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(LeftMotorReverse, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorReverse, OUTPUT); // initialize the pin as an output.
Serial.begin(9600); // setup serial
}
// The following Loop is to make the Robot go staright when the Infrared value is below 100
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
if (val <100)
// If the read value is < 100 then run the code below which will turn both Left and Right Motor on making the Robot go straight.
{
digitalWrite(RightMotorForward, HIGH); // turn the Right Motor ON
digitalWrite(LeftMotorForward, HIGH); // turn the Left Motor ON
delay(10000); // wait for 10 seconds
digitalWrite(RightMotorForward,LOW); // turn the Right Motor OFF
digitalWrite(LeftMotorForward, LOW); // turn the Left Motor OFF
delay(10000); // wait for 10 seconds
}
}
int analogPin = 3; // Infrared Sensor (Right lead) connected to analog pin 3
// Infrared Sensor(middle Lead) connected to +5V
// Infrared Sensor(Left Lead) connected to ground
int val = 0; // variable to store the value read
int LeftMotorForward = 10; // Pin 10 has Left Motor connected on Arduino boards.
int LeftMotorReverse = 9; // Pin 9 has Left Motor connected on Arduino boards.
int RightMotorForward = 12; // Pin 12 has Right Motor connected on Arduino boards.
int RightMotorReverse = 13; // Pin 13 has Right Motor connected on Arduino boards.
//------------------------------------------------------------------------------------------------
void setup()
{
pinMode(LeftMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorForward, OUTPUT); // initialize the pin as an output.
pinMode(LeftMotorReverse, OUTPUT); // initialize the pin as an output.
pinMode(RightMotorReverse, OUTPUT); // initialize the pin as an output.
Serial.begin(9600); // setup serial
}
// The following Loop is to make the Robot go staright when the Infrared value is below 100
void loop()
{
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
if (val <100)
// If the read value is < 100 then run the code below which will turn both Left and Right Motor on making the Robot go straight.
{
digitalWrite(RightMotorForward, HIGH); // turn the Right Motor ON
digitalWrite(LeftMotorForward, HIGH); // turn the Left Motor ON
delay(10000); // wait for 10 seconds
digitalWrite(RightMotorForward,LOW); // turn the Right Motor OFF
digitalWrite(LeftMotorForward, LOW); // turn the Left Motor OFF
delay(10000); // wait for 10 seconds
}
}
Step 4: IR Value Is Greater Than 100
When IR Value is greater than 100 then the Motors are off.
Step 5: IR Value Is Less Than 100
When IR Value is Less than 100 then the Motors are turned on and the Robot drives forward for 10 seconds.