proximity sensor with raspberry pi - KJT
搜索

proximity sensor with raspberry pi

  • time:2025-06-25 02:43:43
  • Click:0

How to Detect Motion and Distance with a Proximity Sensor and Raspberry Pi

The Raspberry Pi excels as a gateway into the world of physical computing, allowing you to sense and interact with the environment. One of the most practical and engaging sensors you can integrate is a proximity sensor. Wouldn’t it be cool if your Pi project could react when something approaches? That’s exactly the power proximity sensing unlocks – ideal for security systems, interactive displays, automation tasks, or simply as a fascinating learning experience. This guide will walk you through understanding proximity sensors and connecting them effectively to your Raspberry Pi using simple circuitry and Python code.

Proximity sensors encompass a range of technologies, all designed to detect the presence or absence of a nearby object without requiring physical contact. For Raspberry Pi projects, two common and accessible types are:

  1. Infrared (IR) Proximity Sensors (e.g., Sharp GP2Y0A21YK0F): These sensors emit an infrared beam and measure its reflection intensity. The strength of the reflected signal correlates inversely with the distance to the object. They are relatively simple to use but offer limited range (typically up to 10-80 cm) and can be affected by ambient light and object colour/reflectivity.
  2. Ultrasonic Distance Sensors (e.g., HC-SR04): These sensors emit high-frequency sound pulses and measure the time taken for the echo to return. Using the speed of sound, the Raspberry Pi can then calculate the distance accurately. Ultrasonic sensors generally offer better range (typically 2cm - 4m) and are less affected by light or surface colour than IR sensors, though soft materials absorb sound and can be problematic.

For our hands-on example, we’ll focus on the widely available and cost-effective HC-SR04 ultrasonic sensor, demonstrating its connection and programming with the Raspberry Pi.

Connecting the HC-SR04 Sensor to Your Raspberry Pi

The HC-SR04 ultrasonic sensor has four pins: VCC (Power), Trig (Trigger), Echo, and GND (Ground). While the sensor operates at 5V logic, the Raspberry Pi’s GPIO pins are only safe at 3.3V. Connecting the Echo pin directly to a GPIO pin risks damaging your Pi. Therefore, we need a simple voltage divider circuit to reduce the 5V Echo signal down to 3.3V. This is crucial!

Hardware You’ll Need:

  • Raspberry Pi (any model with standard GPIO pins; Pi Zero to Pi 5)
  • HC-SR04 Ultrasonic Sensor Module
  • Breadboard
  • Jumper wires (Male-to-Female recommended)
  • 1x 1kΩ Resistor
  • 1x 2kΩ Resistor (A 470Ω + 1kΩ combination is a common alternative if a 2kΩ isn’t available)

Circuit Connection Guide:

  1. Power (VCC) Connection: Connect the HC-SR04 VCC pin to a 5V pin on your Raspberry Pi (e.g., physical pin 2).
  2. Ground (GND) Connection: Connect the HC-SR04 GND pin to a Ground (GND) pin on your Raspberry Pi (e.g., physical pin 6).
  3. Trigger (Trig) Connection: Connect the HC-SR04 Trig pin directly to a free GPIO pin on the Raspberry Pi configured as an output (e.g., GPIO 23, physical pin 16).
  4. Echo Pin Voltage Divider: This is essential for safety.
  • Connect the HC-SR04 Echo pin to a column on your breadboard.
  • Connect the GPIO side of the voltage divider to another GPIO pin configured as an input (e.g., GPIO 24, physical pin 18).
  • Connect the free end of the 1kΩ resistor to the same breadboard column as the Echo pin.
  • Connect the other end of the 1kΩ resistor to the column connected to the input GPIO pin (GPIO 24).
  • Connect the free end of the 2kΩ resistor to the same column as the input GPIO pin (GPIO 24).
  • Connect the other end of the 2kΩ resistor to a GND pin on the Raspberry Pi.
  • (Diagrammatically: Echo -> 1kΩ Resistor -> GPIO Input Pin; GPIO Input Pin -> 2kΩ Resistor -> GND).

Programming the Raspberry Pi to Measure Distance

Once the hardware is correctly wired, the magic happens in software. We’ll use Python with the RPi.GPIO library (or gpiozero) to control the Raspberry Pi’s GPIO pins. The core logic involves:

  1. Sending a Trigger Pulse: Set the Trig pin HIGH for a short duration (typically 10 microseconds) to initiate an ultrasonic pulse.
  2. Listening for the Echo Pulse: Immediately after sending the trigger, listen on the Echo pin. The Echo pin will go HIGH and stay HIGH for the duration it takes the sound pulse to travel to the object and back to the sensor.
  3. Measuring Pulse Duration: Precisely measure the length of time the Echo pin remains HIGH.
  4. Calculating Distance: Calculate the distance to the object using the formula: Distance = (Speed of Sound * Time Elapsed) / 2. We divide by 2 because the sound travels to the object and back (round trip). The speed of sound is approximately 343 meters per second (or 34,300 cm/s) at room temperature (20°C).

Sample Python Code (using RPi.GPIO):

”`python import RPi.GPIO as GPIO import time

Set GPIO mode (BCM or BOARD)

GPIO.setmode(GPIO.BCM) # Using Broadcom chip numbering

Define GPIO Pins

TRIG_PIN = 23 # OUTPUT Pin (Trigger) ECHO_PIN = 24 # INPUT Pin (Echo - via voltage divider)

Set up the GPIO channels

GPIO.setup(TRIG_PIN, GPIO.OUT) GPIO.setup(ECHO_PIN, GPIO.IN)

Ensure trigger pin starts LOW

GPIO.output(TRIG_PIN, False) time.sleep(0.5) # Let sensor settle

try: while True:

Send 10us pulse to trigger

GPIO.output(TRIG_PIN, True) time.sleep(0.00001) # 10 microseconds GPIO.output(TRIG_PIN, False)

Record start time while ECHO is LOW

pulse_start = time.time() while GPIO.input(ECHO_PIN) == 0: pulse_start = time.time

Recommended products