3.32 Ultrasonic Ranger

3.32.1 Overview

In this project, we combine the ultrasonic sensor and the OLED module to build a distance meter, whose detection range is 4-300CM.

3.32.2 Test Code

3.32.2.1 Build Code

There are two ways to upload the code: directly open the code file we provide; or manually build blocks.

Directly open the code file we provide:

  1. Click and choose Load from your computer

  1. We have already downloaded the codes on computer desktop, so open the file and choose 3-32-rangeFinder.sb3

Manually build blocks:

  1. Build the two basic blocks:6-1-4-1-1

  2. Initialize OLED module by j53

  3. Add a j54 and set text size to 12x16

  4. Declare a variable distance and assign the value detected by the ultrasonic sensor to distance

QQ_1722046131988

  1. Add a j58 to clear the logo displayed on OLED

  2. Add an OLED module to display “Distance:” on the first line and show the value of distance on the second line. Add the unit of “CM” after the value.

  3. At last, add a refresh block.

    QQ_1722046707083

Complete Test Code

6-32-1-2

3.32.2.2 Test Result

After uploading code, “Distance:” will be displayed on the first line. What followed is the distance value in “CM” in the second line.

6-32-2-2