Arduino Experiment of Ultrasonic Sensor, ToF Laser Range Sensor and Servo Motor, With Raspberry Pi Pico and DumbDisplay

by Trevor Lee in Circuits > Arduino

763 Views, 2 Favorites, 0 Comments

Arduino Experiment of Ultrasonic Sensor, ToF Laser Range Sensor and Servo Motor, With Raspberry Pi Pico and DumbDisplay

IMG_20230715_084359.jpg

In this post, I am going to demonstrate a Raspberry Pi Pico experiment -- with the Arduino framework -- of an ultrasonic sensor and/or a ToF laser range sensor and an optional servo motor, with the help of DumbDisplay as the UI driven by a Raspberry Pi Pico microcontroller board.

It is assumed that you will at least have the ultrasonic sensor (HC-SR04) or the ToF laser range sensor (TOF10120); the servo motor (SG90) is optional. In case a servo motor is not installed, you can still experiment with ultrasonic sensor and/or ToF laser range sensor to detect objects. The servo moto is used in this experiment to rotate the ultrasonic sensor and the ToF range sensors, making them to sweep for 90° (customizable in sketch). The hope is to simulate a radar with such a simple setup.

Although the target microcontroller board is a Raspberry Pi Pico, I believe an ESP32 board should work just fine, if ToF range sensor is not to be supported.

DumbDisplay will be used for the UI -- realized on your Android phone -- of this experiment, driven by the Raspberry Pi Pico. The two are connected together with a USB cable via an OTG adapter, as described in my previous post -- Blink Test With Virtual Display, DumbDisplay. Note that with such a connection, the Raspberry Pi Pico will be powered by your phone; no additional power source is required

UI With Only Ultrasonic Sensor / ToF Laser Range Sensor

pico-distance.png

If you only have an ultrasonic sensor and/or a ToF laser range sensor, without a servo motor, the UI looks a bit simpler.

  • A radar-like canvas on which detected objects are drawn as green dots. Since only the ultrasonic sensor or the ToF range sensor is used at any one time, you actually will ever see a single green dot representing the object in front of the sensor. This line-of-sight is represented by a green line.
  • The chart below the radar-like canvas plots the "measured" distances over time.
  • The 2-digit 7-segment display shows the "measured" distance in centimeters.
  • If you have both the ultrasonic sensor and the ToF range sensor, you can switch between them with the "Ultra Sonic" and the "Laser" buttons.

UI With Servo Motor

pico-radar.png

If you have installed a servo motor, the ultrasonic sensor and/or the ToF range sensor should be sitting on top of it. When it rotates, the sensors will sweep the area in front. As a result, multiple objects can be detected, and they will be shown on the radar-like area as it sweeps. It is kind of like a radar.

Green dots represent the measured distances of the objects. Red dots are supposed to be rough estimations of where the objects should be located.

In addition to the UI components described in the previous section

  • On top of the "Dist" chart is an "Ang" chart which plots the angles of the servo motor over time
  • The servo motor can rotate 1) in a "Single" counter-clockwise direction from 0° to 90° in a loop, or 2) in "Both" directions. (Note that the above screenshot actually shows an angle range from 0° to 120°.)
  • If you choose "No Sweeping", the servo motor will stop rotating. In such a case, you can drive it to rotate with the "slider" UI on top of the "No Sweeping" button.

Ultrasonic Sensor Connection

ultrasonic-connect.png

According ChatGPT

An ultrasonic sensor is a device that uses high-frequency sound waves beyond human hearing range to measure distances, operating on the principle of echolocation, similar to how bats or submarines determine distances. It emits an ultrasonic wave; when this wave hits an object, it's reflected back to the sensor, and by noting the time the reflection takes, the device can calculate the distance to that object. Used widely in fields like automotive, robotics, industry, and security for tasks such as parking assistance, obstacle detection, liquid level sensing, and intruder detection, these sensors primarily consist of a transmitter (to emit waves) and a receiver (to capture the reflection). However, the influence of temperature, wind, and other environmental variables on ultrasonic waves can potentially lead to inaccuracies.

The ultrasonic sensor HC-SR04 uses a 5V input. Hence, the connection can be like

  • Connect Gnd of HC-SR04 to GND of Pico
  • Connect Vcc of HC-SR04 to VBUS of Pico; note that VBUS is the power of the USB, which is 5V
  • Connect Trig of HC-SR04 to GP4 of Pico
  • Connect Echo of HC-SR04 to GP5 of Pico

ToF Range Sensor Connection

tof-connect.png

According to ChatGPT

Time-of-Flight (ToF) range sensor is a type of sensor used primarily for distance measurement and object sensing in various applications, including vehicles. It works by sending a signal or pulse of light towards a target and measuring the time it takes for the signal to bounce back. The time taken corresponds directly to the distance between the sensor and the target. ToF sensors offer substantial benefits, including high accuracy and real-time information, making them highly effective for applications where distance measurement or object detection is key.

The ToF range sensor TOF10120 supports two communication protocols. In this experiment, UART is used. Hence, the connection can be

  • Connect GND of ToF to GND of Pico
  • Connect Vin of ToF to 3.3V of Pico
  • Connect RxD of ToF to GP12 of Pico
  • Connect TxD of ToF to GP13 of Pico

Servo Motor Connection

servo-connect.png

According to ChatGPT

