Arduino UNO Controlled Automated Door with Ultrasonic Sensor and Servo
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit is designed to utilize an Arduino UNO as the central microcontroller to interface with various components including LEDs, a buzzer module, an HC-SR04 ultrasonic sensor, a servo motor, and a power supply consisting of 2x 18650 batteries. The circuit is capable of measuring distance using the ultrasonic sensor and responding to certain distance thresholds by activating LEDs, a buzzer, and a servo motor. A rocker switch is used to control the power supply to the Arduino UNO.
Component List
Arduino UNO
- Microcontroller board based on the ATmega328P
- It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.
LED: Two Pin (red)
- A red light-emitting diode with an anode and cathode pin.
LED: Two Pin (green)
- A green light-emitting diode with an anode and cathode pin.
HC-SR04 Ultrasonic Sensor
- An ultrasonic distance sensor with VCC, TRIG, ECHO, and GND pins.
Servo
- A motor capable of precise position control, with ground (gnd), power (vcc), and control (pulse) pins.
2x 18650 Battery Pack
- A power supply unit consisting of two 18650 batteries with vcc and gnd terminals.
Buzzer Module
- An audio signaling device with ground (GND), power (Vcc), and input/output (I/O) pins.
Rocker Switch
- A switch with two terminals used to control the connection of the power supply to the circuit.
Wiring Details
Arduino UNO
- 5V pin connected to Vcc of Buzzer Module, HC-SR04 Ultrasonic Sensor, and Servo.
- GND pin connected to GND of Buzzer Module, HC-SR04 Ultrasonic Sensor, Servo, red LED, green LED, and 2x 18650 Battery Pack.
- D9 pin connected to anode of red LED.
- D8 pin connected to anode of green LED.
- D10 pin connected to I/O of Buzzer Module.
- D7 pin connected to pulse of Servo.
- D6 pin connected to TRIG of HC-SR04 Ultrasonic Sensor.
- D5 pin connected to ECHO of HC-SR04 Ultrasonic Sensor.
- Vin pin connected to terminal 2 of Rocker Switch.
LED: Two Pin (red)
- Cathode connected to GND.
- Anode connected to D9 of Arduino UNO.
LED: Two Pin (green)
- Cathode connected to GND.
- Anode connected to D8 of Arduino UNO.
HC-SR04 Ultrasonic Sensor
- VCC connected to 5V of Arduino UNO.
- TRIG connected to D6 of Arduino UNO.
- ECHO connected to D5 of Arduino UNO.
- GND connected to GND of Arduino UNO.
Servo
- Gnd connected to GND of Arduino UNO.
- Vcc connected to 5V of Arduino UNO.
- Pulse connected to D7 of Arduino UNO.
2x 18650 Battery Pack
- Gnd connected to GND of Arduino UNO.
- Vcc connected to terminal 1 of Rocker Switch.
Buzzer Module
- GND connected to GND of Arduino UNO.
- Vcc connected to 5V of Arduino UNO.
- I/O connected to D10 of Arduino UNO.
Rocker Switch
- Terminal 1 connected to Vcc of 2x 18650 Battery Pack.
- Terminal 2 connected to Vin of Arduino UNO.
Documented Code
#include <Servo.h>
// Definición de pines
const int trigPin = 6;
const int echoPin = 5;
const int redLedPin = 9;
const int buzzerPin = 10;
const int greenLedPin = 8;
const int servoPin = 7;
Servo myServo;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
myServo.attach(servoPin);
// Estado inicial
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
noTone(buzzerPin); // Detiene el sonido
myServo.write(0); // Posición inicial del servo
}
void loop() {
// Medición de distancia
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration / 58.2; // Convertir tiempo a distancia
// Impresión de variable
Serial.print("Distancia: ");
Serial.print(distance);
Serial.println(" cm");
// Lógica de control
if (distance < 7) {
digitalWrite(redLedPin, LOW);
digitalWrite(greenLedPin, HIGH);
myServo.write(90); // Abrir la puerta
tone(buzzerPin, 220); // Genera un tono de 1000 Hz
delay(1000); // 1segundo de espera
noTone(buzzerPin); // Detiene el sonido
delay(5000); // Mantener abierta por 5 segundos
} else if (distance >= 10) {
digitalWrite(redLedPin, HIGH);
digitalWrite(greenLedPin, LOW);
noTone(buzzerPin); // Detiene el sonido
myServo.write(0); // Cerrar la puerta
}
delay(100); // Espera para estabilizar las lecturas
}
This code is designed to run on an Arduino UNO and controls the circuit's response to distance measurements taken by the HC-SR04 ultrasonic sensor. When an object is detected within 7 cm, the red LED turns off, the green LED turns on, the servo moves to a 90-degree position, and the buzzer emits a tone for 1 second. If the object is further than 10 cm, the red LED turns on, the green LED turns off, the buzzer stops, and the servo returns to the 0-degree position. The system stabilizes for 100 milliseconds before taking another measurement.