Using IR Remote Controls With Arduino - Drone Bot Workshop
Using IR Remote Controls With Arduino - Drone Bot Workshop
Build your own Electronics, IoT, Drones and Robots – Welcome to the Workshop!
Search
3.2 IR Transmitters
8 Conclusion
8.0.1 Resources
Welcome to the
Workshop!
Help me decide which articles and
The IR remote control has changed how we interact with our videos to make for YOU.
appliances, for better or worse.
Subscribe to the Dronebot
Chances are you have a large collection of these devices, Workshop Newsletter so that we
some may even be left over from equipment you no longer can keep in touch and so that YOU
own or use. Most of the ones you do make use of have several can tell me what articles and
buttons that serve you no purpose as they were made to videos YOU want to see.
integrate with additional equipment that you don’t even own.
No spam, no sales - just useful
Time to put those unused buttons and controllers to good use! information and surveys to find out
All you will need are a few very inexpensive parts and an what I should cover in my next
Arduino to decode the outputs from these little electronic video and article.
gems and put them to work in your own custom designs.
Subscribe Today!
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 More for You!
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!
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.
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.
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:
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.
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.
Ken has an excellent blog that goes into great detail about
using the library, I urge you to pay it a visit.
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 IR Receiver Demonstration 1
3 IR-Rcv-Demo1.ino
4 Demonstrates IR codes with IR Receiver
5 Displays results on Serial Monitor
6
7 DroneBot Workshop 2017
8 http://dronebotworkshop.com
9 */
10
11 // Include IR Remote Library by Ken Shirriff
12 #include <IRremote.h>
13
14 // Define sensor pin
15 const int RECV_PIN = 4;
16
17 // Define IR Receiver and Results Objects
18 IRrecv irrecv(RECV_PIN);
19 decode_results results;
20
21
22 void setup(){
23 // Serial Monitor @ 9600 baud
24 Serial.begin(9600);
25 // Enable the IR Receiver
26 irrecv.enableIRIn();
27 }
28
29 void loop(){
30 if (irrecv.decode(&results)){
31 // Print Code in HEX
32 Serial.println(results.value, HEX);
33 irrecv.resume();
34 }
35 }
1 /*
2 IR Receiver Demonstration 2
3 IR-Rcv-Demo2.ino
4 Determine IR codes manufacturer type with IR Receiver
5 Displays results on Serial Monitor
6 DroneBot Workshop 2017
7 http://dronebotworkshop.com
8 */
9
10 // Include IR Remote Library by Ken Shirriff
11 #include <IRremote.h>
12
13 // Define sensor pin
14 const int RECV_PIN = 4;
15
16 // Define IR Receiver and Results Objects
17 IRrecv irrecv(RECV_PIN);
18 decode_results results;
19
20 void setup(){
21 // Serial Monitor @ 9600 baud
22 Serial.begin(9600);
23 // Enable the IR Receiver
24 irrecv.enableIRIn();
25 }
26
27 void loop(){
28 if (irrecv.decode(&results)){
29
30
Serial.println(results.value, HEX);
switch (results.decode_type){
31 case NEC:
32 Serial.println("NEC");
33 break;
34 case SONY:
35 Serial.println("SONY");
36 break;
37 case RC5:
38 Serial.println("RC5");
39 break;
40 case RC6:
41 Serial.println("RC6");
42 break;
43 case DISH:
44 Serial.println("DISH");
45 break;
46 case SHARP:
47 Serial.println("SHARP");
48 break;
49 case JVC:
50 Serial.println("JVC");
51 break;
52 case SANYO:
53 Serial.println("SANYO");
54 break;
55 case MITSUBISHI:
56 Serial.println("MITSUBISHI");
57 break;
58 case SAMSUNG:
59 Serial.println("SAMSUNG");
60 break;
61 case LG:
62 Serial.println("LG");
63 break;
64 case WHYNTER:
65 Serial.println("WHYNTER");
66 break;
67 case AIWA_RC_T501:
68 Serial.println("AIWA_RC_T501");
69 break;
70 case PANASONIC:
71 Serial.println("PANASONIC");
72 break;
73 case DENON:
74 Serial.println("DENON");
75 break;
76 default:
77 case UNKNOWN:
78 Serial.println("UNKNOWN");
79 break;
80 }
81 irrecv.resume();
82 }
83 }
The sketch is identical to the first one until we get to the loop.
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.
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.
Armed with the two HEX codes I wrote the following sketch. It
detects the two aforementioned buttons and does the
following:
1 /*
2 IR Receiver Demonstration 3
3 IR-Rcv-Demo3.ino
4 Control LED's using Unused IR Remote keys
5
6 DroneBot Workshop 2017
7 http://dronebotworkshop.com
8 */
9
10 // Include IR Remote Library by Ken Shirriff
11 #include <IRremote.h>
12
13 // Define sensor pin
14 const int RECV_PIN = 4;
15
16 // Define LED pin constants
17 const int redPin = 8;
18 const int yellowPin = 7;
19
20 // Define integer to remember toggle state
21 int togglestate = 0;
22
23 // Define IR Receiver and Results Objects
24 IRrecv irrecv(RECV_PIN);
25 decode_results results;
26
27
28 void setup(){
29 // Enable the IR Receiver
30 irrecv.enableIRIn();
31 // Set LED pins as Outputs
32 pinMode(redPin, OUTPUT);
33
34
pinMode(yellowPin, OUTPUT);
}
35
36
37 void loop(){
38 if (irrecv.decode(&results)){
39
40 switch(results.value){
41 case 0xFECA35: //Red Keypad Button
42 // Turn on LED for 2 Seconds
43 digitalWrite(redPin, HIGH);
44 delay(2000);
45 digitalWrite(redPin, LOW);
46 break;
47
48 case 0xFE8A75: //Yellow Keypad Button
49 // Toggle LED On or Off
50 if(togglestate==0){
51 digitalWrite(yellowPin, HIGH);
52 togglestate=1;
53 }
54 else {
55 digitalWrite(yellowPin, LOW);
56 togglestate=0;
57 }
58 break;
59
60 }
61 irrecv.resume();
62 }
63
64 }
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!
Denon 15 Bits
Motorola 9 Bits
Samsung 20 Bits
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.
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 IR Transmitter Demonstration 1
3 IR-Xmit-Demo1.ino
4 Control TV using IR Library
5 IR LED must use Pin #3
6 DroneBot Workshop 2017
7 http://dronebotworkshop.com
8 */
9
10 // Include IR Remote Library by Ken Shirriff
11
12 #include <IRremote.h>
13
14 // Define switch pin
15 const int switchPin = 7;
16
17 // Define a variable for the button state
18 int buttonState = 0;
19
20 // Create IR Send Object
21 IRsend irsend;
22
23 void setup()
24 {
25
26
// Set Switch pin as Input
pinMode(switchPin, INPUT);
27 }
28
29 void loop() {
30
31 // Set button state depending upon switch position
32 buttonState = digitalRead(switchPin);
33
34 // If button is pressed send power code command
35 if (buttonState == HIGH) {
36 irsend.sendNEC(0xFEA857, 32); // TV power code
37 }
38
39 // Add a small delay before repeating
40 delay(200);
41
42 }
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!
1 /*
2 IR Receiver Demonstration 4
3 IR-Rcv-Demo4.ino
4 Demonstrates IR codes with Custom Remote and IR Receive
5 Makes use of Repeat function
6
7 DroneBot Workshop 2017
8 http://dronebotworkshop.com
9 */
10
11 // Include IR Remote Library by Ken Shirriff
12 #include <IRremote.h>
13
14 // Include Arduino Servo Library
15 #include <Servo.h>
16
17 // Define Sensor Pin
18 const int RECV_PIN = 4;
19
20 // Define Servo Pin
21 const int SERVO_PIN = 6;
22
23 // Define Variable for Servo position
24 // Start at 90 Degrees (Center position)
25 int pos = 90;
26
27 // Define variable to store last code received
28 unsigned long lastCode;
29
30
31
// Define IR Receiver and Results Objects
IRrecv irrecv(RECV_PIN);
32 decode_results results;
33
34 // Create servo object
35 Servo myservo;
36
37 void setup()
38 {
39 // Start the receiver
40 irrecv.enableIRIn();
41
42 // Attach the servo
43 myservo.attach(SERVO_PIN);
44
45 // Start with Servo in Center
46 myservo.write(pos);
47 }
48
49 void loop() {
50 if(irrecv.decode(&results)) //this checks to see if a cod
51 {
52
53 if(results.value == 0xFFFFFFFF)
54 {
55 // If Repeat then use last code received
56 results.value = lastCode;
57 }
58
59 if(results.value == 0xFF22DD)
60 {
61 // Left Button Pressed
62 lastCode = results.value;
63 // Move left 2 degrees
64 pos += 2;
65 // Prevent position above 180
66 if(pos > 180){pos = 180;}
67 myservo.write(pos);
68 }
69
70 if(results.value == 0xFFC23D)
71 {
72 // Right Button Pressed
73 lastCode = results.value;
74 // Move Right 2 degrees
75 pos -= 2;
76 // Prevent position below 0
77 if(pos < 0){pos = 0;}
78 myservo.write(pos);
79 }
80
81 if(results.value == 0xFF02FD)
82 {
83 // Center Button Pressed
84 lastCode = results.value;
85 // Move to Center
86 pos = 90;
87 myservo.write(pos);
88 }
89
90 // Add delay to prevent false readings
91 delay(30);
92 //receive the next value
93 irrecv.resume();
94 }
95
96 }
Again we begin by including the IR Remote library. We also
include the Arduino Servo Library which is already included
with the Arduino IDE.
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.
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.
Resources
Arduino Sketches All of the code from the experiments in
this article.
Summary
Publisher Logo
Related
6 Leave a Reply
B Mc Carthy
Greetings.
B. Mc Carthy
Bryan Young
“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).”
DroneBot Workshop
noob-code-bunny
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 code
https://pastebin.com/BzEXYbND
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:
DroneBot Workshop
Arduino Projects Arduino Tutorials Powering Your Electronics Stepper Motors with
Projects – Voltage Arduino - Getting Started
Drone Projects Drone Tutorials
Regulators and Converters with Stepper Motors
Electronics Projects Electronics Tutorials
DF Robot LIDAR Sensors – Controlling DC Motors
Internet of Things Projects Internet of Things Getting Started with LIDAR with the L298N Dual H-
Tutorials Bridge and an Arduino
Raspberry Pi Projects DFRobot 5 DOF Robot Arm
Raspberry Pi Tutorials – Building the Robotic Using Inexpensive
Robot Projects
Arm 433MHz Transmit and
Robot Tutorials
Receive Modules with
Using Servo Motors with
Software Tutorials Arduino
the Arduino
Using the HC-SR04
Getting Moving with XOD
Ultrasonic Distance
– Robot Car Part 1
Sensor with Arduino
RGB LEDS – Colorful
Arduino Experiments
Workshop
Arduino Visual
Programming – Getting Connections
Started with XOD
Using LCD Displays with
Arduino