fbpx

Switching computer windows using Door Sensor

It all started with this reel that we saw on Instagram. Let me show you that

After watching that video, we became curious to know how this works and we also decided to make it once at out studio and this is the article for this project. So let’s see how to make it.

Watch out tutorial video if reading bores you

This is the complete tutorial video about the project. Now If you still wants to learn in written format, just continue reading.

Components Required

  • Arduino Pro Micro
  • Arduino Nano
  • HC05 Bluetooth Module x 2
  • Door Sensor
  • 2 Pin Terminal Connector
  • DC Socket
  • USB Connector
  • 5V 1A Power Adapter

Schematic

After getting all the components, you need to connect them all according to below mentioned diagram

Transmitter Part
Receiver Part

To make this project more compact and easy to handle, we designed our own custom PCB for both the parts of the project. Below we have shown the image of the PCBs we received from JLCPCB.

Configuring HC05 in Master & Slave mode

Before making this project, we first need to learn how to configure HC05 modules using AT commands and make them to act as Master and Slave.

Explaining the working part of the project

Basically, we want to measure if the door is open. And once it is opened we need to switch the window on our computer. For that first we will install the transmitter part near the door and door sensor is attached with the door itself.

Now as soon as the door is opened, the sensor will give signal to Arduino Nano board which further gives signal to the Receiver part via Bluetooth.

Now on the receiver part, which is already connected to the computer, as soon as it receives bluetooth signal it will pass that data to Arduino Pro Micro board. Now here Arduino Pro micro board is acting as a USB keyboard with a dedicated script written inside the code. So as soon as bluetooth signal is received, Arduino pro micro board will send the commands to the computer which will switch the windows on it.

Now here in our case we have written the script to change the window, but you can write any script to do any task on your computer. Now to learn how to write script in Arduino board to make it act like a USB keyboard, Kindly watch out this video.

Logic for this project in embedded inside the code. You can download the code from our GitHub repository.

Code used in this project

We have two separate code for separate part, Transmitter Code & Receiver Code. You can copy the code from below

Arduino Nano as Transmitter

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // SRX | STX
// D2 pin of NANO is SRX-pin of NANO; it will have connection with TX-pin of HC-05
// D3 pin of NANO is STX-pin of NANO; it will have connection with RX-pin of HC-05 via voltage divider.


#define DOOR_SENSOR 4
bool flag = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Arduino is ready");
  pinMode(DOOR_SENSOR, INPUT_PULLUP);

  // HC-05 default serial speed for communication mode is 9600
  BTserial.begin(38400);
  Serial.println("BTserial started at 38400");
}

void loop()
{
  // Keep reading from HC-05 and send to Arduino Serial Monitor

  if (digitalRead(DOOR_SENSOR) == HIGH)
  {
    Serial.print("Sending data - "); Serial.println('t');
    BTserial.write('t');
    delay(500);
    flag = 1;
    while (digitalRead(DOOR_SENSOR) == HIGH)
    {
      Serial.println("Door opened. Do nothing");
      delay(1000);
    }
    if (flag)
    {
      Serial.println("Door Closed");
      delay(1000);
      flag = 0;
    }
  }
}

Arduino Pro Micro as Receiver


#include <Keyboard.h>
int dataFromMaster = 0;


void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(38400);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    char inByte = Serial1.read();
    Serial.write(inByte);
    if(inByte == 't')
    {
      Serial.println("Received msg from transmitter");
      Serial.println("Running the Script");
      Change_Window();
      Serial.println("Window Changed Succefully");
    }
  }

}

void typeKey(int key)
{
  Keyboard.press(key);
  delay(50);
  Keyboard.release(key);
}

void Change_Window() {
  // Begining the stream
  Keyboard.begin();

// CMD + TAB
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press(KEY_TAB);
  Keyboard.releaseAll();

}