int buzzer = 11;
int irs = 7;
void setup()
{
pinMode(buzzer,OUTPUT);
pinMode(irs,INPUT);
}
void loop()
{
if(digitalRead(irs)==LOW)
{
digitalWrite(buzzer,HIGH);
}
else
digitalWrite(buzzer,LOW);
}
int buzzer = 11;
int irs = 7;
void setup()
{
pinMode(buzzer,OUTPUT);
pinMode(irs,INPUT);
}
void loop()
{
if(digitalRead(irs)==LOW)
{
digitalWrite(buzzer,HIGH);
}
else
digitalWrite(buzzer,LOW);
}
Objective: This is a simple guide on how to make a distance detector using an Arduino, a HC-SRO4 Ultrasonic Sensor, a Buzzer, and some LED's. The ultimate goal of this tutorial is to use the buzzer and LED's to display how far the object is from the ultrasonic sensor.
Materials Needed:
Instructions:
*DISCLAIMER* Resistors are not absolutely necessary for the build, however the are highly recommended to be used. The only reason I am not using them is because I don't have enough resistors.
*DISCLAIMER* It is HIGHLY recommended to use a resistor in connecting the shorter leg of the buzzer to the ground channel of the breadboard. This greatly reduces the volume of the buzzer. You don't have to use a resistor, but if you don't, the buzzer will be very loud and quite frankly annoying.
CODE
Now that you have finished the physical setup of the build, now its time for the code. I assume that you already have the Arduino program on your computer, so now all you have to do is copy and paste the code from below.
#define trigPin 7
#define echoPin 6
#define led 13
#define led2 12
#define led3 11
#define led4 10
#define led5 9
#define led6 8
#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(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, 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 <= 30) {
digitalWrite(led, HIGH);
sound = 250;
}
else {
digitalWrite(led,LOW);
}
if (distance < 25) {
digitalWrite(led2, HIGH);
sound = 260;
}
else {
digitalWrite(led2, LOW);
}
if (distance < 20) {
digitalWrite(led3, HIGH);
sound = 270;
}
else {
digitalWrite(led3, LOW);
}
if (distance < 15) {
digitalWrite(led4, HIGH);
sound = 280;
}
else {
digitalWrite(led4,LOW);
}
if (distance < 10) {
digitalWrite(led5, HIGH);
sound = 290;
}
else {
digitalWrite(led5,LOW);
}
if (distance < 5) {
digitalWrite(led6, HIGH);
sound = 300;
}
else {
digitalWrite(led6,LOW);
}
if (distance > 30 || distance <= 0){
Serial.println("Out of range");
noTone(buzzer);
}
else {
Serial.print(distance);
Serial.println(" cm");
tone(buzzer, sound);
}
delay(500);
}