IR+ RGB LED

This tutorial instructs you

  • how to control RGB LED to emit any color using ESP32.

Video Tutorial

you can watch this video tutorial

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Remote control
1×IR receiver
1×RGB LED
3×220 Ω resistor
1×Breadboard
n×Jumper Wires

Introduction to RGB LED

The RGB LED can emit any colors by mixing the 3 basic colors red, green and blue. A single RGB LED is composed of 3 LEDs: red, green and blue. These three LEDs are packed into a single case so that it looks like a single LED.

RGB LED Pinout

RGB LED includes four pins:

  • R (red) pin: is to control the red color element
  • G (green) pin: is to control the green color element
  • B (blue) pin: is to control the blue color element
  • Common (Cathode-) pin: connect this pin to GND (0V)
ESP32 Button LED Wiring Diagram

※ NOTE THAT:

According to the common pin, there are two types of LED: common anode and common cathode. This tutorial uses a common cathode LED.

How RGB LED works

In term of physics, a color is a combination of three color elements: Red (R), Green (G) and Blue (B). Each color element's value range is from 0 to 255. The combination of values of three color elements create 256 x 256 x 256 colors in total.

If we generate PWM signals to R, G, B pins, the RGB LED diplays a color corresponding to the PWM duty cycle values. By changing the duty cycle of PWM signals (from 0 to 255), the RGB LED can display any color. The color values of Red (R), Grean (G) and Blue (B) correspond to PWM duty cycle on R, G and B pins , respectively.

Introducing the Infrared (IR) Receiver

The infrared receiver is the component shown in the figure below. This is the TSOP4838.

ESP32 Button LED Wiring Diagram

Infrared receiver Pinout

RGB LED includes four pins:

  • First pin: Vout
  • Second pin: GND
  • Third pin: Vcc

How Infrared receiver works

When you press your remote control, it sends infrared modulated signals. These signals contain information that your receiver collects.

ESP32 Button LED Wiring Diagram

Each button sends specific information. So, we can assign that information to a specific button.

Decode the IR signals

In this part of the project you need to decode the IR signals associated with each button.

To control the IR receiver, you need to install the IRremote Library in the Arduino IDE.

Installing the IRremote library

  • Click here to download the IRremote library. You should have a .zip folder in your Downloads
  • Unzip the .zip folder and you should get IRremote-master folder
  • Rename your folder from IRremote-master to IRremote
  • Move the IRremote folder to your Arduino IDE installation libraries folder
  • Finally, re-open your Arduino IDE

Copy the following code to your Arduino IDE, and upload it to your esp32 board. Make sure that you have the right board and COM port selected.


/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include 

int RECV_PIN = 15;

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(); // Receive the next value
  }
  delay(100);
}        

Open the serial monitor at a baud rate of 9600.

Press, for example, the button number 1 of your remote control. You should see a code on the serial monitor. Press the same button several times to make sure you have the right code for that button. If you see something like FFFFFFFF ignore it, it’s trash.

Do the same for the other buttons.

Write down the code associated with each button, because you’ll need that information later.

Wiring Diagram

Schematic Diagram

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

Breadboard Connections

ESP32 Button LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

click to see ESP32 pin out

ESP32 Code

Now, grab the codes you captured in the previous step. You need to convert your codes from hex to decimal.

For that, you can use this website

Here’s a conversion example for one of my codes:

ESP32 Button LED Wiring Diagram

Repeat that process to all your hex values and save the decimal values. These are the ones you need to replace in the code below.

Download or copy the following sketch to your Arduino IDE. Write your own decimal values in the sketch provided in the case lines and upload it to your esp32 board. Make sure that you have the right board and COM port selected.

Copy

#include <IRremote.h>

int RECV_PIN = 15;
IRrecv irrecv(RECV_PIN);
decode_results results;

#define PIN_RED    23 // GIOP23
#define PIN_GREEN  22 // GIOP22
#define PIN_BLUE   21 // GIOP21

const int freq = 5000;
const int redChannel = 0;
const int greenChannel = 1;
const int blueChannel = 2;

const int resolution = 8;

void setup() {

  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  
  ledcSetup(redChannel, freq, resolution);
  ledcSetup(greenChannel, freq, resolution);
  ledcSetup(blueChannel, freq, resolution);

  ledcAttachPin(PIN_RED, redChannel);
  ledcAttachPin(PIN_GREEN, greenChannel);
  ledcAttachPin(PIN_BLUE, blueChannel);
}

void loop() {

  //decodes the infrared input
  if (irrecv.decode(&results)){
    long int decCode = results.value;
    Serial.println(results.value);
    //switch case to use the selected remote control button
    switch (results.value){
      case 16724175: //when you press the 1 button
        setColor(0, 0, 250);
        break;   
      case 16718055: //when you press the 4 button
        setColor(0, 250, 0);   
        break;
       case 16743045: //when you press the 2 button
        setColor(250, 0, 0);
        break;           
       case 16716015: //when you press the 5 button
        setColor(100, 100, 200);
        break;       
       case 16726215: //when you press the 3 button
        setColor(100, 200, 100);
        break;       
       case 16734885: //when you press the 6 button
        setColor(200, 100, 100);
        break;
    }
    irrecv.resume(); // Receives the next value from the button you press
  }
  delay(10);

}

void setColor(int R, int G, int B) {
  ledcWrite(redChannel, R);
  ledcWrite(greenChannel, G);
  ledcWrite(blueChannel, B);
}             

Quick Steps

  • power up your board
  • Open Arduino IDE
  • Select the right board
  • Select the right port
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Arduino IDE Upload Code
  • Press and keep pressing the button several seconds
  • See the changes you made

Book Tutorial

We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book

References

※ NOTE THAT:

Some components works on 3.3v and others works on 5v!