Overview
In this section, you will learn how the ultrasonic sensor on your car works and how to read data from it using Python. You can follow along using the ultrasonic_sensor_lesson.ipynb notebook in the units/4.ultrasonic_sensor folder.
What Is An Ultrasonic Sensor?
An ultrasonic sensor measures distance by sending out high-frequency sound waves and listening for the echo. By calculating how long it takes for the sound to bounce off an object and return, the sensor can determine how far away the object is.
Similar technologies include:
- LIDAR → uses light waves
- RADAR → uses radio waves
These sensors are commonly used in vehicles for collision avoidance and spatial awareness.
Importing Libraries and Creating Your Car Object
As with all programming tasks, you must import any needed libraries before using them.
For this lesson you will use:
picarx→ gives you access to the ultrasonic sensortime→ used for small delaysclear_output()fromIPython.display→ keeps the notebook output clean
Before reading the sensor, you must create your Picarx object, which provides access to the ultrasonic sensor.
Using Code Blocks in Python
This lesson introduces you to two major types of code blocks:
- Try/Except Blocks:
- A
tryblock attempts to run a piece of code. - If something goes wrong — or if the user interrupts the program (e.g., stop button or
Ctrl + C) — Python jumps to theexceptblock. - In this lesson, the except block specifically looks for:
- KeyboardInterrupt → triggered by stopping the loop manually
- Try/except blocks help your program exit safely without crashing.
- A
- Loops:
- A loop allows you to repeat code without rewriting it.
- Two major types:
- for loops — repeat a set number of times
- while loops — repeat as long as a condition is true
- This lesson uses a while True loop, which runs forever until a KeyboardInterrupt stops it.
Importance of Indentation
Python uses indentation (spaces at the beginning of lines) to group code into blocks.
Examples:
- Code inside the
tryblock is indented once - Code inside the
whileloop is indented a second time - The
exceptblock is aligned withtry, showing they belong together
Indentation is not just style, it determines the structure of your program. If indentation is incorrect, Python cannot understand your code
Using the Sensor
Once you understand blocks and indentation, here’s what the sample program does:
- Import libraries
- Create the PicarX object
- Enter a
tryblock - Begin a
while Trueloop - Inside the loop:
- Read the
distanceusing the ultrasonic sensor - Clear the previous output
- Display the new reading using a formatted string
- Pause briefly using
sleep
- Read the
- When interrupted, exit the loop and print a closing message
This program continually reports distance until you stop it — and can be adapted into obstacle detection, collision avoidance, or autonomous navigation later in the project.
Important Functions and Concepts (Cheat Sheet)
Take a look at the cheat sheet below for a quick reference of the function and concepts used in this lesson:
- Code Blocks
- Try/Except → attempt code, handle errors or interruptions
- While Loop → run continuously until stopped (infinite loop)
- Functions Used
px.get_distance()→ returns distance in centimetersclear_output(wait=True)→ clears notebook output cleanlyprint(f"...{distance}...")→ formatted print statement using f-strings
What’s Next?
Once you have an understanding of how the ultrasonic sensor works, you can continue on to the next section which will be an activity in which you gather data from the ultrasonic sensor!