3. Infrared (IR) Sensor¶
The infrared sensor is used to detect obstacles by measuring distance using infrared radiation. The distance measured however is limited to 80cm. It has a transmitter and a receiver of radiation. The output signal differs in accordance to the obstacles. It remains high ‘1’ when there are no obstacles and low ‘0’otherwise. There is also a red led on its back that glows purple when it meets an obstacle.
3.1. Connecting the IR Sensor¶
The wires of the sensor are as follows:
- Red – 5V
- Green – GND
- Yellow – Digital output
- In this example:
- Red wire is connected to pin#2
- Green wire is connected to pin#6
- Yellow wire is connected to pin#7
The wiring of the Raspberry Pi to the breadboard, and the sensor is shown the figure below:
3.2. Python Program to Show Obstacle Detection¶
1 2 3 4 5 6 7 8 9 10 import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(7,GPIO.IN) while True: print GPIO.input(7) time.sleep(0.5)