# Project - IR Receiver
the **VS1838B** is a common and widely-used infrared (IR) receiver module. It’s designed to receive and decode infrared signals from remote controls, making it ideal for projects involving IR communication.
**Key Features of the VS1838B:**
- **Operating Voltage:** Typically 3.0V to 5.5V, making it compatible with many microcontrollers like Arduino, Lonely Binary UNO R3, and Raspberry Pi.
- **Receiving Distance:** Around 10–15 meters, depending on the strength of the IR transmitter.
- **Frequency:** Optimized for a carrier frequency of 38 kHz, which is standard for most remote controls.
- **Wide Viewing Angle:** Can receive IR signals from a range of angles.
**Pinout:**
- **OUT (Signal):** Outputs the demodulated signal when it detects a valid IR signal.
- **GND:** Ground connection.
- **VCC:** Power supply (3.0V–5.5V).

The Library we are using is **IRremote by shirriff, z3t0, ArminJo**.

Connecting the **VS1838B** IR receiver to the Lonely Binary UNO R3 is straightforward. Simply wire the Signal Out to GPIO 3 on the Lonely Binary UNO R3, VCC to 5V, and GND to ground. You can also add an LED to indicate when the sensor detects a signal. Connect the short leg (negative) of the LED to the Signal Out pin of the VS1838B. The long leg (positive) of the LED should be connected to a 220Ω resistor, and the other end of the resistor goes to ground.
This code uses the **Arduino-IRremote** library to receive and process IR signals.
```cpp
#include
#include
#define SEND_PWM_BY_TIMER //Using a hardware timer to generate the PWM signal
#define IR_RECEIVE_PIN 3 // VS1838B Signal Pin
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_RECEIVE_PIN);
}
void loop() {
if (IrReceiver.decode()) {
IrReceiver.printIRResultShort(&Serial);
switch (IrReceiver.decodedIRData.command) {
case 0x14:
Serial.println("Volume UP");
break;
case 0x15:
Serial.println("Volume Down");
break;
case 0xC:
Serial.println("Power Off");
break;
}
IrReceiver.resume();
}
delay(10);
}
```
```cpp
#define SEND_PWM_BY_TIMER
```
Configures the library to use a **hardware timer** for generating PWM signals (useful when sending IR signals, though this example is only about receiving).
This code is a bit unusual because it doesn’t have any assigned value. It defines a **preprocessor flag**, which can either be **defined** or **undefined**. The presence or absence of this definition controls which parts of your code are included or excluded during compilation.
Try running the below code. It creates a preprocessor macro named ENABLE_FEATURE without a value. The mere presence of this macro acts as a “switch” to enable or disable specific blocks of code.
```cpp
// Define a feature flag
#define ENABLE_FEATURE
void setup() {
Serial.begin(9600);
// Check if the feature is enabled
#ifdef ENABLE_FEATURE
Serial.println("Feature is enabled!");
#else
Serial.println("Feature is disabled!");
#endif
}
void loop() {
// Do nothing
}
```
Sets GPIO pin 3 as the input pin for receiving IR signals from the IR receiver module (e.g., **VS1838B**).
```cpp
IrReceiver.begin(IR_RECEIVE_PIN);
```
Initializes the IR receiver module to start receiving IR signals on the defined pin (23 in this case).
```cpp
IrReceiver.decode()
```
Checks if the IR receiver has received a complete and valid IR signal frame. Returns true if a signal was received; otherwise, it returns false.
```cpp
IrReceiver.printIRResultShort(&Serial)
```
Prints a brief summary of the received IR signal to the Serial Monitor (e.g., protocol type, address, and command).
The &Serial is a **reference to the** Serial **object**, which represents the Lonely Binary UNO R3’s hardware **serial communication interface**. It tells the printIRResultShort() function where to send its output. On the Lonely Binary UNO R3, there are multiple hardware serial ports, and by passing the desired Serial object (e.g., Serial, Serial1, or Serial2), you can select which serial port to use for output.
```cpp
switch (IrReceiver.decodedIRData.command) {
case 0x14:
Serial.println("Volume UP");
break;
case 0x15:
Serial.println("Volume Down");
break;
case 0xC:
Serial.println("Power Off");
break;
}
```
This line checks the value of IrReceiver.decodedIRData.command, which represents the command received by the IR sensor from the remote. If the received command is 0x14, it prints “Volume UP” to the Serial Monitor, indicating that the user pressed the “Volume UP” button on the remote.
You can find the corresponding command address for each button on your remote control in the Serial Monitor.
```cpp
IrReceiver.resume()
```
Resets the receiver to be ready for the **next IR signal**.
Once a signal is received, the IR receiver stops processing further signals until resume() is called. Without calling resume(), the library will not detect or decode any new signals, effectively “freezing” IR reception.
```cpp
delay(10);
```
The purpose of delay(10); in the loop() function is to introduce a short **pause** of 10 milliseconds between iterations of the loop. Without a delay, the loop() function would execute as fast as possible, continuously checking for new IR signals and processing them. Adding a small delay helps to regulate the execution frequency of the loop() and ensures the microcontroller isn’t running at full capacity unnecessarily.

Project - IR Receiver
RELATED ARTICLES
Photoresistor Sensor
Project - Temperature Measurement
NTC Temperature Sensor
Project - IR Sender
Remote infrared Sensor
Project - Motion Detection Alarm
PIR Motion Sensor
- Choosing a selection results in a full page refresh.