Loading...
Hello, my name is Mrs. Holborow, and welcome to Computing.
I'm so pleased that you've decided to join me for the lesson today.
In today's lesson, we're going to be setting up and using ultrasonic sensors to measure distance on a robot buggy.
Welcome to today's lesson from the unit Using Physical Computing to Create a Robot Buggy.
This lesson is called Ultrasonic Sensors to Measure Distance, and by the end of today's lesson, you'll be able to describe and use ultrasonic sensors to calculate distance.
Shall we make a start? We will be exploring these keywords throughout today's lesson.
Let's take a look at them now.
Ultrasonic, ultrasonic, sound waves that have a frequency higher than the upper limit of human hearing.
Distance, distance, the length of the space between two points.
Sensor, sensor, a device which detects or measures a physical property such as sound or light.
Look out for these keywords throughout today's lesson.
Today's lesson is broken into two sections.
We'll start by describing ultrasonic sensors, and then we'll move on to set up and use ultrasonic sensors.
Let's make a start by describing ultrasonic sensors.
Izzy says, "Hey, Jun, do you know what these three images have in common?" Let's have a look at the images that Izzy is talking about.
Oh, so we've got a bat, a dolphin, oh, and a submarine.
Do you know what these images might have in common? Maybe pause the video whilst you have a think.
Hmm, Jun's not sure.
Izzy says, "They all use ultrasonic sound pulses to sense objects around them." Jun has a great question, "What is ultrasonic sound?" Ultrasonic sound waves are at frequency out of range of human hearing.
This means that humans cannot detect them with their ears.
A sound wave is ultrasonic when its frequency is above 20 kilohertz.
Some animals, such as dolphins, use ultrasonic sound waves to detect objects in the distance, such as prey.
A dolphin emits an ultrasonic pulse.
When the ultrasonic sound wave hits an object, it bounces back.
When the ultrasonic sound wave returns back to the dolphin, it gives a very good indication of how far away the object is.
Time to check your understanding.
I have a question for you.
Ultrasonic sound has a frequency: A, below 20 hertz, B, around 400 hertz, or C, above 20 kilohertz? Pause the video whilst you think carefully about your answer.
Did you select C? Well done.
Ultrasonic sound has a frequency above 20 kilohertz.
Jun says, "It's really cool that some animals can use ultrasonic sound waves to detect distance." I agree, Jun.
It is really cool.
Izzy says, "There's an ultrasonic component we can use in the buggy project to detect the distance of objects." The HC-SR04 is a popular ultrasonic distance sensor used in electronics and robotics projects.
So here's a graphic icon of the sensor.
It is used to measure the distance to an object using ultrasonic sound waves, just like the dolphin we've seen so far in today's lesson.
The ultrasonic sensor has what looks like two eyes.
One is a transmitter, and one is the receiver.
So we've got the transmitter and the receiver.
The transmitter sends out the ultrasonic sound wave similar to how a dolphin does.
The ultrasonic sound wave bounces off an object and returns to the receiver.
By measuring the time it takes for the sound wave to return to the receiver, the distance can be calculated.
Distance is the length of space between two points.
The speed of sound in air at room temperature is around 343 metres per second.
Distance can be calculated by multiplying the speed at the time it has travelled for.
So distance is equal to speed multiplied by time.
For example, if the ultrasonic sensor sends out a sound wave and the echo returns in 0.
01 seconds, we can do this calculation.
So, the speed of sound is equal to 343 metres a second, and the time is 0.
01 seconds.
So we multiply those together.
So 343 multiplied by 0.
01, which gives us the distance of 3.
43 metres.
Jun says, "Wait, doesn't that calculate the total distance the sound travels instead of just the distance to an object?" Maybe pause your video whilst you think about Jun's question.
Izzy says, "That's right, Jun.
Good spot.
We forgot to divide the total distance by 2." Did you spot the error, too? So, to calculate the distance to the object, the total distance must be divided by 2.
So, the total distance from the previous calculation was 3.
43 metres.
The distance to the object is equal to the total distance divided by 2.
3.
43 divided by 2 is equal to 1.
715.
So the distance to the object is 1.
715 metres or 171.
5 centimetres.
Ultrasonic sensors such as the HC-SR04 are used in many real-life applications, such as car parking sensors, automatic doors, robots and industrial manufacturing.
Ultrasonic distance sensors work well with solid objects and can even be used in the dark because they use sound, not light.
However, they don't work well with soft or absorbent materials such as cloth or sponge.
This is because the sound waves don't reflect well off these materials.
Time to check your understanding.
I have a question for you.
Which part of the HC-SR04 emits an ultrasonic sound wave? Is it A, the transmitter, B, the receiver, or C, the ground or GND? Pause the video here whilst you think about your answer.
Did you select A, the transmitter? Well done.
Remember, the receiver is used to collect the returned sound wave.
True or false? Ultrasonic sensors are only used in car parking systems. Pause the video whilst you have a think.
Did you select false? Well done.
Why is it false? Ultrasonic sensors can be used in many other applications such as robotics and industrial manufacturing.
Another true or false for you.
Ultrasonic sensors work well on soft and absorbent materials like cloth and sponge.
Pause the video whilst you have a think.
Did you select false? Well done.
But why is it false? Ultrasonic sensors don't work well with soft or absorbent materials because the sound waves don't reflect well off these materials.
Okay, we're moving on to the first task of today's lesson.
For part one, in your own words, describe how ultrasonic sensors work.
For part two, an ultrasonic sensor sends out a sound wave, and the echo returns in 0.
005 seconds.
Calculate the distance if the speed of sound is 343 metres a second.
Pause the video whilst you complete the task.
How did you get on with the task? Did you manage to answer all of the questions? Well done.
Let's have a look at a sample answer together.
For part one, you are asked in your own words to describe how ultrasonic sensors work.
"Ultrasonic sensors work by using sound waves to detect how far away an object is.
They send out a sound wave from the transmitter that's too high for humans to hear.
When this wave hits something, it bounces back to the receiver sensor.
The sensor measures how long it takes for the sound to return, then uses that time to calculate the distance to the object.
Some animals like bats and dolphins use ultrasonics in a similar way to detect the distance of objects." For part two, an ultrasonic sensor sends out a sound wave and the echo returns in 0.
005 seconds.
Calculate the distance.
So, the total distance is equal to speed multiplied by time.
In this case, that will be 343 multiplied by 0.
005.
So, the total distance is 1.
715 metres or 171.
5 centimetres.
To calculate the distance to the object, we have to divide the total distance by 2.
So 1.
715 divided by 2 is equal to 0.
8575 metres.
So the distance to the object is 0.
8575 metres or around 85.
8 centimetres.
Did you have the correct calculation? If you need to make any corrections, remember, you can pause the video here.
Okay, we've described ultrasonic sensors.
Let's now move on to set up and use ultrasonic sensors.
Jun says, "How can we use an ultrasonic distance sensor in our project?" Maybe pause the video whilst you have a think.
Izzy says, "You can connect it to a microcontroller like the Raspberry Pi Pico." That's a great idea, Izzy.
The HC-SR04 has four pin connections, which are shown on the diagram below.
Vcc is where a five-volt power supply must be connected.
GND is a ground pin and must be connected to ground on the microcontroller.
The Trig pin receives a trigger signal from the microcontroller to emit the ultrasonic pulse.
The Echo pin sends a signal back to the microcontroller when it receives the sound wave back.
Note, it's important to connect the pins of an ultrasonic sensor to the correct places on a microcontroller because each pin has a specific role.
If the pins are connected incorrectly, the sensor won't work, or it could even damage the sensor or the microcontroller.
So take care when getting it set up.
Jun says, "How do I connect it to the Raspberry Pi Pico?" First, connect four female-to-male jumper wires onto the pins of the sensor.
So you can see here we've got the sensor, and we've connected the jumper wires.
Then, connect the wire labelled Trig to GP20 on the Pico.
So you can see here the orange jumper wire is showing the connection from the Trig to the Raspberry Pi Pico in this diagram.
Next, connect the wire marked Echo to GP21 on the Pico.
In the diagram, this is represented with the purple jumper wire.
Then, connect the wire coming from Vcc to any point on the red common power rail.
So this is illustrated with the red jumper wire on the diagram.
So you can see, we've highlighted here the breadboard red power rail, and you can see that we've got a red connection from the Pico to the power rail.
Finally, connect the GND wire or ground wire to the blue common ground rail on the breadboard.
So this is illustrated using a black jumper wire on the diagram and you can see here we've now highlighted the breadboard blue common ground rail, which is connected again by a black jumper wire to the Raspberry Pi Pico.
When you're happy that the sensor is correctly connected to the Pico, you're ready to use the sensor with some test code.
If you're connecting the sensor to another project, such as a buggy, you should position and secure it when you have tested that it works correctly.
So you can see here, the sensor has been added to the front of this buggy in the image.
Time to check your understanding.
I have a question for you.
How many pins does the HC-SR04 ultrasonic sensor have? Is it A, four, B, five, or C, six? Pause the video whilst you have a think.
Did you select four? Well done.
Well remembered.
Which pin of the HC-SR04 ultrasonic sensor is connected to the five-volt power supply on the Raspberry Pi Pico? Is it A, GND or ground, B, Trig, or C, Vcc? Pause the video whilst you think carefully about your answer.
Did you select C? Well done.
The Vcc is used to connect to the five-volt power supply on the Raspberry Pi Pico.
I have a true or false statement for you.
Incorrectly connected pins can damage the ultrasonic sensor or the microcontroller.
Is this true or false? Pause the video whilst you have a think.
Did you select true? Well done.
Remember, we need to carefully connect both components.
Otherwise, we could cause damage.
Okay, we're moving on to our second task of today's lesson and you've done a fantastic job so far, so well done.
For part one, if you can, follow the instructions in Task B Activity 1 to set up the ultrasonic sensor with the Raspberry Pi Pico.
The instructions on how to do this are provided as an additional resource for this lesson.
For part two, if you can, follow the instructions in Task B Activity 2 to use the ultrasonic sensor with some test code.
Again, you can access the instructions on how to do this in the additional material for the lesson.
Pause the video whilst you have a go at the task.
How did you get on? Did you manage to correctly set up the ultrasonic sensor with the Raspberry Pi Pico? Well done.
Here's a diagram of how your sensor should be set up with the Pico.
If you haven't got it quite set up correctly, look carefully at the diagram and maybe make some changes.
For part two, you were asked to follow the instructions in Task B Activity 2 to use the ultrasonic sensor with some test code and add it to your project.
Here is some sample test code.
So, on line one, we have from machine import Pin.
On line two, import utime.
On line four, we have trig is equal to Pin, open brackets, 20, comma, Pin.
OUT, close brackets.
Remember, Trig is what's going to send the ultrasonic signal.
On line five, we have echo is equal to Pin, open brackets, 21, comma, Pin.
IN, close brackets.
We then have a subroutine, which we're defining as sensor.
So within that subroutine, we have trig.
low to turn that off.
We have utime.
sleep_us and 2 in bracket.
We then have trig.
high, which will turn it on and then we have utime.
sleep_us 5, okay? And then on line 12, we have trig.
low to turn trig back off.
On line 14, we have a while loop which says while echo.
value is equal to 0, and then inside that while loop, we have start is equal to utime.
ticks_us, open and close brackets.
Here's some code continued.
So we've got another while loop now, which is checking if the echo.
value is equal to 1.
If it is, then stop is going to be equal to utime.
ticks_us and then we have a new variable called elapsed, which is equal to stop minus start.
So, this is gonna calculate the time that it's taken.
And then, we're using our calculations that we've seen in part one of today's lesson.
So, total distance is equal to round, and then we're doing elapsed multiplied by 0.
0343.
And then we've got ,2 because we're rounding it to two decimal places.
And then, on line 20, we have distance is equal to total_distance divided by 2, and then we've got a print statement to just return that value to the user.
So "Distance from object is ", distance, and then we're putting centimetres at the end.
Finally, on line 23, we have while True, and we're calling the subroutine sensor, and then we have a sleep there for one second as well.
Remember, if you struggled with this test code, you can always pause the video now whilst you correct your code and test your project.
Okay, we've come to the end of today's lesson, Ultrasonic Sensors to Measure Distance.
You've done a fantastic job, so well done.
Let's summarise what we've learned in this lesson.
Ultrasonic sensors measure distance using sound waves.
Ultrasonic sounds are at a frequency above the human ear and cannot be heard.
Ultrasonic sensors work well with solid objects, but not with absorbent or soft materials.
Code is used to control and calculate distance values from ultrasonic sensors.
I hope you've enjoyed today's lesson and I hope you'll join me again soon.
Bye.