Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Interface Ir Remote & Reveiver with Arduino

Introduction

Infrared (IR) remotes are commonly used to control electronic devices. They work by emitting infrared light signals that are decoded by an IR receiver. In this tutorial, you will learn how to interface an IR remote with an Arduino, read button presses, and use them to control an LED.

Required Components

  • Arduino UNO R3 SMD
  • IR remote control
  • IR receiver module (e.g., TSOP1738)
  • 5 mm LED
  • 220-ohm resistor
  • Breadboard
  • Jumper wires

Pinout

Circuit Diagram / Wiring

  • IR Receiver:
    • Connect the signal pin of the IR receiver to Arduino digital pin 11.
    • Connect the VCC pin to the 5V pin on the Arduino.
    • Connect the GND pin to the GND pin on the Arduino.
  • LED:
    • Connect the longer leg (anode) of the LED to digital pin 13 through a 220-ohm resistor.
    • Connect the shorter leg (cathode) to the GND pin.

Arduino Code / Programming

  • Go to the “Libraries” tab on the left-hand side of the screen.
  • Click on the “Library Manager” button (book icon) at the top of the Libraries tab.
  • In the Library Manager window, type “IRremote” in the search bar.
  • Locate the “IRremote” library by shirriff and click on the “Install” button next to it.
  • Wait for the library to be installed, and you’re ready to use the IRremote library in your projects.
  • Here’s a basic code example to read IR remote signals and control an LED:
#include <IRremote.h>  // Include the IRremote library

const int RECV_PIN = 11;  // Pin connected to the IR receiver
const int LED_PIN = 13;   // Pin connected to the LED

IRrecv irrecv(RECV_PIN);   // Create an instance of the IR receiver
decode_results results;     // Variable to store the results

// Define the button values for turning the LED on and off
const unsigned long ON_BUTTON_VALUE = 16753245; // Button value to turn LED on
const unsigned long OFF_BUTTON_VALUE = 16736925; // Button value to turn LED off

void setup() {
  Serial.begin(9600);         // Initialize serial communication
  irrecv.enableIRIn();        // Start the receiver
  pinMode(LED_PIN, OUTPUT);   // Set LED_PIN as an output
  digitalWrite(LED_PIN, LOW); // Ensure the LED is initially off
}

void loop() {
  if (irrecv.decode(&results)) {
    // Ignore the repeat code
    if (results.value == 4294967295) {
      irrecv.resume(); // Resume for the next value
      return; // Skip further processing for this loop iteration
    }

    // Print the received value for debugging
    Serial.print("Received value: ");
    Serial.println(results.value, DEC); // Print the value in decimal

    // Check if the received value matches the ON button value
    if (results.value == ON_BUTTON_VALUE) {
      digitalWrite(LED_PIN, HIGH); // Turn the LED on
      Serial.println("LED ON"); // Print message to Serial Monitor
    }
    // Check if the received value matches the OFF button value
    else if (results.value == OFF_BUTTON_VALUE) {
      digitalWrite(LED_PIN, LOW); // Turn the LED off
      Serial.println("LED OFF"); // Print message to Serial Monitor
    }

    irrecv.resume(); // Receive the next value
  }
}

Explanation of the Code

Library Inclusion:

#include <IRremote.h>  // Include the IRremote library

  • This line includes the IRremote library, which provides functions for receiving and decoding signals from IR remote controls.

Pin Definitions:

const int RECV_PIN = 11;  // Pin connected to the IR receiver
const int LED_PIN = 13;   // Pin connected to the LED

  • RECV_PIN is the pin number where the IR receiver is connected to the Arduino.
  • LED_PIN is the pin number where the LED is connected.

IR Receiver Initialization:

IRrecv irrecv(RECV_PIN);   // Create an instance of the IR receiver
decode_results results;     // Variable to store the results

  • An instance of IRrecv is created to handle the IR receiver.
  • decode_results is a structure that stores the decoded IR signals.

Button Value Definitions:

