Purchasing link

Buy from OSOYOO Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP

Content

1Objective

In this lesson, you will upgrade your OSOYOO V3 Robot Car into an autonomous obstacle-avoidance robot.

By combining an ultrasonic distance sensor and a servo motor, the robot can continuously monitor its surroundings, detect obstacles, and automatically choose a safe path without human intervention.

This project introduces one of the most important concepts in mobile robotics: Autonomous Navigation

The same fundamental principles are widely used in warehouse robots, service robots, automated guided vehicles (AGVs), and self-driving platforms.

Before starting this lesson, please complete Lesson 1: Basic Robot Car Assembly.

After completing this lesson, you will learn how to:

2Parts and Devices:

For the initial assembly phase, the following components are required. Please ensure all parts are accounted for prior to commencing the build

No. Picture Device Qty. Accessories Link
1 Ultrosonic Sensor 1 M1.4*8 Screw x 4
M1.4 Nut x 4
Click here to buy
2 Servo Motor 1 M2.2*8 Self Tapping Screw x 2
M2*4 Self Tapping Screw x 1
Click here to buy
3 Ultrasonic Module bracket 1 M1.4*8 Screw x 4
M1.4 Nut x 4
M2*4 Self Tapping Screw x 1
Click here to buy
4 20Pin jumper wire Male to female 20cm some Click here to buy
5 Philips screwdriver 1 Click here to buy
6 Hex Screwdriver 1 Click here to buy
7 M2*4 self tapping screw 1
8 M2.2*8 self tapping screw 2
9 M1.4*8 screw and nut 4
10 M3 plastic screw 1
11 M3 plastic pillar 1
12 M3 plastic nut 1

3Ultrasonic Sensor Working Principle

What Is Ultrasonic Ranging?

The ultrasonic sensor measures distance using sound waves rather than light.

The sensor contains:

The transmitter emits a short burst of ultrasonic sound at approximately 40 kHz.

When the sound wave hits an object, it reflects back toward the receiver.

The sensor measures the travel time of the echo and calculates the distance.

Distance Measurement Formula

The distance is calculated using:

Distance = (Sound Speed × Time) ÷ 2

Why divide by 2?

Because the sound travels:

The measured time includes both directions.

For example:

Speed of Sound = 343 m/s

Echo Time = 2000 μs

Distance ≈ 34.3 cm

This method is known as Time-of-Flight (ToF) Measurement and is commonly used in robotics and industrial automation systems.

4Hardware Installation

Step 1: Install basic framework as robot car Lesson 1 .

Important:

Step 2: Remove the M3 hex screen on upper chassis. Mount the SG90 servo motor on the front section of the upper chassis using 2pcs M2.2×8 self-tapping screws.

Step 3: Attach the ultrasonic module to the ultrasonic module bracket using 4pcs M1.5*8 screws and nuts.

Step 4: Secure the ultrasonic bracket onto the servo with M2.2*4 self-tapping screw. Ensure the sensor can rotate freely without interference.

Step 5: Connect servo motor to OSOYOO MODEL V motor shield, and add 1 jumper wires from pinS of OSOYOO model V motor shield to OSOLINK Wifi/BLE I/O shield as below connection diagram

Step 6: Connect ultrasonic module to OSOLINK Wifi/BLE I/O shield as below connection diagram

Step 7: Reinstall the upper chassis and tighten all mounting screws.

5Software Installation

Open-source Arduino
Software(IDE)
Download software here:
https://www.arduino.cc/en/Main/Software?setlang=en
7 zip is a free zip
utility that unzips zip files
Download 7zip here for free
https://www.7-zip.org/

1. Arduino IDE Setup: Download and install the latest version of the Arduino Integrated Development Environment (IDE) from https://www.arduino.cc/en/Main/Software?setlang=en.


2. Code Acquisition: Download the provided sample code for Lesson 5 from here. Extract the contents and you will get a v3car-lesson5.ino in the folder v3car-lesson5.

3. Board and Port Selection: Connect the OSOYOO Basic Board (compatible with Arduino UNO) to your computer via a USB cable(Crucially, ensure the robot car’s power switch is OFF and the battery is disconnected before connect the board to your PC). Launch the Arduino IDE. Navigate to Tools > Board and select Arduino Uno. Then, go to Tools > Port and select the appropriate serial port. If unsure, check your operating system’s device manager for the assigned port.

4. Install Libraries:
Firstly, download Libraries zip files from following links:
https://osoyoo.com/driver/ServoTimer2-master.zip
Second, do NOT unzip them, open Arduino IDE ->Sketch ->Include Library ->Add Zip Library. Then upload above zip files to Arduino One by One.

5. Code Upload: Open the v3car-lesson2.ino sketch in the Arduino IDE. Click the “Upload” button (right arrow icon) to compile and transfer the sketch to OSOYOO Basic Board.


6Ultrasonic Sensor Alignment

Why Alignment Matters

The obstacle avoidance algorithm assumes the ultrasonic sensor faces directly forward when the servo is centered.

If the sensor is installed at an angle:

Alignment Procedure

After power-up, the servo will turn left and right and finally stops at its center position a moment.

At this moment:

If the ultrasonic sensor point directly forward, keep it and work on.

If not:

  1. Power off immediately.
  2. Remove the sensor bracket.
  3. Reinstall the bracket in the correct orientation.
  4. Restart the robot.

Proper alignment is critical for reliable navigation.

7Test the Car

Now you can put the car onto the ground, turn on the power. The ultrasonic module will turn from right to left to detect surrounding obstacle. If no obstacle is detected, the car will go forward. If any obstacles is detected, the robot car will decide to make left turn, right turn or backward according to obstacle sensor data and our obstacle avoidance algorithm.

8 Troubleshooting

1. Robot Always Turns Left or Right
Possible causes:

2. Robot Does Not Detect Obstacles
Check:

3. Robot Frequently Collides
Possible causes:

4. Unstable Distance Readings
Avoid:

These conditions can weaken ultrasonic reflections.