Categories

Control an RGB led using a remote control

In this example we will control an RGB led with a remote control, this will involve us connecting an infrared reciever to our Arduino as well.

We will use the buttons 1,2 and 3 to control the Red, Green and blue colours of the RGB LED. You will be able to switch the colours ona nd off which of course means you can mix the colours as well

Here is the remote control we will use in this example
1pcs/lot 38khz MCU learning board IR remote control Infrared decoder for protocol remote control For arduino

Layout

Arduino pins used

Pin 3 : IR receiver input
Pin 8 : Red cathode
Pin 9 : Green Cathode
Pin 10 : Blue Cathode

I used an IR module and RGB led module as well, the following shows this

 

Code

The code needs the IRremote remote library to be installed. Then you need to find the codes for the various buttons on the remote control that you wish to use

#include <IRremote.h>
int RECV_PIN = 3;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
irrecv.resume();
}
}

Now upload the code above and open the serial monitor, when you press each button you should see the hex code for the keys you want to use, here is an example

0xFF30CF
0xFF18E7

These are the hex codes for buttons 1 and 2, we will use these in our full example underneath

Here is the complete code example
#include <IRremote.h>
 
int RECV_PIN = 3;
#define  redpin 8 
#define  greenpin 9
#define  bluepin 10 
 
boolean state1 = true;
boolean state2 = true;
boolean state3 = true;
 
IRrecv irrecv(RECV_PIN);
decode_results results;
 
void setup()  
{
    Serial.begin(9600);
    pinMode(redpin, OUTPUT);
    pinMode(greenpin, OUTPUT);
    pinMode(bluepin, OUTPUT);
    // Set high for common anode type
    digitalWrite(redpin, HIGH);  
    digitalWrite(greenpin, HIGH);
    digitalWrite(bluepin, HIGH);
  irrecv.enableIRIn(); // Start the receiver
}
 
 
void loop()  
{
  if (irrecv.decode(&results))
  {
    switch(results.value)
    {
      case 0xFF30CF:  
        Serial.println("1");
        if(state1==true)
        {
          digitalWrite(redpin, LOW);  //red on 
          state1=false; //change the state
        }  
        else
        {
          digitalWrite(redpin, HIGH);  //red off
          state1=true; //change the state
        }      
        break;
        
      case 0xFF18E7:  
        Serial.println("2"); 
        if(state2==true)
        {
          digitalWrite(greenpin, LOW); //green on 
          state2=false;
        }
        else
        {
          digitalWrite(greenpin, HIGH); //green off 
          state2=true; //change the state          
        }
        break;
        
      case 0xFF7A85:  
        Serial.println("3"); 
        if(state3==true)
        {
          digitalWrite(bluepin, LOW);  //blue on
          state3=false;
        }
        else
        {
          digitalWrite(bluepin, HIGH);  //blue off
          state3=true;           
        }
        break;
        
        
      case 0xFF6897:  
        Serial.println("OFF"); 
        digitalWrite(redpin, HIGH);
        digitalWrite(greenpin, HIGH);
        digitalWrite(bluepin, HIGH); 
        state1 = true;
        state2 = true;
        state3 = true;
        break;
        
      default :
          Serial.println("Invalid Option");
        break;
    }
  delay(250);
  irrecv.resume();
  }
}

 

Links

KEYES Infrared receiver sensor module for arduino
5PCS/LOT RGB 3 Color SMD Full Color LED Module For Arduino

MSP-EXP430G2 RGB led example

In this example we connect an RGB led to our MSP-EXP430G2 launchpad and flash the red, green and blue colours

Schematic

I actually had an RGB breakout attached the the MSP-EXP430G2 launchpad but this is the same circuit

launchpad and rgb led

launchpad and rgb led

Code

Again this is written in Energia

void setup() 
{                
  // initialize the digital pin as an output.
  pinMode(P2_0, OUTPUT);
  pinMode(P2_1, OUTPUT); 
  pinMode(P2_2, OUTPUT); 
}
 
// the loop routine runs over and over again forever:
void loop() 
{
  //RED
  digitalWrite(P2_0, LOW);
  digitalWrite(P2_1, HIGH);
  digitalWrite(P2_2, HIGH);
  delay(1000);               // wait for a second
  //GREEN
  digitalWrite(P2_0, HIGH);
  digitalWrite(P2_1, LOW);
  digitalWrite(P2_2, HIGH);
  delay(1000);               // wait for a second
  //BLUE
  digitalWrite(P2_0, HIGH);
  digitalWrite(P2_1, HIGH);
  digitalWrite(P2_2, LOW);
  delay(1000);  
}

Links

Esplora RGB LED example

Another feature of the Arduino Esplora is a built in RGB LED, again its fairly simple to use this using the writeRGB function.

You can see the RGB led in the layout diagram below

esplora

esplora

Code

#include <Esplora.h>
 
void setup() 
{
}
 
void loop() 
{
  //RGB led example
  //Esplora.writeRGB(red, green, blue);
  Esplora.writeRGB(0, 0, 255);
  delay(1000);
  Esplora.writeRGB(0, 255, 0);
  delay(1000);
  Esplora.writeRGB(255, 0, 0);
  delay(1000);
}

 

Links

 

Esplora Joystick + Sensor + LED Module Kit Board for Arduino – Deep Blue – $62.13

ST Nucleo F334R8 RGB led example

This is  a first example with the ST Nucleo F334R8 board, this uses the mbed online compiler. here is a picture of the board

ST Nucleo F334R8

ST Nucleo F334R8

In this example we flash the red, green and blue colours of an RGB led . Our RGB LED was a common anode type, so a low output switches the LED on and a high output switches the LED off. You may have to reverse this.

Code

#include "mbed.h"
 
DigitalOut red(D5);
DigitalOut blue(D8);
DigitalOut green(D9);
 
int main() 
{
    while(1) 
    {
        red = 0; //red on
        wait(1.0); // 1 sec
        red = 1; //red off
        green = 0; //green on
        wait(1.0); // 1 sec
        green = 1; //green off
        blue = 0; //blue on
        wait(1.0); // 1 sec
        blue = 1; // blue off
    }
}

 

Links
Waveshare STM32 Board Nucleo NUCLEO-F334R8 STM32F334R8 STM32 Development Board Integrate ST-LINK/V2-1 Debugger/Programmer Support Arduino

ATMEGA 16 and RGB LED examples

A couple of simple examples, we used an RGB LED breakout. This was a common anode type, so to switch an LED on you need to switch the appropriate port pin low.

 

Schematic

atmega and rgb led

atmega and rgb led

Code

These examples were written in mikroC PRO for AVR

Example 1 : Cycle through Red, green and blue colours


void main() {
DDRB = 0xFF;           // Set direction to be output

do {
PORTB = 0xFF;        // Turn OFF diodes on PORTB
Delay_ms(200);      // 1 second delay
PORTB = 0xFE;
Delay_ms(200);      // 1 second delay
PORTB = 0xFD;
Delay_ms(200);      // 1 second delay
PORTB = 0xFB;
Delay_ms(100);      // 1 second delay
} while(1);            // Endless loop
}

Example 2 : Cycle through all colours


void main()
{

DDRB = 0xFF;
PORTB = 0xFF; // Set RB0 to high 00000001
do // To set infinite loop
{

PORTB = PORTB - 0x01; //
Delay_ms(250);
if(PORTB == 0xF8) //11111000 = R,G,B on
{
PORTB = 0xFF; //LEDS off
Delay_ms(500);
}
}while(1); // To set infinite loop
}

Links
ATMEGA16/ATmega32 AVR Minimum System Board + USB ISP USBasp Programmer

ATMEGA16 AVR microcontroller

ATMEGA16 Minimum System Board