top of page

Improving Ultrasonics

Ultrasonic sensors like the HC-SR04 are effective tools for measuring distances in non-contact applications. However, their accuracy and reliability can sometimes be affected by factors such as noise, environmental conditions, or improper setup. By implementing improvements using capactiors and coding, you can enhance the sensor's performance, accuracy, and consistency. Techniques like signal filtering and averaging multiple readings can help minimize errors and ensure more reliable measurements.

​

How it works:

  • Power Line Filtering: Adding a 0.1 µF ceramic capacitor and a 10 µF electrolytic capacitor between the sensor's VCC and GND pins helps stabilize the 5V supply, reducing voltage fluctuations and high-frequency noise

​

  • Signal Averaging: The Arduino code takes multiple distance readings and calculates their average, minimizing the impact of transient noise and providing more consistent measurements.

Materials Anchor

Materials

To build a simple improved ultrasonic reading project with an Arduino Uno, you'll need the following materials:

Components:

​

1) Arduino Uno

  • Provides the 5V power line for the ultrasonic sensor and serves as the central microcontroller for processing sensor data.

​

2) HC-SR04 Ultrasonic Sensor

  • Standard ultrasonic sensor module for distance measurement, requiring a stable 5V supply for accurate operation.

​​

3) Capacitors

  • 0.1 µF Ceramic Capacitor: Filters out high-frequency noise from the power line.

  • 10 µF Electrolytic Capacitor: Stabilizes voltage by smoothing out fluctuations on the 5V line.

​​

4) Jumper Wires

  • Flexible wires to connect the components.

​​

5) Breadboard

  • Used for clean and temporary connections during prototyping.

​​​​

6) USB Cable (Type A to B)

  • Connects the Arduino Uno to your computer for programming and power supply.

Setup Anchor

Basic Setup

breadboard_GREY.png
schematic_GREY.png

1) Connect the Ultrasonic Sensor to the Arduino Uno:

  • VCC to 5V: Connect the VCC pin of the ultrasonic sensor to the 5V pin on the Arduino Uno.

​

  • GND to GND: Connect the GND pin of the ultrasonic sensor to the GND pin on the Arduino Uno.

​

  • Trigger to Pin 9: Connect the Trigger pin of the ultrasonic sensor to digital pin 9 on the Arduino Uno.

​

  • Echo to Pin 10: Connect the Echo pin of the ultrasonic sensor to digital pin 10 on the Arduino Uno.

​

2) Add Capacitors for Power Line Filtering:

  • Place a 0.1 µF ceramic capacitor between the VCC and GND pins of the ultrasonic sensor to filter out high-frequency noise.

  • Place a 10 µF electrolytic capacitor between VCC and GND on the Arduino Uno (near the power pins) to smooth out any voltage fluctuations.

​

3) Ensure Proper Grounding:

  • Verify that all GND connections (sensor, Arduino, and power source) are securely connected to avoid ground loops.

Code
code part 1_edited.jpg

CODE BREAK-DOWN

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 the settings.

​

pinMode(trigPin, OUTPUT);

  • Configures the Trigger pin as an output to send ultrasonic pulses.

​

pinMode(echoPin, INPUT);

  • Configures 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 measurements and calculations.

​

int numReadings = 5;

  • Defines the number of readings (5) to average for more accurate distance measurement.

​

float distance = getAverageDistance(numReadings);

  • Calls the getAverageDistance() function, passing numReadings as an argument, and stores the result in the distance variable.

​

if (distance >= 2 && distance <= 400) {

  • Checks if the calculated distance is within the valid range of the ultrasonic sensor (2 cm to 400 cm).

​

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.

​

} else {

  • Executes the following code if the distance is outside the valid range.

​

Serial.println("Out of range");

  • Prints "Out of range" to the Serial Monitor if the reading is invalid.

}
Ends the if-else statement.

​

delay(500);

  • Pauses the program for 500 milliseconds before the next iteration of the loop() function.

​

}
Ends the loop() function.

​

float getAverageDistance(int numReadings) {

  • Defines the getAverageDistance() function, which calculates the average distance from multiple readings.

​

long totalDuration = 0;

  • Initializes a variable to store the total duration of multiple Echo pulses.

​

for (int i = 0; i < numReadings; i++) {

  • Starts a loop to take the specified number of readings.

​

totalDuration += measureDistance();

  • Adds the duration from each call to the measureDistance() function to totalDuration.

​

delay(50);

  • Introduces a small delay (50 ms) between successive measurements to avoid sensor overlap.

​

}
Ends the for loop.

​

float avgDuration = totalDuration / numReadings;

  • Calculates the average duration by dividing the total duration by the number of readings.

​

return avgDuration * 0.034 / 2;

  • Converts the average duration to distance in centimeters and returns the result.

​

}
Ends the getAverageDistance() function.

​

long measureDistance() {

  • Defines the measureDistance() function, which triggers the sensor and measures the Echo pulse duration.

​

digitalWrite(trigPin, LOW);

  • Sets the Trigger pin 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 to send 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.

​

return pulseIn(echoPin, HIGH, 30000);

  • Measures the Echo pin's HIGH signal duration using pulseIn(), with a timeout of 30 ms, and returns the result.

​

}
Ends the measureDistance() function.

bottom of page