Lab 5: Ping Sensor, Speaker & LED

Class Activity - Create proximity-based audio alerts using laser ping sensor

Learning Goals

  • Work with laser ping distance sensor
  • Implement conditional logic based on sensor readings
  • Control audio output with different volume levels
  • Create proximity-based feedback system

Software and Hardware

  • BlocklyProp Solo
  • Activity Board
  • Parallax USB programming cable
  • Laser Ping Distance Sensor
  • SD card with audio files
  • Speaker/Audio output

Programming Assignment

Create a script that:

  1. Reads laser ping sensor data in centimeters and assigns it to a variable (e.g., distance); please note the sensor is connected to pin 15.
  2. Prints distance in centimeters in the terminal with its unit (e.g., distance: 10 cm).
  3. Makes decisions based on the distance according to the conditions table below.
Laser Ping Sensor Block

Conditional Logic Table

No. Conditions Action
1 If distance ≥ 50 cm Nothing (speaker is OFF)
& Print("distance > 50") in terminal
2 If 30 ≤ distance < 50 cm Play a sound (e.g., techloop.wav) with volume 7
& Print("30 ≤ distance < 50") in terminal
3 If distance < 30 cm Play a sound (e.g., techloop.wav) with volume 10
& Print("distance < 30") in terminal

Step-by-Step Tutorial

1. Setting Up Audio Files

  • Download techloop.wav from Canvas, or you can download a sound from the Sound Library
  • Paste the music files (.wav) onto the SD card of the activity board.

2. Reading Distance Sensor

  • Connect the laser ping sensor to pin 15
  • Use the appropriate BlocklyProp block to read distance in centimeters
  • Store the reading in a variable called distance

3. Implementing Conditional Logic

  • Use if-else statements to check the three distance conditions
  • Print appropriate messages to the terminal for each condition
  • Control audio playback based on distance ranges
  • Set different volume levels (7 for medium range, 10 for close range)

4. Sample Code for Playing Music

Use the following approach to play audio files:

Sample Code for Playing Music

What to Submit

Submit your code saved as a *.svg file with appropriate naming convention:

Lab5_YourInitials_DescriptiveName (e.g., Lab5_AM_distance_audio)

Useful Resources