Ultrasonic Sensor Code Explained for Beginners

The HC-SR04 ultrasonic sensor is a popular choice for measuring distance using an Arduino. It works by emitting bursts of ultrasonic sound and measuring the time it takes for the sound to reflect back to the sensor. This time is then converted to distance using the speed of sound in air.

Connecting the Ultrasonic Sensor to Arduino

To get started with the HC-SR04, you’ll need to connect it to your Arduino board. The sensor has four pins:

  • VCC (power)
  • GND (ground)
  • Trig (trigger)
  • Echo (echo)

The VCC pin should be connected to 5 volts, GND to ground, Trig to a digital output pin (e.g., pin 7), and Echo to a digital input pin (e.g., pin 8).

Measuring Distance with Ultrasonic Sensor

ultrasonic sensor codeexplained for beginners

Once connected, you can use the Arduino’s pulseIn() function to measure the duration of the echo pulse, which is proportional to the distance to the target. This duration can then be converted to distance using the formula:

distance = (duration * 0.034/2)

Where:
duration is the time in microseconds it takes for the ultrasonic pulse to travel to the target and back
0.034 is the speed of sound in air (34,000 cm/s)
– Dividing by 2 gives the one-way distance to the target

To measure within a specific range, you can use conditional statements to check if the measured distance falls within the desired range. For example, the following code measures the distance to the target and only prints the result if it falls within a certain range:

const int minDistance = 10; // minimum distance in cm
const int maxDistance = 30; // maximum distance in cm

int distance = Sonar1.ping_cm();

if (distance >= minDistance && distance <= maxDistance) {
  Serial.print("Distance: ");
  Serial.println(distance);
}

Technical Specifications of HC-SR04 Ultrasonic Sensor

The HC-SR04 ultrasonic sensor has the following technical specifications:

Specification Value
Measuring Angle 15 degrees
Measuring Range 2-400 cm
Accuracy ±1 cm
Operating Frequency 40 kHz
Supply Voltage 5V

The sensor’s trigger pin should be pulsed with a 10 microsecond high signal to initiate a measurement, and the echo pin will output a pulse whose duration is proportional to the distance to the target.

Building a Ultrasonic Distance Sensor with Arduino

To build your own ultrasonic distance sensor using the HC-SR04 and an Arduino, you’ll need the following components:

  • HC-SR04 ultrasonic sensor
  • Arduino board (e.g., Arduino Uno)
  • Breadboard and jumper wires

Connect the sensor to the Arduino as described earlier, and then upload the following code to the Arduino:

const int trigPin = 7;
const int echoPin = 8;

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  long duration, distance;

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034 / 2) / 10; // convert to cm and divide by 10 to get percentage

  if (distance >= 0 && distance <= 100) {
    Serial.print("Distance: ");
    Serial.print(distance);
    Serial.println("%");
  }

  delay(100);
}

This code will measure the distance to the target and print the result as a percentage of the maximum distance (100 cm in this example).

Conclusion

The HC-SR04 ultrasonic sensor is a versatile and affordable sensor that can be used to measure distance in a variety of Arduino-based projects. By understanding the technical specifications and how to connect and program the sensor, you can create your own custom distance-sensing applications.

References