Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Low‑Cost Infrared Proximity Sensor for Raspberry Pi Using Digital Pins Only

Raspberry Pi boards run on the Broadcom BCM2835 processor, which offers 26 general‑purpose input/output (GPIO) pins. While Python libraries such as RPi.GPIO make controlling these pins straightforward, a notable limitation compared to Arduino is the absence of true analog inputs. GPIO pins are strictly digital: a pin set as an output can only deliver 0 V (LOW) or 3.3 V (HIGH). When configured as an input, any voltage below 0.8 V is interpreted as LOW, and any voltage above 1.3 V is treated as HIGH.

In many real‑world applications—temperature monitoring, distance measurement, light intensity sensing—the underlying signal is inherently continuous. Relying on a binary “on/off” output is often insufficient. The typical workaround is to add an external analog‑to‑digital converter (ADC) such as the MCP3008, TLC549, or MCP23017, which communicate over SPI or I²C. These chips are inexpensive individually, but when combined with commercial sensors, the total cost can climb to $20–$30 and the assembly becomes bulky—an issue for robotics projects that require multiple sensors.

Fortunately, a simple yet effective approach exists that eliminates the need for an external ADC. By converting the analog measurement into a time‑based signal, the Raspberry Pi’s digital pins can indirectly sample continuous data. Time is a naturally analog quantity, and the technique relies on the classic RC (resistor‑capacitor) charging principle.

DIY Infrared Proximity Sensor Design

The circuit uses only inexpensive components: a handful of infrared LEDs, a phototransistor, a 2N3904 NPN transistor, a 100 nF ceramic capacitor, and low‑power resistors. The exact part numbers are interchangeable; the key is maintaining the correct electrical characteristics.

Key component note: The 100 nF capacitor can be a low‑profile ceramic or a class‑1 ceramic/film capacitor for optimal stability.

Hardware Setup

  1. Power: Connect the +5 V and GND leads to an external 5 V supply. Tie the GND to the Raspberry Pi’s ground.
  2. GPIO Assignment: Choose one GPIO pin (e.g., PIN_A) as the trigger output, and another (e.g., PIN_B) as the measurement input.

Measurement Principle

When PIN_A is driven HIGH, the infrared LEDs illuminate the target surface. Reflected light activates the phototransistor, which produces a current proportional to the reflected intensity. This current charges the capacitor via the 2N3904 transistor. The time it takes for the capacitor voltage to cross the Raspberry Pi’s digital threshold on PIN_B is inversely related to the phototransistor current, and therefore directly proportional to the distance to the reflecting object.

Python Control Flow

# 1. Ensure the capacitor is fully discharged
GPIO.setup(PIN_B, GPIO.OUT)
GPIO.output(PIN_B, 0)
time.sleep(0.01)  # 10 ms to exceed 200×RC (≈0.00005 s)

# 2. Switch PIN_B to input mode
GPIO.setup(PIN_B, GPIO.IN)

# 3. Trigger the IR LEDs
GPIO.setup(PIN_A, GPIO.OUT)
GPIO.output(PIN_A, 1)

# 4. Measure low‑duration on PIN_B
counter = 0
start_time = time.time() * 1000  # milliseconds
while GPIO.input(PIN_B) == 0 and counter < 1e4:
    counter += 1
    elapsed = time.time() * 1000 - start_time

# elapsed (in ms) ≈ k × distance

With a 4.3 kΩ resistor and a 100 nF capacitor, the RC time constant is 0.43 ms. The 2N3904 amplifies the phototransistor current by roughly 150×, allowing the LEDs to draw about 50 mA each during activation. The resulting time delay, elapsed, scales linearly with distance, providing a simple analog‑to‑time conversion without any dedicated ADC hardware.

This method offers a compact, low‑cost alternative to conventional ADC‑based proximity sensors, making it ideal for embedded robotics, hobbyist projects, and educational demonstrations.

Manufacturing process

  1. Inductive Proximity Sensor: Circuit Design, Functionality, and Practical Applications
  2. Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
  3. Line Tracking Sensor with Raspberry Pi – Simple KY‑033 Door/Line Detector
  4. How to Connect a DS18B20 One‑Wire Digital Thermometer to a Raspberry Pi – A Step‑by‑Step Guide
  5. Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
  6. Raspberry Pi Light Sensor Tutorial – Wire an LDR and Read Light Levels with Python
  7. Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
  8. Raspberry Pi Home Security System: PIR Motion Detection + Camera Email Alerts
  9. How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi
  10. Digital Light Sensor – Power an LED with Ambient Light Using Windows 10 IoT Core