Elegoo Arduino R3 How To Make A Proximity Sensor Alarm
This build is a motion sensor that is attached to a LED and buzzer. When the motion sensor detects an object in a certain range, the buzzer and LED will react. This build is designed to detect objects inches away.
What you will need:
- Uno R3 Controller Board (Elegoo Recommended)
- Ultrasonic HC-SR04
- Passive Buzzer
- LED
- Resistor
- 830 Tie-Points Breadboard
- (8) Jump Wires
First, you will need to start by adding a motion sensor to your Breadboard. Connect the jump wires according to the coordinates.
( Motn. sens. Grnd – Arduino power Grnd. | Motn. sens. Echo – Arduino digital ~10 | Motn. sens. Trig. – Arduino digital ~9 | Motn. Sens. Vcc. – Arduino 5v)
After you’ve done this, attach the buzzer to the breadboard. Connect the long pin of the buzzer to the digital ~11 and the shorter pin to the other power Grnd. Finally, add the LED to the breadboard. The shorter pin should connect to digital Grnd. The longer pin of the LED should be connected to a resistor and attach the other end of the resistor to a jump wire. The jump wire is connected to digital 13.
We finished the hardware section, on to the software. On an Arduino new draft, copy and paste this code:
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;
// defines variables
long duration;
int distance;
int safetyDistance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input pinMode
(buzzer, OUTPUT); pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
safetyDistance = distance;
if (safetyDistance <= 5){
digitalWrite(buzzer, HIGH)
; digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);
}
// Prints the distance on the Serial Monitor
Serial.print(“Distance: “);
Serial.println(distance);
}