5101+ reviews
Order by 16:00 for same day shipping
14 days return
GB
EN
Individual
Business
In this project, you will learn how to use an infrared (IR) receiver and a remote control to trigger actions on a Raspberry Pi. We will use an LED as output, which is controlled by specific buttons on the remote control.
GPIO | Pin # | Function | Connection |
GPIO 17 | Pin 11 | Input | IR sensor (OUT) |
GPIO 27 | Pin 13 | Output | LED |
3.3V | Pin 1 | Nutrition | IR sensor (VCC) |
GND | Pin 6 | Earth (Ground) | IR sensor and LED |
Open a terminal and run the following commands to install the required IR library:
sudo apt-get update
sudo apt-get install lirc
sudo raspi-config
2. Go toInterface Options > Enable IR and turn it on.
3. Restart the Raspberry Pi.
Open the Thonny Python IDE and enter the following code:
import RPi.GPIO as GPIO
from time import sleep
# GPIO-instellingen
IR_SENSOR_PIN = 17
LED_PIN = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(IR_SENSOR_PIN, GPIO.IN) # IR-sensor als input
GPIO.setup(LED_PIN, GPIO.OUT) # LED als output
try:
print("Druk op een knop op de afstandsbediening...")
while True:
if GPIO.input(IR_SENSOR_PIN) == GPIO.LOW: # Detectie van een signaal
print("IR-signaal ontvangen!")
GPIO.output(LED_PIN, GPIO.HIGH) # Zet LED aan
sleep(1) # Houd LED 1 seconde aan
GPIO.output(LED_PIN, GPIO.LOW) # Zet LED uit
else:
GPIO.output(LED_PIN, GPIO.LOW) # Houd LED uit als geen signaal
except KeyboardInterrupt:
print("\nProgramma gestopt.")
finally:
GPIO.cleanup() # Reset de GPIO-instellingen
Click File > Save As and name the file ir_project.py.
Click the greenRun button (▶) at the top the Thonny interface.