One day I decided to make Krepak's ultrasonic security system. I followed the instructions, used MaddMatt's modifications to the code, tested it, puzzled over inaccurate measurement, found the calculation error, fixed it, and then got bored. Because I have a LCD, I wanted it to also display the distance read by the ultrasonic sensor. In the following parts of this tutorial, I will explain how the main two components work.
The Ultrasonic SensorSo, I thought I would build this. Wait, this tutorial is confusing. He keeps on changing which wire goes to where. I must reverse-engineer the code.
Some Time Later
Finally. I reverse-engineered that, and then had to check if MaddMatt's modifications to the code changed anything. I'll just upload...
Okay, that is done. Wait, what? That is definitely not 5 centimetres. You've messed up your maths, mate. I'll have a look at this.
Now I have the proper equation. For more information on how to use the HC-SR04, (and a more accurate equation than the one I used, which is still pretty accurate), you can look at this tutorial by Isaac100. Now, you might want the wiring diagram. It is at the bottom, where the schematics go. Note that this is of the finished device. So that is the Ultrasonic Sensor done, now for:
The LCDSo I have finished the ultrasonic sensor, now I need to wire up the LCD. Fortunately there is a good tutorial on how to do such a thing, you can find it here. But I have a problem. Some of the pins that are used in this tutorial are already used by the ultrasonic sensor. I will have to use different ones.
Some time goes by...
It is time for you to build it, using the schematic at the bottom, if you haven't already.
Now that you have wired it up, let's get it working! Insert this code into your Arduino IDE of choice and upload it to the board:
const int trigPin = 2;
const int echoPin = 3;
const int LEDlampRed = 4;
const int LEDlampYellow = 5;
const int LEDlampGreen = 6;
const int buzzer = 7;
int sound = 500;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LEDlampRed, OUTPUT);
pinMode(LEDlampYellow, OUTPUT);
pinMode(LEDlampGreen, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
long durationindigit, distanceincm;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
durationindigit = pulseIn(echoPin, HIGH);
distanceincm = (durationindigit*0.034) / 2;
if (distanceincm > 50) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed,LOW);
noTone(buzzer);
}
else if (distanceincm <= 50 && distanceincm > 20) {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, LOW);
digitalWrite(LEDlampRed,LOW);
noTone(buzzer);
}
else if (distanceincm <= 20 && distanceincm > 5) {
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampRed,LOW);
tone(buzzer, 500);
}
else if (distanceincm <= 0) {
digitalWrite(LEDlampGreen, LOW);
digitalWrite(LEDlampYellow, HIGH);
digitalWrite(LEDlampRed, LOW);
noTone(buzzer);
}
else {
digitalWrite(LEDlampGreen, HIGH);
digitalWrite(LEDlampYellow, HIGH);
tone(buzzer, 1000);
digitalWrite(LEDlampRed, HIGH);
delay(300);
digitalWrite(LEDlampRed, LOW);
}
Serial.print(distanceincm);
Serial.println(" cm");
delay(300);
}
Thank
you
for
building!
Comments