TV-B-Gone: one button, the power code of many TVs
A TV-B-Gone is a key-chain remote with one button. When you push it, it sends the power code of one TV brand, then the next brand, then the next. Each TV ignores all the codes except its own. When its code arrives, the TV switches off. The original by Mitch Altman has more than 200 codes and needs about a minute to send them all.
This page builds one with an ESP32, an IR LED driver and the IRremote library. The idea is simple, but it teaches important facts: brands use different protocols (NEC, Samsung, Sony, RC5, Panasonic), the protocols use different carrier frequencies (36–40 kHz), and most power keys are a toggle. To receive and decode codes, start with IR remote (NEC).

Photo: Joe Hall, CC BY 2.0
How it works
A list of power codes
The program has a table: one row for each brand, with its protocol, address and command. A loop sends the rows one after the other, with a short pause between them, so that each TV sees a separate, complete frame. More rows switch off more TVs, but the run takes longer.
Different protocols and carriers
NEC and Samsung blink at 38 kHz, Sony at 40 kHz, RC5 (Philips) at 36 kHz and Panasonic at about
37 kHz. A receiver works best at its own frequency. IRremote sets the correct carrier for each send…() function.
Power is a toggle
Most power keys mean “change on/off”, not “off”. A TV that is already off turns on. This is why a TV-B-Gone sends each code only once. Try it in the simulator: switch one TV off, then push the button.
One code for many TVs
Many brands buy their electronics from the same factories, so they use the same code. For example, many LG and Vizio TVs use NEC address 0x04, command 0x08. A short list switches off a large part of the TVs in a region.
Range is everything
A remote must work across a room. A TV-B-Gone must work across a large room or a street. The kit drives four LEDs with high current pulses. The circuit below drives two LEDs at about 100 mA, which gives several meters.
Use it fairly
Use your TV-B-Gone on your own TVs, or ask first. Never use it where people depend on a screen (an airport display, a presentation, a medical room) or to disturb other people.
The codes in the sketch
| Brand | Protocol | Carrier | Address | Command | IRremote call |
|---|---|---|---|---|---|
| LG, Vizio | NEC | 38 kHz | 0x04 | 0x08 | sendNEC(0x04, 0x08, 0) |
| Toshiba | NEC | 38 kHz | 0x40 | 0x12 | sendNEC(0x40, 0x12, 0) |
| Samsung | Samsung | 38 kHz | 0x07 | 0x02 | sendSamsung(0x07, 0x02, 0) |
| Sony | Sony SIRC 12-bit | 40 kHz | 0x01 | 0x15 | sendSony(0x01, 0x15, 2, SIRCS_12_PROTOCOL) |
| Philips | RC5 | 36 kHz | 0x00 | 0x0C | sendRC5(0x00, 0x0C, 0) |
| Panasonic | Panasonic (Kaseikyo) | 37 kHz | 0x08 | 0x3D | sendPanasonic(0x08, 0x3D, 0) |
Models and years differ, so a code can fail on a TV of the same brand. Sony needs the frame three times (2 repeats), or the TV ignores it. To add a TV, read the code of its remote with the second sketch below and add one row.
Wiring
Connections
| Part | Connect to | Notes |
|---|---|---|
| Transistor base | GPIO 25 through 1 kΩ | IRremote makes the carrier on this pin. |
| IR LEDs | 5 V → 22 Ω → LED → LED → collector | Long leg (anode) toward 5 V on each LED. |
| Transistor emitter | GND | |
| Push button | GPIO 13 and GND | Internal pull-up. GPIO 13 can also wake the ESP32 from deep sleep. |
| Status LED | GPIO 4 → 220 Ω → LED → GND | On while a code is sent. |
LED current
(5 V − 2 × 1.35 V − 0.2 V) ÷ 22 Ω ≈ 95 mA during each flash. The flashes are only about ⅓ of each carrier period and the codes are short, so the average current is small. A TSAL6200 is rated 100 mA continuous. For more range, use more LED strings, each with its own resistor, and a logic-level MOSFET in place of the PN2222.
Code
Install the “IRremote” library 4.x by Armin Joachimsmeyer (Library Manager). The sketch sends every code once, then the ESP32 goes into deep sleep. Push the button to wake it: it starts again from the beginning and sends all the codes.
1 · TV-B-Gone
tv_b_gone.ino
#define NO_LED_SEND_FEEDBACK_CODE // before the include
#include <IRremote.hpp>
#include "driver/rtc_io.h"
const int IR_SEND_GPIO = 25; // to the transistor base through 1 kΩ
const int STATUS_LED = 4;
const gpio_num_t BUTTON = GPIO_NUM_13; // to GND; wakes the ESP32
enum Protocol : uint8_t { P_NEC, P_SAMSUNG, P_SONY12, P_RC5, P_PANASONIC };
struct PowerCode {
const char *brand;
Protocol protocol;
uint16_t address;
uint8_t command;
};
// One row for each brand. Add the codes of your own TVs here.
const PowerCode CODES[] = {
{ "LG / Vizio", P_NEC, 0x04, 0x08 },
{ "Toshiba", P_NEC, 0x40, 0x12 },
{ "Samsung", P_SAMSUNG, 0x07, 0x02 },
{ "Sony", P_SONY12, 0x01, 0x15 },
{ "Philips", P_RC5, 0x00, 0x0C },
{ "Panasonic", P_PANASONIC, 0x08, 0x3D },
};
const int NUM_CODES = sizeof(CODES) / sizeof(CODES[0]);
void sendCode(const PowerCode &c) {
switch (c.protocol) {
// Each send function sets its own carrier: 38, 40, 36 or 37 kHz
case P_NEC: IrSender.sendNEC(c.address, c.command, 0); break;
case P_SAMSUNG: IrSender.sendSamsung(c.address, c.command, 0); break;
case P_SONY12: // Sony: 3 frames (2 repeats)
IrSender.sendSony(c.address, c.command, 2, SIRCS_12_PROTOCOL); break;
case P_RC5: IrSender.sendRC5(c.address, c.command, 0); break;
case P_PANASONIC: IrSender.sendPanasonic(c.address, c.command, 0); break;
}
}
void setup() {
Serial.begin(115200);
pinMode(STATUS_LED, OUTPUT);
IrSender.begin(IR_SEND_GPIO);
// Send every code once. Power is a toggle:
// a second time would switch the TV on again.
for (int i = 0; i < NUM_CODES; i++) {
Serial.printf("%d/%d %s\n", i + 1, NUM_CODES, CODES[i].brand);
digitalWrite(STATUS_LED, HIGH);
sendCode(CODES[i]);
digitalWrite(STATUS_LED, LOW);
delay(150); // pause between codes
}
// Sleep until the button pulls GPIO 13 LOW.
// Waking up restarts the sketch.
Serial.println("Done. Deep sleep.");
Serial.flush();
rtc_gpio_pullup_en(BUTTON); // keep the pull-up on during sleep
rtc_gpio_pulldown_dis(BUTTON);
esp_sleep_enable_ext0_wakeup(BUTTON, 0);
esp_deep_sleep_start();
}
void loop() {} // never runs: the ESP32 sleeps
- The sketch also runs once when you connect the power. If you hold the button down, it runs again and again.
- In deep sleep, the ESP32 chip uses only microamps. A DevKit board still uses some milliamps for its regulator, USB chip and power LED. For a key-chain version, use a bare module or a board made for low power.
2 · Learn the power code of a TV
learn_power_code.ino
#include <IRremote.hpp>
const int IR_RECEIVE_PIN = 27; // receiver wiring from the IR remote page
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
Serial.println("Point the TV remote at the receiver and press POWER.");
}
void loop() {
if (!IrReceiver.decode()) return;
if (!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)) {
// protocol, address and command
IrReceiver.printIRResultShort(&Serial);
// the IrSender.send...() line to use
IrReceiver.printIRSendUsage(&Serial);
Serial.println();
}
IrReceiver.resume();
}
Example output (the format can differ a little between library versions):
Protocol=NEC Address=0x4 Command=0x8 Raw-Data=0xF708FB04 32 bits LSB first
Send with: IrSender.sendNEC(0x4, 0x8, <numberOfRepeats>);
Copy the protocol, address and command into a new row of CODES. If the protocol is not in the
switch, add a case with the send line that the sketch printed. If the protocol is UNKNOWN, the remote uses a
protocol that IRremote does not decode.
Troubleshooting
No TV reacts
Look at the IR LEDs through a phone camera while the codes are sent: you see purple flashes. If not, examine the transistor and the LED direction. Then test the sketch with a second ESP32 and a receiver: the learn sketch must print the codes.
My TV is not switched off
Its model uses a different code. Read the code of its own remote with the learn sketch and add it to the table. Some TVs (and many projectors) need the key held: send more repeats.
It works only near the TV
More current gives more range. Use two LED strings, a MOSFET and a 5 V supply that can give the current. Point the LEDs at the TV. A narrow-beam LED goes farther.
The TV switched on
It was already off, and the power code is a toggle. Send each code only once in each run.
The ESP32 does not wake up
Only RTC GPIOs can wake the ESP32 with ext0: for example 0, 2, 4, 12–15, 25–27 and 32–39. GPIO 13 is one of them. The button must connect the pin to GND.
Compile errors
Use IRremote 4.x and the Arduino-ESP32 core 3.x. Old TV-B-Gone ports for the ESP8266 or for IRremote 2.x use a different API.
Resources
- TV-B-GoneThe original product by Mitch Altman.
- Adafruit: TV-B-Gone kitThe open-source kit in the photo: schematic, firmware and the full list of codes (North America and Europe).
- Arduino-IRremote (GitHub)The send functions, the protocols and the carrier frequencies.
- SB-Projects: IR remote control protocolsNEC, Sony SIRC, RC5 and others, with timing diagrams.
- IR remote (NEC) · IR break-beam · MOSFETs · ButtonsRelated pages.