Introduction:
In this tutorial we are going to make a Android phone controlled Obstacle avoiding robot using HC-SR04 and Arduino Mega.
Apparatus Required:
1.Arduino Mega
2.HC-SR04(Ultrasonic Sensor)
3. 4*100 RPM Gear Motor
4. LM 293D Motor Driver
5. Robot Chesis And Wheels
6. Wire
7. Bread Board
8. 12V Power Supply
Procedure:
1.First Of all Set the robot chesis as described in the video.
2.Connect the left side motors, right side motors parallaly as connected in the diagram bellow.
3. Now Connect all 4 HC-SR04 Ultrasonic Sensors Vcc and Ground in parallal as connected in the diagram below.
4.Now Connect the Echo and Trigger pins of the ultrasonic Sensors as mentioned below:
- Front Left Echo: Arduino Pin 32
- Front Left Trig: Arduino Pin 33
- Front Echo: Arduino Pin 34
- Front Trig: Arduino Pin 35
- Front Right Echo: Arduino Pin 36
- Front Right Trig: Arduino Pin 37
- Back Trig: Arduino Pin 30
- Back Echo: Arduino Pin 31
Connect the pins as the diagram below:
5. Now Connect the Vcc and Gnd Of the Bluetooth module to the Vcc and Gnd of the Arduino As Connected in the diagram below.
6. Now Connect the RX of the bluetooth module to the TX1 of the Arduino Mega and TX of Bluetooth Module to RX1 of the Arduino Mega.
7. Now Connect the Enable pin 1,2,3,4 Of LM293D to Pin Number 4,5,6,7 Of Arduino Mega Respectively and Vcc ang Gnd Of the Motor Driver to Vcc and Ground of the Arduino Mega As connected in the diagram below.
8.Now connect the Motors to the Motor Driver as connected in the diagram below.
9.Now connect the Gnd of the Buzzer to the Gnd Of the Arduino Mega and Vcc to the Pin 3 Of Arduino Mega.
10. Now Copy the Arduino Code below and upload the code to the arduino mega.
int front_left_trig=33;
int front_left_echo=32;
int front_trig=35;
int front_echo=34;
int front_right_trig=37;
int front_right_echo=36;
int back_trig=31;
int back_echo=30;
double front_left=0.0;
double front=0.0;
double front_right=0.0;
double back=0.0;
int motor_left1=7;
int motor_left2=6;
int motor_right1=5;
int motor_right2=4;
int horn=3;
double min_distance=20.00;
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600);
Serial.println("Welcome To Hybrid Robot V.0.01");
pinMode(front_left_trig,OUTPUT);
pinMode(front_left_echo,INPUT);
pinMode(front_trig,OUTPUT);
pinMode(front_echo,INPUT);
pinMode(front_right_trig,OUTPUT);
pinMode(front_right_echo,INPUT);
pinMode(back_trig,OUTPUT);
pinMode(back_echo,INPUT);
pinMode(motor_left1,OUTPUT);
pinMode(motor_left2,OUTPUT);
pinMode(motor_right1,OUTPUT);
pinMode(motor_right2,OUTPUT);
pinMode(horn,OUTPUT);
}
void loop() {
if(Serial1.available())
{
int data= Serial1.read();
Serial1.flush();
if(data=='F'){
goStraight();
}else if(data=='L'){
goLeft();
}else if(data=='R'){
goRight();
}else if(data=='B'){
goBack();
}else if(data=='V'){
hornOn();
}else if(data=='v'){
hornOff();
}else{
Stop();
}
}
}
double getDistance(int pin_echo,int pin_trig,String SensorName){
// Clears the trigPin
digitalWrite(pin_trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(pin_trig, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trig, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
double duration = pulseIn(pin_echo, HIGH);
// Calculating the distance
double distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
return distance;
}
void goLeft(){
front_left=getDistance(front_left_echo,front_left_trig,"Front Left");
if(front_left>min_distance){
//Serial.println("Go Left:");
digitalWrite(motor_left1,LOW);
digitalWrite(motor_left2,HIGH);
digitalWrite(motor_right1,LOW);
digitalWrite(motor_right2,LOW);
}else{
Stop();
hornOn();
delay(100);
hornOff();
}
}
void goRight(){
front_right=getDistance(front_right_echo,front_right_trig,"Front Right");
if(front_right>min_distance){
//Serial.println("Go Right:");
digitalWrite(motor_left1,LOW);
digitalWrite(motor_left2,LOW);
digitalWrite(motor_right1,HIGH);
digitalWrite(motor_right2,LOW);
}else{
Stop();
hornOn();
delay(100);
hornOff();
}
}
void goStraight(){
front=getDistance(front_echo,front_trig,"Front");
if(front>min_distance){
//Serial.println("Go Straight:");
digitalWrite(motor_left1,LOW);
digitalWrite(motor_left2,HIGH);
digitalWrite(motor_right1,HIGH);
digitalWrite(motor_right2,LOW);
}else{
Stop();
hornOn();
delay(100);
hornOff();
}
}
void goBack(){
back=getDistance(back_echo,back_trig,"Back");
if(back>min_distance){
//Serial.println("Go Back:");
digitalWrite(motor_left1,HIGH);
digitalWrite(motor_left2,LOW);
digitalWrite(motor_right1,LOW);
digitalWrite(motor_right2,HIGH);
}else{
Stop();
hornOn();
delay(100);
hornOff();
}
}
void Stop(){
//Serial.println("Stop");
digitalWrite(motor_left1,LOW);
digitalWrite(motor_left2,LOW);
digitalWrite(motor_right1,LOW);
digitalWrite(motor_right2,LOW);
}
void hornOn(){
digitalWrite(horn,HIGH);
}
void hornOff(){
digitalWrite(horn,LOW);
}
11. Now Download the Arduino Bluetooth Software from the Play Store and install it in the Phone, Connect 12v Power Supply to the Vcc, Gnd Of the Lm293D Motor Driver and Enjoy Your Android Phone Controlled Obstacle avoiding robot.
12, Here is the Video Tutorial of this post:
Thanks For Watching, Please Subscribe our channel For More Projects.
Leave Your Comments here !!!