Menu

Using IR Remote Controls with Arduino

IR Remote controls are everywhere these days, you probably have several of them around your home to control your video and audio equipment as well as air conditioners, fans and even light bulbs.

These devices can also be used with your Arduino projects and in this article I’ll show you how you can decode the outputs from your remotes, repurpose them to control additional devices and even build your own custom remote controls.

Introduction

It’s hard to imagine that there was a time when you actually had to get out of your chair to change channel and adjust volume on your television. Every television and most other audio and video equipment come equipped with a remote control that allows you to be a true “couch potato”, controlling your world from the comfort of your favorite chair.

The IR remote control has changed how we interact with our appliances, for better or worse.

Chances are you have a large collection of these devices, some may even be left over from equipment you no longer own or use. Most of the ones you do make use of have several buttons that serve you no purpose as they were made to integrate with additional equipment that you don’t even own.

Time to put those unused buttons and controllers to good use! All you will need are a few very inexpensive parts and an Arduino to decode the outputs from these little electronic gems and put them to work in your own custom designs.

Or maybe you’d like to build your own custom control, one that controls all your device or a simplified one to replace that 40-button monster you fumble with daily.

In this article we will examine how IR remotes work and how you can decode their signals with an Arduino. We will also look at how you can use the Arduino as a remote control itself, emulating the signals that the original controller emitted.

Finally we will see how you can use very inexpensive remote controls in your own custom projects. Let’s get started!

How IR Remote Controls Work

Before we start hacking our remote controls it would be a good idea to discuss how they work.

IR remote controls, as the name would imply, make use of pulses of infrared light to send signals to a receiving device such as a television or sound system. Each button on the remote control sends out a unique pattern of pulses which are decoded by the receiver so that the appropriate action (i.e. raise the volume, change channels) can be taken.

IR remotes make use of IR LED’s which are very inexpensive, in fact some of the very first LED’s manufactured in the 1970’s were infrared as it was actually easier (and cheaper) to make IR light with an LED than it was to output visible light.

The infrared part of the light spectrum is just below the visible spectrum, the name “infrared” actually means “below red” which is where they sit on a spectrum chart. This is the opposite of ultraviolet or UV light which is just above the visible spectrum, “ultraviolet” meaning “above violet”.

Infrared light is perfectly safe to work with, you can stare into a working IR remotes LED without any fear of damaging your eyes. In fact you are exposed to infrared light every day, often without being aware of it.

Preventing Interference

Sunlight and the interior lighting in your home have infrared components along with their visible components, and this abundance if infrared light can make it difficult for IR remotes to function as it interferes with them. To resolve this problem IR remotes don’t just pulse their LED’s on and off, instead they modulate them in the same fashion as analog radio modulates a carrier wave to send a signal.

Most IR remotes use a modulation frequency of 38 kHz although a few use different frequencies. The IR receivers are designed to look for this modulated infrared light and to ignore the rest, effectively filtering out the sea of infrared ambience flooding the room.

Infrared light covers a broad range of the spectrum, from 700 Nm to 1 mm. Most IR LED’s used in remote controls operate at 850 Nm to 940 Nm range so most receivers are tuned to receive light within this range. This is another way that they prevent interference from sunlight and other infrared sources.

Manufacturer Codes

As I mentioned earlier different manufacturers use different codes for their remote controls, in addition there are different codes for different devices made by the same manufacturer. Codes are also shared between manufacturers so there is a possibility that two remotes may conflict with one another.

Working with IR Remote Controls

Working with IR remotes is pretty simple. You’ll need an IR sensor to pick up signals and an IR LED to send them.

IR Receivers

Receiving IR codes requires a special infrared sensor, and there are many inexpensive ones available. One common device is the 1838T infrared receiver, a tiny 3-pin sensor that is often included with those inexpensive remote control kits that you can get on eBay of Amazon.

You can purchase this sensor by itself or mounted on a small module board. The latter usually will accept a 3-pin Dupont connector which makes it easy to use in your project. Either way the three pins on the sensor or module have the following functions:

Ground. Labeled GND or G on most modules.

5 Volts power. Labeled VCC or R on most modules

Signal Out. Labelled S or Y on most modules

