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

Arduino UNO Ultrasonic Distance Sensor with LED Indicator

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

Circuit Documentation

Summary

This circuit utilizes an Arduino UNO microcontroller to control an HC-SR04 Ultrasonic Sensor and an Adafruit LED Sequin. The ultrasonic sensor measures distance, and based on the measured distance, the LED's brightness is adjusted. If an object is detected within 15 cm, the LED is set to full brightness. If the object is between 15 cm and 30 cm, the LED is set to half brightness. If the object is farther than 30 cm, the LED is turned off.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor

Adafruit LED Sequin - Emerald Green

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensor

Adafruit LED Sequin - Emerald Green

Code

#define TRIG_PIN 10
#define ECHO_PIN 11
#define LED_PIN 7

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long duration, distance;
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  duration = pulseIn(ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 15) {
    analogWrite(LED_PIN, 255); // Full brightness
  } else if (distance < 30) {
    analogWrite(LED_PIN, 128); // 50% brightness
  } else {
    analogWrite(LED_PIN, 0); // LED off
  }
  delay(100);
}

This code initializes the pins for the ultrasonic sensor and the LED. In the loop function, it measures the distance using the ultrasonic sensor and adjusts the LED brightness based on the measured distance.