Introduction
This experiment demonstrates how to control the color of an RGB LED using an IR remote control and an IR receiver with Arduino.
Components Needed
- RGB LED
- IR Receiver
- Arduino (e.g., Uno, Nano)
- IR Remote Control
- 220-ohm Resistors (3)
- Jumper wires
Circuit Setup
- Connect the red, green, and blue pins of the RGB LED to digital pins on the Arduino (e.g., 9, 10, 11).
- Connect the anode of the RGB LED to the 5V pin of the Arduino and use 220-ohm resistors for each color pin.
- Connect the IR receiver's signal pin to a digital pin on the Arduino (e.g., D2).
Make sure the IR receiver is properly positioned to receive signals from the remote.
Code for RGB LED with Remote Control
Upload the following code to your Arduino to control the RGB LED with the IR remote:
#include
const int recv_pin = 2;
IRrecv irrecv(recv_pin);
decode_results results;
int redPin = 9;
int greenPin = 10;
int bluePin = 11;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn();
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
long int decCode = results.value;
Serial.println(decCode);
if (decCode == 0xFF30CF) {
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
} else if (decCode == 0xFF18E7) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(bluePin, LOW);
} else if (decCode == 0xFF7A85) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, HIGH);
}
irrecv.resume();
}
}
Explanation
The code uses the IRremote library to read signals from the remote control. Each button press corresponds to a different color setting for the RGB LED.
Troubleshooting
- If the LED does not respond, check the wiring and ensure the IR receiver is connected to the correct pin.
- If the IR remote does not work, ensure the battery is fresh and the remote is in range of the receiver.