Using an IR receiver with an Arduino is as simple as supplying 5 volts and Ground from the Arduino and connecting the module or sensors output to one of the Arduino digital I/O pins. We will do exactly that in our first few experiments.

IR Transmitters

The Transmitting part of a remote control is basically an IR LED. An IR LED is used exactly like a visible LED, you supply current with the proper polarity and limit it with a current limiting resistor.

The IR LED is packaged in the same package as a visible LED. The Anode (positive) lead is the longer of the two leads. The Cathode (negative) lead is the shorter one. In case your leads are both the same length the Cathode side of the LED is flat.

As with any LED light is emitted from the top of an IR LED. Keep that in mind when you are doing the transmitter experiments that follow, infrared light is very directional so you will want to aim your LED at the receiver for best performance (although, as most people are aware from using IR remotes, the light can be “bounced” of of white and reflective surfaces).

“Seeing” Infrared Light

One problem you can run into when you’re working with IR remote control projects is that your human eye is incapable of seeing the IR light. If your project doesn’t work it’s often hard to determine if the problem is with the transmitter or receiver.

The way to get around this is to use a video camera that is capable of seeing and displaying infrared light. Fortunately there are plentiful and you probably have one already – in your phone or tablet.

While dedicated cameras (such as a DSLR) have IR filters they aren’t as common on phone and tablet cameras, as a result these cameras are sensitive in the IR range as well as in the visible range of light.

It’s pretty easy to see if your phone or tablet is capable of “seeing” IR. Just take a remote control that you know to be working and aim it at the lens of your camera while you view the screen. If all goes well you will see the IR LED flash as you press the keys on your remote.

Once you have established that your phone or tablet can see infrared you have a valuable troubleshooting tool that you can use if things don’t work out exactly as you planned them.

Decoding IR Remote Controls

Time for our first experiment! You’ll need an Arduino, I used an Uno but other Arduino’s will work as well. Don’t use an ATTiny85 however as the library we will make use of isn’t fully compatible with it.

You’ll also need an IR Sensor. I’m using one that is on a module but you could also use an 1838T sensor by itself and it will work just fine.

Before we begin you’ll need to install a new library into your Arduino IDE

The IR Remote Library

All of the experiments we will be performing make use of an amazing library caller “IRRemote”. The library was written and is maintained by a fellow named Ken Shirriff with contributions by Paul Stoffregen and the Arduino community should be indebted to both of them for all of the hard work that they put into it.

While you can get the Arduino IR Remote Library on GitHub it is also available (and easier to install) directly from the Library Manager in your Arduino IDE. Open the Library Manager and search for “IR Remote”. Don’t be fooled by some of the other libraries that have a similar name (including the Robot IR Remote that is already installed in your IDE), look for “IR Remote by Shirriff” – that’s the one you need. Click the “more info” link to get the library info and then click the button to install it into your IDE.

The beauty of this library is that it can be used for both receiving and transmitting IR Remote Control codes. Once you have it installed you’re ready to start working with remote controls with your Arduino.

Getting Remote HEX Codes

To start off we will build a very simple IR receiver using an IR sensor and an Arduino. Here is how we will hook it up:

As you can see the hookup is pretty simple. We use the 5 Volt and Ground outputs from the Arduino to connect to the sensors VCC (G) and GND (R) pins respectively. Then we connect Arduino pin 4 to the sensors Signal (Y) pin to collect information from it.

The actual digital I/O pin we’re using on the Arduino isn’t important so you could use a different one if you wish, just be sure to modify the sketch if you do.

In our first sketch we will capture the unique codes sent when each button the remote is pressed. We’ll use the serial monitor to display them.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/*

IR Receiver Demonstration 1

IR-Rcv-Demo1.ino

Demonstrates IR codes with IR Receiver

Displays results on Serial Monitor

DroneBot Workshop 2017

http://dronebotworkshop.com

*/

// Include IR Remote Library by Ken Shirriff

#include <IRremote.h>

// Define sensor pin

constintRECV_PIN=4;

// Define IR Receiver and Results Objects

IRrecv irrecv(RECV_PIN);

decode_results results;

voidsetup(){

// Serial Monitor @ 9600 baud

Serial.begin(9600);

// Enable the IR Receiver

irrecv.enableIRIn();

}

