Introduction
Ultrasonic distance measurement combined with servo motor control is a popular application of Arduino Uno that offers precise distance sensing and object tracking capabilities. In this article, we will explore how to create a distance measurement system using an ultrasonic sensor and control a servo motor accordingly. This DIY project opens up a wide range of possibilities, from robotics to home automation.
Things You Need
- Arduino Uno
- Ultrasonic Sensor HC-SR04
- Servo Motor
- Jumper Wires
- Breadboard
Working Principle
The ultrasonic sensor emits high-frequency sound waves and measures the time it takes for the waves to bounce back after hitting an object. By calculating the time and knowing the speed of sound, the distance to the object can be determined. The Arduino Uno then uses this distance information to control the servo motor’s position.
Circuit Diagram
Code
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 6;
Servo myServo;
void setup() {
Serial.begin(9600);
myServo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance >= 2 && distance <= 400) {
myServo.write(map(distance, 2, 400, 0, 180));
}
delay(200);
}
Troubleshooting
- Double-check the wiring connections of the ultrasonic sensor and servo motor.
- Ensure the ultrasonic sensor is correctly oriented, with the Trig pin connected to the Arduino’s output pin and the Echo pin connected to the input pin.
- Verify that the servo motor is properly powered and connected to the correct pins.
- Make sure you have installed the Servo library in the Arduino IDE.
Conclusion
By combining an ultrasonic sensor and a servo motor with an Arduino Uno microcontroller, you can create a sophisticated distance measurement and servo motor control system. This DIY project enables accurate distance sensing and object tracking, making it ideal for various applications such as robotics, automation, or even motion-activated systems. The Arduino Uno’s versatility, along with its compatibility with various sensors and actuators, allows for endless possibilities in creating interactive and intelligent systems.