A servo motor is a highly precise motor, controlled by a signal which adjusts its rotation. It uses closed-loop control with a feedback device, like an encoder, to correct discrepancies in positioning. Its precision, power, and size make it suitable for applications from robotics to home automation and RC cars.

The servo motor SG90 also uses a 5V power source. Hence, the connection can be like

  • Connect brown wire (ground) of servo to GND of Pico
  • Connect red wire (Vin) of servo to VBUS of Pico; note that VBUS is the power of the USB, which is 5V
  • Connect orange wire (signal) of servo to GP10 of Pico

Since this component will also be connecting to Pico's VBUS and GND, you may want to use a breadboard or a Raspberry Pico extension board for easier wiring.

The Sketch

You can download the sketch used in this experiment here🔗.

As mentioned previously, you do not need everything -- ultrasonic sensor, ToF range sensor, and servo motor. But you should at least have an ultrasonic sensor or a ToF range sensor

  • If you do not have ultrasonic sensor, comment out US_TRIG_PIN and US_ECHO_PIN
  • If you do not have ToF range sensor, command out TOF_RX_PIN and TOF_TX_PIN
  • If you do not have servo motor, comment out SERVO_PIN
// * you do not need everything, but at least ultrasonic sensor or laser range finder (tof10120)
// * assign pins and/or comment out below pin assignment macros as appropriate
// * - if you do not have ultrasonic sensor, comment out US_TRIG_PIN and US_ECHO_PIN
// * - if you do not have laster range finder (tof10120), command out TOF_RX_PIN and TOF_TX_PING
// * - if you do not have servo, comment out SERVO_PIN

#define US_TRIG_PIN 4
#define US_ECHO_PIN 5
#define TOF_RX_PIN  12
#define TOF_TX_PIN  13
#define SERVO_PIN   10

Due to the UI as well as the options, the sketch is fairly complex. Nevertheless, it should be worthwhile to pinpoint where ultrasonic sensor and ToF range sensor are used to measure distances.

For ultrasonic sensor to measure distance:

In the setup block, set the pin mode of the trigger and echo pins like

void setup() {
...
  pinMode(US_TRIG_PIN, OUTPUT); // Sets the trigPin as an Output
  pinMode(US_ECHO_PIN, INPUT);  // Sets the echoPin as an Input
...
}

The actual measurement is like

    // read ultrasoncic sensor for detected object distance
    digitalWrite(US_TRIG_PIN, LOW);
    delayMicroseconds(2);
    // . set the trigPin on HIGH state for 10 micro seconds
    digitalWrite(US_TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(US_TRIG_PIN, LOW);
    // . read the echoPin, returns the sound wave travel time in microseconds
    long duration = pulseIn(US_ECHO_PIN, HIGH);
    // . calculating the distance ... 0.034: sound travels 0.034 cm per second
    usDistance = duration * 0.034 / 2;

For ToF range sensor to measure distance:

First define a global TOF, which actually is just a macro of a Serial object (Serial3 in this case)

  UART Serial3(TOF_RX_PIN, TOF_TX_PIN, 0, 0); // TX: 12; RX: 13
  #define TOF Serial3

In the setup block, initialize TOF (Serial3) like

  TOF.begin(9600);
  TOF.print("s5-1#"); // set not to auto send distance readins
  • Recall that in this experiment, UART is the means for Pico to communicate with ToF range sensor. I.e. via Serial3
  • The baud rate is 9600
  • The command "s5-1#" sent tells the ToF range sensor not to auto send back distance measurements

Distance measurement is read from the ToF range sensor like

    TOF.print("r6#"); // send command to read distance once ... will get back something like L=156mm
    int count = 0;
    while (count++ < 5)
    {
      String dist = TOF.readStringUntil('\n');
      if (dist.startsWith("L="))
      {
        tofDistance = dist.substring(2, dist.indexOf('m')).toInt() / 10; // dist is like "L=156mm"; divide by 10 to get cm
        break;
      }
    }
  • The comment "r6#" is first sent to the ToF range sensor.
  • The ToF range sensor should respond back a measured distance of the object in front
  • The measured distance read back is in the form "L=156mm"; extract "156", turn it into an Integer, then divide it by 10 to get a centimeter distance.

Rather than using Arduino IDE, I normally use VSCode + PlatformIO for microcontroller development. BTW, I regularly use a trick to trick PlatformIO to support building sketch (.ino) directly as described by my previous post -- A Way to Run Arduino Sketch With VSCode PlatformIO Directly. Nevertheless, using src/main.cpp is as easy.

To build this experiment sketch using PlatformIO:

First, create a PlatformIO project, say PicoRadar

Then replace the content of the project's platformio.ini

[env:pico]
platform = raspberrypi
board = pico
framework = arduino
lib_deps =
  https://github.com/trevorwslee/Arduino-DumbDisplay
  arduino-libraries/Servo@^1.1.8

Note that it specifies two libraries as dependencies -- DumbDisplay and Servo

Finally, replace the content of src/main.cpp with the content of the sketch.

Build and upload!

As for the DumbDisplay Android app, you may want to refer to my previous post -- Blink Test With Virtual Display, DumbDisplay

Enjoy!

Demo of Experiment on Ultrasonic Sensor, ToF Range Sensor and Servo Moto, with Raspberry Pi Pico

Hope that you can have fun with this little experiment. Enjoy!

Peace be with you! May God bless you! Jesus loves you!