Project 5 Ultrasonic Sensor

1.Description

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like what bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes complete with an ultrasonic transmitter and receiver modules.

Img

The HC-SR04 or the ultrasonic sensor is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications. Here we have brought the simple method to measure the distance with arduino and an ultrasonic sensor and how to use the ultrasonic sensor with Arduino.

2.Specification

  • Working Voltage :+5V DC

  • Quiescent Current : <2mA

  • Working Current: 15mA

  • Effectual Angle: <15°

  • Distance Range : 2cm – 300 cm

  • Precision : 0.3 cm

  • Measuring Angle: 30 degree

  • Trigger Input Pulse width: 10uS

3.Components

Development Board *1

8833 Motor Driver *1

Red LED Module*1

Ultrasonic Sensor*1

img

img

img

img

4P Dupont Wire*1

USB Cable*1

3P Dupont Wire*1

img

img

img

4.Working Principle

As the above picture shown, it is like two eyes. One is transmitting end, the other is receiving end.

The ultrasonic module will emit the ultrasonic waves after triggering a signal. When the ultrasonic waves encounter the object and are reflected back, the module outputs an echo signal, so it can determine the distance of the object from the time difference between the trigger signal and the echo signal.

The t is the time that emitting signal meets obstacle and returns. And the propagation speed of sound in the air is about 343m/s, and distance = speed * time. However, the ultrasonic wave emits and comes back, which is 2 times of distance. Therefore, it needs to be divided by 2, the distance measured by ultrasonic wave = (speed * time)/2.

Use method and chart of ultrasonic module:

1).Use the GPIO pin to give a high level signal of at least 10μs to the Trig pin of SR04, which can trigger it to detect distance.

2).After triggering, the module will automatically send eight 40KHz ultrasonic pulses and detect whether there is a signal return. This step will be completed automatically by the module.

3).If the signal returns, the Echo pin will output a high level, and the duration of the high level is the time from the transmission of the ultrasonic wave to the return.

image-20250509143833078

Circuit diagram of ultrasonic sensor:

5.Wiring Diagram

VCC, Trig, Echo and Gnd of the ultrasonic sensor are connected to 5V(V), D12, D13 and Gnd(G)

6.Test Code

Before writing the code, it is necessary to import the library file of the ultrasonic sensor. The specific steps are as follows:

Click to enter the extension library interface of sensors/modules/components, then search for “Ultrasonic” sensor and click it. In this way, “Not loaded” changes to “loaded”, indicating that “Ultrasonic” sensor was added successfully.

Img

Click to return to the code editor interface, the instruction block of the added “Ultrasonic” sensor can be seen in the module area.

You can drag blocks to edit. Blocks listed below are for your reference.

(1).

(2).

(3).

(4).

(5).

(6).

(7).

Complete Test Code

7.Test Result

After successfully uploading the code to the V4.0 board, connect the wirings according to the wiring diagram, then connect the computer via a USB cable to power the board. After powering on, click to set baud rate to 9600.

The detected distance will be displayed, and the unit is cm and inch. Hinder the ultrasonic sensor by hand, the displayed distance value gets smaller.

8.Extension Practice

We have just measured the distance displayed by the ultrasonic. How about controlling the LED with the measured distance? Let’s try it and connect an LED light module to the D9 pin.

You can drag blocks to edit. Blocks listed below are for your reference.

(1).

(2).

(3).

(4).

(5).

(6).

(7).

Complete Test Code

After successfully uploading the code to the V4.0 board, connect the wirings according to the wiring diagram, then connect the computer via a USB cable to power the board. After powering on, block the ultrasonic sensor by hand(the distance is between 2-10cm), then check if the LED is on.