Measuring Distance Over Time With Arduino HC-SR04 Ultrasonic Sensor

6,763

17

7

Introduction: Measuring Distance Over Time With Arduino HC-SR04 Ultrasonic Sensor

The goal of this project is to create an experimental set up based on arduino micro controller and an ultrasonic sensor that will allow us to take data measurements of distance over time. This set up could be used in a school Physics lab to make experiments in motion and kinematics.

Step 1: What You Will Need

For this project you will need the following:

- an Arduino board (I used my Arduino MEGA 2650 board)

- a breadboard

- an ultrasonic sensor HC-SR04

- a LED

- a pushbutton

- a 220 ohm resistor

- a 10K ohm resistor

- jumper wires

Step 2: Set Up the Circuits

Now that you have all the components you need, try to connect them all together as shown in the diagram and the photos.

Step 3: Load the Code to Arduino

The next step is to write the code in the arduino IDE to control the ultrasonic sensor, the pushbutton and the led. The goal is when you push the button the led will light up and the ultrasonic sensor will begin the collection of data and send them to the serial monitor of the arduino IDE. When you press the button again the led will go off and the sensor will stop collecting data.

You can download the code and import to your arduino IDE to use it as you wish. There are a lot of comments that explain what each code line does.

Step 4: Ready to Take Measurements

When you have finished importing the code to arduino IDE. Compile it again to be sure and flash it to your arduino board. Press the button and you will start receiving data. In order to see the data, go to the tools menu of the arduino IDE and select "serial monitor". When you click the button again the data flow stops.

You can then select all data from the serial monitor window by pressing "ctrl+A", and then copy them by "ctrl+C" and paste them (ctrl+V) to an excel file for instance for further manipulation (graphs, analysis etc).

NOTE: sometimes it doesn't function properly, the led goes off on its own and the data flow stops without pressing the button. I am not sure if it is my code or a bouncing effect or something else. If you have any suggestions for eliminating this issue you are welcome to share it with me please.

Enjoy!

Be the First to Share

    Recommendations

    • Electronics Contest

      Electronics Contest
    • Science Fair Challenge

      Science Fair Challenge
    • Tinkercad to Fusion 360 Challenge

      Tinkercad to Fusion 360 Challenge

    7 Comments

    0
    R4Y Arduino
    R4Y Arduino

    Tip 1 year ago

    i would reccomend not putting the button and led system bc its hard to understand for starters

    0
    Fajarmuhr
    Fajarmuhr

    Question 2 years ago

    can you tell me how to use push button but not switch button

    0
    bewley59
    bewley59

    6 years ago

    Nice work! I plan on using it for approach speed warnings on a robotics job.

    0
    orfanak
    orfanak

    Reply 6 years ago

    Great!

    0
    orfanak
    orfanak

    Reply 6 years ago

    thanks!