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!




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.
Sets the button pin (pin 21) as an input.
The IR sender is initialized on pin 22.
The code constantly checks if the button connected to pin 21 is pressed (digitalRead(BTN_PIN) == HIGH).
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.
- 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
Output:
2. Printing in Hexadecimal
Output:
3. Printing Floating-Point Numbers
Output:
The %.2f format specifier limits the output to 2 decimal places.
4. Printing Multiple Variables
Output:
5. Printing a String
Output:
Note: When printing a String object, you need to use .c_str() to convert it to a C-style string.
The IrSender.sendSamsung() function sends the IR signal with the defined address, command, and repeats using the Samsung protocol.
Don’t forget to add button debounce delay before the if end.