keyestudio
Share your creative ideas with us here
By Alisa | 11 June 2024 | 0 Comments

How to Detect Distance Between Module and Obstacle?

Overview
 

Ultrasonic sensor is great for all kind of projects that need distance measurements, avoiding obstacles as examples.

In this project, you will learn how to use a ultrasonic module and micro:bit to detect the distance between the module and an obstacle ahead.
 

Component Introduction
 

Ultrasonic Module:
 

This Ultrasonic detector module HC-SR04 can provide 2cm-450cm non-contact measurement distance, and its ranging accuracy is up to 3mm. It includes an ultrasonic transmitter, receiver and control circuit.
 

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

The basic principle of work as follows:

  1. First pull down the TRIG, and then trigger it with at least 10us high level signal;

  2. After triggering, the module will automatically transmit eight 40KHZ square waves, and automatically detect whether there is a pulse signal back.

  3. If there is a signal back, through the ECHO to output a high level, the duration time of high level is actually the time from sending ultrasonic to returning back.

Test distance = high level duration * velocity of sound (340m/s) * 0.5


Component Required:
 

  • Micro:bit main board*1

  • Keyestudio Micro bit Sensor V2 Shield*1

  • USB Cable*1

  • keyestudio Ultrasonic Module*1

  • Dupont jumper wire*4

  • Premium Battery Holder 6-cell AA*1

  • 1.5V AA Battery*6
     

Connection Diagram
 

21


Test Code 1
 

Img


Code 2
 

Here you should add the package to set the code. Shown as follows:

Then on the bar search the“sonar”, you should see as below.

The code is as follows:

Img


Test Results
 

Connect micro:bit to computer via USB cable and transfer code to micro:bit. Open CoolTerm, click Options and select SerialPort, set COM port and baud rate(115200). Click OK and Connect.

The detected distance value will be displayed as below:

Leave a Reply

Your email address will not be published.Required fields are marked. *
Name
E-mail
Content
Verification code