Rotational Distance Mapping System Using Ultrasonic Sensor and Servo Motor

About The Project

This project involves interfacing an ultrasonic sensor with a servo motor to measure and display distances at various angles. The system rotates the sensor across a 180-degree range, providing real-time distance readings for enhanced spatial awareness and measurement applications.

Real World Applications of the Project

  1. Robotics and Automation: It can be used in robotic systems to enable obstacle detection and avoidance, allowing robots to navigate safely in their environment.
  2. Security Systems: The system can be integrated into surveillance and security systems to monitor a wide area for intrusions or to detect objects moving within a specific range.
  3. Mapping and Navigation: The setup can be utilized in applications requiring environment mapping, such as in drones or autonomous vehicles, to create a 2D map of the surroundings based on distance measurements.

Ultrasonic Sensor (HC-SR04)

The working principle of an ultrasonic distance sensor is based on the echo of high-frequency sound (Ultrasonic Sound)waves.

  1. Sound Wave Emission: The sensor emits short pulses of ultrasonic sound waves (typically above the range of human hearing, around 40 kHz).
  2. Travel to Object: These sound waves travel through the air and bounce off any obstacle or object in their path.
  3. Echo Reception: The sensor then listens for the echoes of the sound waves reflected back from the object.

Time Calculation : By measuring the time interval between sending the sound pulse and receiving its echo, the Arduino Uno calculates the distance to the object using the formula: Distance = Speed of Sound ×Time interval between sending the sound pulse and receiving its echo/2.

Speed of Sound: The speed of sound wave in air is approximately 343 meters per second .

That is

Distance in meter = 171.5 ×Time interval between sending the sound pulse and receiving its echo in second.

Servo Motor

A servo motor is a type of motor where precise control of angular position is required.

    • A servo motor receives a PWM (Pulse Width Modulation) signal on its control wire.
    • The width of the pulse determines the position of the servo horn (output shaft).
  • Input Signal (PWM): The control circuit receives a PWM (Pulse Width Modulation) signal, which dictates the desired position of the servo.
    • Pulse Width: The length of the pulse determines the angle:
      • 1 ms pulse width: Typically corresponds to 0 degrees.
      • 1.5 ms pulse width: Corresponds to 90 degrees (midpoint).
      • 2 ms pulse width: Corresponds to 180 degrees.
    • The pulse width is sent every 20 ms in a 50 Hz (meaning the pulse is repeated every 20 ms) signal.

However,  Arduino programming with a servo motor is straightforward, the servo motor’s position is determined by a value ranging from 0 to 180 degrees

The Servo.h library, handles the conversion of the angle value to a PWM signal with the appropriate duty cycle and frequency, controlling the servo motor’s position.

Circuit Wiring

Program Code

This Arduino code interfaces a servo motor with an ultrasonic sensor to measure the distance to objects at different angles. The setup continuously sweeps the servo motor from 0 to 180 degrees and then back to 0 degrees while measuring the distance using the ultrasonic sensor at each step. The results (angle and distance) are printed on the Serial Monitor.

C++
// www.matthewtechub.com
// servomotor -Ultra Sonic Sensor
#include <Servo.h>

Servo myServo;  // Create a servo object

// Define pins for ultrasonic sensor
const int trigPin = 11;
const int echoPin = 10;

// Variable to store distance
long duration;
int distance;

void setup() {
  myServo.attach(9);  // Attach the servo to pin 9
  pinMode(trigPin, OUTPUT);  // Set the trigPin as an output
  pinMode(echoPin, INPUT);   // Set the echoPin as an input
  Serial.begin(9600);        // Start the serial communication for debugging

  // Initial test to move the servo
  myServo.write(0);  // Move servo to 0 degrees
  delay(1000);       // Wait for 1 second
  myServo.write(90); // Move servo to 90 degrees
  delay(1000);       // Wait for 1 second
  myServo.write(180);// Move servo to 180 degrees
  delay(1000);       // Wait for 1 second
}

void loop() {
  for (int angle = 0; angle <= 180; angle += 5) { // Sweep from 0 to 180 degrees in 15-degree steps
    myServo.write(angle);  // Move the servo to the current angle
    delay(500);  // Wait for the servo to reach the position

    // Send a 10us pulse to trigger the ultrasonic sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

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

    // Calculate the distance in centimeters
    distance = duration * 0.034 / 2;

    // Print the distance and the angle
    Serial.print("Angle: ");
    Serial.print(angle);
    Serial.print(" degrees, Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    delay(500);  // Short delay between readings
  }

  // Return the servo to the initial position
  for (int angle = 180; angle >= 0; angle -= 5) { // Sweep back from 180 to 0 degrees
    myServo.write(angle);  // Move the servo to the current angle
    delay(500);  // Wait for the servo to reach the position

    // Send a 10us pulse to trigger the ultrasonic sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

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

    // Calculate the distance in centimeters
    distance = duration * 0.034 / 2;

    // Print the distance and the angle
    Serial.print("Angle: ");
    Serial.print(angle);
    Serial.print(" degrees, Distance: ");
    Serial.print(distance);
    Serial.println(" cm");

    delay(500);  // Short delay between readings
  }
}

Code Explanation

C++
#include <Servo.h>
  • The `Servo` library is included to control the servo motor. This library simplifies the process of controlling servo motors with an Arduino.
C++
Servo myServo;
  • An object `myServo` of the `Servo` class is created. This object will be used to control the servo motor.
C++
   const int trigPin = 11;
   const int echoPin = 10;
  • `trigPin`: The pin connected to the trigger pin of the ultrasonic sensor (used to send out a pulse).
  • `echoPin`: The pin connected to the echo pin of the ultrasonic sensor (used to receive the reflected pulse).
C++
   long duration;
   int distance;
  • `duration`: Stores the time taken by the ultrasonic pulse to travel to the object and back.
  • `distance`: Stores the calculated distance to the object in centimetres.
C++
   void setup() {
     myServo.attach(9);
     pinMode(trigPin, OUTPUT);
     pinMode(echoPin, INPUT);
     Serial.begin(9600);
   }
  • `myServo.attach(9)`: Attaches the servo motor to pin 9.
  • `pinMode(trigPin, OUTPUT)`: Sets `trigPin` as an output, used to send out the ultrasonic pulse.
  • `pinMode(echoPin, INPUT)`: Sets `echoPin` as an input, used to receive the reflected pulse.
  • `Serial.begin(9600)`: Initializes serial communication at a baud rate of 9600 for debugging purposes.
  • The servo motor is initially tested by moving it to 0°, 90°, and 180° positions with 1-second delays between movements.
C++
   void loop() {
     for (int angle = 0; angle <= 180; angle += 5) {
       myServo.write(angle);
       delay(500);
  • The servo motor sweeps from 0° to 180° in 5-degree increments. The `delay(500)` allows the servo time to reach each position.
C++
     digitalWrite(trigPin, LOW);
     delayMicroseconds(2);
     digitalWrite(trigPin, HIGH);
     delayMicroseconds(10);
     digitalWrite(trigPin, LOW);
  • A 10-microsecond pulse is sent through `trigPin` to initiate the ultrasonic sensor. This pulse triggers the sensor to send out a sound wave.
C++
       duration = pulseIn(echoPin, HIGH);
  • The `pulseIn()` function measures the time it takes for the reflected sound wave to return to the sensor. This time is stored in `duration`.
C++
distance = duration * 0.034 / 2;
  • The distance to the object is calculated based on the time taken by the pulse. The speed of sound is approximately 0.034 cm/µs, and since the pulse travels to the object and back, the time is divided by 2.
C++
       Serial.print("Angle: ");
       Serial.print(angle);
       Serial.print(" degrees, Distance: ");
       Serial.print(distance);
       Serial.println(" cm");
  • The angle and corresponding distance are printed to the Serial Monitor.
C++
     for (int angle = 180; angle >= 0; angle -= 5) {
       myServo.write(angle);
       delay(500);
  • The servo motor sweeps back from 180° to 0°, repeating the process of measuring and displaying the distance.

Summary:

  • The servo motor continuously rotates from 0° to 180° and back, while the ultrasonic sensor measures the distance to objects at each angle.
  • The system calculates the distance based on the time it takes for the sound wave to return after hitting an object.
  • The distance and the angle are displayed on the Serial Monitor, providing real-time data on the object’s distance at various angles.
  • This setup can be used for applications such as scanning an area to detect obstacles, mapping environments, or as part of a more complex robotic system.

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!