Arduino Tutorial 14 - Infrared Controlled Light

An infrared receiver, or IR receiver, is hardware device that sends information from an infrared remote control to another device by receiving and decoding signals. In general, the receiver outputs a code to uniquely identify the infrared signal that it receives. This code is then used in order to convert signals from the remote control into a format that can be understood by the other device. Because infrared is light, it requires line-of-sight visibility for optimal operation, but can still be reflected by items such as glass and walls. Poorly placed IR receivers can cause a condition known as "tunnel vision," where the operational range of a remote control is reduced due to their being set too far back into the device's chassis.

 

 

COMPONENT LIST

 

HARDWARE


Note that the “Vout”lead of the infrared receiver must be connected 
to Digital Pin 11 on the Arduino.  Then add an LED and a resistor. Connect the LED to digital pin 10 and the signal LED of infrared receiver to digital pin 11.

fig 1 Infrared Controlled Light Circuit

 

 

 

CODE


For remote control infrared modules, we provide a ready-to-use IRremote library. Simply install the IRremote library (version 2.6.0 is required, as shown in Fig 2) directly in the Arduino IDE. Input the sample code 1 and run it. If you're still unsure about how to load the library, you can revisit the post-class assignment section of the project[RGB LED], where detailed instructions on how to add libraries are provided.

Sample code 1:

CODE
// Project - Infrared Receiver test  
#include <IRremote.h> // Include the IRremote.h library  
int RECV_PIN = 11; // Define RECV_PIN variable as 11  
IRrecv irrecv(RECV_PIN); 
// Set RECV_PIN (which is pin 11) as the IR receiver pin  
decode_results results; 
// Define results variable to store IR decoding results  
  
void setup()  
{  
  Serial.begin(9600); // Set serial baud rate to 9600  
  irrecv.enableIRIn(); // Enable IR decoding  
}  
  
void loop() {  
  /* Check if decoded data is received, store it in the results variable  */
  if (irrecv.decode(&results)) {  
    /* Output the received data in hexadecimal format via serial port  */
    Serial.println(results.value, HEX);  
    irrecv.resume(); // Resume waiting for the next signal  
  }  
}

After uploading the code, open the serial monitor of the Arduino IDE and configure the baud rate 9600 in line with Serial.begin(9600).

 

After configuration, press the button on the remote controller towards the infrared receiver. Each button has a hexadecimal code. We can see the code on the serial monitor no matter which button we press. For example, we press the button “0”, and the hexadecimal code received is “FD30CF”.

If you keep pressing one button, the serial monitor reads “FFFFFFFF”.

 

If it is received properly in the serial port, the code should be six digits starting with FD. If the controller does not send out the signal towards the infrared receiver, it might receive wrong code as we can see below.

 

The original infrared decoding is too complicated to manipulate, which is why we use the library that is built by others without completely understanding it. Since we have got the idea of decoding for infrared signal, let’s make an infrared controlled LED.

 

Sample code 2:

CODE
//Project - Infrared Controlled Light
#include <IRremote.h>           // Include the IRremote.h library  
int RECV_PIN = 11;              // Define RECV_PIN variable as 11  
int ledPin = 10;                // LED connected to digital pin 10  
boolean ledState = LOW;         
// ledState used to store the state of the LED  
IRrecv irrecv(RECV_PIN);        
// Set RECV_PIN (pin 11) as the IR receiver pin  
decode_results results;         
// Define results variable to store IR decoding results  
  
void setup(){  
  Serial.begin(9600);          // Set serial baud rate to 9600  
  irrecv.enableIRIn();          // Enable IR decoding  
  pinMode(ledPin, OUTPUT);      // Set the LED pin as OUTPUT  
}  
  
void loop() {  
  /* Check if decoded data is received, store it in the results variable  */
  if (irrecv.decode(&results)) {    
   // Output the received data in hexadecimal format via serial port  
       Serial.println(results.value, HEX);  
      
       /* If the power button code is received, toggle the LED state (HIGH to LOW or LOW to HIGH)  */
       if(results.value == 0xFD00FF){  
           ledState = !ledState;             // Toggle the state  
           digitalWrite(ledPin, ledState);  
// Change the LED state accordingly  
       }       
       irrecv.resume();  // Resume waiting for the next signal  
   }  
}

Defining the infrared receiver is the same as the last sketch.

CODE
#include <IRremote.h>          // Include the IRremote.h library  
int RECV_PIN = 11;              // Define RECV_PIN variable as 11  
int ledPin = 10;                // LED connected to digital pin 10  
boolean ledState = LOW;         
// ledState used to store the state of the LED  
IRrecv irrecv(RECV_PIN);
// Set RECV_PIN (pin 11) as the IR receiver pin  
decode_results results;         
// Define results variable to store IR decoding results 

In the setup() function, we use serial port to boot the infrared decoding and configure pinMode of digital pins. In the main program, we test if receive infrared signal and store data in the results variable.

 

if (irrecv.decode(&results))

 

Once the Arduino receives data, the program does two things: first it tests whether infrared code is received from the power button.

 

if(results.value == 0xFD00FF)

 

The second thing is to make the LED change state.

 

ledState = !ledState;             // Toggle the state  
digitalWrite(ledPin, ledState);  
// Change the LED state accordingly

 

You might not be so familiar with “!”. “!” is a logical NOT. “!ledState” is the opposite state of “ledState”. “!” is only used in the variable that only holds 2 states or a boolean type of variable.

 

Next, the Arduino will wait for the next signal.

 

irrecv.resume();

 

 

EXERCISE


1. Combine the fan project with the current project. Add one more function to the mini controller to control an LED and a fan.

2. Make a DIY a remote controlled project, e.g.: a small figure that can move with servos controlled by infrared signals.

icon Infrared_Controlled_Light.zip 2KB Download(0)
License
All Rights
Reserved
licensBg
0