กลับไปหน้ารวมไฟล์
rgb-led-based-on-distance-718600-en.md

Project Overview: Visualizing Proximity

Visual feedback is one of the most intuitive ways to interact with sensor data. This project, RGB LED Based on Distance, serves as a foundational tutorial for beginners to bridge the gap between physical sensors and visual actuators. By utilizing an ultrasonic sensor to measure distance, the Arduino changes the color of an RGB LED setup (using individual Red, Green, and Blue LEDs) to indicate how close an object is.

Whether you're building a parking sensor for a garage or a simple experimental setup, this project teaches critical concepts in timing, signal processing, and conditional logic.

Materials & Components

  • Arduino Uno R3: The main microcontroller platform.
  • HC-SR04 Ultrasonic Sensor: A popular "sonar" sensor that uses high-frequency sound pulses to detect objects.
  • 3x 5mm LEDs (Red, Green, Blue): To represent different distance zones.
  • 3x 220-ohm Resistors: Vital for current limiting to protect the LEDs from burnout.
  • Breadboard & Jumper Wires: For easy, solderless circuit prototyping.

Engineering Deep Dive: The Physics of Sound

The HC-SR04 operates on a simple but refined principle of physics. It sends out a sonic burst (Ping) and then waits for the echo to bounce back from an object.

  1. The Trigger: The Arduino sends a 10-microsecond pulse to the trigPin.
  2. The Pulse: The sensor converts this into an eight-cycle sonic burst at 40 kHz.
  3. The Echo: The sound travels at 340 m/s (approx. 0.034 cm/µs). When the pulse hits an object and returns, the echoPin goes HIGH for a duration proportional to the distance.
  4. The Calculation: Distance = (Time × Speed of Sound) / 2. We divide by 2 because the sound traveled to the object and back.
Animation demonstrating the Echo/Ping cycle of the HC-SR04 sensor.

Code Logic Analysis

The provided code is structured neatly into functional blocks:

  • Custom Color Functions: Instead of repeating analogWrite commands in the loop, the author created helper functions like Red(), Green(), and Blue(). This makes the main loop much easier to read and debug.
  • Clamping Mechanism: The clamp() function is a critical addition. It prevents the system from processing "noise" or unrealistic distance values (e.g., negative numbers or distances beyond 50cm where the sensor loses accuracy).
  • Threshold Zoning:
    • Near (1 - 15 cm): Blue LED lights up, indicating very close proximity.
    • Mid (16 - 32 cm): Green LED lights up, indicating a safe middle distance.
    • Far (33 - 50 cm): Red LED lights up, indicating the object is further away but within the monitored range.

Wiring & Setup

The hardware setup is straightforward. The LEDs are connected to PWM-capable pins (3, 4, 5) to allow for brightness control via analogWrite(). The ultrasonic sensor uses pins 8 (trigger) and 9 (echo).

Circuit diagram for the RGB Distance Sensor.

Conclusion

This project is an excellent entry point into the world of "Sensing and Control." By mastering the HC-SR04 sensor and basic LED actuation, you can move on to more complex projects like autonomous robots, smart home security systems, or advanced user interfaces.

ข้อมูล Frontmatter ดั้งเดิม

title: "RGB LED Based on Distance"
description: "Make a circuit that show how far away something is by either shinning a red, green, or blue LED."
author: "codernoob"
category: ""
tags:
  - "hc-sr04"
  - "rgb light based on distance"
  - "rgb lighting"
  - "led lights"
  - "sonar sensor"
views: 11984
likes: 5
price: 699
difficulty: "Easy"
components:
  - "1x Ultrasonic Sensor - HC-SR04 (Generic)"
  - "1x Arduino UNO"
  - "1x LED, Blue Green"
  - "1x Jumper wires (generic)"
  - "1x 5 mm LED: Red"
  - "1x 5 mm LED: Green"
  - "3x Resistor 220 ohm"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles:
  - "https://projects.arduinocontent.cc/da441d7e-fd1b-4b1b-ae98-00f56f0adbb1.ino"
  - "https://projects.arduinocontent.cc/da441d7e-fd1b-4b1b-ae98-00f56f0adbb1.ino"
documentationLinks: []
passwordHash: "4c04e33b7e40a314e0dfabedfc083bae2bc1f15df307288e505b45c2852700b3"
encryptedPayload: "U2FsdGVkX1+hsqcquKrI1dk6SbHGgNGLvs+T16esLwXt8Jv2ESpm2obkwduMWckGtp/a0B8yrTdO35WwjyGlOShaTxHJNeysU9Gl6HwXpUE="
seoDescription: "Build an Arduino circuit that displays Distance using a Red, Green, or Blue LED."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/rgb-led-based-on-distance-718600_cover.jpg"
lang: "en"