voidloop(){

if(irrecv.decode(&results)){

// Print Code in HEX

Serial.println(results.value,HEX);

irrecv.resume();

}

}

We start the sketch by including the IRRemote library. Next we define a pin for our IR Receiver, after which we create two objects for the IR Receiver and the Results. We then move on to the Setup routine.

We’ll be using a serial monitor to display our results so we setup the connection, this example uses 9600 baud but any speed will do – just be sure to set your Serial Monitor window to match the speed you have chosen. We also enable the IR Receiver in the setup routine.

Finally the loop. We use an If statement to verify that we have indeed received something and have a valid result. We then print the result value in hexadecimal, which is the standard way of displaying IR receiver codes. Finally we resume listening and repeat the loop.

Compile the code and upload it to your Arduino. Next open your serial monitor. Now grab an IR Remote control (just about any one will do) and aim it at the IR receiver module. If everything is working you’ll see the codes displayed in the serial monitor.

You may note a number of codes that read “FFFFFFFF”. These are “repeat codes” which are used with most remote controls to repeat the last key pressed, they are generated when you hold a key down. We will see how to deal with these a bit later.

Determining Manufacturer Codes

As I mentioned previously there are a number of groups of IR codes used by various manufacturers. For the experiments where we emulate IR Remote controls we’ll need to know which manufacturer codes to use.

The next sketch expands upon the last one to include a display of the Manufacturer Code along with the HEX codes. The wiring for this experiment is identical to the previous experiment.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

/*

IR Receiver Demonstration 2

IR-Rcv-Demo2.ino

Determine IR codes manufacturer type with IR Receiver

Displays results on Serial Monitor

DroneBot Workshop 2017

http://dronebotworkshop.com

*/

// Include IR Remote Library by Ken Shirriff

#include <IRremote.h>

// Define sensor pin

constintRECV_PIN=4;

// Define IR Receiver and Results Objects

IRrecv irrecv(RECV_PIN);

decode_results results;

voidsetup(){

// Serial Monitor @ 9600 baud

Serial.begin(9600);

// Enable the IR Receiver

irrecv.enableIRIn();

}

voidloop(){

if(irrecv.decode(&results)){

Serial.println(results.value,HEX);

switch(results.decode_type){

caseNEC:

Serial.println("NEC");

break;

caseSONY:

Serial.println("SONY");

break;

caseRC5:

Serial.println("RC5");

break;

caseRC6:

Serial.println("RC6");

break;

caseDISH:

Serial.println("DISH");

break;

caseSHARP:

Serial.println("SHARP");

break;

caseJVC:

Serial.println("JVC");

break;

caseSANYO:

Serial.println("SANYO");

break;

caseMITSUBISHI:

Serial.println("MITSUBISHI");

break;

caseSAMSUNG:

Serial.println("SAMSUNG");

break;

caseLG:

Serial.println("LG");

break;

caseWHYNTER:

Serial.println("WHYNTER");

break;

caseAIWA_RC_T501:

Serial.println("AIWA_RC_T501");

break;

casePANASONIC:

Serial.println("PANASONIC");

break;

caseDENON:

Serial.println("DENON");

break;

default:

caseUNKNOWN:

Serial.println("UNKNOWN");

break;

}

irrecv.resume();

}

}

The sketch is identical to the first one until we get to the loop.

The loop starts as before with an If statement to verify that we have indeed received a valid bit of data. Once again we display the HEX code for the data in the serial monitor.

We then go on to evaluate the “decode_type”, which is related to the manufacturer code received. This is done in a Case statement, for each case we print out the manufacturer in the serial monitor and then break out of the case statement. If we can’t determine the manufacturer we just print “Unknown”.

Again we finish and repeat the loop. The results on the serial monitor will now display both the HEX value and Manufacturer name for each code received.

You might want to make note of some of this information for the next experiment where we will put our knowledge to something more practical – repurposing unused buttons on our remote controls.

Repurposing Remote Controls

If you’re anything like me you likely have an abundance of remote controls around your home, maybe even several in the same room. Chances are that some of them have a LOT of buttons, including buttons that are of no use to you as they work with additional equipment that you don’t even own.

