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 sensor
  • time → used for small delays
  • clear_output() from IPython.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:

  1. Try/Except Blocks:
    • A try block 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 the except block.
    • 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.
  2. 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 try block is indented once
  • Code inside the while loop is indented a second time
  • The except block is aligned with try, 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:

  1. Import libraries
  2. Create the PicarX object
  3. Enter a try block
  4. Begin a while True loop
  5. Inside the loop:
    • Read the distance using the ultrasonic sensor
    • Clear the previous output
    • Display the new reading using a formatted string
    • Pause briefly using sleep
  6. 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 centimeters
    • clear_output(wait=True) → clears notebook output cleanly
    • print(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!