Challenge

Project 2 - Measuring distance of an object using ultrasonic sensor (HC-SR04)

Objective: To measure the distance of an object using the HC-SR04 ultrasonic sensor by utilizing the Atmega328p's Timer1 in Input Capture Mode. This allows us to measure the pulse width of the echo signal from the ultrasonic sensor, which corresponds to the distance to the object.

Goals

  1. Measure the Distance Using HC-SR04 Ultrasonic Sensor: The sensor emits an ultrasonic wave, which reflects back upon hitting an object. The time taken for this round trip can be used to calculate the distance to the object.
  2. Utilize Input Capture Mode of Timer1: Input Capture Mode on the Atmega328p allows precise measurement of pulse durations, which is essential for calculating the distance accurately.
  3. Distance Measurement Using Echo Timing:
    • The HC-SR04 sensor operates by sending an ultrasonic wave that bounces back when it hits an object. The sensor calculates the time taken for this wave to return as a pulse width on the echo pin.
  4. Input Capture Mode for Pulse Width Measurement:
    • Using the Input Capture Mode of Timer1 on the Atmega328p, the code can accurately measure the time between the start and end of the echo pulse.
    • Timer1 will count clock pulses and trigger an interrupt, capturing the time when the echo pulse goes high and low, allowing calculation of the pulse width.

  5. Deliverables

    1. Measured Distance to Object:
      • Calculate and display the distance by converting the echo pulse duration using the speed of sound.
    2. Pulse Width Measurement:
      • Code utilizing Timer1 in Input Capture Mode to obtain precise pulse widths from the HC-SR04 echo output.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.