---
# System prepended metadata

title: Obstacle avoidance with bluetooth control robot

---

---
title: 'Obstacle avoidance with bluetooth control robot'
disqus: hackmd
---

Bluetooth Controlled Obstacle Avoidance Robot 
===
## Table of Contents

[TOC]


## Introduction
This project uses mobile phone to communicate with a robot via bluetooth. It also senses obstacles and stops to avoid collision.
This robot senses the obstacle near time in real time and calculates the distance between the robot and the obstacle, and if the distance is not safe the car stops.



![](https://ibb.co/tcJXw3c.jpg)
<a href="https://ibb.co/tcJXw3c"><img src="https://i.ibb.co/BjCNHwj/Whats-App-Image-2021-12-22-at-8-06-33-PM.jpg" alt="Whats-App-Image-2021-12-22-at-8-06-33-PM" border="0"></a><br />



## Motivation

The main goal was to learn how the we can use ultrasonic sensor and implement it to solve real world problems and provide a safety system for alot of automobiles. So this is my approach towards developing a  safety mechanism for autonomous cars.

## Components used

- Arduino UNO
- HC-05 Bluetooth Module
- Ultrasonic Sensor - HC-SR04 (Generic)
- Rechargeable Battery, Lithium Ion
- H-Bridge motor drivers L298
-  Jumper Wires
-  DC Motor, 12 V
-  Resistor 330 ohm
-  5 mm LED: Red


## Theory

This project is designed to build a robot that automatically detects the obstacle on its path and guides itself whenever an obstacle comes ahead of it. This robotic vehicle is built, using Arduino UNO board. 

An ultrasonic sensor is used to detect any obstacle ahead of it. A motor driver IC and 2 DC motors are used for controlling the movement of the robot. A servo motor is also used in this project. 

The ultrasonic sensor is then mounted on the servo and by rotating the servo to different angles we will obtain the readings from the ultrasonic sensor in those angles. 

This will help the controller to detect the exact path to navigate. A Bluetooth module is also added to the project  in order to control the robot from your android phone when it is in manual mode.

---

## Working of the sensors used:






 **Bluetooth chipset: HC-05**
 
  
   
   
   ![](https://i.imgur.com/LbsdCBC.jpg)



-Wireless communication is swiftly replacing the wired connection when it comes to electronics and communication. Designed to replace cable connections HC-05 uses serial communication to communicate with the electronics. Usually, it is used to connect small devices like mobile phones using a short-range wireless connection to exchange files. It uses the 2.45GHz frequency band. The transfer rate of the data can vary up to 1Mbps and is in range of 10 meters. 

-The HC-05 module can be operated within 4-6V of power supply. It supports baud rate of 9600, 19200, 38400, 57600, etc. Most importantly it can be operated in Master-Slave mode which means it will neither send or receive data from external sources. 


 **Ultrasonic sensor HCSR-04**


![](https://i.imgur.com/g52opIp.jpg)



-The working principle of this module is simple. It sends an ultrasonic pulse out at 40kHz which travels through the air and if there is an obstacle or object, it will bounce back to the sensor.  By calculating the travel time and the speed of sound, the distance can be calculated.

Ultrasonic sensors are a great solution for the detection of clear objects.  For liquid level measurement, applications that use infrared sensors, for instance, struggle with this particular use case because of target translucence.

For presence detection, ultrasonic sensors detect objects regardless of the color, surface, or material (unless the material is very soft like wool, as it would absorb sound.) 



## Objectives of the Project

Navigate safely by avoiding obstacles comes ahead.
Detecting the exact path by checking the sensor readings in different angles.
Send status of the robot movement (using Bluetooth module) to the nearby android phone when the robot is in automatic mode.
Navigate in manual mode by receiving signals from the phone.


## Circuit diagram 
---
![](https://i.imgur.com/oibskUa.jpg)


## Code Explanation


At the beginning ,program will continuously check the reading of the ultrasonic sensor when the servo is at 90 degree. A library called “NewPing.h” is used here for taking the sensor reading. By calling the inbuilt function “sonar.ping_cm()” we will get distance between the sensor and the obstacle in centimeters.
One more library called “Servo.h” is used  in this program for controlling the servo. The function used  is “myservo.write(angle)”.

![](https://i.imgur.com/twwIXgp.png)




The position of the servo will be at 90 degree at the beginning.  
If the reading from the sensor at 90 degree position is less than 30cm, the robot  will stop moving forward.
Then it takes the readings at 60 and 120 degree positions by tilting the servo. These readings are then compared and will turns to the side where the reading is greater. If both these readings are also below 30cm, then the servo will turns to 30 and 150 degree positions. 
And will turns to the side which gives maximum reading. Again if the readings are still below 30cm, it will check the readings at 0 and 180 degree. 
If all the readings are less than 30 cm, the robot will moves backward and checks the reading again at 0 and 180 degree positions.  This process will continue until it takes a turn and the jumps to the starting of the program. 
Then it will checks the reading at 90 degree and the process will repeat again.

![](https://i.imgur.com/Ob4cjTN.png)


The program will continuously check the availability of serial data from the Bluetooth module.
If a character ‘M’ is received, the robot will switch to manual mode. Further movement of the robot will be according to the characters received via Bluetooth.
If character ‘A’ is received, the robot will again switch back to automatic mode.

![](https://i.imgur.com/Moss9JD.png)



## Code

[Code download link](https://github.com/MohitVazirani/Bluetooth-and-obstacle-avoidance-robot)


## Conclusion 
 
 The use of ultrasonic sensor can be implemented in real world application of a safety mechanism automobiles, and that can be done with basic knowledge of how these components function.





