Introduction
In this experiment, you will use an infrared (IR) remote control and receiver to interact with an Arduino. The Arduino will decode the signals sent by the remote and perform specific actions, such as turning an LED on or off.
Components Needed
- Arduino (e.g., Uno, Nano)
- IR remote control
- IR receiver module (e.g., VS1838B)
- LED
- 330Ω resistor
- Breadboard and jumper wires
- USB cable for programming
Circuit Setup
- Connect the IR receiver module:
- GND pin to Arduino GND
- VCC pin to Arduino 5V
- OUT pin to Arduino digital pin 2
- Connect the LED:
- Long leg (+) to Arduino digital pin 9 through a 330Ω resistor
- Short leg (-) to GND
Code for Infrared Remote Control
Upload the following code to your Arduino to decode IR signals and control the LED:
// Include the IRremote library
#include
const int RECV_PIN = 2; // Pin connected to IR receiver
const int LED_PIN = 9; // Pin connected to LED
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup() {
Serial.begin(9600);
irrecv.enableIRIn(); // Start the IR receiver
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.println("Ready to receive IR signals");
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX); // Print the decoded value in hexadecimal format
// Check the received value and control the LED
switch (results.value) {
case 0xFFA25D: // Replace with your remote's ON button code
digitalWrite(LED_PIN, HIGH); // Turn LED on
Serial.println("LED ON");
break;
case 0xFF629D: // Replace with your remote's OFF button code
digitalWrite(LED_PIN, LOW); // Turn LED off
Serial.println("LED OFF");
break;
default:
Serial.println("Unknown signal");
}
irrecv.resume(); // Receive the next signal
}
}
Explanation
The code uses the IRremote
library to decode the signals sent by the IR remote. Here's how it works:
irrecv.enableIRIn()
: Activates the IR receiver to begin decoding signals.results.value
: Stores the hexadecimal code of the received signal.switch
statement: Compares the received signal to predefined codes and performs actions like turning the LED on or off.- Use the Serial Monitor to find the button codes for your specific remote control.
Troubleshooting
- If the IR receiver is not working, check the wiring and ensure it is powered correctly.
- If the LED does not respond, verify the remote's button codes using the Serial Monitor.
- Install the IRremote library via Tools > Manage Libraries in the Arduino IDE if you encounter library errors.