The example I use in the video is for an RCA television set that I own. The remote for this TV has buttons for a DVD or Blu-Ray player that I don’t own. These buttons are useless to me so I thought I’d put them to work controlling other things. With an Arduino and the IR Remote library it’s pretty easy to do that.

Before I started coding I needed to get both the HEX code for each of the buttons I was interested in. In my example I chose to use two buttons, a Red one with a HEX value of FECA35 and a Yellow one whose value turned out to be FE8A75.

Of course if you choose to perform the same experiment you’ll be using one of your own remote controls so you’ll need to use one of the previous sketches to determine and record the HEX codes for the buttons you are interested in.

To demo this I hooked up the Arduino with the IR Receiver module and a couple of LED’s, each LED also has a dropping resistor (I used 330 ohms but anything around that value will work fine).

Armed with the two HEX codes I wrote the following sketch. It detects the two aforementioned buttons and does the following:

When the REd button is pressed the Red LED in my experiment will flash on for two seconds and then turn off.

When the YELLOW button is pressed the Yellow LED will turn on and stay on. The next time that the YELLOW button is pressed the Yellow LED will turn off. In other words it’s a toggle switch.

So let’s take a look at the sketch:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

/*

IR Receiver Demonstration 3

IR-Rcv-Demo3.ino

Control LED's using Unused IR Remote keys

DroneBot Workshop 2017

http://dronebotworkshop.com

*/

// Include IR Remote Library by Ken Shirriff

#include <IRremote.h>

// Define sensor pin

constintRECV_PIN=4;

// Define LED pin constants

constintredPin=8;

constintyellowPin=7;

// Define integer to remember toggle state

inttogglestate=0;

// Define IR Receiver and Results Objects

IRrecv irrecv(RECV_PIN);

decode_results results;

voidsetup(){

// Enable the IR Receiver

irrecv.enableIRIn();

// Set LED pins as Outputs

pinMode(redPin,OUTPUT);

pinMode(yellowPin,OUTPUT);

}

voidloop(){

if(irrecv.decode(&results)){

switch(results.value){

case0xFECA35://Red Keypad Button

// Turn on LED for 2 Seconds

digitalWrite(redPin,HIGH);

delay(2000);

digitalWrite(redPin,LOW);

break;

case0xFE8A75://Yellow Keypad Button

// Toggle LED On or Off

if(togglestate==0){

digitalWrite(yellowPin,HIGH);

togglestate=1;

}

else{

digitalWrite(yellowPin,LOW);

togglestate=0;

}

break;

}

irrecv.resume();

}

}

Again we start by including the IR Remote library and defining a pin for the IR Receiver module. We also define a couple of pins that we will connect our LED’s to the Arduino with. None of this is cast in stone so you may use other pins if you like, just be sure to reflect your changes in the sketch.

Next we define a variable I called “togglestate”, this is used to represent the last state (on or off) of the Yellow LED which you’ll recall is being used as a toggle.

We then move on to setup the Receiver and Results objects as we did in the previous sketches.

In the setup loop we no longer define a serial port connection as there is no serial monitor being used. We just enable the IR Receiver and use the Arduino pinmode function to set the two LED pins as outputs.

On to the loop! Again we use an If statement to check to see we have a valid code received and then we evaluate the results with a couple of case statements.

In the first case statement we look for the code “FECA35” which means that the Red button has been pressed. If it evaluates then we turn on the Red LED, wait two seconds and then turn it off. We then break out of the case statement.

The second case statement looks for the code “FE8A75”, meaning that the Yellow button has been pressed. In this case we want to toggle the Yellow LED so we use an If statement to evaluate the current value of “togglestate”. If “togglestate” is zero the the LED was off so we turn it on and then set “togglestate” to 1. If the “togglestate” is already 1 then we turn off the yellow LED and set “togglestate” to zero. We then exploit the case statement and wait until another key press is detected.

In “real life” this circuit and code can have many practical applications, especially if you replace the LED’s with relays and use them to control devices. If you decide to do that be VERY CAREFUL as you’ll likely be working with AC line voltage. Don’t experiment directly with high voltage, use all the proper wiring precautions and test everything with a multimeter before you hook up any high voltage. If you are not comfortable working with line voltage then simply don’t do it!

