In this tutorial we make simple project on Obstacle Avoiding Robot using Arduino.
It is an Arduino based robot car that uses Ultrasonic Sensors to avoid obstacles.
I hope to do step by step guide on making this robot in very easy way.
Here’s a video of the build and the car running. Read on for the full step by step instructions
Thanks to NextPCBfor helping me with this project.
NextPCB is one of the most experienced PCB manufacturers globally. It has specialized in the PCB and assembly industry for over 15 years. Not only could NextPCB provide the most innovative printed circuit boards and assembly technologies in the highest quality standards, the fastest delivery turnaround as fast as 24 hours.
If you have a PCB project, please visit their website and get exciting discounts and coupons!
Only 0$ for 5-10pcs PCB Prototypes: https://www.nextpcb.com/?code=Kumar
Register and get $100 from NextPCB: https://www.nextpcb.com/register?code=Kumar
See more info about PCB Assembly Capabilities: https://www.nextpcb.com/pcb-assembly-services
Step 1: Hardware Required- Arduino Uno
- Ultrasonic HC – SR04 sensor
- Motor Driver IC – L293D
- Servo Motor (Tower Pro SG90)
- Geared Motors x 2
- Robot Chassis
- 18650 Battery
- battery Holders
Arduino
Arduino Uno is an cheapest Microcontroller based prototyping board. It is an open source electronic prototyping platform th can be used with various sensors and actuators.
HC – SR04
Ultrasonic Range Finder Sensor. It is a non-contact based distance measurement system and can measure distance of 2cm to 4m.
The object sensing is done by an ultrasonic sensor that uses ultrasonic sound waves to measure the distance to an object by timing how long it takes.
Distance = Pulse Time x Speed of Sound In Air / 2
L293D motor driver shield
It is a motor driver which can provide bi-directional drive current for two motors.
Servo Motor
The Tower Pro SG90 is a simple Servo Motor which can rotate 90 degrees in each direction (approximately 180 degrees in total).
Step 3: WIRING COMPONENTSMake the connection as given in the following diagram. The Power Jumper on the motor shield works as Switch to motor it will be useful while debugging.
Step 4: Assembling the Robot CarI designed a simple chassis for the car to be 3D printed. I printed mine using yellow PLA.
- Once you’ve got all of your components, let’s start assembling the obstacle avoiding robot car.
- If you are using a rechargeable battery, put the battery into place in the middle chassis
- Next, screw your Arduino onto your top chassis
- Then plug your motor driver shield into your Arduino
- motor is connected to the correct pair of terminals, the front motors to the front terminals, and the back motors to the back terminals.
- Plug the servo into the servo 1 header pins on the shield with the signal wire facing inwards
Then plug four wires into the sensor and over to the shield. Plug the ground and Vcc wires into the ground and 5V pins on the shield and then the trigger pin to pin 2 and the echo pin to pin 13.
Lastly, put the four wheels onto the geared motors and the car is now complete.
Step 6: Programming the ArduinoNow that the obstacle avoiding robot car is complete, let’s have a look at the code.
Download and Install the Arduino Desktop IDE
windows - https://www.arduino.cc/en/Guide/Windows
Mac OS X - https://www.arduino.cc/en/Guide/MacOSX
Linux - https://www.arduino.cc/en/Guide/Linux
We start by importing two libraries, one to control the motor shield and one to control the servo.
Once you have downloaded this library. Extract this and copy the folder to My Documents/Arduino/Libraries or other similar locations.
Upload the provided code on your robot...------------------------------------------------------- Download All Files : -------------------------------------------------------
<pre title="Obstacle Avoiding Robot Car">#include <AFMotor.h> //Import library to control motor shield
#include <Servo.h> //Import library to control the servo
AF_DCMotor rightBack(1); //Create an object to control each motor
AF_DCMotor rightFront(2);
AF_DCMotor leftFront(3);
AF_DCMotor leftBack(4);
Servo servoLook; //Create an object to control the servo
byte trig = 2; //Assign the ultrasonic sensor pins
byte echo = 13;
byte maxDist = 150; //Maximum sensing distance (Objects further than this distance are ignored)
byte stopDist = 50; //Minimum distance from an object to stop in cm
float timeOut = 2*(maxDist+10)/100/340*1000000; //Maximum time to wait for a return signal
byte motorSpeed = 55; //The maximum motor speed
int motorOffset = 10; //Factor to account for one side being more powerful
int turnSpeed = 50; //Amount to add to motor speed when turning
void setup()
{
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE); //Ensure all motors are stopped
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
servoLook.attach(10); //Assign the servo pin
pinMode(trig,OUTPUT); //Assign ultrasonic sensor pin modes
pinMode(echo,INPUT);
}
void loop()
{
servoLook.write(90); //Set the servo to look straight ahead
delay(750);
int distance = getDistance(); //Check that there are no objects ahead
if(distance >= stopDist) //If there are no objects within the stopping distance, move forward
{
moveForward();
}
while(distance >= stopDist) //Keep checking the object distance until it is within the minimum stopping distance
{
distance = getDistance();
delay(250);
}
stopMove(); //Stop the motors
int turnDir = checkDirection(); //Check the left and right object distances and get the turning instruction
Serial.print(turnDir);
switch (turnDir) //Turn left, turn around or turn right depending on the instruction
{
case 0: //Turn left
turnLeft (400);
break;
case 1: //Turn around
turnLeft (700);
break;
case 2: //Turn right
turnRight (400);
break;
}
}
void accelerate() //Function to accelerate the motors from 0 to full speed
{
for (int i=0; i<motorSpeed; i++) //Loop from 0 to full speed
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}
void decelerate() //Function to decelerate the motors from full speed to zero
{
for (int i=motorSpeed; i!=0; i--) //Loop from full speed to 0
{
rightBack.setSpeed(i); //Set the motors to the current loop speed
rightFront.setSpeed(i);
leftFront.setSpeed(i+motorOffset);
leftBack.setSpeed(i+motorOffset);
delay(10);
}
}
void moveForward() //Set all motors to run forward
{
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
}
void stopMove() //Set all motors to stop
{
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
void turnLeft(int duration) //Set motors to turn left for the specified duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(BACKWARD);
leftBack.run(BACKWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
void turnRight(int duration) //Set motors to turn right for the specified duration then stop
{
rightBack.setSpeed(motorSpeed+turnSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed+turnSpeed);
leftFront.setSpeed(motorSpeed+motorOffset+turnSpeed);
leftBack.setSpeed(motorSpeed+motorOffset+turnSpeed);
rightBack.run(BACKWARD);
rightFront.run(BACKWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
delay(duration);
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
int getDistance() //Measure the distance to an object
{
unsigned long pulseTime; //Create a variable to store the pulse travel time
int distance; //Create a variable to store the calculated distance
digitalWrite(trig, HIGH); //Generate a 10 microsecond pulse
delayMicroseconds(10);
digitalWrite(trig, LOW);
pulseTime = pulseIn(echo, HIGH, timeOut); //Measure the time for the pulse to return
distance = (float)pulseTime * 340 / 2 / 10000; //Calculate the object distance based on the pulse time
return distance;
}
int checkDirection() //Check the left and right directions and decide which way to turn
{
int distances [2] = {0,0}; //Left and right distances
int turnDir = 1; //Direction to turn, 0 left, 1 reverse, 2 right
servoLook.write(180); //Turn servo to look left
delay(500);
distances [0] = getDistance(); //Get the left object distance
servoLook.write(0); //Turn servo to look right
delay(1000);
distances [1] = getDistance(); //Get the right object distance
if (distances[0]>=200 && distances[1]>=200) //If both directions are clear, turn left
turnDir = 0;
else if (distances[0]<=stopDist && distances[1]<=stopDist) //If both directions are blocked, turn around
turnDir = 1;
else if (distances[0]>=distances[1]) //If left has more space, turn left
turnDir = 0;
else if (distances[0]<distances[1]) //If right has more space, turn right
turnDir = 2;
return turnDir;
}
It has following predefined function for robot motion-
1. forward() : forward movement of robot.
2. backward() : backward movement of robot.
3. turn_left() : for turning left.
4. turn_right(): for turning right.
5. halt() : for stopping robot.
Step 7: Add Power SupplyConnect the Lipo battery to the L293d motor driver as follows:
Lipo battery (+) → +12V Lipo battery (- ) → GND
Step 8: Testing the Robot...Here is Testing the Robot video
if you like this project please follow me and subscribe my channel.
Thank You
Comments