Projects / TV sleep timer that sees when you leave

TV sleep timer that sees when you leave

Intermediate1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANOVS1838B

    Wire the IR receiver

    Open the Serial Monitor.

  2. NANO

    Learn the power button

    Note address and command.

  3. NANOBC54715.1 mA

    Wire the IR LED and transistor

    Pulses on D3.

  4. NANO

    Test sending

    Point at the TV with a one-minute timer.

  5. NANOmotion

    Add the PIR

    Reset the timer on motion.

  6. NANO

    Set the real timer

    20 minutes.

  7. NANOmotion

    Place the PIR

    Aim it at the seating.

In short

How do you build a TV sleep timer that sees when you leave?

A PIR notices the room is empty for 20 minutes and an IR LED sends the TV's power-off code. You need Arduino Nano, PIR Motion Sensor HC-SR501, IR receiver VS1838B, IR LED 940 nm (5 mm), BC547 NPN transistor, Resistor 47 Ω and Resistor 1 kΩ. It takes 7 steps and about 90 minutes.

Level
Intermediate
Time
About 90 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
PIR OUTNano D2Motion signal
PIR VCC and GNDNano 5V and GNDPower
IR receiver OUTNano D11Learns remote codes
IR receiver VCC and GNDNano 5V and GNDPower
Nano D31 k resistor to BC547 baseSend signal
BC547 emitterNano GNDReturn
BC547 collectorIR LED cathodeSwitches the LED
IR LED anode47 ohm resistor to Nano 5VAbout 70 mA pulses

Code

Most remotes send a short train of infrared pulses that the receiver can read, so you can learn the TV's power code by pointing the remote at the receiver and reading it over serial. Then the Nano waits: every movement seen by the PIR restarts a 20-minute timer, and when the room has been still for that long it sends the code twice, once only, until the next movement. The LED is driven through a transistor, because the Nano pin alone is too weak for a good range. This sketch assumes the NEC protocol, which many TVs use; if the monitor shows Samsung, Sony or another protocol, change the send call to match, and check the library examples. A PIR sees movement, not people, so someone sitting very still can be missed: place the PIR where it sees the sofa and test with the one-minute value first. The code toggles power on most TVs, so it could switch a TV on that you had already turned off; many remotes have a separate off code if you want to avoid that. The timer is only for convenience; it is not a safety device. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// TV sleep timer: learn the power code, then send it after the room has been still for a set time.
// Library: IRremote 4.x by Arduino-IRremote. Board: Arduino Nano.
// 1. Upload, open the Serial Monitor at 115200, press the TV power button at the receiver, copy Address and Command below.
#include <IRremote.hpp>
const byte PIR_PIN = 2, RECV_PIN = 11, SEND_PIN = 3;
const uint16_t TV_ADDRESS = 0x0000;          // replace with the learned address (NEC protocol assumed)
const uint8_t  TV_COMMAND = 0x00;            // replace with the learned command
const unsigned long STILL_MS = 20UL * 60UL * 1000UL;     // 20 minutes; use 60000 (1 minute) for testing
unsigned long lastMotion = 0;
bool sent = false;
void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
  IrReceiver.begin(RECV_PIN);
  IrSender.begin(SEND_PIN);
  lastMotion = millis();
  Serial.println("Press a remote button to learn its code.");
}
void loop() {
  if (IrReceiver.decode()) {
    Serial.print("Protocol "); Serial.print(getProtocolString(IrReceiver.decodedIRData.protocol));
    Serial.print(" Address 0x"); Serial.print(IrReceiver.decodedIRData.address, HEX);
    Serial.print(" Command 0x"); Serial.println(IrReceiver.decodedIRData.command, HEX);
    IrReceiver.resume();
  }
  if (digitalRead(PIR_PIN)) { lastMotion = millis(); sent = false; }
  if (!sent && millis() - lastMotion > STILL_MS) {
    for (int i = 0; i < 2; i++) { IrSender.sendNEC(TV_ADDRESS, TV_COMMAND, 0); delay(250); }
    sent = true;
    Serial.println("Sent power code");
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Remote buttonSerial shows the code
No motion for 1 minuteTV turns off
Wave at the PIRTimer restarts
Code sent onceNot again until movement

If it does not work

Code not recognised

Check the protocol and change the send call.

TV ignores it

Check the LED wiring and aim.

Triggers too soon

Use a longer time and move the PIR.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the TV sleep timer that sees when you leave?

Arduino Nano, PIR Motion Sensor HC-SR501, IR receiver VS1838B, IR LED 940 nm (5 mm), BC547 NPN transistor, Resistor 47 Ω and Resistor 1 kΩ.

How long does the TV sleep timer that sees when you leave take to build?

About 90 minutes for a intermediate maker, in 7 steps.

How do I know the TV sleep timer that sees when you leave works?

Remote button: Serial shows the code. No motion for 1 minute: TV turns off. Wave at the PIR: Timer restarts.

Code not recognised: what should I check?

Check the protocol and change the send call.

TV ignores it: what should I check?

Check the LED wiring and aim.

Triggers too soon: what should I check?

Use a longer time and move the PIR.