Emulating IR Remote Controls

So far we have been experimenting with receiving and decoding remote control signals. Now let’s move on to transmitting them.

The wonderful IR Remote library also has facilities to transmit IR codes using an IR LED. An IR LED is used pretty well in the same way as a visible light LED except of course it transmits light in the infrared band so you can’t directly see it. Remember that you can use the camera in your phone or tablet if you need to “see” the IR light.

IR LEDs are quite inexpensive so stock up on a few the next time you place an order for parts on eBay or visit your local electronics store.

In this experiment I’ll use an IR LED with an Arduino and a push button to turn on (and off) my television. While this is a simple application you could extend it to control several different devices with the push of a button. You could also design a custom or simplified remote control to make it easier to control all of the home entertainment equipment you have piled up in your living room or bedroom.

Manufacturers Codes and Bits

Before we begin we’ll need to use the second experiment to determine both the HEX code and Manufacturers code for the device(s) we want to control. Once we do that we will need another bit of information – the number of bits to send with each code. This differs between manufacturers and, as it turns out, isn’t the easiest information to obtain.

I’ve summarized a few of the more popular codes here in a chart, along with links to get more details (if I have them). There is also very detailed information in some of the links in the Resource Box at the end of this article. If you find info that I’m missing here I’d appreciate you make a comment to share it with others.

Additionally the Sony and RC6 codes are sent three times for each button press while most of the others are only sent once.

Assuming you can find the information for the button(s) you need to emulate we can move on to building our circuit and coding for it.

Arduino Remote Emulator

In my simple experiment I’m going to emulate the Power button for the same RCA TV remote control I used earlier. I have determined that this button sends a code of “FEA857” and uses an NEC code that has a 32 bit bitstream.

One idiosyncrasy of the IR Remote library is that in order to send IR codes your IR LED needs to be on Pin 3 of the Arduino. Unlike the other demos this is cast in stone and can’t be changed, this is because Pin 3 is associated with an internal timer within the Arduino which the library uses to transmit codes.

Aside from the IR LED (and its associated 150 ohm dropping resistor) the circuit also uses a push button with a 10k pulldown resistor. The wiring is as follows:

Now that we have hooked up the circuit let’s take a look at the sketch that is used to send our power code to the television.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

/*

IR Transmitter Demonstration 1

IR-Xmit-Demo1.ino

Control TV using IR Library

IR LED must use Pin #3

DroneBot Workshop 2017

http://dronebotworkshop.com

*/

// Include IR Remote Library by Ken Shirriff

#include <IRremote.h>

// Define switch pin

constintswitchPin=7;

// Define a variable for the button state

intbuttonState=0;

// Create IR Send Object

IRsend irsend;

voidsetup()

{

// Set Switch pin as Input

pinMode(switchPin,INPUT);

}

voidloop(){

// Set button state depending upon switch position

buttonState=digitalRead(switchPin);

// If button is pressed send power code command

if(buttonState==HIGH){

irsend.sendNEC(0xFEA857,32);// TV power code

}

// Add a small delay before repeating

delay(200);

}

As always we start by including the versatile IR Remote library. We also define the pin used for the push button, as well as a variable “buttonstate” that holds the value (on or off) of the push button.

Next we’ll create an IR Send object as this time we’ll be using the library to send IR codes instead of receiving them

The Setup routine is pretty simple as all we are doing is defining the push button pin as an input.

The loop is also pretty simple. We check the button state using the Arduino “digitalRead” function and assign the state to the ‘buttonstate” variable. Then we use an If statement to determine if the button has been pressed.

If the button was pressed we use the libraries “irsend” function to transmit our code. The “irsend” function uses a “”sendNEC” property as our code is in NEC format (there is also a “sendSONY, sendRC6 and other properties for the other types of remote codes). The “irsend” function uses the code we wish to send (“FEA857” in my case) and the number of bits (32 in my case) as parameters.

All that is left is to add a tiny delay to debounce the push button. And it works, I can control my television with the push of a button – imagine that!

An IR Remote for your Arduino Projects

So far we have experimented with decoding and emulating the remote controls we have around the home. It’s now time to put an IR remote control to work in our own Arduino project.

