MECH 339 - Lab 4 - Ultrasonic Sensor (Fall2023)
MECH 339 - Lab 4 - Ultrasonic Sensor (Fall2023)
Objectives
By the end of this lab, you will have acquired the following skills:
To learn how to use a microcontroller and Arduino IDE to measure distance from an object using
ultrasonic sensor.
Needed Material
1. Teensy or Arduino
2. Ultrasonic Sensor
HC-SR04
Ultrasonic Sensor
3. Buzzer
4. Breadboard
Microcontroller
5. USB Cable
7. Wires
USB Cable
Buzzer
MECH 339 3
Procedure
Please refrain from making connections when the power is on. Always remember to turn
Experimental Procedures
2. Find the datasheet of the ultrasonic sensor you are using and complete the table below.
Working Voltage
Working Current
Working Frequency
Max Range
Min Range
3. Establish the wiring connection between the ultrasonic sensor and your microcontroller as
follows:
MECH 339 4
Modify the pins numbers code accordingly with the microprocessor you are using
5. Write the following sketch to calculate the distance between the sensor and the object. Save
Take a screenshot of the Serial Monitor and add to your lab report.
a. Echo and Trig pins are defined as pins number 11 and 12 on the microcontroller
b. The travel time that we will get from the sensor is assigned to the variable duration
c. The calculated distance is assigned to the variable distance with type integer.
d. In the setup, the trigPin is defined as an output and the echoPin as an Input. Also start
the serial communication is opened for showing the results on the serial monitor.
In the loop
MECH 339 6
e. Set the trigPin on HIGH State for 10 µs to generate the Ultrasound wave.
f. Use the pulseIn() function you must read the travel time and put that value into the
variable “duration”.
i. pulseIn() function has 2 parameters, the first one is the name of the echo pin
and for the second one you can write either HIGH or LOW.
ii. In this case, HIGH means that the pulsIn() function will wait for the pin to go
HIGH caused by the bounced sound wave and it will start timing, then it will
wait for the pin to go LOW when the sound wave will end which will stop the
timing. At the end, the function will return the length of the pulse in
microseconds.
g. Multiply the duration by 340 and divide it by 20 000 to get the distance in cm (see
appendix A).
h. Print the value of the distance and duration of the Echo pin on the Serial Monitor.
7. Connect the ultrasonic sensor to the channels one and two of the oscilloscope as shown
below.
MECH 339 7
8. Configure the oscilloscope with a timescale of 100 microseconds and a voltage scale of 1
10. Calculate the error between the measurement of Channel 1 and the trigerTime set in the
program.
𝐸𝑟𝑟𝑜______________________________________
|𝑀𝑒𝑎𝑠𝑢𝑟𝑒𝑑 − 𝑡𝑟𝑖𝑔𝑒𝑟𝑇𝑖𝑚𝑒|
𝐸𝑟𝑟𝑜𝑟 = × 100
𝑡𝑟𝑖𝑔𝑒𝑟𝑇𝑖𝑚𝑒
MECH 339 8
11. Calculate the error between the measurement of Channel 2 and the duration printed on the
𝐸𝑟𝑟𝑜______________________________________
|𝑀𝑒𝑎𝑠𝑢𝑟𝑒𝑑 − 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛|
𝐸𝑟𝑟𝑜𝑟 = × 100
𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛
Be sure to incorporate the measured and calculated values into your lab report
12. Measure the distance between the falling edge of the trigger and the rising edge of Channel
13. Identify the type of buzzer you will be using. Is it active or linear?
MECH 339 9
14. Find the datasheet of the ultrasonic sensor you are using and complete the table below.
Rated Voltage
Operating Voltage
Rated current
Resonant Frequency
15. Build the circuit as shown below if you are using active a buzzer.
The code makes the buzzer produce a gradually increasing and then decreasing pitch
sound, with a pause in between. It demonstrates how to control the frequency of a buzzer using a
microcontroller.
The first for loop starts with a frequency of 200 and increases it by 1 each time until it
reaches 800.
Inside the loop, tone() generates a tone with the current frequency on the 'buzzerPin', and
After reaching 800Hz, there is a delay(4000) which pauses the program for 4 seconds.
The second for loop starts with a frequency of 800 and decreases it by 1 each time until it
reaches 200.
MECH 339 11
Again, tone() generates a tone with the current frequency on the 'buzzerPin', and
Keep in mind that the sound wave will persist until either the microcontroller is powered
off or the buzzer is disconnected. Another way to stop the sound is by using the function
noTone(). For further information on the tone() function, please refer to the link provided below.
LinkToToneFunctionDetails
Lab Challenge
Revise the ultrasonic sensor code to trigger the buzzer when the distance is equal to or
1. Add comments to explaining your code, algorithms, or any parts that might not be
To submit your results, please adhere to the provided lab template. The
submission dropbox will remain open for a duration of one week
MECH 339 12
Ultrasonic Sensor
The ultrasonic sensor employs sound waves to precisely detect objects and measure
distances. It emits ultrasonic waves and then transforms them into electronic signals for
Principle
This sensor operates by emitting a sound wave and measuring the time it takes for the
wave to return to the ultrasonic sensor. This enables it to determine the distance between objects
The HC-SR04 Ultrasonic Ranging Module combines both an ultrasonic transmitter and a
receiver. The transmitter transforms electrical signals (electric energy) into high-frequency sound
waves (mechanical energy) beyond the range of human hearing. Conversely, the receiver
performs the reverse function. Below, you can find an image and a diagram illustrating the HC-
The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground
and the VCC pins of the module need to be connected to the Ground and the 5 volts pins on the
Teensy Board respectively and the trig and echo pins to any Digital I/O pin on the Teensy Board.
MECH 339 13
Pin description:
The Ultrasonic Ranging Module operates on the principle that ultrasonic waves reflect
upon encountering obstacles. This is achieved by measuring the time interval between the
transmission and reflection of the ultrasonic wave after it encounters an obstacle. The counting of
this time interval concludes upon receiving the reflected ultrasonic wave. The resulting time
difference (∆𝑡) represents the total duration of the ultrasonic wave's round trip from transmission
to reception.
Given the constant speed of sound in air, V = 340m/s, we can calculate the distance (s)
between the Ultrasonic Ranging Module and the obstacle using the formula S = V∆𝑡 /2.
MECH 339 14
To generate the ultrasound, you need to set the Trig on a High State for 10 µs. That will
send out an 8-cycle sonic burst which will travel at the speed of sound, and it will be received in
the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.
For example, if the object is 10 cm away from the sensor, and the speed of the sound is
340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you
will get from the Echo pin will be double that number because the sound wave needs to travel
forward and bounce backward. So, to get the distance in cm we need to multiply the received
travel time value from the echo pin by 0.034 and divide it by 2.
Buzzer
A buzzer is a device used for audio signaling. Buzzers are classified into two main types:
active and passive (as described below). When you position two buzzers with their pins facing
upward, you'll notice that the one featuring a green circuit board is classified as a passive buzzer.
In contrast, the other one, distinguished by a black tape in lieu of a board, is an active buzzer, as
illustrated below.
MECH 339 15
An active buzzer contains a built-in oscillating source, producing sound when powered.
On the other hand, a passive buzzer lacks this source and will not produce sound if supplied with
DC signals. Instead, it requires square waves with frequencies ranging between 2 kHz and 5 kHz
for operation. Active buzzers tend to be pricier than passive ones due to the inclusion of multiple
built-in oscillating circuits. There are many circuits and crystal oscillator elements inside active
buzzers; all of this is usually protected with a waterproof coating (and a housing) exposing only
its pins from the underside.
One significant advantage of passive buzzers is their capacity to allow control over the
tone or pitch of the produced sound. Active buzzers are limited to a single tone, whereas passive
buzzers can produce any tone within their dynamic range.
To generate sound, passive buzzers require a square wave signal. Modifying the
frequency of this square wave allows you to adjust the pitch of the sound.