Calculating distance with the HC-SR04

0
3080

Introduction:

Module HC SR04, and how important is to  robotics and Arduino projects, so today I bring a test of this module, which will test a small code to measure the distance at which it is an obstacle. Recall that the measurement range of this module is from 2cm to 400cm, so we will put to test  an obstacle to 20cm in front of the ultrasonic sensor and this is enough for now to test the module and verify it’s accurate detection.

 Calculating distance with Arduino

Calculating distance with Arduino shield HC SR04

To perform this little test we require the following materials:

1.  Arduino Mega 2560

2.  HC-SR04 Ultrasonic Sensor

3.  meter or ruler to determine distances

4 Protoboard

5 connection cables

For this test also connect the sensor to 5V Arduino Mega and implement the following code:

HCSR04-code1

In this code we see the implementation of key instructions, as in the case of attachinterrupt()instruction, that allows you to enable interrupts on the Arduino Mega board and micros()  instruction, also allows to take the exact time in micro seconds of the moment that wish,  additionally we use the formula provides by the manufacturer of HC-SR04 sensor that is:

range = high level time * velocity (344M/S) / 2