Labels

Thursday, May 4, 2017

How to program HC-SR04 Ultrasonic sensor with Arduino

HC-SR04 Ultrasonic Sensor




The HC-SR04 sensor use ultrasonic sound to determine distance to object. We can measure from 2cm to 400cm  using HC-SR04 sensor. It operation is not affected by sunlight and black surface , but temperature and humidity can be affected to accuracy of reading. It also hard to detect soft material ex - cloth. HC-SR04 sensor has ultrasonic receiver and transmitter.Here, my goal is to help you to understand how this sensor works and program with arduino .

NOTE :- NewPing is a arduino library, that makes easier to use HC-SR04 with arduino.

HC-SR04 Ultrasonic sensor Technical Specification

HC-SR04

power supply                  :- 5DC
Quiescent Current           :- 2mA
Working Current               :-15mA
Effect Angle                    :<15 degrees
Distance                           :- 2cm to 400cm
Resolution                        :-0.3cm
Trigger input pulse width :-10us
Ultrasonic Frequency       :-40KHz

Pins


VCC  :- arduino 5v pin
TRIG  :- arduino Digital pin
ECHO :- arduino Digital pin
GND  :- arduino GND pin

TRIG pin will be used to send ultrasonic sound and ECHO pin will be used to listen.


Example

int triggerPin= 8;
int echoPin= 7;
long pulsDuration =0;
long distanceCM =0;
long distanceINC =0;
void setup() {
  // put your setup code here, to run once:
  pinMode(triggerPin,OUTPUT);
  pinMode(echoPin,INPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
   digitalWrite(triggerPin,LOW);
   delayMicroseconds(5);
   digitalWrite(triggerPin,HIGH);
   delayMicroseconds(10);
   digitalWrite(triggerPin,LOW);

  pulsDuration =pulseIn(echoPin,HIGH);

  distanceCM = (pulsDuration/2) / 29.1;
  distanceINC = (pulsDuration/2) / 74; 

   Serial.println("CM");
  Serial.println(distanceCM);

   Serial.println("INCH");
  Serial.println(distanceINC);

  delay(1000);

   Serial.println("**********************************************");
}








No comments:

Post a Comment