logo

How to connect ultrasonic sensor to servo motor

  • Home
  • Blog
  • How to connect ultrasonic sensor to servo motor


In this guide, we'll break down how to connect and program an ultrasonic sensor with a servo motor. We’ll use a microcontroller as the central piece to process data from the sensor and control the servo.

What You Need

To connect an ultrasonic sensor to a servo motor, you will need:

  • An Arduino or another microcontroller (e.g., Raspberry Pi, ESP32)
  • An Ultrasonic Sensor, such as the HC-SR04
  • A Servo Motor, such as an SG90
  • Jumper Wires for connections
  • A Breadboard (optional for organized wiring)
  • A computer with the Arduino IDE installed for programming

Step-by-Step Connection Guide

1. Understanding the Components

Before diving into the connections, it’s crucial to understand the basic operation of the components:

Ultrasonic Sensor (HC-SR04)

The HC-SR04 sensor works by emitting ultrasonic sound waves through a transmitter and measuring the time it takes for the waves to reflect off an object and return to the receiver. This time is used to calculate the distance to the object.

  • VCC: Power supply (usually 5V)
  • GND: Ground
  • Trig: Trigger pin (used to send out a pulse)
  • Echo: Echo pin (receives the pulse and returns the distance)

Servo Motor (SG90)

A servo motor is a small motor that allows for precise control of angular position, commonly in a range from 0° to 180°. It has three wires:

  • Power (Red): Connect to 5V
  • Ground (Brown or Black): Connect to GND
  • Signal (Yellow or Orange): Connect to a PWM-capable pin on the microcontroller.

2. Wiring the Components

To connect the ultrasonic sensor and servo motor to an Arduino, follow these wiring instructions:

Connecting the Ultrasonic Sensor

  • VCC: Connect to the 5V pin on the Arduino.
  • GND: Connect to the GND pin on the Arduino.
  • Trig: Connect to any available digital pin, such as pin 9.
  • Echo: Connect to another digital pin, such as pin 10.

Connecting the Servo Motor

  • Power: Connect to the 5V pin on the Arduino.
  • Ground: Connect to the GND pin on the Arduino.
  • Signal: Connect to a PWM-capable digital pin on the Arduino, like pin 3.

3. Programming the Arduino

Now that the components are connected, it's time to upload the code to the Arduino. Here’s a sample code snippet that moves the servo motor based on the distance measured by the ultrasonic sensor.

#include 

// Define pins
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 3;

// Initialize the servo
Servo myServo;

// Function to get the distance from the ultrasonic sensor
long getDistance() {
  // Send a 10 microsecond pulse to trigger the sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the duration of the pulse
  long duration = pulseIn(echoPin, HIGH);

  // Calculate distance in cm
  long distance = duration * 0.034 / 2;
  return distance;
}

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Configure pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Attach servo to the pin
  myServo.attach(servoPin);
  myServo.write(90);  // Set servo to the middle position
}

void loop() {
  // Get distance from the sensor
  long distance = getDistance();

  // Print distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Move the servo based on distance
  if (distance < 10) { myServo.write(0); // Object is close, turn to 0 degrees } else if (distance < 30) { myServo.write(45); // Object is at a moderate distance } else if (distance < 50) { myServo.write(90); // Object is further away } else { myServo.write(180); // Object is far, turn to 180 degrees } // Short delay before the next reading delay(500); } 

4. Explanation of the Code

The code above performs the following steps:

  • Setup: The pins for the ultrasonic sensor and servo are configured. The servo is initialized to a neutral 90° position.
  • Distance Measurement: A 10-microsecond pulse is sent to the trigger pin, and the time it takes for the echo to return is measured. This duration is used to calculate the distance to an object.
  • Servo Control: Depending on the distance, the servo’s angle is adjusted. For example, if an object is closer than 10 cm, the servo rotates to 0°, while it moves to 180° for objects farther than 50 cm.

5. Testing and Adjustments

To ensure proper functionality:

  • Upload the code to the Arduino and open the Serial Monitor to view the distance measurements.
  • Place objects at different distances in front of the ultrasonic sensor and observe the servo's response. Adjust the angles and distance thresholds in the code to suit your application.

Connecting an ultrasonic sensor to a servo motor allows you to create interactive and automated systems capable of responding to distance measurements. By following the steps outlined, you can easily set up a functioning prototype with minimal components. This setup not only aids in understanding the basics of robotics but also provides a foundation for more advanced projects involving sensors and actuators. Remember, precise wiring and thorough testing are key to ensuring a smooth experience.

This guide emphasized clarity, reliability, and step-by-step guidance to provide a solid understanding of the topic. Through this project, you’ll not only gain practical skills but also develop a deeper appreciation for electronics and robotics.


Have project in mind?

Get in touch with us to see how we can help you with your project.

Thank you Massage Sended
Error occurred while sending email. Please try again later.