There are a number of inexpensive remote control kits you can pick up on eBay and Amazon. Each one comes with a remote as well as an IR receiver module and an IR LED. They are all very inexpensive and ideal for creating our own remote control for our Arduino projects.

Using these remotes is exactly the same as using the commercial IR remotes we have been experimenting with, and you may use all of the previous sketches and wiring diagrams to put them to work.

Using the Repeat Function

One thing we have only touched on is the “repeat” function. As you may recall most remote controls (including the ones you get on eBay and Amazon) transmit a code of “FFFFFFFF” whenever a button is held down. This code indicates that the receiver should repeat the function until the button is released.

You have likely seen this in action on your own equipment as this function is used for controls like volume, brightness and contrast on most televisions.

Let’s put the repeat function to work in our own Arduino project!

In this project I’m going to control a small servo motor with one of those inexpensive custom remote controls. I have decided to only use three buttons for this demonstration, of course you’re free to expand upon this and use all of them if you wish.

My project will control the servo motor as follows:

The servo will start in the center position.

Pressing the Left (<) button once will move the servo two degrees to the left. Holding it down will make it continuously move left until it reaches the end of travel.

Pressing the Right (>) button once will move the servo right by two degrees. Holding it down will make it continuously move right until it reaches the end of travel.

Pressing the OK button will center the servo.

To achieve this I wired up my circuit as follows:

Note that I used a separate 5-volt power supply for the servo motor instead of using the Arduino 5-volt output. I found that the servo put too much electrical noise onto the 5-volt line and this interfered with the IR Sensor. I tried putting a 100uf capacitor across the power supply line to attempt to reduce the noise, it did but it still did not eliminate it completely. So I used my 5-volt bench supply to drive the servo.

If you wish you can also use a 6-volt battery to power the servo as most small servos are rated to accept 6-volts. Just don’t use the Arduino supply!

Now onto the sketch that makes the magic happen:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

/*

IR Receiver Demonstration 4

IR-Rcv-Demo4.ino

Demonstrates IR codes with Custom Remote and IR Receiver

Makes use of Repeat function

DroneBot Workshop 2017

http://dronebotworkshop.com

*/

// Include IR Remote Library by Ken Shirriff

#include <IRremote.h>

// Include Arduino Servo Library

#include <Servo.h>

// Define Sensor Pin

constintRECV_PIN=4;

// Define Servo Pin

constintSERVO_PIN=6;

// Define Variable for Servo position

// Start at 90 Degrees (Center position)

intpos=90;

// Define variable to store last code received

unsignedlonglastCode;

// Define IR Receiver and Results Objects

IRrecv irrecv(RECV_PIN);

decode_results results;

// Create servo object

Servo myservo;

voidsetup()

{

// Start the receiver

irrecv.enableIRIn();

// Attach the servo

myservo.attach(SERVO_PIN);

// Start with Servo in Center

myservo.write(pos);

}

voidloop(){

if(irrecv.decode(&results))//this checks to see if a code has been received

{

if(results.value==0xFFFFFFFF)

{

// If Repeat then use last code received

results.value=lastCode;

}

if(results.value==0xFF22DD)

{

// Left Button Pressed

lastCode=results.value;

// Move left 2 degrees

pos+=2;

// Prevent position above 180

if(pos>180){pos=180;}

myservo.write(pos);

}

if(results.value==0xFFC23D)

{

// Right Button Pressed

lastCode=results.value;

// Move Right 2 degrees

pos-=2;

// Prevent position below 0

if(pos<0){pos=0;}

myservo.write(pos);

}

if(results.value==0xFF02FD)

{

// Center Button Pressed

lastCode=results.value;

// Move to Center

pos=90;

myservo.write(pos);

}

// Add delay to prevent false readings

delay(30);

//receive the next value

irrecv.resume();

}

}

Again we begin by including the IR Remote library. We also include the Arduino Servo Library which is already included with the Arduino IDE.

Next we define constants representing the pins that our IR Sensor and Servo are connected to. There is also a variable called “pos” defined to hold the value (in degrees) that our servo is positioned. As we want to start the servo in its center position we initialize this with a value of 90. Finally we define a long variable called “lastcode” that holds the most recent key code value, we will use this with the repeat function to “remember” what key we are supposed to be repeating.

