Maker Pro
Arduino

Build An Obstacle-Avoiding Robot with Arduino & Ultrasonic Sensor

May 12, 2017 by Ryan Jones
Share
banner

Use an Arduino and an ultrasonic sensor to build this obstacle-avoiding robot.

This obstacle avoiding robot was inspired by the Roomba, but doesn't work as well. In fact, the cleaning mechanism has been downgraded from a vacuum to a broom. For these reasons, it has been dubbed the "Zambroombi", an obstacle avoiding Zamboni with broom bristles. Here is a quote from one happy Zambroombi customer:

"It changed my life. I don't know where I'd be today without it. I used to just have to clean everything once, but thanks to The Zambroombi, I get to clean everything twice!"  —Real Zambroombi© Customer

The Zambroombi Bill of Materials

Why?

Thanksgiving was around the corner and I was overwhelmed with stress. How was I going to host a family gathering AND cook and clean all by myself!? I knew I had to do something. I dug through my workbench for spare parts, I discovered the ultrasonic sensor and went to work immediately. Minutes later (It's an easy project, try it out!), I had my new and fabulous household appliance, The Zambroombi! Its appearance leaves a lot to be desired, and it doesn't scoop very well, but it has potential! The Zambroombi serves many functions: it can sweep up popcorn, give your dog a pal, and even be a friend to talk to at night.

How Does the Obstacle-Avoiding Robot Work?

The Zambroombi can simply be turned on and you can let it run through. It has the ability to calculate an object's distance and correct its own driving path. This means it can drive on its own endlessly—until it gets stuck on a potato or something.

It's ability to operate autonomously is based on the bot's ultrasonic sensor. The ultrasonic sensor emits high-frequency sound waves (these waves can't be detected by human ears because they are too high), and waits for those sound waves to reflect off of an object, and calculates how long it takes for the sound to return to the sensor. The sensor I used comes with send and receive pins labeled "trigger" and "echo," respectively. These pins do most of the work for us. You can find additional details about interfacing to the sensor module if you look through the code. You can find a downloadable version of the Arduino code at the end of this tutorial.

Notice the "T" for transmit and the "R" for receive

We need to control the sensor's high-frequency emission. To do this, we need to send the sensor module a logic HIGH signal that's at least 10µs wide to the trigger pin. We first write the pin LOW, then HIGH. It waits for at least 10µs, and then writes it LOW again. This tells the sensor to emit the signal. When the sound emitted by the sensor reflects off of an object and returns to the sensor, the sensor sends a digital signal to the Arduino. This digital signal indicates the ultrasonic signal's round-trip travel time.

digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(trigPin, LOW);

We need to do a few things in order to convert this distance in microseconds into a distance in centimeters. Since the echo pin indicates how long it takes for sound to reach an object and return, we first need to divide the echo-pin pulse width by 2 to determine the one-way travel time.

Sound travels at 340 m/s under typical conditions. Using some simple math, we find that this corresponds to 29 µs/cm. So far so good! To convert travel time to a distance in centimeters, we must divide the one-way travel time, in microseconds, by 29. Here is the code:
duration = pulseIn(echoPin, HIGH); 
distance = (duration/2) / 29;
Now that our Arduino can do some basic math to convert the echo-pin pulse width into a distance in centimeters, we can tell it to turn any time it gets within 25cm of an object. To do so, we will use an If/Else statement. In this case: If our sensor is within 25cm of an object, turn; if not (else), drive forward. This could be considered a crude "obstacle avoidance" system, but it also ensures that the Zambroombi will drive right off the table if nothing is in its way!

To make the robot turn, we'll need to spin the wheels in opposite directions. Thanks to our handy Adafruit Motor Shield and its accompanying library, we can do this easily by just telling one motor to spin forward and the other to spin backward.

if(distance < 25){ 
myMotor->run(FORWARD); 
myMotor2->run(BACKWARD); 
} 
 else{ 
delay(40); 
myMotor->run(FORWARD); 
myMotor2->run(FORWARD); 
} 
}

That's it! Now find some bristles, scoops, squeegees, or whatever you have lying around, and create a sophisticated cleaning robot that isn't very good at cleaning!

Add a mustache to give it some character!

If you'd like to get started on your own obstacle avoiding robot, you can find the code right here: Obstacle Avoidance Code. Here are some other Arduino and Raspberry Pi self-driving robots that might pique your interest as well. Happy hacking!

Other DIY Autonomous Robot Projects

Author

Avatar
Ryan Jones

Start my career as an Audio Engineer, now turned professional maker!

Related Content

Comments


You May Also Like