The circuit in question is designed around an Arduino UNO microcontroller and includes a variety of peripheral components. The primary function of this circuit is to utilize an HC-SR04 Ultrasonic Sensor to detect the presence of an object within a certain distance threshold and to respond by controlling a Servomotor SG90 and two LEDs. The Arduino UNO controls the logic and processing of sensor data, drives the servomotor, and manages the state of the LEDs based on the proximity of an object.
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
const int redLEDPin = 12;
const int greenLEDPin = 11;
const int servoPin = 6;
Servo gateServo;
const int distanceThreshold = 10;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
gateServo.attach(servoPin);
digitalWrite(redLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
gateServo.write(0);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW);
delay(1000);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
if (distance < distanceThreshold) {
digitalWrite(redLEDPin, LOW);
digitalWrite(greenLEDPin, HIGH);
gateServo.write(90);
} else {
digitalWrite(redLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
gateServo.write(0);
}
delay(10);
}
Filename: sketch.ino
Description: This code initializes the Arduino UNO's serial communication and configures the pins connected to the HC-SR04 Ultrasonic Sensor, LEDs, and Servomotor SG90. In the main loop, the ultrasonic sensor measures the distance to an object. If the object is within the defined threshold, the red LED is turned off, the green LED is turned on, and the servomotor is moved to 90 degrees. If the object is outside the threshold, the red LED is turned on, the green LED is turned off, and the servomotor is returned to 0 degrees.