Select Page

In this assignment, I programmed my Arduino to calculate the distance to the nearest object in the range of an ultrasonic sensor and to turn a servo motor once an object is within 5 inches of the sensor. Once there are no longer any objects within 5 inches, a warning alarm will beep and the servo motor will return to its starting position.

Ultrasonic sensors are used to calculate the distance to objects similar to sonar or radar. They work by emitting a pulse of sound at a frequency far higher than what humans can perceive (on mine, it’s about 40,000 Hz), and then measuring the length of the pulse when it returns. The longer the pulse travels through the air, the more it lengthens out. Therefore, you can use the length of the pulse when it returns to the ultrasonic sensor to determine how far it traveled. The distance that the sound pulse traveled equates to half the distance to the nearest object that is within the range of the ultrasonic sensor. The servo motor is controlled by using a set of servo-specific commands within Arduino. All you have to do is tell the Arduino what angle you want the servo motor to turn to, and the Arduino will do the rest.

Here is the link to my video showing how this setup works.