A self-controlled Robotic Car using Arduino.
This robotic car uses Ultra Sonic Sensor to detect Obstacles which are in front of it, whenever it detects obstacles then its Ultra Sonic Sensor moves in both directions Right and Left to calculates the best possible distance to move freely.
It's Ultra Sonic Sensor range is up to 150 cms.
2. Demonstration3. Steps for making this Robotic Car
Import Servo.h and NewPing.h Library in Arduino IDE.
// Library
#include <Servo.h> // Include Servo Library
#include <NewPing.h> // Include Newping Library
Initialize Pins
// L298N Control Pins
const int LeftMotorForward = 4;
const int LeftMotorBackward = 5;
const int RightMotorForward = 6;
const int RightMotorBackward = 7;
const int LEDext = 1;
const int Buzzer = 0;
#define TRIGGER_PIN A1 // Arduino pin to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A2 // Arduino pin to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 250 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 250cm.
Create objects and variables
Servo servo_motor; // Servo's name
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
boolean goesForward = false;
int distance = 50;
Write Setup Part of Arduino code
void setup()
{
// Set L298N Control Pins as Output
pinMode(RightMotorForward, OUTPUT);
pinMode(LeftMotorForward, OUTPUT);
pinMode(LeftMotorBackward, OUTPUT);
pinMode(RightMotorBackward, OUTPUT);
pinMode(LEDext, OUTPUT); //set led as output
pinMode(Buzzer, OUTPUT); //set buzzer as output
servo_motor.attach(9); // Attachs the servo on pin 9 to servo object.
servo_motor.write(115); // Set at 115 degrees.
delay(2000); // Wait for 2s.
distance = readPing(); // Get Ping Distance.
delay(100); // Wait for 100ms.
}
Write Void Loop Part of Arduino Code
void loop()
{
int distanceRight = 0; //Initialize right side distance
int distanceLeft = 0; //Initialize left side distance
delay(50);
if (distance <= 30) //If distance of obstacle less than 30 cm from robot
{
Stop(); //call stop function to stop the robot
digitalWrite(LEDext, HIGH); //Turn led ON
digitalWrite(Buzzer, HIGH); //Turn Buzzer ON
delay(300); //wait for 300ms
moveBackward(); //call moveBackward function to move robot in backward direction
delay(400); //wait for 400ms
Stop(); //call stop function to stop the robot
delay(300); //wait for 300ms
distanceRight = lookRight(); //call lookRight function to save distance in distanceRight variable
delay(300); //wait for 300ms
distanceLeft = lookLeft(); //call lookLeft function to save distance in distanceLeft variable
delay(300); //wait for 300ms
if (distanceRight >= distanceLeft) //If distance of right greater or equall to distance of left
{
turnRight(); //call function to turn right robot
delay(300); //wait for 300ms
Stop(); //call stop function to stop robot
}
else //else
{
turnLeft(); //call function to turn left robot
delay(300); //wait for 300ms
Stop(); //call stop function to stop robot
}
}
else //else
{
moveForward(); //call moveForward function to move robot in forward direction
}
distance = readPing(); //call readPing function to calculate Distance
}
Make Function to calculate Right Side Distance
int lookRight() // lookRight Function for Servo Motor
{
servo_motor.write(0); //make servo position at 0 degree
delay(500); //wait for 500ms
int distance = readPing(); //read distance
delay(100); //wait for 100ms
servo_motor.write(90); //make servo position 90 degree
return distance; //return distance whenever lookRight function is called
}
Make Function to calculate Left Side Distance
int lookLeft() // lookLeft Function for Servo Motor
{
servo_motor.write(180); //make servo position at 0 degree
delay(500); //wait for 500ms
int distance = readPing(); //read distance
delay(100); //wait for 100ms
servo_motor.write(90); //make servo position 90 degree
return distance; //return distance whenever lookLeft function is called
}
Make Function to take Distance from Ultra Sonic Sensor
int readPing() // readPing Function for Ultrasonic Sensor.
{
delay(100); // Wait 100ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
int cm = sonar.ping_cm(); //Send ping, get ping distance in centimeters (cm).
if (cm==0)
{
cm=250;
}
return cm; //return distance whenever readPing function is called
}
Make Function to Stop the Robot
void Stop() // Stop Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, LOW);
}
Make Function to move Robot in forwarding Direction
void moveForward() // Move Forward Function for Motor Driver.
{
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
digitalWrite(LEDext, LOW);
digitalWrite(Buzzer, LOW);
}
Make Function to move Robot in backward Direction
void moveBackward() // Move Backward Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
Make Function to move Robot in Right Side Direction
void turnRight() // Turn Right Function for Motor Driver.
{
digitalWrite(RightMotorForward, LOW);
digitalWrite(RightMotorBackward, HIGH);
digitalWrite(LeftMotorForward, HIGH);
digitalWrite(LeftMotorBackward, LOW);
}
Make Function to move Robot in Left Side Direction
void turnLeft() // Turn Left Function for Motor Driver.
{
digitalWrite(RightMotorForward, HIGH);
digitalWrite(RightMotorBackward, LOW);
digitalWrite(LeftMotorForward, LOW);
digitalWrite(LeftMotorBackward, HIGH);
}
Upload code to your Arduino Board.
4. Make connections of all components as shown in the Schematic.
Comments
Please log in or sign up to comment.