Ultrasonic Sensor

  1. Introduction:



    This experiment demonstrates how to use an ultrasonic sensor (HC-SR04) with a Raspberry Pi Pico to measure the distance to the nearest object. The measured distance is displayed in real-time on the serial console, and an LED is used to indicate when an object is within a specified range. This setup is useful in various applications, including robotics, obstacle detection, and proximity sensing.


    1. Scope:



      The scope of this experiment is to demonstrate the integration and functionality of an ultrasonic sensor with a Raspberry Pi Pico microcontroller to measure distances and provide real-time feedback. This includes the detailed steps of connecting the hardware components, writing and uploading embedded C code, and interpreting the output. The experiment aims to showcase how the ultrasonic sensor can be utilized to detect the presence and distance of objects within its range, and how the Raspberry Pi Pico can process and display this information via a serial console. Additionally, an LED indicator is incorporated to provide a visual signal when an object is within a predefined close proximity.


      1. Connection Details:



        To set up the experiment, you need to connect the HC-SR04 ultrasonic sensor and an LED to the Raspberry Pi Pico as follows:

        1. Ultrasonic Sensor (HC-SR04) to Raspberry Pi Pico:

          • VCC: Connect the VCC pin of the HC-SR04 to the 3.3V (out) pin on the Raspberry Pi Pico (Pin 36). This provides the necessary operating voltage for the sensor.

          • GND: Connect the GND pin of the HC-SR04 to a GND pin on the Raspberry Pi Pico (Pin 38). This completes the power circuit.

          • TRIG: Connect the TRIG pin of the HC-SR04 to GPIO 3 (Pin 5) on the Raspberry Pi Pico. This pin will be used to send a trigger signal to the sensor.

          • ECHO: Connect the ECHO pin of the HC-SR04 to GPIO 2 (Pin 4) on the Raspberry Pi Pico. This pin will receive the echo signal, which is used to measure the distance.

        2. LED to Raspberry Pi Pico:

          • Anode (+): Connect the anode (longer leg) of the LED to GPIO 15 (Pin 20) on the Raspberry Pi Pico through a 220Ω resistor. This resistor limits the current flowing through the LED to prevent it from burning out.

          • Cathode (-): Connect the cathode (shorter leg) of the LED to a GND pin on the Raspberry Pi Pico (Pin 18). This completes the circuit for the LED.



            Working Principle

            The HC-SR04 ultrasonic sensor operates by emitting an ultrasonic pulse and measuring the time it takes for the pulse to return after reflecting off an object. Here’s a detailed explanation of how this process works:

            1. Triggering the Sensor:

              • The Raspberry Pi Pico sends a 10-microsecond pulse to the TRIG pin of the HC-SR04 sensor. This pulse instructs the sensor to emit an ultrasonic wave.

            2. Emitting the Ultrasonic Pulse:

              • Upon receiving the trigger pulse, the HC-SR04 emits an ultrasonic pulse at a frequency of 40 kHz. This pulse travels through the air and reflects off any object in its path.

            3. Receiving the Echo:

              • After emitting the pulse, the sensor waits for the echo to return. When the reflected ultrasonic pulse is detected by the sensor, it generates an output signal on the ECHO pin.

            4. Measuring the Echo Pulse Width:

              • The Raspberry Pi Pico measures the duration of the echo pulse. The time taken for the echo to return is directly proportional to the



                    1. Application code:




                    Ultrasonic Code - Download




                    • MicroPython:


                      from machine import Pin, time_pulse_us

                      import time


                      # Set up pins

                      trigger = Pin(3, Pin.OUT)

                      echo = Pin(2, Pin.IN)

                      led = Pin(15, Pin.OUT)


                      # Function to measure distance

                      def measure_distance():

                      # Send a 10us pulse to trigger

                      trigger.low()

                      time.sleep_us(2)

                      trigger.high()

                      time.sleep_us(10)

                      trigger.low()

                      # Measure the duration of the echo pulse

                      duration = time_pulse_us(echo, 1)

                      # Calculate the distance

                      distance = (duration / 2) / 29.1

                      return distance


                      # Main loop

                      try:

                      while True:

                      distance = measure_distance()

                      print("Distance: {:.2f} cm".format(distance))


                      # Light up the LED if distance is less than 10 cm

                      if distance < 10:

                      led.high()

                      else:

                      led.low()

                      time.sleep(1)

                      except KeyboardInterrupt:

                      print("Program stopped")













Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/2)