Obstacle Avoidance Robot using Ultrasonic and evive
We have built an obstacle avoiding robot using evive. Unlike others, we have mounted our ultrasonic sensor on a servo. So after detecting a collision, the robot analyses which direction is the best to go using distance at various angles from ultrasonic sensor.
- Two motors
- Ultrasonic sensor
- 12 V Battery
- A robot car body (like the one in the image)
First of all we will see the building part of it, then we will proceed to the coding part. You can see the image - how our robot looks like. This structure actually makes the work easier.
All the CAD files can be downloaded from our GrabCAD profile.
We have three devices to be connected to the evive.
To connect the two motors of the robot insert the wires of motors in the slot made in evive that are M1 and M2 in the correct orientation.
Now, we have to connect the servo motor in evive, in this we have not used the inbuilt servo pinouts because it uses the same pin as that of motor so instead we have used digital pin 9 for communication of servo and other power related pins to the arduino for example VCC to 5 volt and GND to ground.
To connect the ultrasonic sensor just connect the VCC to 5 volt, GND to ground and for trig pin and echo pin we have used the digital pins 3 and 2 respectively.
Please have a detailed look at the evive's schematic pinout diagram, so that you can easily connect motors and servos.
We have mounted an ultrasonic sensor atop a servo. When the ultrasonic sensor is triggered, it sends out a pulse and simultaneously starts an internal clock. When the pulse hits an obstacle, it gets reflected back to the sensor. The sensor calculates the time difference between the sending and receiving of the pulse. Since the speed of the pulse in air (let it be 'v') and the time gap ('t') is known, the distance to the obstacle can be calculated as-
Time for total trip = t
Time for reaching the obstacle = t/2
=> distance = v*(t/2)
This value has been calculated in our code and can be stored and manipulated accordingly.
- Normally, while moving forward, the ultrasonic sensor 'looks' in the front, i.e. the sensor provides the arduino the value of the nearest distance that an obstacle is present in front of it.
- As soon as this value becomes less than a minimum threshold, we stop the bot.
- Then, we turn the ultrasonic sensor, once to the left and then to the right.
- Each time, we obtain the distance of the nearest obstacle in the respective direction.
- We store both of these values and calculate the maximum of these values.
- Then, we turn our bot in that particular direction. This is the optimum direction, where the bot can travel the farthest without needing to stop and check again.
- The robot starts moving forward, until another obstacle comes in front of it.
Due to limitations of present servo library, we cannot use the servo along with motor drivers. So we have modified the servo.cpp file in Arduino/Libraries/Servo. Download the Servo library code here and replace the original library in Arduino IDE using this.
NOTE: evive uses PWM Pins 44, 45 for motors and the same are used for servos, so you CANNOT use plug and play interface for servo motor. You will have to use any of the 3 to 13 pin for connecting the signal wire of servo. ALSO since default servo library uses TIMER 5 (aka PWM pins 44, 45) for controlling the servos. SO PLEASE REPLACE THE DEFAULT SERVO LIBRARY IN ARDUINO LIBRARIES using the attached file "Servo.rar". Please have a look atSchematic PinOut Diagram of evive here.
Now we need to include the motor library. Please find it here. It is very extensive in a way that it makes controlling motor very easy. Please look the motor.h for understanding the available functions in this library. Screenshots are provided for describing how to add a library to arduino IDE.
evive also have inbuilt 1.8" SPI interfaced TFT screen (explore the details here). You can check the internal wiring for making/updating your Arduino sketches atthis link. The following are the connections:
- VCC to 3.3V
- GND to evive's GND
- CS to arduino pin 48
- RESET to arduino pin 47
- D/C (or A0) arduino pin 49
- SDA to MOSI (For Arduino Mega on Pin 51)
- SCL (or SDA) to SCLK (or SCK) (For Arduino Mega on Pin 52)
- LED (or LEDA) to 3.3V or as per brightness level required.
The most important thing is that while the robot moves, you can display and monitor the distance, PWM etc on the screen. Hence it reduces the dependence on Arduino Serial Monitor via using USB cable with your laptop or desktop. It makes the debugging process wire free.
We have made the Arduino IDE ready for our robot. Although its not necessary to use the motor library, then you can yourself do the pinMode settings and control the motor related pins yourself. So, its recommended to use the motor library.
Download the well documented codes from here to make your own obstacle avoidance robot.