Introduction: Obstacle Avoidance Robot
Building your own obstacle-avoidance robot is a fun and beginner-friendly way to dive into the world of Arduino and robotics. In this project, we’ll walk through how to create a simple autonomous robot that uses an ultrasonic sensor to detect walls, turn around, and continue exploring. With just a few common components and some straightforward code, you’ll have a base design of a rover you can use to create anything that comes to your imagination. Let's get started!
Supplies
This project will need supplies that can all be found in the Sparkfun Inventors kit for aruduino uno.
- Arduino Uno (1)
- Breadboard Mini (1)
- Arduino mounting plate (1)
- Arduino-compatible wires (23)
- Black wires (5)
- Blue wires (3)
- White wires (4)
- Red wires (5)
- Green wires (3)
- Yellow wires (3)
- Hobby Gear Motor (2)
- Wheels (2)
- Ultrasonic distance sensor (1)
- Motor controller (1)
- Switch (1)
Not pictured:
- Batterypack (1)
- AA batteries (4)
- Velcro tape (at least 2 in)
For the casing nad obsticles:
- Cardboard
- Ruler
- Pencil
- Box cutter/X-acto Knife
- Packing tape
Step 1: Prepare Breadboard
Attach the breadboard mini and the Arduino Uno to the mounting plate utilizing the tape, screws, and screwdriver provided in the kit.
Step 2: Attach the Components
Now we need to insert the distance sensor, switch, and motor controller into the breadboard.
The distance sensor should be pointed away from the Arduino Uno, facing the direction designated as forward. The pins should be placed in the slots J14 - J17 or A14-A17, depending on which direction your board faces.
The switch should be placed on the same side as the distance sensor. This will either be slots G26-G28 or C3-C5.
The motor controller should be placed on the left-hand side of the breadboard mini. Make sure that the controller bridges between the two sides of the breadboard. The sides should be put in rows D and H
After the board should look like the photo above.
Step 3: Begin Wiring
To begin, we must connect power and ground to the breadboard.
- Connect a red wire from the power row on the Breadboard, shown by the red positive sign, to 5V on the Arduino Uno.
- Connect a black wire from ground row on the Breadboard, shown by the black negative sign, to GND on the Arduino Uno.
- Use a red wire to wire the two power rows on the Breadboard together.
- Use a black wire to wire the two ground rows on the Breadboard together.
Step 4: Wiring the Motor Controller
The motor controller will have a top side ( side further from the arduino ) and a bottom side (side closer to the arduino)
The steps for the top side are as follows.
- Connect a black wire from GND to ground row on the Breadboard.
- Wire the first hobby gearmotor by connecting the black motor wire to BO1 and the red motor wire to BO2.
- Wire the second hobby gearmotor by connecting the black motor wire to AO2 and the red motor wire to AO1.
- Connect a white wire from GND to ground row on the Breadboard.
- Connect a red wire from VCC to power row on the Breadboard.
- Connect a white wire from VM to VIN on the Arduino Uno
The bottom side is wired similarly. The steps are as follows.
- Connect a black wire from GND to ground row on the Breadboard.
- Connect a yellow wire from PWMB to pin 9 on the Arduino Uno.
- Connect a yellow wire from BI2 to pin 10 on the Arduino Uno.
- Connect a red wire from STBY to power row on the Breadboard.
- Connect a blue wire from AI1 to pin 13 on the Arduino Uno.
- Connect a blue wire from AI2 to pin 12 on the Arduino Uno.
- Connect a blue wire from PWMA to pin 11 on the Arduino Uno.
Step 5: Wiring the Distance Sensor
The distance sensor should be set up as follows.
- Connect the ultrasonic distance sensor’s GND pin to ground on the Breadboard via a black wire.
- Connect the ECHO pin on the ultrasonic distance sensor to pin 5 on the Arduino Uno via a green wire.
- Connect the TRIG pin to pin 6 on the Arduino Uno via a green wire.
- Connect VCC to power on the Breadboard via a red wire.
Step 6: Wiring the Switch
The switch is the simplest to wire.
- Connect the leftmost pin to power on the Breadboard via a white wire.
- Connect the middle pin to pin 2 on the Arduino Uno via a green wire.
- Connect the rightmost pin to ground on the Breadboard via a white wire.
Step 7: Attaching the Motors
Grab a strip of Velcro, or any other temporary adhesive, and place one side on the motors and one side on the underside of the Arduino board. The big wheels are then inserted into the white pegs on the motors.
Step 8: Open Arduino Coding Platform
- Download the attached file and upload to the following link to run the program.
- Arduino SSO <-- Link to Arduino Cloud
The full code is attached, but if you want to try to write it alongside instructions, feel free!!
Attachments
Step 9: Initalize Variables in Code
- Before all of the functions, initialize your variables.
- For the sensor, the pins to which the TRIG and ECHO are connected need to be noted. Additionally, a tracker variable for the distance values being reported by the sensor is required. In our project, we set these as follows:
- int trigPin = 6;
- int echoPin = 5;
- float distance;
- For the motor, the pins to which AIN1, AIN2, BIN1, BIN2, and their respective PWMs are connected need to be noted. For our project, these were noted as follows:
- int AIN1 = 13;
- int AIN2 = 12;
- int PWMA = 11;
- int BIN1 = 8;
- int BIN2 = 10;
- int PWMB = 9;
- For the switch, the switch status and the pin connection must be noted.
- int switchpin = 2;
- int switchval;
Step 10: Initialize Board Elements in Code
- In the void setup {} function, initialize your board elements.
- For the motors, notify Arduino that each variable is an output as follows:
- pinMode(AIN1, OUTPUT);
- pinMode(AIN2, OUTPUT);
- pinMode(PWMA, OUTPUT);
- pinMode(BIN1, OUTPUT);
- pinMode(BIN2, OUTPUT);
- pinMode(PWMB, OUTPUT);
- For the ultrasonic distance sensor, notify Arduino that the trigPin is an output (sending out a signal) and echoPin is an input (sending in a signal).
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- For the switch, initialize the pin as an input (to send data to the Arduino).
- pinMode(switchpin, INPUT);
- Finally, open and begin the Serial Monitor for the print statements.
- Serial.begin(9600);
Step 11: Writing the Code Loop
- In the void loop() {} function, the code logic is as follows.
- Start with switchval = digitalRead(switchpin); to read the position of the switch. This switch will control whether the robot runs or not.
- Then, check if (switchval == LOW) . Essentially, if the switch is flipped to LOW, run the robot.
- In the case the switch is HIGH, turn off all robot functions through one line of code: stopmotors();
- In the case the switch is LOW, acquire the distance through the line of code: distance = getdistance(trigPin, echoPin); .
- Print the distance to the Serial Monitor via Serial.print("Distance: ");and Serial.print(distance); .
- Then, check if there is an obstacle with the following line of code: if (distance > 20)
- If this is true, there is no obstacle, the path is clear, and the robot should run this code to drive forward: driveforward(150);
- If this is false, there is an obstacle, and the robot needs to handle it. Run the avoidobstacle(); function.
- Do a quick delay for Arduino processing purposes: delay(50);
Step 12: Creating Stop Motor Function
- Outside of the void loop() {} function, create a function called stopmotors() {}
- Turn off the spinning of the wheels by setting the PWMA and PWMB to zero in speed: analogWrite(PWMA, 0); / analogWrite(PWMB, 0);
Step 13: Creating Drive Forward/backward Function
- Create a function called driveforward(int speed) {}
- This function takes an input value (the speed at which you want the wheels to spin) and influences the motors accordingly. Speed can be any value 0-225.
- Set the left motor to move forward through: digitalWrite(AIN1, HIGH); / digitalWrite(AIN2, LOW);
- Set the right motor to move forward through: digitalWrite(BIN1, HIGH); / digitalWrite(BIN2, LOW);
- Set the PWM of each to the passed-in speed value: analogWrite(PWMA, speed); / analogWrite(PWMB, speed);
- Create a drivebackward(int speed) {} function
- Reverse the logic for the driveforward(int speed) {} function.
- Code looks like: digitalWrite(AIN1, LOW); / digitalWrite(AIN2, HIGH); / digitalWrite(BIN1, LOW); / digitalWrite(BIN2, HIGH);
- Set the PWM of each to the passed-in speed value: analogWrite(PWMA, speed); / analogWrite(PWMB, speed);
Step 14: Creating Turn Left Function
- Start with declaring the function
- void turnleft(int speed) {
- Move the left motor forewards and the right motor backwards
- digitalWrite(AIN1, LOW);
- digitalWrite(AIN2, HIGH);
- digitalWrite(BIN1, LOW);
- digitalWrite(BIN2, LOW);
- analogWrite(PWMA, speed)
- analogWrite(PWMB, speed);
- end with
Step 15: Create Get Distance Function
The purpose of this function is to capture the data gathered by the ultrasonic distance sensor.
- start the function with declaring the function
- float getdistance(int trigpin, int echopin) {
- send out echo and get distance
- digitalWrite(trigpin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigpin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigpin, LOW);
- long echoTime = pulseIn(echopin, HIGH);
- float distance = echoTime / 58.0;
- return distance;
- end with }
Step 16: Make the Casing for the Robot
- Collect a 2ft x 2ft piece of cardboard
- Out of this piece, we need two 3.5 in x 5.4 in pieces for the sides, and two 3.5 in x 4.2 in for the front and back sides.
- Grab one of the 3.5 in x 4.2 in pieces and measure 1.4 inches in FROM the long side of the piece on both sides and make a mark at each point.
- From those marks, measure 2 inches up. You should now have four points.
- Connect the points to make a rectangle and cut the rectangle out. This will be the hole for our distance sensor.
- For the lid, cut out a 6 in x 4.5 in piece of cardboard and measure 1 in from each edge, and connect the marks, making another rectangle within the piece.
- Score along the inner box and cut off the outside corners so you end up with a shape that is similar to an addition sign.
- Fold the flaps of the cardboard down and tape the corners together on the inside and outside.
- Attach the front piece to the two side pieces with tape on the inside and outside.
- Finally, attach the back piece to oppose the front piece.
- Add an additional piece of velco tape to the back piece to attach the battery pack.
- When putting the casing on the Arduino, tape the sides to the bottom of the mounting plate.
Step 17: Create the Obsticles for the Robot
Acquire a large, flat piece of cardboard. Cut 6 pieces that are 17.25 inches by 4.25 inches in size. These will be Parts A. Next, we need to cut 12 pieces of 5 inches by 2.75 inches in size. These will be called parts B. Glue two part B pieces to one part A piece, as shown in the picture.
Now we can use these pieces to create a hexagonal pattern for the robot to explore!
Happy building!!


