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!