HELLO THERE!!!!
How are you, guys? I hope everybody is fine and happy always. This week I am going to share to all of you to a simple system I made while learning the Arduino. I like to call it the reverse parking sensor. Basically, what the system do is that it detect distance(via HC-SR04) and produce readable output(via serial monitor) and audible output(via speaker). The items needed to build this system is:
Hardware:
1.Arduino (I used arduino Leonardo because my Arduino UNO is in another project)
2.HC-SR04 module
3.Piezo speaker
How are you, guys? I hope everybody is fine and happy always. This week I am going to share to all of you to a simple system I made while learning the Arduino. I like to call it the reverse parking sensor. Basically, what the system do is that it detect distance(via HC-SR04) and produce readable output(via serial monitor) and audible output(via speaker). The items needed to build this system is:
Hardware:
1.Arduino (I used arduino Leonardo because my Arduino UNO is in another project)
2.HC-SR04 module
3.Piezo speaker
Software
1.Arduino IDE
Now, the schematics of the circuit is fairly simple as shown below:
The connections:
HC-SR04 to Arduino:
VCC-->5V
GND-->GND
echo-->pin 10
trig-->pin 9
Piezo to Arduino:
Red wire-->pin 4
black wire-->GND
Ok, the circuit assembly is done, what's next? The code of course!! The Arduino code for this system is a bit long but don't worry because its simple and easy to be understood:
int tonep = 4;
int trigp = 9;
int echop = 10;
int duration;
int distance;
int val = 0;//declare the pin I/O of the circuit
void setup() {
Serial.begin(9600);//serial communication at 9600 baud rate
pinMode(tonep,OUTPUT);
pinMode(trigp,OUTPUT);
pinMode(echop,INPUT);
//declare which pin is I/O
}
void loop()
{
digitalWrite (trigp,LOW);
delayMicroseconds(5);
digitalWrite(trigp,HIGH);
delayMicroseconds(10);
digitalWrite (trigp,LOW);//give the ultrasonic module initial startup
duration = pulseIn(echop,HIGH);
distance = (duration/2)/29.1;//duration formula
while(Serial.available()){
delay(1000);
char c = Serial.read();
distance += c;
}//counter(not compulsory)
if(distance >= 26 &&distance <= 30){//range of distance
val = analogRead(distance);//read the ultrasonic module
Serial.print("depth: ");
Serial.print(distance);
Serial.print("cm");
Serial.println();//print the distance
tone(tonep, 1000,200);//tone for the speaker
delay(1000);//delay so that the reading do not overload
//same goes for other ranges below
}
if( distance >= 20 && distance <= 25){
val = analogRead(distance);
Serial.print("depth: ");
Serial.print(distance);
Serial.print("cm");
Serial.println();
tone(tonep,1000,100);
delay(1000);
}
if( distance >= 10 && distance <= 19){
val = analogRead(distance);
Serial.print("depth: ");
Serial.print(distance);
Serial.print("cm");
Serial.println();
tone(tonep,1000,50);
delay(1000);
}
if(distance >= 1 && distance <= 9){
val = analogRead(distance);
Serial.print("depth: ");
Serial.print(distance);
Serial.print("cm");
Serial.println();
tone(tonep,1000,0);
delay(1000);
}
if (distance > 30 || distance <= 0){
Serial.println("OUT OF RANGE");
val = analogRead(distance);
Serial.print("depth: ");
Serial.print(distance);
Serial.print("cm");
Serial.println();
noTone(tonep);
delay(1000);
}
delay(600);//delay to avoid overloading
noTone(tonep);//notone
}
You can modify many things in the code such as the distance variable and the tone variables.You can also add LED's to enhance the project. After writing the code, upload to the Arduino and test it. Experimental videos are shown below:
Comments
Post a Comment