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

Building an Ultrasonic Sensor with Servo Motor on Tinkercad: A Beginner’s Guide

小编

Published2025-10-15

Introduction to Ultrasonic Sensors and Servo Motors in Robotics

In the world of robotics and electronics, the combination of an ultrasonic sensor and a servo motor is a powerful tool. It allows for interactive projects such as object detection and movement in response to environmental changes. If you’re just starting out with Arduino or are looking to design a simple robotic system, understanding how to use an ultrasonic sensor and a servo motor can be a great foundation.

What is an Ultrasonic Sensor?

An ultrasonic sensor is a device used to measure the distance between the sensor and an object by emitting ultrasonic waves. These sound waves travel through the air, and when they hit an object, they bounce back to the sensor. The sensor calculates the time it takes for the waves to return, using this data to determine the distance.

Ultrasonic sensors are commonly used in robotics and automation for object detection, obstacle avoidance, and range measurement. The sensor is typically composed of two main components: a transmitter (which emits the ultrasonic waves) and a receiver (which detects the returning waves). Popular models, like the HC-SR04, are inexpensive and easy to use, making them ideal for beginner projects.

What is a Servo Motor?

A servo motor is a type of motor that can rotate to a precise angle. Unlike standard motors, which rotate continuously, a servo motor can move to specific positions. It operates using a feedback system that ensures it stops at the desired angle. This makes it perfect for applications where precise control over movement is needed, such as in robotics, cameras, and automation.

A servo motor typically has three wires: one for power (VCC), one for ground (GND), and one for signal control (PWM). By sending PWM (Pulse Width Modulation) signals to the servo motor, you can control the angle of rotation. Servo motors are widely used in robotic arms, pan-and-tilt camera systems, and, in this case, as part of a system that responds to distance changes detected by an ultrasonic sensor.

Why Use Tinkercad for This Project?

Tinkercad is a free, user-friendly online tool that allows you to design and simulate electronic circuits, especially with Arduino. It is ideal for beginners because it provides a straightforward way to test and refine ideas without needing physical hardware immediately. Tinkercad is great for simulating both the circuit and the Arduino code, making it perfect for learning and experimenting with basic electronics.

In this guide, we will walk you through the process of setting up an ultrasonic sensor with a servo motor on Tinkercad. You’ll learn how to wire the components, write the code, and simulate the entire system without ever needing to leave your browser.

Building the Circuit: Setting Up the Components

Before jumping into the coding part, let's first assemble the basic components in Tinkercad.

Step 1: Setting Up the Arduino Board

Open Tinkercad and create a new project. Start by dragging an Arduino Uno onto the workspace. The Arduino Uno will serve as the brain of your circuit, processing input from the ultrasonic sensor and sending commands to the servo motor.

Step 2: Adding the Ultrasonic Sensor

The next step is to add the HC-SR04 ultrasonic sensor to your project. In Tinkercad, search for the ultrasonic sensor and drag it into the workspace.

The sensor has four pins:

VCC (Power)

GND (Ground)

Trig (Trigger)

Echo (Receiver)

Connect the VCC pin to the 5V pin on the Arduino. Similarly, connect the GND pin to one of the GND pins on the Arduino. The Trig pin should be connected to a digital pin on the Arduino (for example, pin 9), and the Echo pin should be connected to another digital pin (for example, pin 10). These connections will allow the Arduino to send a trigger pulse and receive the echo response from the sensor.

Step 3: Adding the Servo Motor

Now, add the servo motor to the workspace. In Tinkercad, you can find a variety of servo motors; just drag one onto your design. Servo motors usually have three pins:

VCC (Power)

GND (Ground)

Signal (Control)

The VCC pin connects to the 5V on the Arduino, while the GND pin connects to a ground pin. The Signal pin should be connected to a PWM-capable pin on the Arduino, such as pin 6.

Step 4: Wiring Everything Together

Once all the components are in place, double-check the connections:

The ultrasonic sensor’s VCC and GND go to the Arduino’s 5V and GND, respectively.

The Trig pin of the ultrasonic sensor is connected to pin 9 on the Arduino.

The Echo pin of the ultrasonic sensor is connected to pin 10 on the Arduino.

The VCC and GND of the servo motor go to 5V and GND on the Arduino.

The Signal pin of the servo motor is connected to pin 6 on the Arduino.

At this point, the hardware setup is complete, and you are ready to move on to the coding section.

Coding the Arduino and Testing the Circuit

With the hardware set up, the next step is to write the Arduino code that will control the ultrasonic sensor and the servo motor based on the distance detected.

Step 1: Initialize the Code

In Tinkercad’s code editor, we’ll begin by including the necessary libraries. For this project, we’ll need the Servo library, which provides the functions needed to control the servo motor.

#include

Servo myServo; // create servo object to control a servo

Next, we’ll define the pins connected to the ultrasonic sensor and the servo motor.

const int trigPin = 9; // Trigger pin of the ultrasonic sensor

const int echoPin = 10; // Echo pin of the ultrasonic sensor

const int servoPin = 6; // Signal pin for the servo motor

Step 2: Setting Up the Sensor and Servo

In the setup() function, we initialize the ultrasonic sensor pins and the servo motor. The servo will be attached to the defined signal pin.

void setup() {

pinMode(trigPin, OUTPUT); // set the trigPin as an OUTPUT

pinMode(echoPin, INPUT); // set the echoPin as an INPUT

myServo.attach(servoPin); // attaches the servo on pin 6 to the servo object

}

Step 3: Measuring Distance and Controlling the Servo

Now comes the core functionality: measuring the distance using the ultrasonic sensor and moving the servo motor based on that distance. We will use the pulseIn() function to calculate the time taken for the echo pulse to return, and then we will convert that time into distance.

void loop() {

// Send a pulse to the trigger pin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

// Read the pulse duration from the echo pin

long duration = pulseIn(echoPin, HIGH);

long distance = duration * 0.0344 / 2; // Distance in cm

// Move the servo based on the distance

if (distance < 20) {

myServo.write(0); // Move the servo to 0 degrees if object is too close

} else if (distance < 40) {

myServo.write(90); // Move the servo to 90 degrees if object is at medium distance

} else {

myServo.write(180); // Move the servo to 180 degrees if object is far

}

delay(100); // Small delay for stability

}

In this code, we measure the distance and adjust the servo’s angle based on the measured distance. The closer the object, the smaller the angle of the servo. The farther the object, the larger the angle. This can be useful in many applications, such as a robot that adjusts its movement based on proximity.

Step 4: Testing and Simulating

Now that the code is ready, it’s time to test the project in Tinkercad. Click on the "Start Simulation" button and observe the results. The ultrasonic sensor will detect objects within the range, and the servo will rotate accordingly.

You can adjust the distance thresholds in the code to better fit your project’s needs. For example, you might want the servo to stop moving after a certain distance or trigger a specific action.

By completing this simple project, you’ve learned how to integrate an ultrasonic sensor with a servo motor on Tinkercad. This basic setup is just the beginning, and you can now expand it into more complex robotics projects, such as obstacle-avoiding robots, automated doors, or interactive systems.

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.