To control an LED and a Buzzer using an IR remote. When a specific button on the IR remote is pressed, the LED will light up, and the buzzer will sound.
Components Needed:
- Arduino Board (e.g., Arduino Uno)
- IR Receiver Module (e.g., 1838 IR receiver)
- LED (1)
- Resistor (220Ω for LED)
- Buzzer (1)
- Jumper wires
- Breadboard (optional, for easy connections)
- IR Remote (NEC protocol remote)
Wiring Instructions:
- IR Receiver Connections:
- VCC pin of the IR receiver → 5V on the Arduino
- GND pin of the IR receiver → GND on the Arduino
- OUT pin of the IR receiver → Pin 2 on the Arduino (this is the input pin to read the IR signal)
- LED Connections:
- Anode (long leg) of the LED → Pin 11 on the Arduino
- Cathode (short leg) of the LED → 220Ω resistor → GND on the Arduino
- The resistor is used to limit the current through the LED and prevent damage.
- Buzzer Connections:
- Positive leg (longer leg) of the Buzzer → Pin 6 on the Arduino
- Negative leg (shorter leg) of the Buzzer → GND on the Arduino
Arduino Code:
#include <IRremote.h> // Include IRremote library
#define DECODE_NEC // Define the protocol (NEC)
const byte IR_RECEIVE_PIN = 2; // IR receiver pin
// Declare LED and Buzzer pins
const int ledPin = 11; // Pin for LED
const int buzzerPin = 6; // Pin for Buzzer
void setup()
{
Serial.begin(9600); // Start serial communication
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN); // Initialize IR receiver
// Set LED and Buzzer pins as OUTPUT
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
if (IrReceiver.decode()) // Check if IR signal received
{
IrReceiver.resume(); // Prepare for next signal
}
if (IrReceiver.decodedIRData.command != 0) // If valid IR command received
{
checkIRcode(); // Process and print IR code
}
}
void checkIRcode()
{
Serial.print("Raw = ");
Serial.print(IrReceiver.decodedIRData.decodedRawData, HEX); // Print raw data in HEX
Serial.print(" Command = ");
Serial.println(IrReceiver.decodedIRData.command); // Print decoded command
// Check for button 1
if (IrReceiver.decodedIRData.decodedRawData == 0xBA45FF00) // Compare the raw data (use HEX format)
{
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Wait for 500ms
digitalWrite(ledPin, LOW); // Turn off the LED
}
// Check for button 2
if (IrReceiver.decodedIRData.decodedRawData == 0xB946FF00) // Compare the raw data (use HEX format)
{
digitalWrite(buzzerPin, HIGH); // Turn on the Buzzer
delay(500); // Wait for 500ms
digitalWrite(buzzerPin, LOW); // Turn off the Buzzer
}
IrReceiver.decodedIRData.command = 0; // Reset command after processing
}
How the Code Works:
- IR Receiver: The IR receiver reads signals from the IR remote, and each button on the remote sends a unique raw data signal. The Arduino decodes this signal and processes it.
- LED Control: When a specific button (e.g., Button 1 with raw data
0xBA45FF00
) is pressed, the Arduino turns on the LED for 500 milliseconds and then turns it off. - Buzzer Control: When another button (e.g., Button 2 with raw data
0xB946FF00
) is pressed, the Arduino turns on the buzzer for 500 milliseconds and then turns it off. - Serial Monitor: The
Serial.print
statements output the raw IR data and the command received, allowing you to identify which button is pressed.
Testing:
- Upload the Code: Upload the provided Arduino code to your Arduino board.
- Open the Serial Monitor: Go to the Arduino IDE and open the Serial Monitor. Set the baud rate to 9600.
- Press Buttons on the Remote: Press buttons on your IR remote and observe the printed raw data on the Serial Monitor.
- The raw data for the buttons will be displayed in the Serial Monitor in hexadecimal format.
- Based on the printed data, the Arduino will trigger actions (LED on or buzzer sound).
- Observe the Output:
- When the correct button is pressed (based on the raw data), the LED should light up or the buzzer should sound for 500 milliseconds.
Conclusion:
This project demonstrates how to use an IR remote to control electronic components like an LED and a buzzer. You can expand this project by adding more buttons to control different outputs or even by integrating sensors or motors for more complex actions.