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
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
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 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.
The same in MicroPython with micropython_ir's ir_tx, which makes the carrier with the ESP32's RMT peripheral or the Pico's PIO. It sends the placeholder code every two seconds; replace it with your remote's.
"""
IR Sender - send a NEC code, MicroPython 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
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 22 on an ESP32, GPIO 6 on an ESP32-S3,
GP17 on a Raspberry Pi Pico
Install once, from a computer:
mpremote mip install "github:peterhinch/micropython_ir/ir_tx"
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
"""
import time
from machine import Pin
from ir_tx.nec import NEC
# The GPIO number SIGNAL is wired to. ESP32: 22. ESP32-S3: 6. Pico: 17.
IR_TX_PIN = 6
# REPLACE THESE with what the key-code sketch printed for your remote.
ADDRESS = 0x04
COMMAND = 0x16
nec = NEC(Pin(IR_TX_PIN, Pin.OUT, value=0)) # LOW: LED off
while True:
print("sending")
nec.transmit(ADDRESS, COMMAND)
time.sleep(2)micropython_ir documents sending on the ESP32 and the Pico; the ESP32-S3 runs the same ESP32 port and should work, but is untested here. The ESP8266 cannot send at all. transmit() returns at once and the frame goes out in the background. Stop it with Ctrl-C.
When it does not work
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.
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.
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.
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.
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.
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
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.