IR remote/The sender/09. Be the remote
The sender · 09 of 11

Be the remote

Three wires and one call, and the TK16 sends a code you captured with the receiver. The library does the only hard part, switching the LED 38,000 times a second inside every mark, and on an Uno nothing may interrupt it while it does.

Three wires

Three wires to the sender
Your board
Wires
3
VCC to
5V
SIGNAL to
D3
GND to GND, VCC to 5V, SIGNAL to D3. D3 is any digital pin: on an Uno the library makes the carrier in software, so no pin is special. VCC never reaches SIGNAL, so 5 V is safe beside a 3.3 V board: the pin only supplies about 4.3 mA to the transistor, and the 24 mA the LED takes comes from the 5 V rail. Let Serial finish printing before sending: an interrupt mid-frame puts a gap in the carrier.

GND to GND, VCC to 5V if your board has it, SIGNAL to a digital pin, NC to nothing. The send pin is the same in every sketch in this book: D3 on an Uno, GPIO 22 on an ESP32, GPIO 6 on an ESP32-S3, GP17 on a Pico. Any other output pin works too.

Put the address and command from your first key code into the sketch. The values in it are placeholders; there is nowhere else to get yours.

What the library does

What is actually inside a mark
21 cycles per bit
Your board
Cycles per bit
21
One cycle
26.3 µs
Duty cycle
30 %
A whole frame: about 68 milliseconds of marks and gaps. On an Uno every one of those toggles is the processor itself, in a timed loop. An interrupt mid-frame, such as Serial finishing a print, puts a gap in the tone, so flush Serial first. Either way the TK16 makes none of it: it is an LED and a switch.

IrSender.sendNEC(address, command, 0) looks like one line. Underneath is about a thousand cycles of carrier, because every mark is 560 µs of 38 kHz, 21 cycles, each on for 30 per cent of its period, the library's default.

On an Uno those cycles are made in software, in a timed loop, which is why any pin works and no timer is taken. It also means an interrupt mid-frame puts a gap in the tone, so the sketch calls Serial.flush() first. On an ESP32, ESP32-S3 or Pico IRremote hands the tone to a hardware PWM channel by default, and the gap problem goes away. On every board, sendNEC blocks for the whole frame, about 68 ms.

Test it with the other board

You have three of each. Run the key-code sketch on a second board with a TK15, point the TK16's lens at the TK15's window about 30 cm away, and every send should print the same address and command at the other end.

Closer than about 20 cm is the most confusing failure in the kit: the receiver is swamped, the marks it measures come out too long, and a link that works perfectly prints UNKNOWN. Backing away fixes it.

Repeats, and codes with no protocol

The third argument to sendNEC is a repeat count. 0 sends one data frame, one press. More sends that frame and then that many repeat frames 110 ms apart, as a held key would: right for volume, wrong for power.

If the device you want to control sent something the library called UNKNOWN, an air conditioner usually, IrSender.sendRaw() plays back the marks and spaces the receiver captured. The receiver at the far end only measures times, so it cannot tell the difference.

The code

Sends one NEC code every two seconds, and again whenever you type anything in the Serial Monitor. Put the address and command your key-code sketch printed into the two constants; the values here are placeholders and will not match your device.

ir_send_nec.ino
/*
  IR Sender - send a NEC code                            TK16 / /p/tk16

  Wiring. Count from the square pad on the TinkerBlock board, parts
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V for full range (VBUS on a Pico); 3V3 works, shorter
              (VCC only feeds the LEDs, so 5V is safe on any board)
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> D3 on an Uno, GPIO 22 on an ESP32, GPIO 6 on an
              ESP32-S3, GP17 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. Arduino Uno
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    Tools > Manage Libraries      IRremote by shirriff, z3t0 and
                                  ArminJo, version 4 or later
    Serial Monitor                115200
*/

#include <IRremote.hpp>

// The pin SIGNAL is wired to.
// Uno: 3. ESP32: 22. ESP32-S3: 6. Pico: 17.
const int IR_TX_PIN = 3;

// REPLACE THESE with what the key-code sketch printed for your
// remote. There is no table to look them up in.
const uint16_t ADDRESS = 0x04;
const uint8_t COMMAND = 0x16;
const uint8_t REPEATS = 0;     // 0 is one press; more is a held key

unsigned long lastSend = 0;

void sendOnce() {
  Serial.println("sending");
  // On an Uno the carrier is made in software: let Serial finish
  // first, or its interrupt puts a gap in the tone.
  Serial.flush();
  IrSender.sendNEC(ADDRESS, COMMAND, REPEATS);   // about 68 ms
}

void setup() {
  Serial.begin(115200);
  IrSender.begin(IR_TX_PIN);   // NEC is 38 kHz; nothing else to set
  Serial.println("Sending every 2 s. Type anything to send at once.");
}

void loop() {
  if (Serial.available()) {
    while (Serial.available()) Serial.read();
    lastSend = 0;              // send at once
  }
  if (lastSend == 0 || millis() - lastSend >= 2000) {
    sendOnce();
    lastSend = millis();
  }
}

If the TK15 decodes it but your television ignores it, check the protocol: sendNEC only sends NEC. A Samsung set wants sendSamsung, a Sony set sendSony with a bit count, and the key-code sketch already told you which one your original remote speaks.

When it does not work

The receiver hears nothing at all

Watch the TK16's red LED while the sketch runs. It is on the same transistor as the infrared LED, so it flickers each time a frame goes out. Dark means this board: pin number, wiring, or GND. Flickering means the frames are leaving, and the problem is aim, distance or the receiver.

The other end decodes UNKNOWN

Usually the two boards are too close. Within about 20 cm the receiver is swamped, the marks it measures come out long, and the decode fails. Move them 30 cm apart: the one failure fixed by making things harder.

My television ignores it but the TK15 decodes it

The frames are good and the protocol is wrong. sendNEC sends NEC; a Samsung set wants sendSamsung and a Sony set sendSony with a bit count. Run the key-code sketch against the original remote and use the protocol it names.

Sending stalls the rest of my sketch

sendNEC does not return until the frame is out, about 68 ms, and repeats add 110 ms each. Anything that must happen during that time will not. In MicroPython transmit() returns at once instead.

analogWrite stopped working on pins 3 and 11

That is receiving, not sending. If the same sketch also receives, IrReceiver.begin takes Timer2 on an Uno. Sending on its own claims no timer there.

Where this goes next

Two beams, one window, and the honest answer about range.

How far, and how wide

Edit this page — content/books/ir-receiver/be-the-remote.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum