Lesson 21 Infrared-Receiver

Share for us

Introduction

An infrared-receiver is a component which receives infrared signals and can independently receive infrared ray and output signals compatible with TTL level. It’s similar with a normal plastic-packaged transistor in size and it is suitable for all kinds of infrared remote control and infrared transmission.

Components

– 1 * SunFounder Uno board (or SunFounder Mega2560 board)

– 1 * USB cable

– 1 * Infrared-receiver module

– 1 * Remote controller

 – Several jumper wires

Experimental Principle

Control a certain key (for example, Power key) via a remote controller by programming. When you press the key, infrared rays will be emitted from the remote controller and received by the infrared receiver, and the LED on the SunFounder Uno board will light up.

Experimental Procedures

Step 1: Build the circuit

Infrared-receiver moduleSunFounder Uno
S2
“-”GND
+5V

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorials on our website)

Note: Here you need to add a library. Refer to the description in Lesson 4 previously in the manual.

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Now, press the Power key of a remote controller, and the LED attached to pin 13 on the SunFounder Uno board will light up. Then press any other key, and the LED will go out.

 Code

/***********************************************
* name:Infrared-Receiver
* function:press the Power key of a remote control,
* and both the LED attached and that connected to pin 13 on the SunFounder Uno board will light up.
* Then press any other key, and the LEDs will go out.
**********************************************/
//Email: support@sunfounder.com
//Website: www.sunfounder.com#include <IRremote.h>
const int irReceiverPin = 2; //ir receiver module attach to pin2
const int ledPin = 13; //pin 13 built-in led

IRrecv irrecv(irReceiverPin); //Creates a variable of type IRrecv
decode_results results;

void setup()
{
  pinMode(ledPin,OUTPUT); //set ledpin as OUTPUT
  Serial.begin(9600); //initialize serial monitor
  irrecv.enableIRIn(); //enable ir receiver module
}

void loop()
{
  if (irrecv.decode(&results)) //if the ir receiver module receiver data
  {
    Serial.print(“irCode: “); //print”irCode: “
    Serial.print(results.value, HEX); //print the value in hexdecimal
    Serial.print(“, bits: “); //print” , bits: “
    Serial.println(results.bits); //print the bits
    irrecv.resume(); // Receive the next value
  }
  delay(600); //delay 600ms
  if(results.value == 0xFFA25D)
  {
    digitalWrite(ledPin,HIGH); //turn on the led
  }
  else
  {
    digitalWrite(ledPin,LOW); //turn off the led
  }
}