Ultrasonic sensor sinhala tutorial

Introduction

The HC-SR04 Ultrasonic sensor is one of the most popular components in the Arduino ecosystem. It is widely used in robotics for obstacle avoidance, in smart bins, and for water level monitoring. In this tutorial, we will explore the science behind ultrasonic waves and how to interface this sensor with an Arduino Uno to measure distance accurately.




How the HC-SR04 Ultrasonic Sensor Works

The HC-SR04 operates on the same principle as radar or sonar. It measures distance by sending out a high-frequency sound pulse and timing how long it takes for that pulse to bounce off an object and return to the sensor.

The sensor consists of two main parts:

  1. The Transmitter (Trigger): Sends an ultrasonic sound wave (at 40kHz).
  2. The Receiver (Echo): Listens for the reflected wave.

By knowing the speed of sound in the air and the time taken for the "Echo" to return, we can calculate the distance with high precision.



Technical Specifications

  • Operating Voltage: 5V DC
  • Operating Current: 15mA
  • Measuring Range: 2cm to 400cm (4 meters)
  • Resolution: 0.3cm
  • Effectual Angle: < 15°


Circuit Wiring Diagram

Connecting the HC-SR04 to your Arduino is straightforward. It requires two digital pins: one to trigger the sound and one to listen for the return.

Connections:

  • VCC: Connect to Arduino 5V
  • Trig: Connect to Arduino Digital Pin 2
  • Echo: Connect to Arduino Digital Pin 4
  • GND: Connect to Arduino GND





Understanding the Code:

While the hardware does the "listening," the Arduino does the "calculating." This specific code converts the time it takes for a sound wave to travel into two different units of measurement: Inches and Centimeters.

1. Triggering the Sonic Burst

The code starts by sending a precise pulse to the sensor:

  • digitalWrite(trig, LOW): We clear the pin for 2 microseconds to ensure a clean start.
  • digitalWrite(trig, HIGH) with a 10µs delay: This tells the sensor to fire a burst of 8 ultrasonic pulses at 40kHz.
  • pulseIn(echo, HIGH): This is the most important command. It measures how many microseconds the echo pin stays HIGH. This duration is directly proportional to the distance of the object.

2. Converting Time to Centimeters (cm)

To calculate centimeters, the code uses the line: long cm = t / 29 / 2;

  • Why 29? Sound travels at roughly 340 meters per second. When you convert that to microseconds per centimeter, it takes about 29 microseconds for sound to travel 1 cm.
  • Why divide by 2? The time t represents the trip to the object AND back. We only want the distance to the object, so we divide by two.

3. Converting Time to Inches (in)

To calculate inches, the code uses the line: long inches = t / 74 / 2;

  • Why 74? Sound travels at approximately 1,130 feet per second. In microseconds, it takes about 74 microseconds to travel 1 inch.
  • Why divide by 2? Just like the centimeter calculation, we must divide by two to account for the "round trip" of the sound wave.

4. Outputting to Serial Monitor

Finally, the code uses \t (a tab space) to organize the data neatly in the Serial Monitor. This allows you to see both the imperial (inches) and metric (cm) measurements side-by-side in real-time.

CODE

#define trig 2
#define echo 4

void setup(){
 pinMode(trig,OUTPUT);
 pinMode(echo,INPUT);
 Serial.begin(9600);
}

void loop() {
  digitalWrite(trig,LOW);
  delayMicroseconds(2);
  digitalWrite(trig,HIGH);
  delayMicroseconds(10);
  digitalWrite(trig,LOW);

  long t=pulseIn(echo,HIGH);
  
  long inches=t/74/2;
  long cm=t/29/2;
  
  Serial.print(inches); //6in 15cm
  Serial.print("in\t");
  Serial.print(cm);
  Serial.println("cm");
}


⚠️Troubleshooting: Fixing Common HC-SR04 Errors

If your Serial Monitor is showing "0," "3000," or jumpy numbers that don't make sense, check these common issues and their solutions.

1. The Distance is Stuck at 0 or a Very High Number

This usually happens when the Arduino cannot hear the "Echo" pulse back.

  • Check the Trig/Echo Pins: It is very easy to swap these two wires. Ensure the wire from Trig goes to Pin 2 and Echo goes to Pin 4 (as defined in our code).
  • Verify the Voltage: The HC-SR04 is a 5V device. If you connect it to the 3.3V pin on your Arduino, it may have enough power to light up, but not enough power to send the ultrasonic burst.
  • Loose Wires: On breadboards, the pins of the HC-SR04 are thin. Make sure the sensor is pushed all the way into the breadboard.

2. The Readings are "Jumpy" or Unstable

If the distance jumps from 10cm to 150cm even though the object hasn't moved:

  • Check the Surface: Sound waves reflect best off hard, flat surfaces ( like a wall or a box). If you are pointing the sensor at a curtain, a fluffy carpet, or a soft sweater, the sound is absorbed rather than reflected.
  • The Angle Matters: If the sensor is at an angle to the object, the sound wave will bounce away like a mirror, and the receiver won't hear it. Keep the sensor perpendicular (90°) to the target.
  • Multiple Sensors: If you have two ultrasonic sensors running at the same time nearby, they might "hear" each other’s pulses, causing interference.

3. The Serial Monitor is Blank

  • Baud Rate Mismatch: In the code, we use Serial.begin(9600). Ensure the dropdown menu in the bottom-right corner of your Serial Monitor window is also set to 9600 baud.
  • USB Cable: Some cheap USB cables are for charging only and do not transfer data. Try a different cable if the computer doesn't recognize the Arduino.

4. Mathematical Errors in Distance

  • The "Round Trip" Logic: Remember that if you forget to divide by 2, your distance will be double the actual value. Our code handles this with t / 29 / 2, but if you write your own math, always account for the sound traveling there and back.

Post a Comment

Post a Comment (0)

Previous Post Next Post