IR Remote Control Lesson

From DFRobot Electronic Product Wiki and Tutorial: Arduino and Robot Wiki-DFRobot.com

Introduction

This is the simple hands-on experiments. Use the IR remote controller to control the LED and buzzer by the IR receiver. You must firstly follow the diagram to complete the connection.Then,you can use the single components or the existing sensors, which all can be help you to realize the function.

Hardware Requierments

For Reference Only

  1. 1x DFRduino UNO R3
  2. 1x IR receiver
  3. 1x IR Remote controller or IR kit
  4. 1x Red Led or 5mm LED pack
  5. 1x Buzzer or Digital Buzzer Module
  6. 1x 1K Resistor or Resistor Pack
  7. 1x Mini Bread Board
  8. 1x Arduino Jumper Cables

Connection Diagram

Lesson 10 B.png

Code Sample

/*
 # The sample code displayed the decode processing.
 # When you press the button 'VOL+' of IR remote controller,the red led will be on.
 # And, when you press the button 'VOL-' of the controller ,the buzzer will rang.
*/


#define BUZZER  10                        //Connect Buzzer to Digital Pin 10
#define LED_RED 11                        //Connect a Red LED to Digital Pin 11
#define IR_IN   8                         //Connect the Infrared receiver to Digital Pin 8
 
int Pulse_Width=0;                       //Pulse width
int  ir_code=0x00;                       //IR command code

void timer1_init(void){                  //Initilize timer
    TCCR1A = 0X00; 
    TCCR1B = 0X05; 
    TCCR1C = 0X00;
    TCNT1  = 0X00;
    TIMSK1 = 0X00;	
}

void remote_deal(void){                         //Get IR command
    switch(ir_code){
    case 0xff00:                                //Press stop button on the remote controller
        digitalWrite(LED_RED,LOW);              //Turn off red led
        digitalWrite(BUZZER,LOW);               //Silence the buzzer
        break;
    case 0xfe01:                                //Press VOL+ button
        digitalWrite(LED_RED,HIGH);             //Turn off Red LED
        break;
    case 0xf609:                                //Press VOL- button
        digitalWrite(BUZZER,HIGH);              //Turn on Buzzer
        break;
    }
}

char logic_value(){                                         // The function determine the logic value "1" and "0".
    while(!(digitalRead(8)));                               //Wait low 
    Pulse_Width = TCNT1;
    TCNT1 = 0;
    if( Pulse_Width >= 7 && Pulse_Width <= 10 ){               //low level 560us
        while(digitalRead(8));                                //Value is high, then wait.
        Pulse_Width = TCNT1;
        TCNT1=0;
        if( Pulse_Width >= 7 && Pulse_Width <= 10 )            //High level 560us
             return 0;
        else if( Pulse_Width >= 25 && Pulse_Width <= 27 )      //High level 1.7ms
             return 1;
    }
    return -1;
}

void pulse_deal()  {                                 //Receive address code and command code pulse function
    int i;   
    // Run 8 zeros
    for(i=0; i<8; i++) {
      if( logic_value() != 0 )                       //If it isn't 0.
          return; 
    }
    // Run 6 ones
    for(i=0; i<6; i++) {
      if(logic_value()!= 1)                        //If it isn't 1.
          return;
    }
    // Run 1 zero
    if(logic_value()!= 0)                          //If it isn't 0.
        return;
    //Run 1 one
    if(logic_value()!= 1)                          //If it isn't 1.
        return;
  
    //decode the commands of IR remote control codes 
    ir_code = 0x00;                                  //clear
    for(i=0; i<16;i++ )  {
      if(logic_value() == 1)   {
        ir_code |=(1<<i);
      }
    }
}

void remote_decode(void){                          //decode function
    TCNT1 = 0X00;       
    while(digitalRead(8)){                         // Value is high, then wait.
      if(TCNT1>=1563) {                            // High level duration exceeds 100ms,which means "no button pressed".
        ir_code = 0xff00;
        return;
      }  
    }
    
    // High level duration doesn't exceed 100ms.
    TCNT1 = 0X00;
    while(!(digitalRead(8)));                           // wait low
    Pulse_Width=TCNT1;
    TCNT1 = 0;
    if(Pulse_Width>=140&&Pulse_Width<=141) {            // 9ms  
        while(digitalRead(8));                          //Value is high, then wait.
        Pulse_Width=TCNT1;
        TCNT1=0;
        if(Pulse_Width>=68&&Pulse_Width<=72) {          //4.5ms
          pulse_deal();
          return;
        }
        else if(Pulse_Width>=34&&Pulse_Width<=36){     //2.25ms
          while(!(digitalRead(8)));                    //wait low
          Pulse_Width=TCNT1;
          TCNT1=0;
          if(Pulse_Width>=7&&Pulse_Width<=10){         //560us
            return; 
          }
        }
    }
}

void setup(){
  unsigned char i;
  pinMode(LED_RED,OUTPUT);                      //Set red led pin output
  pinMode(BUZZER,OUTPUT);                       //Set buaaer pin output
  pinMode(IR_IN,INPUT);                         //Set ir receiver input
}

void loop(){  
  timer1_init();                                //timer init
  while(1){
    remote_decode();                            //decode
    remote_deal();                              //Run decodeerout
  }  
}