Recent

IR Remote Example

IR Remote Example



This example shows you how to command a led by a remote control. 
In particular in this sketch is used the "tsop 31236" IR receiver but you can use an other model. 
The IR receiver has three pin, the first from left is the Ground, the second pin is the Vcc (it supports supply voltage between 2,5V and 5,5V)and the third is the pin that it is used to transmit the received signal. 
Click here if you want consult the datasheet of IR receiver. 
IR Receiver 
The other device used is a normal remote control as that shown in the figure 
Remote Control

Hardware Required

  • Arduino Board
  • Remote control (with battery)
  • IR receiver (tsop 31236)
  • A led
  • A 220Ω resistor
  • Wires
  • Breadboard

Circuit

Fritzing Circuit 
Connect the first pin of left of the IR receiver to Ground of the board, the second pin to 3.3V and the third pin to Pin12. 
Also connect the anode of the LED (usually the longer pin) in series to a 220Ω resistor and connect it to pin 13 of board, instead connect the cathode of led to GND of board. 
Finally connect the board to PC with a USB cable and upload the sketch. 
Note: 
You need the IRremote library to use this sketch.

Schematic

Schematic

Code

This sketch allows to turn on/off a led using a remote control. 
First it is included the IRremote.h library. 
Second are defined the constant used and created the new instances of IRrecv and decode_results. 
In the set up function is initialized the Serial communication, it is enabled the IR receiver and it is declared the led as output pin.
Finally in the loop block it is printed on serial monitor the value in hexadecimal of the button pressed and it is called the "IRsignal" function to execute the action relative to button pressed (using the "switch case" construct). 
Note: 
Follow this steps to programm the remote control: 
-Upload the sketch. 
-Press the button that you want use to pilot the led. 
-Read the hexadecimal code of the button pressed on Serial monitor and insert it into the "switch case".
The complete code and its detailed description are shown down.
 
 
/*IR remote Example*/
#include "IRremote.h"//include IRremote library to decode the IR signals 
/*Declare Constants*/
int IRreceiver = 12; //pin connected to IR receiver
int led = 13; //pin connected to anode of led
/*Declare objects*/
IRrecv irrecv(IRreceiver);//create instance of 'IRrecv'
decode_results results;//create instance of 'decode_results'
/*Setup*/
void setup()   
{
  Serial.begin(9600);//initialize Serial communication
  Serial.println("IR signals decoded");
  irrecv.enableIRIn(); //start the IR receiver
  pinMode(led, OUTPUT);//initialize the led as output pin
}
/*loop*/
void loop() 
{
  if (irrecv.decode(&results)) //check if it is received an IR signal
  {
    Serial.println(results.value, HEX);//print on Serial monitor the value received in hexadecimal.Read the value of the button that you want use and put it into the switch case
    IRsignal();//call the function IRsignal() 
    irrecv.resume(); //receive the next value
    delay(200);//wait 200 millisecond  
  } 
  }
void IRsignal() //takes action based on IR code received
{
  int status_led=0;//state of led
  status_led = digitalRead(led);//read the status led
  Serial.println(status_led);//print the status led
  switch(results.value)//check the IR signal received 
  {
  case 0xFFA25D://Put your button code 
    if (status_led==0){
      
      Serial.println(" Turn on Led ");//print on Serial monitor:"Turn on Led" 
      digitalWrite(led, HIGH); //turn on led
      break;
    }
    if (status_led==1){
      
      Serial.println(" Turn off Led ");//print on Serial monitor:"Turn off Led"
      digitalWrite(led, LOW); //turn off led
      break;
    }
  default: 
    Serial.println(" other button");
  }
}
 

Share on Google Plus

About Unknown

0 comments:

Post a Comment