Introduction: Utrasonic Avoidance Robot Using Arduino
In this tutorial, I will show you how to make your own obstacle avoiding robot!
We will use the Arduino UNO board and an ultrasonic sensor. If the robot detects an object in front of it, with the help of a small servo motor, it scans the area left and right in order to find the best way to turn. It has also a notification LED, a buzzer to play a tone when an object is detected and a button for changing the function of the robot (stopped/moving forward).
It's very easy to make it!
Step 1: Things Nedded to Make !
For this project you will need:
- Arduino UNO (buy it from gearbest.com)
- Mini breadboard (buy it from gearbest.com)
- L298 motor driver module (buy it from gearbest.com)
- 2x dc motors with wheels HC-SR04 ultrasonic sensor (buy it from gearbest.com)
- Micro servo motor (buy it from gearbest.com)
- Button Red LED220 Ohm resistor9V battery holder (with or without power jack)
- 8 spacers (male-female),
- 8 nuts and 8 screws you will also need one big (metal)
paperclip and a bead to make the back supporting wheel.
For the robot base, I used a Acryllic Chasis from Aliexpress. You can also use a piece of wood or metal (or two electrical Plates ).
The cost of the entire project is about 20$!
Tools: Drill machine super glue crew driver hot gun glue (optional) Power:
We will use a 9V battery to power on our robot because it's small and cheap, but it's not very powerful and will be empty after about one hour. Consider if you want to use a rechargeable battery pack (min 6V, max 7V) that will be more powerful but it will be also more expensive and bigger than the 9V battery.
SubscribeOur YouTube Channel Click Here
Step 2: Understanding Concepts
The goal is to make the robot aware of obstacles in front of him, so he
can change direction and avoid them. In the previous article we made the robot move – now we will give him some autonomy.
HC-SR04 is a circuit able to measure a distance to objects up to 4 meters using ultrasonic waves. It sends a ping (like a submarine) and measures the time (in microseconds) between sending and receiving anything back. This time is then divided by 2 as the wave travels back and forth. And then divide by 29 to get a distance in centimeters (or 74 for inches), because sound travels 29.4µs per centimeter (340 m/s). The sensor is very accurate with ~3 mm tolerance and easy to integrate with Arduino.
Interfacing Ultrasonic Sensor with AVR Microcontroller
Any autonomous robot should have an obstacle avoiding and a distance measuring sensor attached. An IR transceiver pair or a grayscale sensor can easily work for obstacle detection in the range of 1cm-10cm. IR rangefinders (for example those from sharp) can measure a distance to the nearest obstacle with the range up to 100cm. However, IR sensors are affected by sunlight and other light sources. IR rangefinders have less range and also expensive for what it does. Ultrasonic sensors (also known as ultrasonic proximity sensors or sonar for the geeks) do both these tasks at a reasonable cost and exceptional accuracy. The range is anything between 3 cm to 350cm with ~3mm accuracy. Tying one of these ultrasonic sensors into our robot, it can act as both an obstacle avoider and a distance measuring sensor.
“Ultrasonic” sound refers to anything above the frequencies of audible
sound, and nominally includes anything over 20,000 Hz, or 20kHz! Inexpensive Ultrasonic sensors used for robotics generally work in a range of 40 kHz to 250 kHz while those used in medical equipment’s goes up to 10Mhz.
Step 3: Tools Needed
- Needle nose pliers
- Wire Stripper
- Wire Cutter
- Glue Gun
A Multimeter is actually a simple device used primarily to measure voltage and resistance and to determine if a circuit is closed. Similar to debugging computer code, the Multimeter helps you “debug” your electronic circuits.
A readily available supply of thin wood and/or Plexiglas to make the mechanical frame is very useful. Metals such as aluminum and steel are often restricted to those with access to a machine shop although thin aluminum can be cut with shears and bent by hand. Mechanical frames can even be built from household items such as plastic containers.
Although other materials such as plastics (aside from Plexiglas), or more exotic materials like fiberglass and carbon fiber are possible, they will not be considered in this guide. Several manufacturers have noted that it is not easy for most hobbyists to produce their own mechanical parts and have created modular mechanical parts. A leader in this is Lynxmotion which offers a wide range of robotic designs as well as the parts needed to make your own custom robots.
Screwdrivers and pliers of various types and sizes (including jeweler’s toolset: small screwdrivers commonly available at dollar stores) are necessary. A drill (preferably a drill press for straight holes) is also important. A hand saw for cutting building materials (or a router) is also an important asset. If budget allows, a small tabletop band saw ($200 range) is definitely a tool to consider.
A solderless breadboard allows you to optimize your layout and connect components with ease. Along with a solderless breadboard, you should purchase a pre-formed jumper wire kit which consists of pre-cut and bent wires intended to be used with a solderless breadboard. This makes connections very easy.
Small screwdriver set
These small screwdrivers are necessary when working with electronics. Don’t force them too much though – their size makes them more fragile.
Regular screwdriver set
All workshops need a multi-tool or toolset which includes flat / Phillips and other screwdriver heads.
Needle nose pliers
set of needle nose pliers is incredibly useful when working with small components and parts and is a very inexpensive addition to your toolbox. These are different from regular pliers because they come to a point which can get into small areas.
You are planning to cut any wires, a wire stripper will save you considerable time and effort. A wire stripper, when used properly, will only remove a cable insulation and will not produce any kinks or damage the conductors. The other alternative to a wire stripper is a pair of scissors, though the end result can be messy. Scissors, ruler, pen, marker pencil, Exacto knife (or other handheld cutting tool)These are essentials in any office.
Step 4: Cocepts for Codeing AVR
Calculating the speed of sound relative to ultrasonic sensors
Little math, but don’t be scared. It is simpler than you think.
The speed of sound in dry air at room temperature (~20°C) = 343 meters/second
For the sound wave to hit and make a round trip to the nearby object is = 343/2 = 171.5 m/since the maximum range of a cheap ultrasonic sensor is not more than 5 meters (round trip), it would make more sense to change the units to centimeters and microseconds.
1 Meter = 100 centimeter
1 second = 10^6 microseconds = (s / 171.5) x (m / 100 cm) x ((1x10^6)/s) = (1/171.5) x (1/100) x (1000000/1) = 58.30903790087464 us/cm = 58.31 us/cm (rounding off to two digits to make calculations easier)
Therefore, time taken for a pulse to travel to an object and bounce back 1 centimeter is 58.31 microseconds.
the little background on AVR clock cycles
It takes an entirely different chapter to understand AVR clock cycles, but we will briefly understand how it works to make our calculations easier
For our example, we will use AVR Draco board which has an 8-bit AVR – Atmega328P microcontroller. To keep things simple, we will not tweak the settings of a microcontroller. No fuse bits touched; No external crystal attached; No headache. At factory settings, it runs on an internal 8MHz oscillator with a /8 prescaler; If you do not understand all this, it simply means that the microcontroller is running at 1MHz internal RC Oscillator and each clock cycle takes 1 microsecond.
1 2 1MHz = of 1000000 cycles per second Therefore, 1s/1000000 = 1/1000000 = 1us
AVR clocks and distance conversion
We are almost there! Once we know how to convert AVR clock cycles to distance traveled by sound waves, implementing the logic in a program is easy.
We know the speed of ultrasonic sound at ideal environment is: 58.31 us/cm
We know that resolution of AVR microcontroller is 1us/clock cycle (CLK)
Therefore, distance traveled by sound per clock cycle (CLK) is:
1 2 3 = (58.31 us/cm) x (1us/ clk) = 58.31 clock cycles / cm or = 1/58.31 cm/ clk
If the number of clock cycles it takes for sound to travel and bounce back is known, we can easily compute the distance. For example, if the sensor takes 1000 clock cycles to travel and bounce back, then the distance from a sensor to nearest object is = 1000/58.31 = 17.15 cm (approx.)
Does everything make sense now? No? Read it again
If you are clear with all the logic mentioned above, we will implement it in a real-world scenario by connecting an inexpensive HC-SR04 ultrasonic sensor to our AVR Arduino board.
Step 5: Hardware Connections:
makes it easy to connect any external sensors and also view the results on LCD. For ultrasonic range sensing, we use an inexpensive HC-SR04 module. The module has 4 pins that can be connected to microcontroller board: VCC, TRIG, ECHO, and GND.
Connect VCC pin to 5V and GND pin to ground on the Arduino board.
TRIG pin and ECHO pin can be connected to any available pins on the board. Sending a minimum of 10us ‘high’ signal to trigger pin sends eight 40 kHz sound waves and pulls echo pin high. If the sound bounces off a nearby object and returns, it is captured by receiving transducer and echo pin is pulled ‘low’.
Other variants of ultrasonic sensor modules are also available with just 3 pins. The working principle is still the same, but functionality of trigger and echo pins are combined into a single pin.
Once connected, Trigger and Echo Pins can be configured via software. To keep this example simple, we will not use any interrupt pins (or Input Capture Pin) in this example. Not using designated interrupt pins also gives us a freedom to connect the module to any available pins on the board.
Step 6: Code
The code below contains only an “ultrasonic” extension to DC motor control using an H-Bridge from the previous article. When the robot detects an obstacle in front of it, he turns around (random degree) and continues moving forward. This functionality could be easily extended to keep turning and detecting obstacles at the same time – so the robot would not turn randomly, but start moving forward only when no object is detected.
For Code Explanation Refer Youtube Video Listed on the Channel.
Step 7: Video
Watch The Video For the Whole Process.