Using HCSR04 - Ultrasonic Sensor


How the Ultrasonic Sensor works:
Ultrasonic sensors work by emitting sound waves at a frequency too high for humans to hear. They then wait for the sound to be reflected back, calculating distance based on the time of the journey. This is similar to how radar measures the time it takes a radio wave to return after hitting an object.While some sensors use a separate sound emitter and receiver, it’s also possible to combine these into one package device, having an ultrasonic element alternate between emitting and receiving signals. This type of sensor can be manufactured in a smaller package than with separate elements, which is convenient for applications where size is at a premium.
Requirements:

Hardwares
  • Arduino Uno
  • HCSR04 Module
  • Jumper wires
  • LED series with 200 ohm resister(here we will use in-built led on 13 pin)
Softwares
Circuit Diagram

Code to be run:
//In this project, if a object appears in front of Ultrasonic Sensor with in the range 30-50 cm, the led will glow for 5 seconds.

#include <HCSR04.h>

HCSR04 hc(2,3);//initialisation class HCSR04 (trig pin , echo pin)
const int led= 13;
const int uplimit= 50, lowlimit= 30; //Any object between 30cm to 50cm to trigger led//

void setup()
  Serial.begin(9600); 
  pinMode(13,OUTPUT);
}

void loop()
  int distance = hc.dist();
  Serial.println(distance); 
  if( (distance>=lowlimit) && (distance<= uplimit))//OBJECT IN RANGE=LED ON FOR 5 SECS
  {
    digitalWrite(led, HIGH);
    delay(5000);
    digitalWrite(led, LOW);
  }
}


No comments:

Post a Comment