const unsigned long ON_BUTTON_VALUE = 16753245; // Button value to turn LED on
const unsigned long OFF_BUTTON_VALUE = 16736925; // Button value to turn LED off

  • ON_BUTTON_VALUE and OFF_BUTTON_VALUE are constants that represent the values transmitted by the remote control for turning the LED on and off, respectively.
  • You need to replace these values with the actual values received from your remote.

Setup Function:

void setup() {
  Serial.begin(9600);         // Initialize serial communication
  irrecv.enableIRIn();        // Start the receiver
  pinMode(LED_PIN, OUTPUT);   // Set LED_PIN as an output
  digitalWrite(LED_PIN, LOW); // Ensure the LED is initially off
}

  • Initializes serial communication for debugging and monitoring.
  • Starts the IR receiver to begin listening for signals.
  • Sets LED_PIN as an output pin to control the LED.
  • Ensures the LED is off when the program starts.

Loop Function:

void loop() {
  if (irrecv.decode(&results)) {
    // Ignore the repeat code
    if (results.value == 4294967295) {
      irrecv.resume(); // Resume for the next value
      return; // Skip further processing for this loop iteration
    }

  • The loop() function continuously checks for incoming IR signals.
  • If a signal is detected, it decodes it and stores the result in the results variable.
  • The code checks if the received value is 4294967295, which indicates a repeat signal. If it is, the code resumes listening for the next signal and skips the rest of the loop.

Processing Received Values:

    // Print the received value for debugging
    Serial.print("Received value: ");
    Serial.println(results.value, DEC); // Print the value in decimal

    // Check if the received value matches the ON button value
    if (results.value == ON_BUTTON_VALUE) {
      digitalWrite(LED_PIN, HIGH); // Turn the LED on
      Serial.println("LED ON"); // Print message to Serial Monitor
    }
    // Check if the received value matches the OFF button value
    else if (results.value == OFF_BUTTON_VALUE) {
      digitalWrite(LED_PIN, LOW); // Turn the LED off
      Serial.println("LED OFF"); // Print message to Serial Monitor
    }
    
    irrecv.resume(); // Receive the next value
  }
}

  • The received value is printed to the Serial Monitor for debugging purposes.
  • The code checks if the received value matches the predefined ON_BUTTON_VALUE. If it does, the LED is turned on, and a message is printed.
  • Similarly, if the received value matches OFF_BUTTON_VALUE, the LED is turned off, and a message is printed.
  • Finally, irrecv.resume() is called to continue listening for the next IR signal.

Challenges:

Challenge 1: IR Remote Code Lock with Timer

Problem Statement:

  1. Use the IR remote to create a simple code lock system. The user must enter a sequence of specific remote button presses to “unlock” the system.
  2. The correct sequence is, for example: Button 1 → Button 2 → Button 3.
  3. If the user takes too long (e.g., more than 5 seconds) between presses, the system will reset and require the user to start over.
  4. The LED will turn ON if the sequence is entered correctly and OFF if not.

Code :

#include <IRremote.h>

const int receiverPin = 11;
const int ledPin = 13;
unsigned long lastButtonPress = 0;
int currentStep = 0;

const unsigned long timeout = 5000;  // 5 seconds to enter code
unsigned long sequence[] = {0xFF30CF, 0xFF18E7, 0xFF7A85};  // Example: Button 1, Button 2, Button 3

IRrecv irrecv(receiverPin);
decode_results results;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned long code = results.value;
    lastButtonPress = millis();

    // Check if current code matches sequence step
    if (code == sequence[currentStep]) {
      currentStep++;
    } else {
      currentStep = 0;  // Reset if wrong code
    }

    // If the sequence is completed
    if (currentStep == 3) {
      digitalWrite(ledPin, HIGH);  // Unlock by turning on LED
      currentStep = 0;             // Reset to start over
    }

    irrecv.resume();  // Receive next value
  }

  // Check for timeout
  if (millis() - lastButtonPress > timeout) {
    currentStep = 0;  // Reset the sequence on timeout
    digitalWrite(ledPin, LOW);  // Turn off LED if not unlocked
  }
}

Challenge 2: Random Pattern Generation with IR Control

