Arduino Alert! Design and Deploy Your Own Proximity Alarm System

For this tutorial, I’ll be showing you how to create your own proximity alarm using an ultrasonic sensor, a piezo buzzer and Arduino. To start, you’ll need the following supplies.

Supplies:

Once you’ve gathered all of your supplies, you can start assembling the device. The first step is to connect the ultrasonic sensor to the breadboard and Arduino. You can do that by plugging the ultrasonic sensor into the breadboard and connecting the pins as follows:

  • VCC > 5V

  • Trig > 2 Pin

  • Echo > 3 Pin

  • GND > The Ground Bus on the Breadboard

After you’ve connected the ultrasonic sensor, the next step will be the piezo buzzer. This is a pretty simple process as there are only two wires (or pins) to connect. The power (Red Wire or pin labelled with a + sign) will connect to the 4 pin on the Arduino and the ground (Black Wire or pin labelled with a - sign) will connect to the ground bus on the breadboard. For this project, I connected my buzzer to the breadboard then to the Arduino but you can choose to connect the buzzer directly to the Arduino if you’d like.

From here, all that’s left is to connect the 5V and GND from the Arduino to the breadboard, add the code and test out the device.

To start, you’ll want to define the ultrasonic sensor pins, as well as the buzzer and attach them to their corresponding pins on the Arduino board.

//Setup Ultrasonic Sensor
#define trigPin 2
#define echoPin 3


//Setup Piezo Buzzer
#define alarm 4

Once that’s been done, the next step is to go into the void setup() and set the trig pin and the buzzer to HIGH and the echo pin to LOW. I also chose to start the Serial Monitor, so I could verify that the ultrasonic sensor is actually measuring distances later on.

void setup() {
  Serial.begin(9600);
  //Set Components as input or output
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(alarm, OUTPUT);

}

For the final part of the code, you can go to the void loop() and add the code to tell the ultrasonic sensor to start sending and receiving pulses, print the distance readings in the Serial Monitor and tell the alarm to sound if the sensor detects an object within a certain range.

 //Tell Ultrasonic Sensor to Send/Receive Pulses to Measure Distance
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  float distance = duration/29/2.0;

  Serial.print(distance);
  Serial.println(" CM");
  delay(20);


  if(distance < 50) {
    digitalWrite(alarm, HIGH);
    delay(100);
    digitalWrite(alarm, LOW);
  } else {
    delay(200);
  }
}

Once you’ve finished with the code, you can connect the USB cable to your Arduino board and your computer’s USB port, then click on the Upload button to compile to compile and upload your code.

Once the code finishes uploading, the last step is to make sure that your device works.

And with that, you’ve successfully created your very own proximity alarm, using Arduino. From here, play around with the code a bit and see what happens if you increase or decrease the distance the alarm responds to. You can also connect the device to a battery shield and make it portable, just be sure that you’re following all safety precautions if you decide to go that route.

Now that you have some experience with a piezo buzzer and an ultrasonic sensor, see what else you can create. There is no right or wrong answer as long as you’re learning and not doing anything that could put you or others at risk!

If you need a complete diagram of the device or the complete code to troubleshoot or if you’d just like to see everything laid out in order, you can find both below.

//トラビス

//Setup Ultrasonic Sensor
#define trigPin 2
#define echoPin 3


//Setup Piezo Buzzer
#define alarm 4


void setup() {
  Serial.begin(9600);
  //Connect Components to Pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(alarm, OUTPUT);

}

void loop() {
  //Tell Ultrasonic Sensor to Send/Receive Pulses to Measure Distance
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  long duration = pulseIn(echoPin, HIGH);
  float distance = duration/29/2.0;

  Serial.print(distance);
  Serial.println(" CM");
  delay(20);


  if(distance < 50) {
    digitalWrite(alarm, HIGH);
    delay(100);
    digitalWrite(alarm, LOW);
  } else {
    delay(200);
  }
}