Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

Robotics Challenge: Ultrasonic Sensor

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

This week’s robotics challenge focuses on ultrasonic sensors and will enhance your programming skills by utilizing pre-existing code.

The true way to get good at something is to PRACTICE. I designed the robotics challenge to help you practice basic skills in an applied way.

Program an Ultrasonic Sensor: Have a Solution? Share Your Work!

Use #learnrobotics #roboticschallenge in your Facebook, Twitter, or Instagram post!

I want these challenges to be as interactive as possible, so if you include #review with your post, I will provide personal feedback on your solution.

Challenge Overview

You’re given three ultrasonic sensors and one Arduino Uno with necessary wiring used to prevent a 2WD mobile robot from driving into an obstacle. The motor control methods have already been written (and working 😛 ) and can be called using the following commands:

forward(motorName, duration); //forward for the specified motor & duration
backward(motorName, duration); //backward for the specified motor & duration
left(); //90-deg turn left
right(); //90-deg turn right
stop(motorName); //stop motor in current position

Here’s the current hardware configuration of the device.

Smraza 5pcs Ultrasonic Module HC-SR04 Distance...
Power supply: 5V DC; Quiescent current: less than 2mA.; Test distance = ((Duration of high level)*(Sonic :340m/s))/2
$8.99
Sale
Arduino Uno REV3 [A000066]
Arduino Uno is the most used and documented board in the world
$32.00 −$4.05 $27.95

Your Task

In this week’s robotics challenge, create a method, int avoidObstacles(int ultrasonicPin, int distance) in an Arduino-C program to prevent the robot from crashing into an obstacle using the ultrasonic sensors. You should be at least 3 inches away from the obstacle while running. You do not need to use all of the sensors. The purpose is to configure them appropriately to solve the issue.

Here’s a template to help you get started on a solution:

/* GLOBAL DEFINITIONS
 * Define your global variables here.
 */
 //left ultrasonic
 const int leftULTRATRIG=3; //pin on Digital port 3
 const int leftULTRAECHO=2;
//right ultrasonic
 //FILL THIS OUT
//center ultrasonic
 //FILL THIS OUT
/* REQUIRED METHOD
 * Configure your pins as inputs or outputs here.
 */
void setup(){
 //*** ultrasonics ***
 //*** motors ***
 //Setup Channel A - Right Side
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Break Channel A pin
  //Setup Channel B - Left Side
  // FILL THIS OUT
}
/* Input Parameters: const int ultrasonicPin, int inputDistance
* Return Statement: bool
* Determines if the given ultrasonic sensor is within the given distance.
* Returns TRUE if the sensor is <= to input distance * Returns FALSE if the sensor is >= to input distance
* Use case: avoidObstacles(ultrasonicPin, inputDistance)
*/
bool avoidObstacles(const int ultrasonicPin, int inputDistance){
// fill this out
}
// OPTIONAL: Create additional helper methods here.
/* REQUIRED METHOD
* This method is like "main." Loops forever.
* The following comments are things to consider
* when determining your solution.
*/
void loop(){
// CHECK TO SEE IF THERE'S AN OBSTACLE WITHIN 3IN OF THE LEFT SIDE
// BACK UP & TURN IN THE OPPOSITE DIRECTION
// CHECK TO SEE IF THERE'S AN OBSTACLE WITHIN 3IN OF THE RIGHT SIDE
// BACK UP & TURN IN THE OPPOSITE DIRECTION
// CHECK TO SEE IF THERE'S AN OBSTACLE WITHIN 3IN OF CENTER
// BACK UP & TURN LEFT OR RIGHT
}

This template is provided as a GUIDELINE. You do not need to use it in your solution, as there are many ways to solve this problem.

It’s there if you need a quick nudge in the right direction. As always, if you’re really stuck, feel free to ask a question below or Tweet Us (@learnroboticsx) for help!

Not sure how this works?

Get the Robotics Engineering Bundle and learn how to interface electronics, software, and sensors to create autonomous devices and robots.

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES