Pages

Wednesday, September 21, 2016

Use any infrared remote to control an RGB led with an arduino uno [beginner]



This is a very simple tutorial article for those who want to start using an infrared remote with an arduino for controlling different things. This will cover the basic principles and can be used to perform complicated things as in my previous posts here and here

Things needed


1) An arduino uno or compatible boards (atmega328 based)
2) IR Receiver Module 38 kHz TSOP4838  (ebay)
3) 3 Colour RGB  LED Module
4) A TV remote or any ir remote
5) Connecting wires


Connecting things


Connections are pretty simple, being beginners tutorial i will explain a bit: See the figure below



IR module has a vcc and gnd pin which is connected to 5volt pin and ground pin on the arduino. Similarly there are four connections on the rgb led, the common ground is connected to ground (-ve) pin on arduino and each colour is connected to digital pins (D9, 10 and 11 respectively)

Uploading the firmware



Now connect the usb cable and connect it to the computer with arduino software. It needs a library for infrared remotes which can be downloaded from here. After setting up the library paste the source code below and upload to the arduino.

Now after uploading the code and if you have an nec remote, pressing 7, 8, and 9 will turn on each colors on the rgb led and 4,5,and 6 will turn it off. If nothing happens and probably you have some other remote controls, just open a serial monitor  (it is a window on the arduino software, see here) and set it to 9600 and start pressing the remote buttons to see some numbers appearing over there. Just replace those numbers in the section below ( complete source code at the end of the article)

     if(results.value==3782926367)  // number 7
     digitalWrite(led, HIGH);  
     else if (results.value==3782877407)  //number 4
     digitalWrite(led, LOW);  
     // result  
     if(results.value==3782873327)  //number 8
     digitalWrite(blue, HIGH);  
     else if (results.value==3782910047) //number 5 
     digitalWrite(blue, LOW);  
     // result  
     if(results.value==3782905967)  //number 9
     digitalWrite(green, HIGH);  
     else if (results.value==3782893727)  //number 6
     digitalWrite(green, LOW);  

See the video :



Source code 


 /* Include the IRremote library */  
 #include <IRremote.h>  
 /* Define the DIO pin used for the receiver */  
 #define RECV_PIN 5  
 /* Structure containing received data */  
 decode_results results;  
 /* Used to store the last code received. Used when a repeat code is received */  
 unsigned long LastCode;  
 /* Create an instance of the IRrecv library */  
 IRrecv irrecv(RECV_PIN);  
 int led = 9;  
 int green = 10;  
 int blue = 11;  
 void printHex(int num, int precision) {  
   char tmp[16];  
   char format[128];  
   sprintf(format, "0x%%.%dX", precision);  
   sprintf(tmp, format, num);  
   Serial.print(tmp);  
 }  
 void setup()  
 {  
   /* Configure the serial port to display the received codes */  
   Serial.begin(9600);  
   /* Start receiving codes */  
   irrecv.enableIRIn();  
   irrecv.blink13(1);  
   /* Initialise the variable containing the last code received */  
   LastCode = 0;  
   pinMode(led, OUTPUT);  
   pinMode(blue, OUTPUT);  
   pinMode(green, OUTPUT);  
 }  
 /* Main program */  
 void loop()  
 {  
   /* Has a new code been received? */  
   if (irrecv.decode(&results))  
   {  
     /* If so get the button name for the received code */  
     //Serial.println(GetIRIndex(results.value));  
     Serial.println(results.value);  
     //printHex(results.value,7);  
     if(results.value==3782926367)  
     digitalWrite(led, HIGH);  
     else if (results.value==3782877407)  
     digitalWrite(led, LOW);  
     // result  
     if(results.value==3782873327)  
     digitalWrite(blue, HIGH);  
     else if (results.value==3782910047)  
     digitalWrite(blue, LOW);  
     // result  
     if(results.value==3782905967)  
     digitalWrite(green, HIGH);  
     else if (results.value==3782893727)  
     digitalWrite(green, LOW);  
     /* Start receiving codes again*/  
     irrecv.resume();  
   }  
 }  


No comments:

Post a Comment