webwinkelkeur logo

4.7 avg.

5101+ reviews
webwinkelkeur logoView all

5101+ reviews

5101+ reviews

Order by 16:00 for same day shipping

14 days return

GB

EN

Individual

Business

GPIO Project 10 - IR Project

Beginner
30 Minuten
114,90

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.

Connection diagram

Connect the IR sensor:

  1. VCC (or +): Connect to 3.3V (pin 1). span>
  2. GND (or -): Connect toGND (pin 6).
  3. OUT: Connect withGPIO 17 (pin 11).

Connect the LED:

  1. Long leg (anode):
    • Connect via a 220Ω resistor to GPIO 27 (pin 13).
  2. Short leg (cathode):
    • Connect toGND (pin 6).

Pinout Reference

GPIO

Pin #

Function

Connection

GPIO 17Pin 11InputIR sensor (OUT)
GPIO 27Pin 13OutputLED
3.3VPin 1NutritionIR sensor (VCC)
GNDPin 6Earth (Ground)IR sensor and LED

Preparation

Step 1: Install required packages

Open a terminal and run the following commands to install the required IR library:

sudo apt-get update
sudo apt-get install lirc

Step 2: Activate IR function

  1. Open the configuration menu:
sudo raspi-config

2. Go toInterface OptionsEnable IR and turn it on.
3. Restart the Raspberry Pi.

Python-code in Thonny

Step 1: Write your code

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

Step 2: Save the file

Click File > Save As and name the file ir_project.py.

Step 3: Run the script

Click the greenRun button (▶) at the top the Thonny interface.

How does it work?

  1. IR sensor:
    • Receives the infrared signal from the remote control.
    • When a button is pressed, the sensor sends a signal to GPIO 17.
  2. LED:
    • The LED turns on when the IR sensor receives a signal receives.
  3. Repeat:
    • The program continuously checks whether a signal is coming in from the remote control.

Result

  • Button pressed: The LED turns on for 1 second and then turns off.
  • No button pressed: The LED remains off.

Experimenting

  1. Identify different buttons:
    • Add a library like lirc or pylirc to detect specific buttons and associate different actions.
  2. More output devices:
    • Use multiple LEDs or other devices, such as a buzzer, to respond to different buttons.
  3. Logging button codes:
    • Log the received IR codes to identify specific buttons on the remote.