






Arduino Design Course (Part 5)

Ultrasonic Obstacle Avoidance Car


Course Introduction

This lesson involves manually building a smart car that can autonomously avoid obstacles using ultrasonic technology, exploring the application of ultrasonic technology and the implementation of simple artificial intelligence. The car is centered around an Arduino board, mimicking biological perception of the environment: the ultrasonic sensor emits high-frequency sound waves like a “bat’s ears”. When the sound waves encounter an obstacle, they are reflected back, and the sensor calculates the time difference between emission and reception, allowing it to measure the distance to the obstacle like a “rangefinder”. For example, if the round-trip time of the sound wave is 0.6 milliseconds, the distance to the obstacle can be calculated to be approximately 10 centimeters based on the speed of sound formula.




The car’s “body” consists of motors, wheels, and a chassis. The motor drive module acts as the “muscle”, converting the Arduino’s commands into wheel rotation; the battery pack provides energy, akin to the car’s “heart”. During assembly, care must be taken to install the ultrasonic sensor at the front of the car, slightly elevated to avoid ground reflection interference, similar to how human eyes are positioned above the head.
When the car starts, it will continuously execute the “detect-decide-act” intelligent process: it scans the front area; if the distance is greater than 20 centimeters, it moves forward; upon detecting an obstacle, it will immediately brake, briefly reverse, and then randomly turn left or right to avoid getting stuck in a “wall collision loop”. This process simulates the reflex mechanism of living organisms, such as a gecko’s obstacle avoidance behavior in narrow spaces. By adjusting the delay parameters in the program, the car’s “personality” can also be altered—acting agile when detecting longer distances and cautious when detecting shorter distances.




Challenges and Growth During Debugging
During the course practice, students discovered that the seemingly simple obstacle avoidance logic encountered many unexpected situations during actual operation. For example, when the car repeatedly spun in circles at a corner, they observed that this was due to the ultrasonic sensor’s “blind spot”—just like a person standing in a corner cannot judge the distance on both sides with one eye, the sensor can only detect obstacles directly in front. When the car spun in place, they found that it was caused by the motor wiring polarity being reversed, leading to “left-right conflict”. They improved this by comparing the relationship between the tire rotation direction and the program instructions. When a few curious children suddenly entered the testing area, the originally orderly obstacle-avoiding car faced the “ultimate challenge”. Students found that when the children jumped across the path, the ultrasonic sensor became confused like a “teased electronic eye”—the rapidly moving limbs caused the echo time to fluctuate, as if the obstacles were appearing and disappearing in an instant. One group observed that when the car moved straight towards the running children, the traditional 20-centimeter stopping distance was insufficient to react, similar to a cyclist encountering a small animal suddenly darting onto the road.




Transition from Laboratory to Real World
Faced with a complex terrain built with foam blocks and desks, students experienced a transition from an ideal environment to real-world challenges. The initial program ran well on a flat desk, but when encountering uneven obstacles, the ultrasonic sensor might misjudge the lower desks and gaps between blocks as pathways. When multiple cars were tested in the same area, frequent collisions raised thoughts about “swarm intelligence”—some groups attempted to set different turning preferences for each car, mimicking the collision avoidance rules of bird flocks in nature; these innovative attempts, stemming from real problems, brought the code from the books to life and allowed students to deeply understand that the perfection of technological products often requires countless failures and iterations.


This project combines the principles of acoustics from physics, logical reasoning from programming, and mechanical design from engineering, vividly demonstrating how technology mimics natural intelligence. Through hands-on practice, students not only understand the simplified working principles of autonomous vehicles but also lay the foundation for future studies in robotics. For example, the obstacle avoidance logic of household vacuum robots is highly similar to this project, with just more sensors and algorithm optimizations added. This extension of thought from “toy cars” to “smart devices” is precisely the starting point of technological innovation.


Student Reflections

Class 6, Guo Yichen
One of the three great short story writers in the world, Anton Chekhov once said: “Science is the most important, beautiful, and necessary thing in people’s lives.” This shows the importance and greatness of technology. This Friday, I participated in the Arduino technology innovation class organized by the school and felt the wonders of technology.
At the beginning of the course, the teacher introduced the operation code of the obstacle avoidance car in detail. The lines of code on the screen resembled little sprites, forming a path to the world of technology. However, the “road of technology” is not smooth; with over fifty lines of code and thousands of letters and symbols, there cannot be a single error. From this, I experienced the complexity and precision of technology.
Finally, after adjusting the code and machine, when the data cable connected them, the wonderful technological current surged between the two, giving the cold machine a soul, relying on the magical ultrasonic sensor to avoid numerous obstacles. The agile movements revealed the advanced and powerful nature of technology.
As the sun sets, besides the calls of returning birds, there are also the sounds of machines operating. This activity was not just about creating and building the obstacle avoidance car, but also about experiencing the brilliance of technology, the wonders of the technological journey, and the beauty of technology blooming!
Class 6, Jia Bowen
In this Arduino design class, we assembled a car using the Arduino control board, motor module, and ultrasonic sensor. While connecting the wires, we repeatedly checked the ultrasonic sensor’s emission and reception signal interfaces to ensure it could properly sense obstacles. When it was time for testing, the small rectangular area surrounded by blocks saw different reactions from each group’s car: some braked suddenly and turned around, almost flying out, while others moved steadily along the edge, and some were blocked by mischievous classmates’ hands, turning in circles.
The competition segment was the most lively, with one group’s car spinning in place, caught in a dilemma. Everyone laughed while helping to find the problem. We shortened the sensing distance to help the car avoid unnecessary turns. At the end, someone poked the car’s sensor and joked, “It’s like a bat exploring the path.”
The sunset gilded the scattered parts, and the mechanical little guys seemed to have warmth, allowing everyone to taste the sweet fruit of their achievements.
Class 6, Wang Sirui
Today, learning about the ultrasonic obstacle avoidance car was so interesting! I learned that the ultrasonic sensor is like a “little ear”; it has a transmitter and a receiver. It first emits ultrasonic pulses, waits for the echo to return, and by measuring the time difference between sending and receiving, combined with the speed of sound in air, it can calculate the distance. The principle is similar to a bat’s echolocation. There are also motors that convert electrical energy into power, making the car run. Watching the theory transform into a running, obstacle-avoiding car gave me a great sense of accomplishment, and I want to explore more fun technology in the future!
Class 6, Zou Shizhi
This intelligent ultrasonic obstacle avoidance car programming class inspired me greatly. During the teacher’s explanation, I was amazed at how the ultrasonic sensor acts as the car’s “eyes”, accurately measuring distances by emitting and receiving sound waves, providing the basis for obstacle avoidance decisions. During the programming segment, we focused intently on debugging the code, and when we finally saw the car skillfully avoid obstacles, a sense of achievement arose. This class not only taught me the programming application of ultrasonic obstacle avoidance but also made me feel the wonderful charm of the combination of hardware and software—technology can give machines “intelligence”. This experience broadened my thinking and strengthened my determination to continue exploring this field.

Shanghai Jiao Tong University Affiliated Second Middle School
Website: http://www.jd2fz.sjtu.edu.cn
WeChat ID: shjd2fz
