📌 Project Overview
This project demonstrates how to use an Ultrasonic Sensor (HC-SR04) and a Servo Motor (SG90 or MG995) with an Arduino to create an automated distance-based servo control system. The servo motor moves based on the distance detected by the ultrasonic sensor, making it ideal for automated gates, robotic arms, and obstacle avoidance systems.
Arduino Uno/Nano – Microcontroller
HC-SR04 Ultrasonic Sensor – Measures distance
SG90 Servo Motor – Rotates based on detected distance
Jumper Wires – For connections
Breadboard (Optional) – For easy wiring
5V Power Supply (if using multiple servos)
To set up the Ultrasonic Sensor (HC-SR04) and Servo Motor (SG90 or MG995) with the Arduino, you need to make the following connections:
Start by connecting the HC-SR04 Ultrasonic Sensor. The Trigger Pin (TRIG) of the sensor should be connected to digital pin 9 on the Arduino, and the Echo Pin (ECHO) of the sensor should go to digital pin 10 on the Arduino. The VCC pin of the sensor should be connected to the 5V pin on the Arduino, while the GND pin of the sensor should be connected to the GND pin of the Arduino.
Next, connect the SG90 Servo Motor. The Signal Pin of the servo should be connected to digital pin 6 on the Arduino. The VCC of the servo motor should be powered from the 5V pin of the Arduino, and the GND of the servo motor should be connected to the GND pin of the Arduino. These connections ensure that the Arduino can send the necessary control signals to both the sensor and the motor.
This wiring setup allows the Arduino to use the ultrasonic sensor to measure distances and then control the servo motor's position based on the measured distance.
🔹 Working:
The ultrasonic sensor sends sound waves and measures the time taken for them to bounce back.
The Arduino calculates the distance and controls the servo motor accordingly.
If an object is detected within a certain range, the servo rotates to a defined angle.
CODE:
#include <Servo.h>
// Define sensor and servo motor
#define TRIG_PIN 9
#define ECHO_PIN 10
#define SERVO_PIN 6
Servo myServo; // Create servo object
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
myServo.attach(SERVO_PIN);
Serial.begin(9600);
}
void loop() {
long duration;
int distance;
// Send ultrasonic pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo time
duration = pulseIn(ECHO_PIN, HIGH);
distance = duration * 0.034 / 2; // Convert to cm
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Control servo based on distance
if (distance > 0 && distance <= 10) {
myServo.write(0); // Move to 0 degrees
} else if (distance > 10 && distance <= 20) {
myServo.write(90); // Move to 90 degrees
} else {
myServo.write(180); // Move to 180 degrees
}
delay(500);
}
Library & Pin Setup:
The Servo.h library is included to control the servo motor.
HC-SR04 pins (Trigger and Echo) and Servo signal pin are defined.
Measuring Distance:
The ultrasonic sensor sends out a pulse and listens for the echo.
The Arduino calculates the distance based on the time delay.
Controlling the Servo:
If the object is less than 10 cm, the servo moves to 0°.
If the object is between 10–20 cm, the servo moves to 90°.
If the object is beyond 20 cm, the servo moves to 180°.
Serial Monitor Output:
Prints the measured distance for debugging and monitoring.
✅ Automated Doors/Gates – Opens or closes based on distance
✅ Obstacle Avoidance Robots – Adjusts direction when detecting objects
✅ Smart Parking Systems – Controls barriers based on vehicle proximity
This project is an excellent starting point for learning Arduino, sensor integration, and motor control!