Projects / Window sensor that stops the AC

Window sensor that stops the AC

Beginner2 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. ESP32

    Wire the reed switch

    Magnet on the sash, switch on the frame.

  2. ESP32VS1838B

    Wire the IR receiver

    OUT to GPIO14.

  3. ESP32BC54713.3 mA

    Wire the IR LED driver

    Through the BC547.

  4. ESP32

    Install the library and upload

    IRremoteESP8266.

  5. ESP32

    Learn the OFF and ON codes

    Hold the button and use your remote.

  6. ESP32

    Test with the window open

    Wait one minute.

  7. ESP3213.4 mA

    Aim the LED at the AC

    From about a metre.

  8. ESP32

    Fit it on the window

    Check it closes the loop when closed.

In short

How do you build a window sensor that stops the AC?

A reed switch on a window sends your AC's own IR off code after a minute of open window, and the on code when it closes again. You need ESP32 DevKit V1, Door reed switch MC-38, IR LED 940 nm (5 mm), IR receiver VS1838B, BC547 NPN transistor, Resistor 1 kΩ, Resistor 220 Ω, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 8 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
10

Parts list

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

Wiring

FromToWhy
Reed switch MC38ESP32 GPIO27 and GND, magnet on the windowClosed window gives LOW
IR receiver VS1838B OUTESP32 GPIO14Learning input
IR receiver VCC and GNDESP32 3V3 and GNDPower
GPIO41 k to BC547 baseIR LED driver
BC547 emitterGNDSwitch ground
IR LED cathodeBC547 collectorSwitched side
IR LED anode220 Ω to the ESP32 5V (VIN) pinAbout 15 mA, enough for a metre or two
Learn buttonESP32 GPIO26 and GNDInternal pull-up

Code

Most AC remotes send a long IR message that contains the whole state (mode, temperature and power) in one go. This project records that message from your own remote and replays it, so nothing is wired into the AC. Hold the learn button, point the remote at the VS1838B and press OFF, then press ON when the sketch asks. Set the temperature and mode on the remote you want restored before you capture the ON message, because replaying it will bring back exactly that state. The reed switch has a magnet on the window sash: closed gives a low signal, open gives high. If the window stays open for one minute the OFF message is sent, and the ON message when it closes again. If you or someone switched the AC off by hand in the meantime it will come on when you close the window, so use this only where that is fine. The IR LED is driven by a transistor because the ESP32 pin cannot supply enough current; the 220 ohm resistor limits it to about 15 mA, well inside the 940 nm LED rating (a lower value such as 100 ohms gives more range but check your LED datasheet first). Point the LED at the AC's receiver from a metre or two. Some AC models use messages the raw replay cannot match, usually when the remote sends different frames for each button, and a few brands need a library for their own protocol instead. Setup: Arduino IDE with the ESP32 boards package 2.0.x, board "ESP32 Dev Module". Install IRremoteESP8266 from the Library Manager.

Runs on a virtual ESP32 Dev Module 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).

#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
#include <Preferences.h>

const uint16_t RECV_PIN = 14, SEND_PIN = 4;
const uint8_t REED_PIN = 27, LEARN_PIN = 26;
const uint32_t OPEN_DELAY_MS = 60000UL;       // wait a minute before cutting the AC

IRrecv irrecv(RECV_PIN, 1024, 50, true);      // big buffer and 50 ms timeout for long AC frames
IRsend irsend(SEND_PIN);
decode_results results;
Preferences prefs;

uint16_t *offCode = nullptr, *onCode = nullptr;
uint16_t offLen = 0, onLen = 0;
bool acPaused = false;
uint32_t openSince = 0;

bool learn(const char *key, uint16_t **dst, uint16_t *len) {
  Serial.print("Point the remote at the receiver and press: "); Serial.println(key);
  uint32_t t = millis();
  while (millis() - t < 15000) {
    if (irrecv.decode(&results)) {
      uint16_t *raw = resultToRawArray(&results);
      uint16_t n = getCorrectedRawLength(&results);
      prefs.putBytes(key, raw, n * sizeof(uint16_t));
      free(*dst);
      *dst = (uint16_t *)malloc(n * sizeof(uint16_t));
      memcpy(*dst, raw, n * sizeof(uint16_t));
      *len = n;
      delete[] raw;
      irrecv.resume();
      Serial.print("Saved "); Serial.print(n); Serial.println(" marks");
      return true;
    }
    delay(10);
  }
  Serial.println("Timed out");
  return false;
}

void load(const char *key, uint16_t **dst, uint16_t *len) {
  size_t bytes = prefs.getBytesLength(key);
  if (!bytes) return;
  *dst = (uint16_t *)malloc(bytes);
  prefs.getBytes(key, *dst, bytes);
  *len = bytes / sizeof(uint16_t);
}

void setup() {
  Serial.begin(115200);
  pinMode(REED_PIN, INPUT_PULLUP);
  pinMode(LEARN_PIN, INPUT_PULLUP);
  prefs.begin("ac", false);
  load("off", &offCode, &offLen);
  load("on", &onCode, &onLen);
  irsend.begin();
  irrecv.enableIRIn();
}

void loop() {
  if (!digitalRead(LEARN_PIN)) {                 // hold the button: learn OFF, then ON
    delay(300);
    learn("off", &offCode, &offLen);
    delay(2000);
    learn("on", &onCode, &onLen);
    return;
  }
  bool windowOpen = digitalRead(REED_PIN);       // magnet away means open
  if (windowOpen) {
    if (!openSince) openSince = millis();
    if (!acPaused && offLen && millis() - openSince > OPEN_DELAY_MS) {
      irsend.sendRaw(offCode, offLen, 38);       // 38 kHz carrier
      acPaused = true;
      Serial.println("AC off sent");
    }
  } else {
    openSince = 0;
    if (acPaused && onLen) {
      irsend.sendRaw(onCode, onLen, 38);
      acPaused = false;
      Serial.println("AC on sent");
    }
  }
  delay(200);
}

Test checklist

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

CheckExpected
Open the window for 30 secondsNothing is sent
Keep it open past a minuteAC turns off
Close the windowAC turns on again
Press the learn buttonSerial prompts to point the remote
Reboot the ESP32Codes still stored

If it does not work

Nothing captured

Receiver wired to the wrong pin, or the remote is out of line of sight.

AC does not react

LED not pointed at the AC, or the protocol is not replayed by raw timing.

LED seems dim

Check the transistor orientation and use a phone camera to see IR flashing.

Sent at the wrong moment

Reed switch on the wrong side of the magnet; swap the logic.

ESP32 resets when sending

Power it from a good USB supply.

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

Ask Lark AI

Common questions

What parts do I need for the window sensor that stops the AC?

ESP32 DevKit V1, Door reed switch MC-38, IR LED 940 nm (5 mm), IR receiver VS1838B, BC547 NPN transistor, Resistor 1 kΩ, Resistor 220 Ω, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the window sensor that stops the AC take to build?

About 120 minutes for a beginner maker, in 8 steps.

How do I know the window sensor that stops the AC works?

Open the window for 30 seconds: Nothing is sent. Keep it open past a minute: AC turns off. Close the window: AC turns on again.

Nothing captured: what should I check?

Receiver wired to the wrong pin, or the remote is out of line of sight.

AC does not react: what should I check?

LED not pointed at the AC, or the protocol is not replayed by raw timing.

LED seems dim: what should I check?

Check the transistor orientation and use a phone camera to see IR flashing.

Sent at the wrong moment: what should I check?

Reed switch on the wrong side of the magnet; swap the logic.