Teaching Your Raspberry Pi About Echolocation

Jul 15, 2024

Lecture Notes: Teaching Your Raspberry Pi About Echolocation

Introduction

  • Presenter: Paul McCarter from talktechboy.com
  • Episode: #20 in the Raspberry Pi Tutorial Series
  • Sponsor: Sun Founder with their Raphael Kit

Topic Overview

  • Primary Focus: Echolocation
  • Definition: Using sound to detect objects and measure distances.

Examples of Echolocation in Nature and Technology

  • Animals:
    • Bats: Use echolocation to navigate in dark caves.
    • Whales: Employ echolocation for long-distance communication underwater.
  • Technology:
    • Radar: Uses radio waves to detect objects and measure their distances.
    • Sonar: Employs sound waves to detect objects underwater, commonly used in submarines.

Practical Application with Raspberry Pi

  • Device: HC-SR04 Ultrasonic Sensor for echolocation.
  • Setup Instructions:
    • Connect Trigger Pin to GPIO pin 23.
    • Connect Echo Pin to GPIO pin 24.
    • Leftmost pin: Positive Voltage (3.3V)
    • Rightmost pin: Ground
    • Power and Ground rail configuration

Coding the Echolocation Functionality

Libraries and Setup

  • Libraries: RPi.GPIO, time
  • GPIO Mode: BCM numbering system
  • Pin Setup:
    • trig_pin = 23
    • echo_pin = 24
    • GPIO.setup(trig_pin, GPIO.OUT)
    • GPIO.setup(echo_pin, GPIO.IN)

Main Program Loop

  • Use a try-except for clean exit on keyboard interrupt:
    try:
        while True:
            # Ping Send: Set trigger pin to low, wait, set high, wait, set low
            GPIO.output(trig_pin, 0)
            time.sleep(2e-6)
            GPIO.output(trig_pin, 1)
            time.sleep(10e-6)
            GPIO.output(trig_pin, 0)
    
            # Echo Wait: Wait for echo pin to go high, then start timer
            while GPIO.input(echo_pin) == 0:
                pass
            start_time = time.time()
    
            # Timer Stop: Wait for echo pin to go low, then stop timer
            while GPIO.input(echo_pin) == 1:
                pass
            stop_time = time.time()
    
            # Calculate Ping Travel Time
            ping_travel_time = stop_time - start_time
            ping_travel_time_microseconds = int(ping_travel_time * 1e6)
    
            # Print Result
            print(ping_travel_time_microseconds)
    
            # Sleep to avoid ping overlap
            time.sleep(0.2)
    except KeyboardInterrupt:
        GPIO.cleanup()
        print("GPIO Good to Go")
    

Testing and Validation

  • Initial Measurements: Use a ruler to measure and validate ping travel times:
    • E.g., At 3 inches, expect ~385 microseconds; at 1.5 inches, ~159 microseconds.

Homework Assignment

  • Task: Calculate and print the distance to a target based on ping travel time.
  • Hint: Use the speed of sound and convert it to inches per microsecond.
    • Distance = Speed * Time / 2 (divided by 2 since it's a round trip).
  • Submission: Post your solution on YouTube, link it to the lecture video.

Engineering Best Practices

  • Lay Out Plans on Paper: Sketch designs and problem-solving steps before coding.
  • Understand the Concepts: Know the underlying principles to facilitate troubleshooting.

Additional Tips

  • Ensure target is perpendicular to the sensor for accurate measurements.
  • Avoid extraneous objects in the sensor's path.
  • Check the useful range of your sensor through experimentation.

Conclusion

  • Engagement: Encourage feedback and interaction to promote engineering learning.
  • Final Thoughts: Engineering problem-solving over passive consumption.