Difficulty Level: Intermediate
In this experiment, we'll build a simple infrared (IR) remote control system using an IR receiver and an IR remote. The Arduino will decode the remote’s signals and perform actions based on the button presses, such as toggling an LED or controlling a servo motor.
This system works by receiving IR signals from the remote. Each button on the remote sends a unique hexadecimal code, which the Arduino can interpret and use to trigger specific actions.
We will use the IRremote
library to decode the signals from the remote. Install it from the Arduino Library Manager before uploading the code.
// Include the IRremote library
#include
const int RECV_PIN = 11; // Pin connected to the IR receiver
const int LED_PIN = 13; // LED to be controlled by the remote
IRrecv irrecv(RECV_PIN); // Create instance of 'irrecv' to receive signals
decode_results results; // Create structure to store received codes
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
irrecv.enableIRIn(); // Start the receiver
pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
}
void loop() {
if (irrecv.decode(&results)) { // Check if an IR signal is received
Serial.println(results.value, HEX); // Print the received value in hexadecimal
// Example remote codes (you need to change these based on your remote)
if (results.value == 0xFFA25D) { // Power button pressed
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle the LED
}
irrecv.resume(); // Prepare to receive the next value
}
}
When the Arduino detects a signal from the IR receiver, it decodes the signal using the IRremote
library and identifies which button on the remote was pressed by comparing the signal’s hex value to known values. Based on the button press, the Arduino can perform different tasks like turning an LED on/off or controlling a motor.
By using an IR remote and receiver, you can easily create a remote-controlled system for home automation or any other project where wireless control is needed. This experiment can be expanded by adding more devices (like motors, fans, etc.) and writing code to control them via remote commands.