The HC-ultrasonic sensor SR04 – ARDUINO

The estimated reading time for this post is 239 seconds

One of the initial ideas that I had in mind in the implementation phase of my anti theft was using an ultrasonic sensor for detecting distance.

Then I used the PIR.

Now I decided to study distance sensor and to read some documentation and datasheets.

Measuring with this sensor is made by measuring the time it takes for a sound signal to reach an object and return to the sensor.

The measure is therefore not in terms of distance but in time, in specific microseconds.

HC-sensor SR04 has 4 pin: Vcc (+ 5V), Trigger, Echo, GND.

FullSizeRender

In principle, the operation is as follows:

  1. sends a ping sound Trigger pin
  2. If an obstacle is trapped the wave is reflected back.
  3. the Echo pin receive the signal returned

The time taken allows us to determine the distance between the sensor and the barrier.

How to do it?

To convert the unit of time in space is necessary to know the speed of sound in air: helps wikipedia.

In the air, the speed of sound is to 331.45 m/s at 0° C (equal to 1193.04 km/h) and 343.8 m/s (equivalent to 1237.68 km/h) at 20° C (and generally varies according to the relationship a = + 331.45 0.62 t with t is measured in° C).

Then consider about 343 m/s, expressed in centimeters: cm/0.0343 microseconds.

As space = speed * time (s = v * t) it follows that s = 0.0343 * t.

But we must consider that time identified by the Echo pin is when it departs from the Trigger pin, arrival to the obstacle and back towards the sensor.

Then you will have to split time: 0.0343 s = s * t/2.

The formula we use will be: s = 0.01715 * t

Let now understand how will be connection with Arduino:

Arduino Pin Sensor Pin
POWER 5V VCC
7 digital Trig
Digital 8 Echo
GND GND

SensoreDistanza2 SensoreDistanza1

DistanceSensor_bb

Then the code:

int trigger_Port = 7;
int echo_Port = 8;

void Setup() {
  pinmode (trigger_Port, OUTPUT);
  pinmode (echo_Port, INPUT);
  Serial .begin method (9600);
  Serial.println ("Ultrasonic Distance Sensor:");

}

void loop() {
  output low septum on the trigger pin
  digitalwrite (trigger_Port, LOW);

  send 10 pulse micro seconds on the trigger pin
  digitalwrite (trigger_Port, HIGH);
  delayMicroseconds (10);
  set  low again on the trigger pin
  digitalWrite (trigger_Port, LOW);
  I read the pin duration Echo
  long durata = pulseIn( echo_Port, HIGH );
  calculating the duration
  long r = 0.034 * durata / 2;

  Serial.print( "durata: " );
  Serial.print( durata );
  Serial.print( " ," );
  Serial.print("distanza:" );

  After 38ms is out sensor range
  if( durata > 38000 ) Serial.println( "out of sensor range");
  else {
    Serial.print( r );
    Serial.println( "cm" );
  }

  //wait for 1.0 seconds
  delay (1000);
}

You can also use the new_ping library.

Once you’ve downloaded the library and copied the folder libraries of Arduino, you can simplify writing code:

#include <NewPing.h>

#define trigger_Port 7
#define echo_Port 8
#define max_distance 200

NewPing sonar(trigger_Port, echo_Port, max_distance); 

void setup() {
 Serial.begin(9600);
}

void loop() {
 delay(50);
 unsigned int uS = sonar.ping_cm();
 Serial.print(uS);
 Serial.println("cm");
}

With this library you can also send a ping and get back the average of readings, simply use:

nsigned int uS = sonar.ping_median(numero misurazioni);

#include <NewPing.h>

#define trigger_Port 7
#define echo_Port 8
#define max_distance 200

NewPing sonar(trigger_Port, echo_Port, max_distance); 

void setup() {
 Serial.begin(9600);
}

void loop() {
  delay(50);
  unsigned int durata = sonar.ping_median(5);
  long r = 0.034 * durata / 2;
  Serial.print(r);
  Serial.println("cm");
}

In this case I set 5 the number of measurements. Then there will be the average of 5 measurements.
What can I say … it’s definitely a library as useful as easy to use!

One thought to “The HC-ultrasonic sensor SR04 – ARDUINO”

Rispondi