To use the IRLib2 library for receiving data from an IR remote via a TSOP sensor and sending data from an IR remote, you can follow these steps:
Receiving Data from an IR Remote
- Install the IRLib2 Library:
- Download the IRLib2 library from its GitHub repository.
- Extract the ZIP file and move the IRLib2 folder to your Arduino libraries directory.
- Connect the TSOP Sensor:
- Connect the VCC pin of the TSOP sensor to the 5V pin on the Arduino.
- Connect the GND pin of the TSOP sensor to the GND pin on the Arduino.
- Connect the OUT pin of the TSOP sensor to a digital input pin on the Arduino (e.g., pin 2).
- Write the Code:
- Use the following code to receive and decode IR signals:
#include <IRLibAll.h> // Include the IRLib2 library
IRrecvPCI irrecv(2); // Set up the IR receiver on pin 2
IRdecode decoder; // Create a decoder object
void setup() {
Serial.begin(9600); // Start serial communication
irrecv.enableIRIn(); // Enable the IR receiver to start receiving signals
}
void loop() {
if (irrecv.getResults()) { // Check if we have a valid IR signal
decoder.decode(); // Decode the signal
long irCode = decoder.value; // Get the decoded IR code
Serial.print(“Received IR Code: “);
Serial.println(irCode, HEX); // Print the IR code in hexadecimal
irrecv.resume(); // Prepare for the next IR signal
}
}
Sending Data to an IR Remote
- Connect the IR LED:
- Connect the anode (longer leg) of the IR LED to a digital output pin on the Arduino (e.g., pin 3) through a current-limiting resistor (e.g., 220 ohms).
- Connect the cathode (shorter leg) of the IR LED to the GND pin on the Arduino.
- Write the Code:
- Use the following code to send IR signals:
This setup allows you to both receive and send IR signals using the IRLib2 library with a TSOP sensor and an IR LED. Adjust the pin numbers and IR codes as needed for your specific application. If you have any questions or need further assistance, feel free to ask!
#include <IRLibAll.h> // Include the IRLib2 library
IRsend irsend; // Create an IR send object
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
unsigned long irCode = 0xB748FD02; // Example IR code to send
irsend.send(NEC, irCode, 32); // Send the IR code using NEC protocol
Serial.print(“Sent IR Code: “);
Serial.println(irCode, HEX); // Print the sent IR code in hexadecimal
delay(2000); // Wait for 2 seconds before sending the next code
}
Summary:
In summary, using the IRLib2 library with a TSOP sensor and an IR LED allows you to both receive and send IR signals effectively. By following the steps to install the library, connect the components, and write the appropriate code, you can create a versatile IR communication system for your Arduino projects. This setup is useful for various applications, such as remote-control systems, automation, and more.