Arduino UNO Controlled Servo with Ultrasonic Distance Measurement
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates an Arduino UNO microcontroller with a Servo motor and an HC-SR04 Ultrasonic Sensor. The Arduino UNO controls the Servo motor's position and reads distance measurements from the Ultrasonic Sensor. The Servo motor is controlled via PWM signal from the Arduino, and the Ultrasonic Sensor is used to measure distances by emitting ultrasonic waves and timing their echo. The circuit is designed to sweep the Servo motor across a range of angles, measuring distance at each point and outputting the data through the serial connection.
Component List
Arduino UNO
- Description: A microcontroller board based on the ATmega328P.
- Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13
Servo
- Description: A rotary actuator or linear actuator that allows for precise control of angular or linear position.
- Pins: GND, VCC, PWM
HC-SR04 Ultrasonic Sensor
- Description: An ultrasonic ranging module that provides 2cm to 400cm non-contact measurement functionality.
- Pins: VCC, TRIG, ECHO, GND
Wiring Details
Arduino UNO
- 5V: Connected to VCC of HC-SR04 Ultrasonic Sensor
- GND: Common ground with HC-SR04 Ultrasonic Sensor and Servo
- Vin: Connected to VCC of Servo
- D10: Connected to PWM of Servo
- D9: Connected to ECHO of HC-SR04 Ultrasonic Sensor
- D8: Connected to TRIG of HC-SR04 Ultrasonic Sensor
Servo
- GND: Connected to GND of Arduino UNO
- VCC: Connected to Vin of Arduino UNO
- PWM: Connected to D10 of Arduino UNO
HC-SR04 Ultrasonic Sensor
- VCC: Connected to 5V of Arduino UNO
- TRIG: Connected to D8 of Arduino UNO
- ECHO: Connected to D9 of Arduino UNO
- GND: Connected to GND of Arduino UNO
Documented Code
#include <Servo.h>
const int trigPin = 8;
const int echoPin = 9;
long duration;
int distance;
Servo myServo; // Object servo
void setup() {
pinMode(trigPin, OUTPUT); // trigPin as an Output
pinMode(echoPin, INPUT); // echoPin as an Input
Serial.begin(9600);
myServo.attach(10); // Pin Connected To Servo
}
void loop() {
// Sweep the servo from 15 to 165 degrees
for(int i = 15; i <= 165; i++) {
myServo.write(i);
delay(30);
distance = calculateDistance();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
// Sweep the servo back from 165 to 15 degrees
for(int i = 165; i > 15; i--) {
myServo.write(i);
delay(30);
distance = calculateDistance();
Serial.print(i);
Serial.print(",");
Serial.print(distance);
Serial.print(".");
}
}
int calculateDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
return distance;
}
This code sets up the Arduino UNO to control a Servo motor and read distance measurements from an HC-SR04 Ultrasonic Sensor. The Servo sweeps between 15 and 165 degrees, and the distance is measured at each position. The results are printed to the serial monitor in the format of "angle,distance.".