Close
0%
0%

Measure distance with ultrasonic sensor

A simple and easy project that use ultrasonic sensor to measure the distance.

Public Chat
Similar projects worth following
HC-SR04 is a module that uses ultrasound to measure the distance. It looks like a pair of eyes in its appearance, therefore it’s often installed onto robot-vehicle or mechanical bugs to be their eyes.

Ameba RTL8722DM is a low-power dual-band WLAN and Bluetooth Low Energy SoC by Realtek. The RTL8722DM also include memory for Wi-Fi protocol functions and application making it simple for anyone to develop various kind of IoT applications. At the same time it has a wide range of peripheral interfaces. With all these interfaces, it can connect to most of the electronics components like LEDs, temperature and humidity sensors, and so on.

The way the Ultrasonic module works is that first we “toggle high” the TRIG pin (that is to pull high then pull low). The HC-SR04 would send eight 40kHz sound wave signal and pull high the ECHO pin. When the sound wave returns back, it pull low the ECHO pin.

Assume the velocity of sound is 340 m/s, the time it takes for the sound to advance 1 cm in the air is 340*100*10^-6 = 29 us。 The sound wave actually travels twice the distance between HC-SR04 and the object, therefore the distance can be calculated by (time/29) / 2 = time / 58。

More Resources:

If you need additional technical documents or the source code for this project. Please visit the official websites and join the Facebook group and forum.

  • 1 × Ameba RTL8722DM microcontroller
  • 1 × HC-SR04 Electronic Components / Misc. Electronic Components
  • 1 × Dropping resistor / Level Converter Fiber Optics / Emitters

  • 1
    Setup

    The working voltage of HC-SR04 is 5V. When we pull high the ECHO pin to 5V, the voltage might cause damage to the GPIO pin of Ameba. To avoid this situation, we need to drop the voltage as follows:

    We pick the resistors with resistance 1:2, in the example we use 10kΩ and 20kΩ. If you do not have resistors in hand, you can use level converter instead. The TXB0108 8 channel level converter is a suitable example:

  • 2
    Programming

    The programming of the Ameba RTL8722DM can be done using Arduino IDE which makes it very easy to write code and upload code to the Ameba Board.

    Open the sample code in “File” -> “Examples” -> “AmebaGPIO” -> “HCSR04_Ultrasonic

  • 3
    Final Results!

    Verify and upload the code. Press the reset button on Ameba once the uploading is complete. Open the Serial Monitor, the calculated result is output to serial monitor every 2 seconds.

    Note that the HCSR04 module uses the reflection of sound wave to calculate the distance, thus the result can be affected by the surface material of the object (e.g., harsh surface tends to cause scattering of sound wave, and soft surface may cause the sound wave to be absorbed).

View all 3 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates