Intruder Sensor

Aim

To learn how to use an ultrasonic distance sensor to detect any intrusion and alert with a buzzer.

Components required for the experiment

Arduino UNO Board, Ultrasonic distance sensor.

What is Ultrasonic Sensor and where is it used?

An ultrasonic sensor is a device that can measure distance by sending out high-frequency sound waves and timing how long it takes for the waves to bounce back. It works by emitting sound waves at a frequency too high for humans to hear, typically between 40kHz and 200kHz. When the sound waves encounter an object, they bounce back to the sensor, which calculates the distance based on the time taken for the sound waves to return.

Ultrasonic sensors are commonly used in robotics and automation to detect the presence of objects or measure distances. They are widely used in industries like automotive, manufacturing, and healthcare, as well as in consumer electronics, such as parking sensors and smart home devices. Some common applications include measuring liquid levels in tanks, detecting obstacles for autonomous vehicles, and monitoring the position of industrial equipment. Ultrasonic sensors can be found in both analogue and digital versions, and some of the popular ultrasonic sensors available in the market include HC-SR04 and JSN-SR04T.

 

Circuit Diagram

Make the circuit diagram with the exact pin-to-pin connection as shown below to practice. This will help you to complete the experiment successfully.

 

Code

const int TrigPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

void setup()
{
  pinMode(TrigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600); // Starting Serial Terminal
}
void loop()
{
  long duration, inches, cm;
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = duration / 29 / 2;
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(1000);
}

Code Explained

This code displays the distance between the point and the ultrasonic sensor on Serial monitor.

const int TrigPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

These are constant definitions for the trigger pin of the ultrasonic sensor, and the echo pin of the ultrasonic sensor.

void setup()
{
  pinMode(TrigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600); // Starting Serial Terminal
}

This is the setup() function that runs once when the Arduino is turned on. It initializes the serial communication and sets the modes of the TrigPin and echoPin to output and input as needed.

void loop()
{
  long duration, inches, cm;
  digitalWrite(TrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(TrigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  cm = duration / 29 / 2;
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(1000);
}

This is the main loop() function that runs repeatedly. It triggers the ultrasonic sensor by setting the TrigPin to LOW for a short delay of 2 microseconds. Then HIGH for a longer delay, of 10 microseconds, and then back to LOW. The distance is measured by calling the pulseIn() function with the echoPin and HIGH as arguments.

The formula is used to calculate the distance in centimeters and then this distance value is printed on the serial monitor.

Output

  1. Press the start simulation button in the interface.

  2. Move the mouse cursor to the ultrasonic sensor.

  3. Click on the ultrasonic sensor to activate it.

  4. Once activated, you will see a Dot with a range displayed in centimetres.

  5. By moving the dot, the simulator will display the range in centimetres. If the dot goes beyond the specified range, the simulator will show "out of range" on the display.

 

 

 

Conclusion

The distance value in centimetres is displayed on the Serial monitor.

Assignment

Add a buzzer in the circuit and connect it to pin 12 of Arduino UNO. Change the code to turn ON the buzzer for 2 seconds and then turn OFF the buzzer for 1 second if the distance between the black dot and ultrasonic sensor is above 40 cm.