Sunday, September 2, 2012

Arduino RGB led managed by remote control

Taking advantage of the previous post, we are going to add an IR receiver to the circuit so that we can vary the color and brightness of the led with a remote control of Dealextreme, which is perfect for the project. The controller in question is the SKU: 47019, a very cheap remote control that comes with battery included:

RGB IR Remote control

To get the Arduino detected it, will use a TSOP 1738 or similar type infrared receiver (in my case I have a 1736 and works equally although command uses a 38KHz carrier).
The 5W RGB Led (DealExtreme)

The 5W RGB Led (DealExtreme)

This is the scheme:
RGB led controlled by IR Remote with Arduino


And this the assembly :
RGB led controlled by IR Remote with Arduino

Now we need the IRRemote library, that allows to read multitude of remote control codes. In particular it works perfectly with our remote control (which uses the NEC Protocol).
We must download the library and install it in our Arduino environment.

The code for example would be as follows (to copy the code, double click on it):

/*
*  Control remoto de LED RGB 
*  Ejemplo de como hacer variar el color y el brillo con un led RGB  
*  con un mando a distancia.
*  Se utiliza un receptor de infrarrojos del tipo TSOP1738 
*  Autor: Jose Daniel Herrera
*  Fecha: 28/08/2012
*  http://arduino-guay.blogspot.com.es
*/

#include 

int RECV_PIN = 8;
int R_PIN = 9;
int G_PIN = 6;
int B_PIN = 10;

#define ON                0XFFE01F
#define OFF               0xFF609F
#define BRIGHTNESS_UP     0xFFA05F
#define BRIGHTNESS_DOWN   0xFF20DF
#define FLASH             0xFFF00F
#define STROBE            0xFFE817
#define FADE              0xFFD827
#define SMOOTH            0xFFC837

#define RED               0xFF906F
#define GREEN             0XFF10EF
#define BLUE              0xFF50AF
#define WHITE             0xFFD02F

#define ORANGE            0xFFB04F
#define YELLOW_DARK       0xFFA857
#define YELLOW_MEDIUM     0xFF9867
#define YELLOW_LIGHT      0xFF8877

#define GREEN_LIGHT       0XFF30CF
#define GREEN_BLUE1       0XFF28D7
#define GREEN_BLUE2       0XFF18E7
#define GREEN_BLUE3       0XFF08F7

#define BLUE_RED          0XFF708F
#define PURPLE_DARK       0XFF6897
#define PURPLE_LIGHT      0XFF58A7
#define PINK              0XFF48B7

#define INCREMENTO 10

unsigned long rgb = 0;
byte r,g,b;

IRrecv irrecv(RECV_PIN);

decode_results results;


void setup()
{
  irrecv.enableIRIn(); // Inicializamos el receptor
  Serial.begin(9600);
  pinMode(R_PIN, OUTPUT);   
  pinMode(G_PIN, OUTPUT);   
  pinMode(B_PIN, OUTPUT);   
}


void variar (byte* color, char valor) {
    if (valor > 0) {
        if ( *color + valor <= 255) {
            *color += valor;
        } else {
            *color = 255;
        }
    } else { 
        if (*color + valor >= 0) {
            *color += valor;
        } else {
            *color = 0;
        }
  }
}

void RGB(unsigned long valor) {
   r = valor >> 16; 
   g = (valor >> 8) & 0xFF; 
   b = valor & 0xFF; 
}

void loop() {
  if (irrecv.decode(&results)) {
    if ( results.value != 0xFFFFFFFF) {
      switch (results.value) {
           case BRIGHTNESS_UP : 
               variar (&r, INCREMENTO);
               variar (&g, INCREMENTO);
               variar (&b, INCREMENTO);
               break; 
           case BRIGHTNESS_DOWN : 
               variar (&r, -INCREMENTO);
               variar (&g, -INCREMENTO);
               variar (&b, -INCREMENTO);
               break; 
           case OFF :
               r = g = b = 0;
               break;    
           case RED           : RGB(0x00FF0000); break;
           case GREEN         : RGB(0x0000FF00); break;
           case BLUE          : RGB(0x000000FF); break;
           case WHITE         : RGB(0x00FFFFFF); break;
           case ORANGE        : RGB(0x00FF7F00); break;
           case YELLOW_DARK   : RGB(0x00FFAA00); break;
           case YELLOW_MEDIUM : RGB(0x00FFD400); break;
           case YELLOW_LIGHT  : RGB(0x00FFFF00); break;
           case GREEN_LIGHT   : RGB(0x0000FFAA); break;
           case GREEN_BLUE1   : RGB(0x0000FFFF); break;
           case GREEN_BLUE2   : RGB(0x0000AAFF); break;
           case GREEN_BLUE3   : RGB(0x000055FF); break;
           case BLUE_RED      : RGB(0x00000080); break;
           case PURPLE_DARK   : RGB(0x003F0080); break;
           case PURPLE_LIGHT  : RGB(0x007A00BF); break;
           case PINK          : RGB(0x00FF00FF); break;
      }
      Serial.println(results.value, HEX);
      Serial.println(r,DEC);
      Serial.println(g, DEC);
      Serial.println(b, DEC);
      analogWrite(R_PIN,r);
      analogWrite(G_PIN,g);
      analogWrite(B_PIN,b);
    }
    irrecv.resume(); // Receive the next value
  }
}
Caution!: The IRremote library internally use the TIMER2 of Arduino. This causes outputs 11 and 3 do not work as PWM ( therefore I changed the Green led to output 6). On the other hand there is a newer version of the library IRremote, which recognizes many protocols of IR codes, but apparently after several pulses, the obtained codes seem random...??.Can serve you to support other remote controls or for other projects.
For obtain the constants I used the example that comes with the 'IRrecvDemo' library, which allows display on the console the codes sent by every button on the remote. Same is true if you want to use another different remote control.

A video demo:

If you liked it, remember to share it on your favorite social network. Thanks

31 comments:

  1. Both the receiver and the library seem to have vanished. The library's last known location on that service doesn't exist. Note: Not the one that you reference, the supposed later one. The receiver is considered obsolete by Mouser. Others exist from that vendor but their website is currently off-line.

    ReplyDelete
  2. I've changed the link to the new library.
    The receiver may be any one that works with 38 Khz (I actually use one of 36 kHz), such as TSOP 31238
    Thank you.

    ReplyDelete
  3. What purpose does the ULN2002A IC? What if I wanted to use a universal remote and the Sony protocol? Would this sketch still be valid? What arduino library are you currently using for this project? Thanks!

    ReplyDelete
  4. @Anonymous
    The ULN2003A is a driver for manage the power RGB led, in the example is a 5W led.
    The irremote library support this prortocols :NEC, SONY, RC5, RC6, DISH, SHARP, PANASONIC, JVC, SANYO and MITSUBISHI.
    The IR receiver normally are 36 to 38KHz, depend of remote control, but are not critical.

    ReplyDelete
  5. Hi! I was looking exactly for a sketch like
    yours but when I try to compile on 1.0.3 or 0022
    I obtain several error messages. The IRremote
    librairy is instaled correctly, I can run
    exemples of the librairy flawlessly.
    Is something I missed? I will be very happy
    if you can help me, my hardware is ready,
    I have the same remote then yours.
    Regards.

    ReplyDelete
  6. I have downloaded the most recent ir library from github and i also am having problems. See below:




    sketch_dec27a:12: error: expected constructor, destructor, or type conversion before '<' token
    sketch_dec27a:56: error: 'IRrecv' does not name a type
    sketch_dec27a:58: error: 'decode_results' does not name a type
    sketch_dec27a.cpp: In function 'void setup()':
    sketch_dec27a:63: error: 'irrecv' was not declared in this scope
    sketch_dec27a.cpp: In function 'void loop()':
    sketch_dec27a:94: error: 'irrecv' was not declared in this scope
    sketch_dec27a:94: error: 'results' was not declared in this scope
    sketch_dec27a.cpp: At global scope:
    sketch_dec27a:140: error: expected unqualified-id before '<' token

    ReplyDelete
  7. @Anonymous
    Hello

    If you copied the sketch directly, there is an error due to formatted javascript code on the website:
    The line "# include <irremote .h=".h">" should be "# include <IRremote.h>"
    The last line "</ irremote>" spare, delete it.

    ReplyDelete
  8. Thank you for taking the time to respond.

    ReplyDelete
  9. I've got some errors:

    sketch_jan13a:52: error: 'IRrecv' does not name a type
    sketch_jan13a:54: error: 'decode_results' does not name a type
    sketch_jan13a.ino: In function 'void setup()':
    sketch_jan13a:59: error: 'irrecv' was not declared in this scope
    sketch_jan13a.ino: In function 'void loop()':
    sketch_jan13a:90: error: 'irrecv' was not declared in this scope
    sketch_jan13a:90: error: 'results' was not declared in this scope

    can someone help me?

    ReplyDelete
  10. Can you add a CASE for the fade button to fade between RGB values (swirl effect)?

    Thanks.

    ReplyDelete
  11. I've tried adding a case for a FADE function; what a nightmare. I'm going to get a new China controller and call it a day.

    ReplyDelete
  12. Answering to last anonymous: The FADE function doesnt work because of the delay function can't be interrupted.
    You'll have to count the time by another way, such as this one for exemple:

    void attendre(int interval) {
    currentMillis = millis();

    while (millis() - currentMillis < interval) {
    if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF) {
    return;
    }
    irrecv.resume(); // Receive the next value
    }

    }//fin du while
    }

    and here's the fade fonction:

    //FONCTION FADE
    void Fade() {

    // variation de rouge: montée
    for (int i=0; i<=255; i++) { // défile valeur 0 à 255
    ledRVBpwm(i,0,0); // génère impulsion largeur voulue pour la couleur
    attendre(temps_montee_couleur);
    if (irrecv.decode(&results)) {
    if (results.value != 0xFFFFFFFF) {
    BOUTON = 0;
    return;
    }
    irrecv.resume(); // Receive the next value
    }

    }//fin du for

    }

    If you have any problem, you can contact me at jgrisolia@free.fr


    ReplyDelete
  13. Here's an interesting one. The code presented to the Fritzing project showcase compiles without any problem. And this is using the IR remote based libraries created by a probably different fellow then the one for your blog directly.

    The one here complains about the missing library which I did manage to download but haven't installed yet.

    Also since my RGB LED isn't like yours I won't need the driver transistor block I will only need three resistors.

    ReplyDelete
  14. Update:
    The code (and library) that is presented in that zip file is a fork from the original IR remote code that's presented here http://www.righto.com/2009/08/multi-protocol-infrared-remote-library.html from there he presents a master GIT repository and says it has been updated. I suspect that what you have there is older.

    ReplyDelete
  15. Thanks for posting ^^

    ReplyDelete
  16. Hi can this be made a vise versa , i have a ir controlled RGB led strip and a similar remote for it, and i want to use arduina and a IR led to controll that light strip By ir led

    ReplyDelete
  17. @Arttu Mahlakaarto
    Yes , the IRremote library can 'send' de same codes.
    You must use the object 'IRsend', and de method 'sendNEC'

    ReplyDelete
  18. even with the corrections:
    The line "# include " should be "# include "
    The last line "" spare, delete it.
    The code still doesnt compile: IRRecv does not name a type etcetera

    ReplyDelete
  19. Found it. it should say "IRremote.h" not "irremote.h"

    ReplyDelete
  20. why i am getting this error?

    In file included from sketch_may21c.ino:3:
    C:\Program Files (x86)\arduino\libraries\IRremote/IRremoteInt.h:87: error: 'uint8_t' does not name a type
    C:\Program Files (x86)\arduino\libraries\IRremote/IRremoteInt.h:88: error: 'uint8_t' does not name a type
    C:\Program Files (x86)\arduino\libraries\IRremote/IRremoteInt.h:89: error: 'uint8_t' does not name a type
    C:\Program Files (x86)\arduino\libraries\IRremote/IRremoteInt.h:92: error: 'uint8_t' does not name a type

    ReplyDelete
  21. @Cagatay Erdilmen
    you need to change
    #include <WProgram.h>
    to
    #include <Arduino.h>
    in IRRemote.h.

    ReplyDelete
  22. What RGB LED did you use?

    ReplyDelete
  23. @Anonymous
    I use DX 5W rgb led but you can use any RGB led , a 'normal' RGB led d'ont need de ULN2003A

    ReplyDelete
  24. do not work fade ,strobe and on button.pl help me

    ReplyDelete
  25. Thanks for this example! It helped me throw together a custom remote controlled RGB strip project really fast!

    ReplyDelete
  26. what would i do if i'll use a rgb led strip???

    ReplyDelete
  27. what would i do if i'll use a rgb led strip???

    ReplyDelete
  28. @Mark Gideon Urbano
    You can use the same circuit. The UNL2003 offers 500 mA per output

    ReplyDelete
  29. I have been working on an IR receiver Project (laser tag). Did you use fritzing for that assembly picture? I can't seem to find a TSOP part that looks similar to that and i would like to document each iteration of my project. Thanks.

    ReplyDelete
  30. Hola amigo, si tengo un led rgb anodo comun que valores debo cambiar?

    ReplyDelete
  31. @Anonymous

    Can you paste here/send the complete code with fade function to: amanjosan2008@gmail.com

    ReplyDelete

Note: Only a member of this blog may post a comment.

Arduino Chromatic Clock, with Steampunk style

Arduino Chromatic Clock, with Steampunk  style Need a unique gift for Christmas? ... This watch unlike normal, uses colors instead of...