HC-SR04 Ultrasonic Sensor

### Lesson Plan: Using the HC-SR04 Ultrasonic Sensor with the Raspberry Pi Pico WH

**Introduction**

Hey explorers! Today, we’re venturing into the realm of distance measurement with the HC-SR04 Ultrasonic Sensor. This sensor is like a bat’s echolocation system, using sound waves to measure the distance to an object. It’s perfect for projects like obstacle-avoiding robots, distance measurement tools, and even automated parking systems. Ready to measure the world around you? Let’s get started!

**Learning Objectives**

By the end of this lesson, you’ll be able to connect and use the HC-SR04 Ultrasonic Sensor with your Raspberry Pi Pico WH. You’ll learn to measure distances and interpret the data in your projects. This skill is essential for creating interactive and responsive projects. Ready to unlock the power of ultrasonic sensing? Let’s dive in!

**Materials Needed**

For this lesson, you’ll need a Raspberry Pi Pico WH, an HC-SR04 Ultrasonic Sensor, a breadboard, jumper wires, a Micro USB cable, and your computer with a MicroPython IDE. These tools will help you measure distances with ultrasonic precision.

**Background Information**

The HC-SR04 Ultrasonic Sensor is a popular module that uses ultrasonic sound waves to measure distances. It has four pins: VCC, GND, Trig, and Echo. When a trigger signal is sent to the Trig pin, the sensor emits an ultrasonic pulse. The Echo pin then receives the reflected pulse, and the duration of the echo signal is used to calculate the distance to the object. It’s like having a sonar system in your hands!

**Circuit Diagram**

Before we build the circuit, let’s visualize it. The HC-SR04 sensor has four pins: VCC, GND, Trig, and Echo. We’ll connect VCC to the 5V pin on the Raspberry Pi Pico, GND to ground, Trig to a GPIO pin (let’s use GP14), and Echo to another GPIO pin (let’s use GP15).

**Step-by-Step Instructions**

Let’s get building! Start by placing the HC-SR04 sensor on the breadboard. Connect the VCC pin of the sensor to the 5V pin on the Raspberry Pi Pico. Next, connect the GND pin of the sensor to a ground pin on the Raspberry Pi Pico. Connect the Trig pin to GP14 and the Echo pin to GP15 on the Raspberry Pi Pico using jumper wires.

Once your circuit is set up, connect the Raspberry Pi Pico to your computer using the Micro USB cable. Open your MicroPython IDE, and let’s get ready to code.

**Sample Code**

Time to write some code to measure distances with the HC-SR04 sensor. Copy and paste the following code into your IDE and upload it to the Raspberry Pi Pico WH.

“`python
from machine import Pin, time_pulse_us
from time import sleep

# Initialize the HC-SR04 sensor
trigger = Pin(14, Pin.OUT)
echo = Pin(15, Pin.IN)

def measure_distance():
# Send a 10us pulse to trigger the measurement
trigger.low()
sleep(0.000002)
trigger.high()
sleep(0.00001)
trigger.low()

# Measure the duration of the echo pulse
duration = time_pulse_us(echo, 1)

# Calculate the distance in cm
distance = (duration / 2) / 29.1
return distance

while True:
distance = measure_distance()
print(‘Distance: {:.2f} cm’.format(distance))
sleep(1)
“`

This code sets up the HC-SR04 sensor, sends a trigger pulse, and measures the duration of the echo pulse. It then calculates the distance based on the duration and prints the result to the console. It’s like having your own digital tape measure!

**Testing and Troubleshooting**

Upload the code and place an object in front of the sensor. You should see the distance to the object printed to the console. If it’s not working, double-check your connections and make sure the sensor is correctly placed. Verify that the code is correctly uploaded to the Raspberry Pi Pico WH. Still having trouble? Take a deep breath and check your setup again. You’re on the right track!

**Applications and Extensions**

Now that you’ve got the basics down, let’s get creative. Try using the sensor to control an LED that lights up when an object is within a certain range. Or, use it to create an obstacle-avoiding robot. The possibilities are endless, and the HC-SR04 sensor is your gateway to creating interactive and responsive projects.

**Summary and Review**

Fantastic job! You’ve just mastered using the HC-SR04 Ultrasonic Sensor with the Raspberry Pi Pico WH and MicroPython. You learned how to connect the sensor, measure distances, and display the readings. These skills are crucial for building projects that interact with their environment. Keep experimenting, keep building, and most importantly, have fun!

Remember, the best way to learn is by doing, so keep exploring the amazing world of electronics. Happy building, and as always, stay curious!

Post a comment

Leave a Comment

Your email address will not be published. Required fields are marked *