How Far Is Mom
This is a simple guide on how to make a distance detector using Arduino, HC-SR04, buzzer, And LED light. The purpose of this project is to let student have a percussion of mom coming. The goal of this tutorial is to use the buzzer and LED's to display how far the object is from the ultrasonic sensor.
Step 1: Materials
(1x) Arduino Leonardo
(1x) Breadboard
(1x) HC-SRO4
(1x)Ultrasonic Sensor
(1x)Buzzer
(2x)Green LEDs
(2x)Yellow LEDs
(2x)Red LEDs
(4x)330 ohm Resistors
Step 2: Setup
The photo above shows the setup of the project. The jumper wires should be connected as follows:
Connect a jumper wire from the 5 volt pin on the Arduino to the bottom channel of the breadboard Connect another jumper wire from a ground pin on the Arduino to the upper channel of the breadboard
LED 1 -> pin 8
LED 2 -> pin 9
LED 3 -> pin 10
Put same color LED on the same pin.
Buzzer -> pin 3(On Ultrasonic Sensor)
Echo -> pin 6 Trig -> pin 7
Step 3: Pakage
Once again referring back to the setup picture, attaching the LED is pretty basic, just with a lot of repetition. The way to connect them is to connect the anode, or the longer leg, or the one on the right, to a pin on the Arduino with a jumper wire, and to connect the cathode, or the shorter leg, or the one on the left, to the ground channel on the breadboard using a 330 ohm resistor. Then just repeat that step for all six of the LED, with the red LED all the way on the right being connected to pin 8 on the Arduino, the anode of the red LED to the left of that one being connected to pin 9 on the Arduino, and so on. The last LED, that being the green LED all the way on the left, should have it's anode, or right leg, connected to pin 13 on the Arduino. Once you have done that, your setup should look something like this.
Step 4: Coding
#define trigPin 7<br>#define echoPin 6 #define led 8 #define led2 9 #define led3 10 #define buzzer 3
int sound = 250;
void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(buzzer, 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 <= 180) { digitalWrite(led, HIGH); sound = 250; } else { digitalWrite(led,LOW); } if (distance < 100) { digitalWrite(led2, HIGH); sound = 260; } else { digitalWrite(led2, LOW); } if (distance < 50) { digitalWrite(led3, HIGH); sound = 270; } else { digitalWrite(led3, LOW); }
if (distance > 50 || distance <= 0){ Serial.println("Out of range"); noTone(buzzer); } else { Serial.print(distance); Serial.println(" cm"); tone(buzzer, sound);
} delay(500); }