Lesson 34: Ultrasonic distance
Measure with sound
- An ultrasonic sensor measures distance with sound that is too high for people to hear.
- It sends a short sound from one "eye" and listens for the echo with the other.
- The longer the echo takes, the further away the object is. Cars use this in their parking sensors.
Words for this lesson
| Word | Meaning |
|---|---|
| ultrasonic | sound too high for people to hear |
| echo | sound that comes back from an object |
| pulse | a short HIGH signal on a pin |
| µs | a microsecond: one millionth of a second |
How the sensor works
| Step | What happens |
|---|---|
| 1 | The Arduino sets TRIG HIGH for 10 µs. |
| 2 | The sensor sends the sound. |
| 3 | ECHO stays HIGH until the echo is back. |
| 4 | pulseIn(ECHO, HIGH) gives that time in µs. |
| 5 | time ÷ 58 = distance in cm |
- On the board, the slider puts an object in front of the sensor. Move it and watch the numbers change.
In the exam
A parking sensor is a control system with a sensor, a microprocessor and an actuator (here a buzzer).
- 0478/12/F/M/24 Q6(b)(i): a self-driving tractor stops when a person is within 3 m. Explain how an infra-red sensor, a microprocessor and an actuator do this. The answer has the same steps as your
loop: the sensor sends data, the microprocessor compares it with a stored value, then sends a signal to the actuator, and this repeats.
Dificuldade 4/5 · Moedas 280 + pequeno bônus na primeira conclusão hoje
The ultrasonic sensor has two pins: TRIG (pin 9) and ECHO (pin 10). Send a 10 µs pulse on TRIG, then pulseIn(ECHO, HIGH) gives the time in µs until the echo comes back. Divide by 58 to get centimetres, and print Distance: and the number, 5 times a second. Move the slider to test it.
Clique em Executar para ver a saída aqui.
Dificuldade 5/5 · Moedas 400 + pequeno bônus na primeira conclusão hoje
Make the parking sensor. Write the function readCm() that returns the distance. Then in loop: closer than 20 cm, a long beep (tone(8, 1000)); from 20 to 59 cm, short beeps (tone(8, 1000, 100) and wait 300 ms); 60 cm or more, quiet (noTone(8)).
Clique em Executar para ver a saída aqui.