Skip to content

Latest commit

 

History

History
113 lines (80 loc) · 5.68 KB

ultrasonic_sensor.md

File metadata and controls

113 lines (80 loc) · 5.68 KB

Ultrasonic Sensor

An ultrasonic sensor (range finder / detector) is a type of sensor designed to measure the relative distance between the device and an object that reflects sound waves. This sensor operates by emitting ultrasonic pulses and measuring the time it takes for these pulses to bounce back as echoes. By knowing the speed of sound in air, it is possible to calculate the relative distance to the object based on the time elapsed between the emission of the ultrasonic pulse and the reception of its echo.

Grove-Ultrasonic Ranger V2.0
Grove-Ultrasonic Ranger V2.0

Specifications

Parameter Value
Operating voltage 3.2 ~ 5.2 V
Measuring range 3 - ~200 cm
Size 50 x 25 x 16 mm
Weight 13 g
Measurment angle 15 °
Working temperature -10 ~ 60 °C

NOTE:

  • The measurements showed a relatively high variation above 2 meters, there for the measurment range is limited to 2 meters (with the current driver implementation).

Links

DigiKey
Seeed Studio

Datasheets

Grove-Ultrasonic Ranger V2.0

Practical Tips

  • Do not hot plug the Grove-Ultrasonic Ranger, otherwise it will damage the sensor.
  • Before using the sensor for responsible tasks, make sure that the measurements that are read by the sensor are correct and sufficiently accurate (this might need calibration).

Ultrasonic Sensor Example

The UltrasonicSensor class provides functionality to measure distances by emitting ultrasonic pulses and measuring the time taken for the echo to return. It encapsulates the details of interfacing with the sensor hardware and offers a simple interface for obtaining distance measurements in centimeters. Maximum measurment distance is approximately 2 meters (measured 198.1 cm) with a mearuement period of 12000 microseconds. If no new valid measurement is available, the read() function returns -1.0f.

To start working with the sensor, it is necessary to plug it correclty and create an object in the main.cpp file and assign a digital output.

Connection to the PES Board

The ultrasonic sensor was tested with a 5V power supply and a single pin that transmits the signal. There for, it can use the same ports as the servos. You can use the the following pins:

PB_D0
PB_D1
PB_D2
PB_D3

PES Board pinmap

To establish the connection, utilize the cable displayed in the following image, as it is compatible with the plug and socket on the sensor, and with pins on the PES Board. When connecting the sensor to the PES Board, remember that the yellow wire carries the bidirectional signal. So, make sure to align the blue plug with the PES Board accordingly to ensure a proper connection.

Cables used to connect to the sensor
Cable used to connect to the sensor

Create UltrasonicSensor Object

In the given example, the sensor is plugged into pin D3 on the PES Board. Initially, it's essential to add the suitable driver to the main.cpp file and then create an UltrasonicSensor object inside main() function with the pin's name passed as an argument along with the variable definition that will handle the reading from sensor.

#include "pm2_drivers/UltrasonicSensor.h"
// ultra sonic sensor
UltrasonicSensor us_sensor(PB_D3);
float us_distance_cm = 0.0f;

Details about the driver

By sending a signal to the sensor, we command it to send a pulse with a rising edge followed by a falling edge and a pulse width of 10 microseconds. The device will emit sound with 8 periods of 40 kHz frequency and try to detect the echo (via convolution or similar). The sensor will then send a pulse proportional to the measurment time, which will be detected by the microcontroller. The time between the rising and falling edge is measured and used to calculate the distance on the microcontroller. This process is repeated every 12000 microseconds.

Read the Distance

The operation is straightforward since all processes are encapsulated within the class, yielding a completed function that returns the distance in centimeters. This functionality is accessed through the following command:

// read us sensor distance, only valid measurements will update us_distance_cm
us_distance_cm = us_sensor.read();

If no new valid measurement is available, the read() function returns -1.0f. This needs to be handeled appropriately in the application, as an example:

// read us sensor distance, only valid measurements will update us_distance_cm
const float us_distance_cm_candidate = us_sensor.read();
if (us_distance_cm_candidate > 0.0f) {
    us_distance_cm = us_distance_cm_candidate;
}

NOTE:

  • Do not readout the sensor faster than every 12000 microseconds, otherwise the sensor will report -1.0f frequently.
  • For highly accurate measurements, every sensor unit should be calibrated individually. This depends on the specifications of the task you use the sensor for.