Next we create object for the IR Receiver and the Results, as well as an object representing the servo.

In the Setup routine we enable the IR Receiver, attach the servo to the pin we defined earlier and move the servo to the center position.

Now the loop. As before we will check to see if we have a valid result code from the IR Receiver and assuming we do we check to see what the code was using a series of If statements.

First we check to see if we have received the repeat code, FFFFFFFF. If we do we replace the value of the result with the previously received code, held in the “lastcode” variable. It’s very important that we execute this If statement first.

After that we go through a series of If statements that check for our button presses. If we find the Left button code has been received then we add 2 to the value of “pos” and move the servo to that position. We also check to be sure that “pos” does not exceed 180 as that’s the maximum value for our servo.

Similarly we check to see if the Right button was pressed, if it was then we subtract 2 from the value of pos and move the servo accordingly. Again we test to ensure that the value of “pos” doesn’t go below zero.

Finally we check to see if the OK button was pressed. If it was we reset the value of “pos” to 90, which will center the servo.

A small time delay was added to the end of the code to prevent false readings, you may experiment with the value if you wish.

After coding and compiling we upload the code to the Arduino and test it out. Check out the video to see it working.

Conclusion

As you can see by using the IR Remote library and an Arduino we can really do a lot with IR Remote controls. We can repurpose existing controls, build custom controls and design projects with our own remotes.

I’d really be interested to hear what you manage to build using this information. Please share any of your designs along with any questions you might have in the comments below.

The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

newestoldestmost voted

B Mc Carthy

Hallo Droneworkshop I’m Brian from Limburg the Netherlands.
I have loved experimenting with the remote on the Arduino and I was so encious to controll those two LEDs. After copying your code, I got an error message:
‘irrecv’ has no type of name!
on line command 17 I’ve discovered:
const int redPin = 8; irrecv.resume(); . What does the “irrecv.resume();” do in that line? After copying and compiling again, the same error message appeared again, caused by irrecv.resume(), so I’ve put slashes in front of that command and the program worked.

“If the button was pressed we use the libraries “irsend” function to transmit our code. The “irsend” function uses a “”sendNEC” property as our code is in NEC format (there is also a “sendSONY, sendRC6 and other properties for the other types of remote codes).”

Hello I really enjoy your tutorials and hope someone can help me with my code as i cannot seem to find why its not working probably as I’m a noob.
Here is my codehttps://pastebin.com/BzEXYbND

Vote Up0Vote Down Reply

6 months ago

Marco Macrì

hi, i want to say that i followed your video untill the end and i found it very interesting. you explain concepts narrowly. now, i have a quetsion, what model is your tv? i have an anonymous tv which remote is the same as yours: i would like to pair my tv with a universal remote, but i cant’ t find the model. i detected the code with my arduino and it is the same of your remote. please, i need help. D:﻿

I just checked the television, it’s an RCA model RLEDV2488A. Hope that helps, best of luck with with your project!

Vote Up0Vote Down Reply

4 months ago

PAUL J VIOLA

Hello Bill; Thank you very much for the wonderfully produced videos, I find them clear, concise and very helpful. I am a mechanical engineer and have worked in orthopedics for 40 years. I have my own consulting firm for the last 25 years and have worked on many interesting projects of all types. I have the highest level ham radio classification so I am somewhat familiar with electronics, but being 66 years old these microprocessors have taken me some time to absorb. You have made it enjoyably successful for me, thank you very much and keep up the great work.… Read more »

Vote Up0Vote Down Reply

1 month ago

SBSINGH

HEY , I M HOBBYIST ABOUT AVR/ ARDUINO , I MADE MANY PROJECTS REGARDING INTERFACE WITH IR REMOTE (NEC) BUT MINE QUESTION ABOUT ITS SENSITIVITY. I NOTICED ONE THING THAT BY USING TSOP1838 THE MAXIMUM RANGE FOR OPERATION IS APROX 5-6 FEET WHILE TV REMOTE DOES WORK FROM EVERYWHERE IN ROOM…TV SETS ARE VERY SENSITIVE ABOUT COMMANDS
WHAT MAKE IT MUCH SENSITIVE & HOW THEY CAPTURE EASILY COMMAND FROM REMOTE.