Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Ultrasonic Distance Detector with LED Indicators

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Ultrasonic Distance Detector with LED Indicators

Circuit Documentation

Summary

This circuit is designed to utilize an Arduino UNO microcontroller to interface with an ultrasonic sensor and control three LEDs based on the distance measured by the sensor. The ultrasonic sensor is used to detect the presence of objects within a certain range, and if an object is detected within a predefined threshold distance, all three LEDs are turned on. Otherwise, the LEDs remain off. The circuit includes current-limiting resistors for each LED to ensure safe operation.

Component List

Arduino UNO

Ultrasonic Sensor

Resistor (200 Ohms)

LED: Two Pin (red)

Wiring Details

Arduino UNO

Ultrasonic Sensor

Resistor (200 Ohms)

LED: Two Pin (red)

Documented Code

// Define pins
const int trigPin = 9;
const int echoPin = 10;
const int ledPin1 = 11;
const int ledPin2 = 12;
const int ledPin3 = 13;

// Define a threshold distance (e.g., 100 cm)
const int thresholdDistance = 100;

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  
  // Initialize pin modes
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}

void loop() {
  // Clear the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Set the trigPin HIGH for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echoPin
  long duration = pulseIn(echoPin, HIGH);

  // Calculate the distance
  int distance = duration * 0.034 / 2;

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);

  // Turn all LEDs on if an object is within the threshold distance
  if (distance < thresholdDistance) {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
  } else {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
  }

  // Short delay before the next loop
  delay(100);
}

This code is written for the Arduino UNO and is responsible for controlling the ultrasonic sensor and LEDs. It initializes the pins, measures the distance using the ultrasonic sensor, and turns the LEDs on or off based on the measured distance. The serial communication is used for debugging purposes to output the distance values to the Serial Monitor.