Arduino UNO Controlled Servo with Ultrasonic Sensor Activation
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to interface an Arduino UNO with an HC-SR04 Ultrasonic Sensor and a Servo motor. The Arduino UNO is powered by a 9V battery and controls the Servo motor based on the distance measurements from the Ultrasonic Sensor. The purpose of the circuit is to detect objects within a certain range and actuate the Servo motor accordingly, which could be used for applications such as opening a lid when an object is detected nearby.
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
9V Battery
- Description: A standard 9V battery used to power the circuit.
- Pins: -, +
Servo
- Description: An actuator that can rotate to a specified position.
- Pins: gnd, vcc, pulse
HC-SR04 Ultrasonic Sensor
- Description: A sensor that measures distance by emitting ultrasonic waves and measuring the time it takes for the echo to return.
- Pins: VCC, TRIG, ECHO, GND
Wiring Details
Arduino UNO
- 5V: Connected to the VCC pin of the HC-SR04 Ultrasonic Sensor and the vcc pin of the Servo.
- GND: Connected to the GND pin of the HC-SR04 Ultrasonic Sensor, the gnd pin of the Servo, and the negative terminal (-) of the 9V Battery.
- Vin: Connected to the positive terminal (+) of the 9V Battery.
- D10: Connected to the ECHO pin of the HC-SR04 Ultrasonic Sensor.
- D9: Connected to the TRIG pin of the HC-SR04 Ultrasonic Sensor.
- D6: Connected to the pulse pin of the Servo.
9V Battery
- -: Connected to the GND pin of the Arduino UNO.
- +: Connected to the Vin pin of the Arduino UNO.
Servo
- gnd: Connected to the GND pin of the Arduino UNO.
- vcc: Connected to the 5V pin of the Arduino UNO.
- pulse: Connected to the D6 pin of the Arduino UNO.
HC-SR04 Ultrasonic Sensor
- VCC: Connected to the 5V pin of the Arduino UNO.
- TRIG: Connected to the D9 pin of the Arduino UNO.
- ECHO: Connected to the D10 pin of the Arduino UNO.
- GND: Connected to the GND pin of the Arduino UNO.
Documented Code
#include <Servo.h>
const int trigPin = 9; // Trig pin of Ultrasonic Sensor
const int echoPin = 10; // Echo pin of Ultrasonic Sensor
Servo myServo; // Servo motor object
const int servoPin = 6; // Pin for Servo signal
long duration;
int distance;
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 bps
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
myServo.attach(servoPin); // Attach servo motor to pin 6
myServo.write(0); // Start with the servo at 0 degrees (closed lid)
}
void loop() {
// Send out ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the time it takes for the pulse to return
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2; // Convert time to distance in cm
Serial.print("Distance: ");
Serial.println(distance);
// If an object is detected within 10 cm, open the lid
if (distance < 10) {
myServo.write(90); // Move to 90 degrees (open lid)
delay(5000); // Wait for 5 seconds with the lid open
myServo.write(0); // Move back to 0 degrees (close lid)
}
delay(500); // Small delay before next sensor reading
}
The code is written for the Arduino UNO and is responsible for controlling the Servo motor based on the distance measurements from the HC-SR04 Ultrasonic Sensor. The Servo motor is set to move to 90 degrees when an object is detected within 10 cm, indicating an "open lid" position, and then returns to 0 degrees after 5 seconds, indicating a "closed lid" position. The distance is measured by sending an ultrasonic pulse from the TRIG pin and listening for the echo on the ECHO pin. The time it takes for the echo to return is converted into a distance measurement in centimeters.