Ultrasonic
An ultrasonic sensor, like the HC-SR04, is a device that measures distance by emitting ultrasonic sound waves and calculating the time it takes for the echo to return. Ultrasonic sensors are widely used in robotics, automation, and obstacle detection due to their simplicity and accuracy. When paired with an Arduino Uno, the ultrasonic sensor can provide distance measurements in real-time.
​
How it works:
When connected to an Arduino Uno:
-
Triggering the Pulse: The Arduino sets the sensor's Trigger pin HIGH for 10 microseconds, prompting the sensor to emit an ultrasonic burst
-
Receiving the Echo: The Echo pin goes HIGH for the duration it takes the sound to travel to the object and back
-
Calculating Distance: The Arduino measures this duration and calculates the distance using the formula:
distance = (duration × 0.034) / 2
Here, 0.034 cm/μs is the speed of sound, and dividing by 2 accounts for the round-trip of the pulse.
Materials
Ultrasonic
To build a simple ultrasonic readings project with an Arduino Uno, you'll need the following materials:
Components:
1) Arduino Uno
-
A microcontroller board based on the ATmega328P. The Arduino Uno serves as the central processing unit, interpreting sensor signals and controlling other components.
​
2) HC-SR04 Ultrasonic Sensor
-
A sensor module used for measuring distances. It features:
-
Trigger Pin: To send ultrasonic pulses.
-
Echo Pin: To receive the reflected signal.
-
VCC and GND Pins: For power input.
-
Measurement Range: Typically 2 cm to 400 cm.
-
Accuracy: ±3 mm.
-
​​
3) Jumper Wires
-
Flexible wires used to make connections between components and the Arduino. Male-to-male wires are commonly used for breadboards and sensors.
​​
4) Breadboard (Optional)
-
A prototyping board for connecting components without soldering, providing an easy way to organize and test circuits.
​​
5) USB Cable (Type A to B)
Connects the Arduino Uno to the computer for power and programming.
Basic Setup


​​​​
1) HC-SR04 to Arduino Uno:
-
Connect the VCC pin of the sensor to the Arduino's 5V pin (provided directly by the Arduino Uno).
-
Connect the GND pin of the sensor to the Arduino's GND pin.
-
Connect the Trigger pin of the sensor to Arduino's Digital Pin 9.
-
Connect the Echo pin of the sensor to Arduino's Digital Pin 10.

CODE BREAK-DOWN
const int trigPin = 9;
-
Defines the digital pin 9 as the pin connected to the Trigger pin of the ultrasonic sensor.
​​
const int echoPin = 10;
-
Defines the digital pin 10 as the pin connected to the Echo pin of the ultrasonic sensor.
​​
void setup() {
-
The setup() function runs once when the program starts and initializes settings.
​​
pinMode(trigPin, OUTPUT);
-
Sets the Trigger pin as an output to send ultrasonic pulses.
​​
pinMode(echoPin, INPUT);
-
Sets the Echo pin as an input to receive the reflected ultrasonic signals.
​​
Serial.begin(9600);
-
Starts communication with the Serial Monitor at a baud rate of 9600 bits per second, enabling data display.
​​
void loop() {
-
The loop() function runs continuously, performing tasks repetitively.
​
digitalWrite(trigPin, LOW);
-
Ensures the Trigger pin is set to LOW to stabilize the signal before sending a pulse.
​
delayMicroseconds(2);
-
Pauses for 2 microseconds to ensure a stable signal.
​
digitalWrite(trigPin, HIGH);
-
Sets the Trigger pin HIGH, generating an ultrasonic pulse.
​
delayMicroseconds(10);
-
Holds the HIGH signal for 10 microseconds to create a valid pulse.
​​
digitalWrite(trigPin, LOW);
-
Stops the pulse by setting the Trigger pin LOW.
​​
long duration = pulseIn(echoPin, HIGH);
-
Measures the time (in microseconds) during which the Echo pin stays HIGH, representing the round-trip travel time of the ultrasonic pulse.
​​
float distance = duration * 0.034 / 2;
-
Calculates the distance to the object in centimeters. The time is multiplied by the speed of sound (0.034 cm/μs) and divided by 2 to account for the round trip.
​​
Serial.print("Distance: ");
-
Prints the text "Distance: " to the Serial Monitor.
​​
Serial.print(distance);
-
Prints the calculated distance value to the Serial Monitor.
​​
Serial.println(" cm");
-
Prints " cm" after the distance value and moves to the next line for the next output.
​​
delay(500);
-
Pauses for 500 milliseconds to control the interval between distance measurements.
