Tuesday, September 4, 2018

Ultrasonic Sensor


Ultrasonic Sensor  



The HC-SR04 ultrasonic is sensor used to determine the distance of an object . It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package from 2 cm to 400 cm or 1” to 13 feet.
The operation is not affected by sunlight or black material, although acoustically, soft materials like cloth can be difficult to detect. It comes complete with ultrasonic transmitter and receiver module.


connections with arduino-




Technical Specifications

  • Power Supply − +5V 
  • Ranging Distance − 2cm – 400 cm/1″ – 13ft
  • You will need the following components −
  • 1 × Breadboard
  • 1 × Arduino Uno R3
  • 1 × ULTRASONIC Sensor

code to find the distance of object before ultrasonic sensor -

const int trigPin = 12;
// #define trigpin 12;
const int echoPin = 11;
long microsecondsToCentimeters(long microseconds)
{
  
  return microseconds / 29 / 2;
}
long microsecondsToInches(long microseconds)
{
  
  return microseconds / 75 / 2;
}
void setup()
{
  Serial.begin(9600);
  }
  void loop()
  {
    long duration,inches ,cm;
   pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance

  cm = microsecondsToCentimeters(duration);
  
  Serial.print(cm);
  Serial.print("cm");
  Serial.print("            ");
  inches = microsecondsToInches(duration);
  
  Serial.print(inches);
  Serial.print("inches");
  Serial.println();
 }

                      































































Tech IOT Web Developer

No comments:

Post a Comment