Problem Statement:

  1. Create a random LED blinking pattern generator using the IR remote.
  2. Each time the user presses a specific button (e.g., Button 4), the Arduino will generate a new random blinking pattern.
  3. The LED will follow the pattern (e.g., blink three times quickly, pause, blink once slowly).
  4. Patterns should be generated using random() function and stored in an array for repeated playback until a new pattern is generated.

Code:

#include <IRremote.h>

const int receiverPin = 11;
const int ledPin = 13;
int pattern[5];  // Array to store random pattern
bool patternGenerated = false;

IRrecv irrecv(receiverPin);
decode_results results;

void setup() {
  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();
  randomSeed(analogRead(0));  // Use noise to seed random generator
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned long code = results.value;

    // Generate a random pattern on Button 4 press
    if (code == 0xFF10EF) {  // Replace with your code for Button 4
      generateRandomPattern();
      patternGenerated = true;
    }

    irrecv.resume();  // Receive next value
  }

  if (patternGenerated) {
    playPattern();  // Play the generated pattern
  }
}

void generateRandomPattern() {
  for (int i = 0; i < 5; i++) {
    pattern[i] = random(100, 1000);  // Random delay between 100 and 1000ms
  }
}

void playPattern() {
  for (int i = 0; i < 5; i++) {
    digitalWrite(ledPin, HIGH);
    delay(pattern[i]);
    digitalWrite(ledPin, LOW);
    delay(pattern[i]);
  }
}

Challenge 3: IR Remote-Controlled Brightness Adjustment

Problem Statement:

  1. Use the IR remote to control the brightness of the LED (connected to pin 13).
  2. Use the volume up button to increase brightness in increments of 25 (from 0 to 255) and the volume down button to decrease brightness by 25.
  3. If the brightness exceeds 255, reset it to 0. If it goes below 0, set it to 255.
  4. Use analogWrite() on a PWM pin to control brightness, but avoid using delay() for smooth operation.

Code:

#include <IRremote.h>

const int receiverPin = 11;   // Pin for IR receiver
const int ledPin = 9;         // PWM pin for LED
int brightness = 128;         // Starting brightness level

IRrecv irrecv(receiverPin);   // Create IR receiver object
decode_results results;       // Variable to store IR codes

void setup() {
  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();        // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned long code = results.value;

    // Volume Up button to increase brightness
    if (code == 0xFF629D) { // Replace with your own code for Volume Up
      brightness += 25;
      if (brightness > 255) {
        brightness = 0;      // Reset if it exceeds max brightness
      }
    }

    // Volume Down button to decrease brightness
    if (code == 0xFFA857) { // Replace with your own code for Volume Down
      brightness -= 25;
      if (brightness < 0) {
        brightness = 255;    // Reset if it goes below minimum
      }
    }

    analogWrite(ledPin, brightness); // Adjust LED brightness
    irrecv.resume();                  // Receive the next value
  }
}

More Challenges

Challenge 1: LED Pattern Selection via IR Remote

Problem Statement:

  1. Use the IR remote to select different LED blinking patterns.
  2. For example:
    • Button 1: Slow blink (ON for 1 second, OFF for 1 second).
    • Button 2: Fast blink (ON for 100ms, OFF for 100ms).
    • Button 3: SOS Morse code pattern.
  3. The selected pattern should continue to run until another button is pressed.

Challenge 2: LED Fading with IR Remote Time Control

Problem Statement:

  1. Control the fade speed of an LED using the IR remote.
  2. Use the volume up button to speed up the fading and the volume down button to slow it down.
  3. The LED should gradually increase and decrease brightness between 0 and 255, adjusting the fade speed based on the remote control input.

Testing and Troubleshooting

  • Open the Serial Monitor to see the hexadecimal values printed when you press buttons on the IR remote.
  • Replace the hexadecimal values in the code with the values corresponding to your remote’s buttons.
  • If the receiver does not detect signals, ensure that the connections are correct and the IR receiver is not blocked.

    Leave a Reply

    Your email address will not be published.

    Need Help?