IR Sender

In the previous project, we learned how to capture signals from a remote controller. In this project, we’ll build our own remote controller to replicate the functionality of an existing one.

First, choose a button from an existing remote that you’d like to duplicate, and check the Protocol, Address, and Command information from the serial monitor in the previous project.

Now, let’s write the code to replicate it!

#include <Arduino.h>
#include <IRremote.hpp>

#define SEND_PWM_BY_TIMER //Using a hardware timer to generate the PWM signal
#define IR_SEND_PIN 22
#define BTN_PIN 21

void setup() {
    Serial.begin(115200);
    pinMode(BTN_PIN, INPUT);

    IrSender.begin(IR_SEND_PIN);
}

void loop() {
if (digitalRead(BTN_PIN) == HIGH) {
    uint8_t sAddress = 0xE;
    uint8_t sCommand = 0x14;
    uint8_t sRepeats = 0;

    Serial.printf("Protocl=Samsung, address=0x%X, command=0x%X, repeats=%d\n", sAddress, sCommand, sRepeats); 

    IrSender.sendSamsung(sAddress, sCommand, sRepeats);

    delay(300); //button debounce
}

    delay(10);
}
C++
#define IR_SEND_PIN 22
#define BTN_PIN 21
C++

The right leg of the IR908-7C is connected to GPIO 22 on the ESP32. The left leg is connected to a 220Ω resistor, which is then connected to Ground.

The button is connected to GPIO 21 on the ESP32, with the other end connected to 3.3V. This means that when the button is pressed, GPIO 21 will be in a HIGH state. To ensure that GPIO 21 defaults to a LOW state when the button is not pressed, we use a pull-down resistor of 10kΩ connected to Ground.

pinMode(BTN_PIN, INPUT);
C++

Sets the button pin (pin 21) as an input.

IrSender.begin(IR_SEND_PIN);
C++

The IR sender is initialized on pin 22.

if (digitalRead(BTN_PIN) == HIGH) {
    ... ...
}
C++

The code constantly checks if the button connected to pin 21 is pressed (digitalRead(BTN_PIN) == HIGH).

uint8_t sAddress = 0xE;
uint8_t sCommand = 0x14;
uint8_t sRepeats = 0;

Serial.printf("Protocl=Samsung, address=0x%X, command=0x%X, repeats=%d\n", sAddress, sCommand, sRepeats); 
C++

If the button is pressed, it sets up the address (sAddress = 0xE), command (sCommand = 0x14), and repeat count (sRepeats = 0) for the IR signal. It then prints the protocol, address, and command information to the serial monitor for debugging.

The Serial.printf() function in Arduino is used to print formatted text to the Serial Monitor, similar to how printf() works in C/C++ programming. It allows you to print variables along with text in a formatted manner, which is particularly useful for debugging or logging.

Serial.printf(format, arg1, arg2, ...);
C++
  • format: A string that contains text and format specifiers. The format string can include placeholders where variables will be inserted.
  • arg1, arg2, …: The variables or values that will replace the format specifiers in the format string.

Common Format Specifiers

  • %d or %i: Prints an integer (decimal form).
  • %x: Prints an integer in hexadecimal format.
  • %f: Prints a floating-point number (e.g., float or double).
  • %s: Prints a string.
  • %c: Prints a character.
  • %u: Prints an unsigned integer.

Examples of Usage

1. Printing an Integer

int value = 123;
Serial.printf("The value is: %d\n", value);
C++

Output:

The value is: 123
C++

2. Printing in Hexadecimal

int hexValue = 255;
Serial.printf("The value in hex is: %x\n", hexValue);
C++

Output:

The value in hex is: ff
C++

3. Printing Floating-Point Numbers

float temperature = 25.75;
Serial.printf("The temperature is: %.2f°C\n", temperature);
C++

Output:

The temperature is: 25.75°C
C++

The %.2f format specifier limits the output to 2 decimal places.

4. Printing Multiple Variables

int temperature = 25;
float humidity = 60.5;
Serial.printf("Temp: %d°C, Humidity: %.1f%%\n", temperature, humidity);
C++

Output:

Temp: 25°C, Humidity: 60.5%
C++

5. Printing a String

String message = "Hello, World!";
Serial.printf("Message: %s\n", message.c_str());
C++

Output:

Message: Hello, World!
C++

Note: When printing a String object, you need to use .c_str() to convert it to a C-style string.

IrSender.sendSamsung(sAddress, sCommand, sRepeats);
C++

The IrSender.sendSamsung() function sends the IR signal with the defined address, command, and repeats using the Samsung protocol.

delay(300); //button debounce
C++

Don’t forget to add button debounce delay before the if end.

RELATED ARTICLES