from hcsr04 import HCSR04
from machine import Pin
from time import sleep
# LED setup
green_led = Pin(12, Pin.OUT)
red_led = Pin(13, Pin.OUT)
THRESHOLD = 20 # cm
# ESP32
sensor = HCSR04(trigger_pin=5, echo_pin=18, echo_timeout_us=10000)
# ESP8266 (agar use kare)
# sensor = HCSR04(trigger_pin=12, echo_pin=14, echo_timeout_us=10000)
while True:
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
if distance > THRESHOLD:
green_led.on()
red_led.off()
else:
green_led.off()
red_led.on()
sleep(1)
A simple distance-based indication system using the **HC-SR04 Ultrasonic Sensor** and **ESP32**,
developed in **MicroPython**.
The system indicates whether an object is **near or far** using **Red and Green LEDs**.