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

Arduino UNO Controlled Ultrasonic Distance Sensor with LED Indicator

View and Edit Project In Cirkit DesignerImage of Arduino UNO Controlled Ultrasonic Distance Sensor with LED Indicator

Circuit Documentation

Summary

This circuit integrates an Arduino UNO microcontroller with an HC-SR04 Ultrasonic Sensor and a red two-pin LED. The primary function of this circuit is to measure distance using the ultrasonic sensor and visually indicate the measured distance through the blinking rate of the LED. The LED blinks faster as the distance measured by the sensor decreases.

Component List

Arduino UNO

LED: Two Pin (red)

HC-SR04 Ultrasonic Sensor

Resistor

Wiring Details

Arduino UNO

LED: Two Pin (red)

HC-SR04 Ultrasonic Sensor

Resistor

Documented Code

/*
 * This Arduino sketch controls an LED connected to pin D13. The LED will blink
 * based on the distance measured by the HC-SR04 ultrasonic sensor. The sensor
 * is connected to pins D2 (ECHO) and D3 (TRIG). The LED will blink faster as
 * the measured distance decreases.
 */

const int trigPin = 3; // TRIG pin of HC-SR04
const int echoPin = 2; // ECHO pin of HC-SR04
const int ledPin = 13; // LED connected to pin D13

void setup() {
  pinMode(trigPin, OUTPUT); // Set TRIG pin as output
  pinMode(echoPin, INPUT);  // Set ECHO pin as input
  pinMode(ledPin, OUTPUT);  // Set LED pin as output
  Serial.begin(9600);       // Initialize serial communication
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); // Clear the TRIG pin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); // Set the TRIG pin high for 10us
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH); // Read the ECHO pin
  distance = (duration / 2) / 29.1;  // Calculate distance in cm
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
  int blinkDelay = map(distance, 0, 200, 100, 1000); // Map distance to delay
  blinkDelay = constrain(blinkDelay, 100, 1000); // Constrain delay
  digitalWrite(ledPin, HIGH); // Turn LED on
  delay(blinkDelay);
  digitalWrite(ledPin, LOW);  // Turn LED off
  delay(blinkDelay);
}

This code is designed to be uploaded to the Arduino UNO microcontroller. It initializes the pins connected to the HC-SR04 Ultrasonic Sensor and the LED, then enters a loop where it triggers the sensor to measure distance and blinks the LED with a delay inversely proportional to the measured distance. Serial communication is also established for debugging purposes, allowing the measured distance to be output to the serial monitor.