Home Industry InsightBLDC
Looking for a suitable motor? Looking for a suitable motor?
Looking for a suitable motor?

How to Control a Servo Motor with an Ultrasonic Sensor Using Arduino

小编

Published2025-10-15

Introduction to Servo Motors, Ultrasonic Sensors, and Arduino

When embarking on electronics projects, it’s common to come across two widely used components—servo motors and ultrasonic sensors. These components, when integrated into an Arduino-based system, offer countless possibilities for innovative and functional projects. Imagine building a robot that uses distance measurements to control its movements or a system that automatically adjusts its position based on surroundings.

In this tutorial, we'll explore how to interface a servo motor with an ultrasonic sensor using an Arduino board. You will learn the basics of servo motors, ultrasonic sensors, and how to use Arduino to control the motor’s movement based on distance readings from the sensor.

What is a Servo Motor?

A servo motor is a small, precise electric motor used in various applications like robotics, remote-controlled cars, and automation systems. What sets servo motors apart from standard motors is their ability to rotate to specific angles with high accuracy, making them ideal for tasks that require controlled movement.

Servo motors consist of a small DC motor, a potentiometer, and a feedback mechanism. The DC motor drives the shaft, while the potentiometer provides feedback to the system, allowing the motor to know its position. This combination gives the servo motor its ability to rotate precisely to a specified angle, typically ranging from 0 to 180 degrees.

What is an Ultrasonic Sensor?

An ultrasonic sensor is a type of distance-measuring device that works by emitting high-frequency sound waves (ultrasound) and measuring the time it takes for the waves to bounce back after hitting an object. The sensor calculates the distance based on the speed of sound in air. Ultrasonic sensors are commonly used for object detection, collision avoidance, and distance measuring in robotics and automation systems.

The sensor has two main components: a transmitter and a receiver. The transmitter emits the sound waves, and the receiver picks up the echoes. The time delay between the emission and reception of the sound wave allows the sensor to calculate the distance to an object.

Why Combine Servo Motors with Ultrasonic Sensors?

Combining a servo motor with an ultrasonic sensor opens the door to numerous possibilities. For instance, a simple system could use the ultrasonic sensor to measure the distance to an object and adjust the servo motor’s angle based on that distance. This creates an interactive system where the movement of the servo motor is directly influenced by the surroundings.

This type of setup can be used in various real-world applications such as:

Robotics: For making robots capable of avoiding obstacles or following a path based on distance measurements.

Automation: Creating systems that automatically adjust their position or orientation based on proximity sensors.

Security Systems: Using ultrasonic sensors to detect the presence of objects and move a camera or other device to focus on the object.

With that in mind, let’s break down the process of setting up an Arduino-based system that can control a servo motor using an ultrasonic sensor.

How to Build the System: Wiring, Code, and Explanation

Now that we’ve covered the basics of the components involved, let’s dive into the step-by-step process of building the system that controls a servo motor using an ultrasonic sensor and Arduino.

Components Required

To complete this project, you will need the following:

Arduino Uno (or any compatible Arduino board)

Ultrasonic Sensor (e.g., HC-SR04)

Servo Motor (e.g., SG90 or any standard servo motor)

Jumper Wires

Breadboard (optional for easy wiring)

Power Supply (for the Arduino and servo motor, depending on your setup)

Wiring the Components

Ultrasonic Sensor:

The ultrasonic sensor has four pins: VCC, GND, TRIG, and ECHO. Connect them as follows:

VCC to 5V on the Arduino.

GND to GND on the Arduino.

TRIG to pin 9 on the Arduino.

ECHO to pin 10 on the Arduino.

Servo Motor:

A typical servo motor has three wires: power (usually red), ground (usually black or brown), and signal (usually yellow or orange). Connect the servo motor as follows:

Power to the 5V pin on the Arduino.

Ground to the GND pin on the Arduino.

Signal to pin 6 on the Arduino (this can be changed in the code if needed).

Writing the Code

Now, let’s move on to the Arduino code that will control the servo motor using the ultrasonic sensor. Here’s the basic structure of the code:

#include // Include the Servo library

// Define the pins for the ultrasonic sensor

#define TRIG_PIN 9

#define ECHO_PIN 10

// Define the pin for the servo motor

#define SERVO_PIN 6

Servo myServo; // Create a Servo object to control the servo motor

void setup() {

// Initialize the serial communication

Serial.begin(9600);

// Initialize the ultrasonic sensor pins

pinMode(TRIG_PIN, OUTPUT);

pinMode(ECHO_PIN, INPUT);

// Initialize the servo motor

myServo.attach(SERVO_PIN);

}

void loop() {

long duration, distance;

// Send a pulse to trigger the ultrasonic sensor

digitalWrite(TRIG_PIN, LOW);

delayMicroseconds(2);

digitalWrite(TRIG_PIN, HIGH);

delayMicroseconds(10);

digitalWrite(TRIG_PIN, LOW);

// Measure the duration of the pulse that travels back from the object

duration = pulseIn(ECHO_PIN, HIGH);

// Calculate the distance based on the duration of the pulse

distance = (duration / 2) * 0.0343;

// Print the distance to the serial monitor

Serial.print("Distance: ");

Serial.print(distance);

Serial.println(" cm");

// Map the distance to an angle between 0 and 180 degrees

int angle = map(distance, 0, 100, 0, 180); // Adjust the 100 based on the expected range

// Control the servo motor based on the calculated angle

myServo.write(angle);

// Wait for a short time before repeating the process

delay(500);

}

Code Explanation

Servo Library:

The Servo library is included at the beginning of the code. This library makes it easy to control the servo motor by allowing you to send specific angles to the motor.

Pin Configuration:

We define the pins for the ultrasonic sensor and the servo motor. The ultrasonic sensor’s trigger pin is connected to pin 9, and the echo pin is connected to pin 10. The servo motor is connected to pin 6.

Ultrasonic Sensor Logic:

The pulseIn() function is used to measure the time it takes for the ultrasonic pulse to travel from the sensor, hit an object, and return. The distance is then calculated using the speed of sound in air (0.0343 cm per microsecond).

Servo Motor Control:

Based on the calculated distance, we map the value to a servo motor angle (from 0 to 180 degrees). The map() function scales the distance into an angle that the servo motor can use to adjust its position.

Serial Monitor Output:

The distance is printed on the serial monitor to help you debug the project and observe how the ultrasonic sensor is functioning.

Testing and Calibration

Once you’ve uploaded the code to your Arduino, open the serial monitor to see the distance readings in real time. The servo motor should rotate based on the distance measured by the ultrasonic sensor. Adjust the map() function if you want to tweak the range or behavior of the servo motor.

Conclusion

This project offers a great introduction to the world of robotics and sensor-based control systems. By combining a servo motor and an ultrasonic sensor, you’ve created a simple yet functional system that adjusts the servo's position based on the measured distance. The same principles can be applied to a wide range of applications, from automated systems to advanced robotics.

With a basic understanding of how these components work together, you can expand on this project by adding more sensors, motors, or even integrating it with other Arduino-compatible components for more complex systems.

Happy building!

Leveraging innovations in modular drive technology, Kpower integrates high-performance motors, precision reducers, and multi-protocol control systems to provide efficient and customized smart drive system solutions.

Update:2025-10-15

Contact a motor expert for product recommendation.
Contact a motor expert for product recommendation.

Powering The Future

Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.