.. note:: Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts. **Why Join?** - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team. - **Learn & Share**: Exchange tips and tutorials to enhance your skills. - **Exclusive Previews**: Get early access to new product announcements and sneak peeks. - **Special Discounts**: Enjoy exclusive discounts on our newest products. - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions. 👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today! .. _ar_ultrasonic: 6.1 - Measuring Distance ====================================== The ultrasonic sensor module works on the principle of sonar and radar systems for determining the distance to an object. * :ref:`cpn_ultrasonic` **Required Components** In this project, we need the following components. It's definitely convenient to buy a whole kit, here's the link: .. list-table:: :widths: 20 20 20 :header-rows: 1 * - Name - ITEMS IN THIS KIT - PURCHASE LINK * - Kepler Kit - 450+ - |link_kepler_kit| You can also buy them separately from the links below. .. list-table:: :widths: 5 20 5 20 :header-rows: 1 * - SN - COMPONENT INTRODUCTION - QUANTITY - PURCHASE LINK * - 1 - :ref:`cpn_pico_w` - 1 - |link_picow_buy| * - 2 - Micro USB Cable - 1 - * - 3 - :ref:`cpn_breadboard` - 1 - |link_breadboard_buy| * - 4 - :ref:`cpn_wire` - Several - |link_wires_buy| * - 5 - :ref:`cpn_ultrasonic` - 1 - |link_ultrasonic_buy| **Schematic** |sch_ultrasonic| **Wiring** |wiring_ultrasonic| **Code** .. note:: * You can open the file ``6.1_ultrasonic.ino`` under the path of ``kepler-kit-main/arduino/6.1_ultrasonic``. * Or copy this code into **Arduino IDE**. * Don't forget to select the board(Raspberry Pi Pico) and the correct port before clicking the **Upload** button. .. raw:: html Once the program is running, the Serial Monitor will print out the distance of the ultrasonic sensor from the obstacle ahead. **How it works?** About the application of ultrasonic sensor, we can directly check the subfunction. .. code-block:: arduino float readSensorData(){// ...} ``PING`` is triggered by a HIGH pulse of 2 or more microseconds. (Give a short ``LOW`` pulse beforehand to ensure a clean ``HIGH`` pulse.) .. code-block:: arduino digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); The echo pin is used to read signal from PING, a ``HIGH`` pulse whose duration is the time (in microseconds) from the sending of the ping to the reception of echo of the object. .. code-block:: arduino microsecond=pulseIn(echoPin, HIGH); The speed of sound is 340 m/s or 29 microseconds per centimeter. This gives the distance travelled by the ping, outbound and return, so we divide by 2 to get the distance of the obstacle. .. code-block:: arduino float distance = microsecond / 29.00 / 2; Note that the ultrasonic sensor will pause the program when it is working, which may cause some lagging when writing complex projects.