Distance meter with 6 LEDs using arduino and ultrasonic sensor
This is simple distance meter with 6LEDs. you can replace the last red led with a buzzer. distance range: <5 to 30cm ultrasonic module: HC-SR04 NOTE: Ultrasonic module can detect distance range from 2cm to 450cm but in this project i set the distance range from <5 to 30cm. code: #define trigPin 7 #define echoPin 6 #define led 13 #define led2 12 #define led3 11 #define led4 10 #define led5 9 #define led6 8 int sound = 250; void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(led5, OUTPUT); pinMode(led6, OUTPUT); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance <= 30) { digitalWrite(led, HIGH); sound = 25