Introduction: The Bedroom Automation Box

Automate any appliance of your bedroom with this smart arduino 'Bedroom Automation Box'. From fans to mobile chargers, from room coolers to night lamps, this versatile box can control almost anything automatically! The box has a set of six sensors which can be used in combination with each other to control various home appliances especially in your bedroom like temperature sensor for controlling fans and coolers, light sensor to control desktop lamps and ceiling lights and clap sensor to control anything! You just need to connect your appliance to the power socket present on the box with a plug that makes connecting anything easily. The device asks you to set a mode which means, which sensor you want to use for controlling the output (see all the modes below). Further it asks you to threshold for the sensor you have chosen (like threshold temperature for temperature sensor) which on crossing that threshold, the appliance connected either turns on or off (depending on the sensor). But that's not it! This device can also protect your valuables at night by switching on the security mode. Still not over! It also has a timer mode in which you can just set the time in hours and minutes you want your device to be switched on. Just one more. It can also act as a weather station by sending you the temperature and humidity of the place that it is kept.

The best thing about this home automation project is that is does not require any internet connections, WiFi or GUI's which makes it a simple yet effective project. Most home automation projects are cloud based which makes it too complicated to understand and difficult to make for a beginner. Those projects anyway provide better capabilities than this but to start with home automation projects, this is just the perfect thing. This project is free from all networking and connections. You just have to set a mode (like temperature sensor mode, timer mode, security mode etc.) through two buttons present on the box unlike other projects where you have to switch on your laptop or hunt for your smartphone to control it.

It would seem complicated to make this project at first but as you start looking through this instructable, you will realize how easy it is. The reason is that it uses just an arduino as the brain of the project that does all the processing, some sensors connected through arduino, lcd and bluetooth module. The role of bluetooth connectivity is nothing but to get serial data from the device and to get to know what's happening in the device through serial monitor. You don't need to control the device through bluetooth. The whole device is controlled using just two buttons present on the box.

_____________________________

Here is the video of the project in action. Sorry my video editing skills are not too good.

_______________________________

The reason why I made this project: I wanted to try something new with 'home automation'. At first, I was not even knowing what I was going to make. I glanced through all the home automation projects on instructables and other websites but did not get a good idea. When I started thinking about it more, I thought of making something that would not be limited to automate just lights and fans but also other home appliances. I also wanted to make it free from wireless connectivity and easy to make. After a lot of planning, I came up with this idea. I ordered the parts, started making the project and wrote the code. I experienced a bunch of difficulties on the way which I managed to go through somehow. The biggest problem came when working with ac current which I did with by body full up with fear. I had never made a power supply before and most of my projects were battery powered. When I saw that working, I realized that It was not too difficult to work with it when you take proper precautions. I agree that the project is not an epic project that can change the world but still not that bad to start with.

Disclaimer: This project works on 240v ac which is quite dangerous if you don't take safe measures while working with it. If you do something wrong and if it results in a damage, I am not responsible in any way. Make this at your own risk. YOU HAVE BEEN WARNED!

I have worked really hard on this instructable and made sure that all the instructions are easy to understand. If still there is a problem, feel free to comment or ask a question.

Please vote for me in all the contests by clicking on the button present in the top right corner of the page.

______________________________

Before starting this instructable, I have enlisted all the modes/features, advantages and disadvantages of this project which will give a better understanding to those who watch it.

1) Toggle Mode: Simple mode that switches the power outlet on when the first button is pressed. On again pressing the button, it switches off. In simple words, it acts as a toggle switch. I added this mode for some situations when I wanted to use it just like an extension cord by not using any of the sensors.

2) Temperature Sensor Mode: Uses the temperature sensor to control the outlet by switching it on when the actual temperature crosses the set threshold temperature. This mode can be used to run, ceiling fans, desktop fans, room coolers, low power air conditioners and any appliance related to temperature. The arduino code can be modified to use it for heating appliances.

3) Humidity Sensor Mode: Uses the humidity sensor to control the outlet by switching it off when the actual humidity crossed the set threshold humidity. This mode can be used to control any humidity related appliances especially room coolers. The reason is that room coolers when used for a long time increase the humidity of air causing less cooling and uncomfortable feeling. This mode will turn it off in such a case which will give better cooling.

4) Light Sensor Mode: Uses an LDR to find the light of the place and switch the outlet on when the light becomes less than the set threshold intensity. LDR is not accurate to measure light in S.I. unit so I didn't use it but used the word 'unit' for it. The recommended threshold is 250 units. This mode can be useful to operate desktop lamps, ceiling lights and even a bell to wake you up in the morning. Any other light related appliance can also be powered.

5) Clap Sensor Mode: Uses an Electret Microphone to detect loud noises and switch on an appliance by clapping. Clapping again will switch it off. Use this mode to control any appliance by just clapping. This is similar to 'The clapper' used in old times.

6) Motion Sensor Mode: Uses a PIR motion sensor that detects the motion of a human being. Use this mode to turn on an appliance by just waving your hand in front of the box upto a range of 6 meters. Wave your hand again to switch it off. This mode can be used to control almost any appliance.

7) Presence Sensor Mode: Uses an Ultrasonic distance sensor to check your presence near the box. It works by switching the outlet off when the distance is more than the set threshold distance. The threshold distance should be a little more than the distance between you and the box when you are sitting. This is the best mode from all of them as it helps you to save energy by switching off the appliance when you are not sitting in front of it. Use this mode in your workshop or when you are studying. It can be used to power desktop fans, reading lamps, soldering iron, hot glue gun and many more tools and other appliances.

8) Security Mode: This mode can be used to protect your valuables at night when you're sleeping. It uses the motion sensor to detect the motion of a thief at night. On detecting a thief, it switches on the outlet and sets the buzzers high to wake you up. You can connect a light or a loud bell to wake you up easily.

9) Timer Mode: Set the number of hours and minutes you want your device to be on. This timer mode will help you save energy. Control almost any appliance with this mode.

10) Weather Display Mode: A simple mode that displays the temperature and humidity of the place it is kept. It displays it on the lcd as well as sends through bluetooth to be seen through a laptop or smarphone.

______________________________

Ok, so after watching all the modes you may have got an idea of all the features of this box. Now I will give the pros and cons of this project. Ofcourse, it can't be just perfect with no limitations or disadvantages. If it has a lot of advantages, it has some disadvantages too.

Advantages:

  1. Will automate your bedroom. All the modes of this project given above help you to automate your bedroom. You don't need to always switch on your lights and fans by going to the place where the switch is located. Just plug in your appliance to this box and it will automatically control it.
  2. Will help you save energy and thus lower your electricity bills. The reason is that this device switches an appliance off when it is not required. Normally you just switch your lights and fans on and they remain so even when your are not in your room just because you forgot to turn them off.
  3. Will keep you safe by protecting you from thieves at night when you are peacefully sleeping on your bed.
  4. The device consumes less power for all its inner workings and powering all the sensors (Not including the power consumed by the appliance connected to the power outlet. The total power consumed only by the device is around- 5-10 Watts.
  5. The project is free from any cloud networking, complex circuits, too much of coding and expensive parts which makes it easy to make and understand. The cost of the project is also very less.
  6. The box is lightweight thus can be carried from one place to another easily.

Disadvantages:

  1. The box has only one power outlet thus can power only one appliance at a time. Several things can be powered together by using extension boxes but they will turn on and off all at the same time and will not be able to controlled separately. The outlets can be increased in no. but that would mean, more relays and more programming.
  2. The box can't control high power appliances like big air conditioners, heaters. This can be made possible by using bigger relays.
  3. The security mode will beep the buzzer even if you or any other person (who is not a thief) passes through the front of it spoiling the sleep of your family members.
  4. The range of bluetooth module is just 10-15m.

_______________________________________

Subscribe to my YouTube channel here:

Step 1: How It Works?

So after watching the introduction you may have an idea about the entire project. But before building, it is important to know the working of this project which means, the sensors used in this project, the code used to run this thing, and other minor things.

Power Supply:

The power supply used in this project comprises mainly of a transformer and a bridge rectifier. The transformer lowers the high ac voltage from 240v ac to 12v ac and bridge rectifier converts it to dc. It also has a voltage regulator that converts that 12v dc to 5v.

The Power Outlet:

The beige color power socket present on the box is the place where you can connect all your electrical appliances. A question comes that the power outlet gives 240v ac to power the appliances when the arduino tells it to do. But the arduino gives out only 5v. So how is this possible to have 240v ac with a low signal of 5v dc? This is made possible by using a device called relay that acts as a switch which is on when a low signal is received through it. This switch can handle a large current so can be used to control this socket easily.

Sensors:

As already told, the project uses six sensors in total. Below is a description on how they work.

  1. Temperature sensor (LM35): Gives a voltage on one of its pins which becomes high or low depending on the temperature of the sensor. The arduino reads the voltage and converts it to readable temperature.
  2. Humidity sensor (DHT11): Gives a certain clock signal of both temperature and humidity. The arduino converts those signals to readable temperature and humidity. (Used here only for humidity).
  3. Light sensor (LDR): Gives a high resistance when placed in dark and low resistance when placed on a place full of light. The resistance changes with change in intensity of light. The arduino reads the resistance.
  4. Clap sensor (Electret Microphone): Gives a low power signal when a loud sound is detected. The arduino detects the low signal and sets the outlet high.
  5. Motion Sensor (PIR Sensor): Detects the motion of a human and gives a high output when detected. Actually detects that radiation given out by a human.
  6. Presence Sensor (Ultrasonic Distance Sensor- HC-SR04): Transmits an ultrasound that goes, reflects and comes back. Arduino measures the time taken by it and gives the distance. When the distance becomes low, that means that you are present in front of it so it detects your presence.

Code:

The process begins when the arduino switches on and asks you to select a mode through buttons by displaying it on the liquid crystal display. The part of the code below processes and displays the mode you've selected.

<p>while(digitalRead(11) == LOW) <br>{
if(digitalRead(10) == HIGH)
{
delay(500);
count++;    
}
lcd.clear();
lcd.print("Choice:");
lcd.print(count);
delay(100);
}</p>

11 is the pin connected to button 2. So the first line means that until you don't press the second button (until it is low) the loop will go on running forever. And if pin 10 is high (button 1 pressed) it will advance the count variable by one. In this way the loop counts the number of times the button is pressed. This thing is also used to set the threshold which you may see in some parts of the code.

Next is the main part of the code, the 'switch statement' that enters a mode according to the value of count. It is written as:

<p>switch(count)</p>

Next is the case statement which is the part of switch. It executes that statement mentioned under it if the value of the variable of switch is equal to the value mentioned after case. For eg if I write:

switch(count)
{
case 1:
{
statement a;
}
break;
case 2:
{
statement b;
}
break;
} 

This means that if the value of count is 1, the program will execute statement a and if it is two, the program will execute statement 2. You may see the case statements in the code like this. So that value present after every case statement in the main code is actually he code no. like:

<p>case 1:  // Toggle mode<br>  {
    delay(1000);
    Serial.println("Toggle mode selected");
    Serial.println("Press the button to switch the outlet on/off");
    int a8 = 0;
    while(a8 == 0)
    {
    lcd.clear();
      lcd.print("Curr. State: ");
    lcd.print(state);
    lcd.setCursor(0,1);
    lcd.print("Press Button");
    if(digitalRead(10) == HIGH && state3 == 0) // if button pressed and outlet is off, turn it on
   {
    digitalWrite(13, HIGH);
    state = "ON";
    state3++;
    Serial.println("Switched on");
    delay(1000);
   }
   if(digitalRead(10) == HIGH && state3 == 1) // if button pressed and outlet is on, turn it off
   {
     digitalWrite(13, LOW);
     state = "OFF";
     state3--;
     Serial.println("Switched off");
     delay(1000);
   }
    delay(100);
  }
  }
  break;</p>

The rest of the code is pretty straightforward and easy to understand.

Step 2: Parts and Tools

The following parts and tools are required to make this project. The total cost of the project is around $50 or 3000 INR. The parts can be obtained from your local hobby store or over online stores.


PARTS:

  • 1x Arduino nano (Ebay Link)
  • 1x Liquid crystal display (16x2) (Ebay Link)
  • 1x DHT11 temperature and humidity sensor (Ebay Link)
  • 1x Temperature sensor (LM35) (Ebay Link)
  • 1x Ultrasonic distance sensor (HC-SR04) (Ebay Link)
  • 1x LDR (light dependent resistor) (Ebay Link)
  • 1x Electret microphone (Ebay Link)
  • 1x Bluetooth module (HC-05) (Ebay Link)
  • 1x 12v SPST or SPDT Relay (7A) (Ebay Link)
  • 2x bc547 or 2n3904 or 2n2222 Transistor
  • 1x Power socket
  • 1x 240v - 12v AC step down transformer
  • 1x Power plug
  • 3x Momentary push buttons
  • 2x 10K resistors
  • 1x 1K resistor
  • 5x Diodes - In4007 or 1n4001 or 1n4148
  • 1x 7805 Voltage regulator
  • 1x 470uf Capacitor
  • 2x 0.1uf Ceramic capacitors
  • 2x LEDs
  • 2x 100 ohm resistors
  • 1x Buzzer
  • Project enclosure
  • Perfboard
  • Rainbow cable
  • Thick cable

TOOLS:

  • Soldering iron
  • Soldering wire
  • Hot glue gun w/glue sticks
  • Wire cutter/stripper
  • Pliers
  • Drill
  • Double sided tape

Step 3: Mark

The the first step of this project is to mark the holes for connecting everything in your enclosure. Use a marker and make the holes in such a way that the jumper wires can pass through the holes. Do this for humidity sensor, motion sensor and ultrasonic sensor. For microphone, ldr and temperature sensor, make small holes so that the leads can pass through. For lcd and power socket, simply measure the size using a ruler and mark it on the enclosure.

When it comes to the type of enclosure to use, do not use a metal enclosure as you are working with ac mains and taking any type of risk by adding insulating pads. Use plastic or wooden enclosures. You can also use insulated black enclosures from readioshack. I used simple transparent kitchen ware box available cheap in the market.

Step 4: Make Holes

Using a drill and metal saw, make the holes in the marked positions. You can also use a hot knife instead of power tools if you are using a plastic enclosure.

You should cover or spray paint the enclosure at this time. I forgot to do this earlier so did it after the project was made. Its better to do it earlier as doing it later will cause great difficulties in doing it.

Step 5: Make a Power Supply

This is the most important part of the project. Since the project has an ac mains socket which can only be powered through an ac output that comes in your house. So you have to have an external plug that needs to be put from an ac outlet. This makes the device non-portable but there is no proper solution for this. Putting an inverter will double the size of the box and will make the weight ten times so that is not the proper solution.

Another thing is to power all the other things like arduino, lcd, bt module and all the sensors. This is a home automation project which should be capable enough to run for days, months and even years! So powering all these leftover parts with a battery will make it run maximum for a day or two. Since we have an ac output connected to the device, we can just use that to power all the things that can make it run forever.

But powering the project with 120v or 240v is not at all safe (Don't even think of trying it, it will blow up all your sensors and arduino). To make it work, we need a converter that will convert that 120 or 240v ac to stable and safe 5-12v dc. For this you need to make a power supply that will convert the current.

An ac-dc converter comprises of mainly two parts- the transformer and the bridge rectifier. The transformer lowers the ac voltage to a safe value and bridge rectifiers converted the ac current to dc. In this project I added a voltage regulator which is optional but makes your arduino safe from unstable voltages given by the bridge rectifier. While choosing your transformer keep in mind the ratings of yours. If you live in a country that gives 120v as the ac mains output, use a 120v-6v or 120v-12v transformer. On the other hand if your country's ac mains output is 240v use a 240v-6v or 240v-12v. Do not use any other type of transformer.

Solder the parts onto a perfboard as per the schematic given above. Connect a power cable to the primary coil wires of your transformer and plug it in on an ac mains socket. Make sure you correctly identify the primary and secondary coils of your transformer. Connecting it in the wrong way can cause severe problems. Extract three wires from the power supply- gnd, 12v, 5v. Check through a multimeter if your supply is working or not. You can also connect and led to check it.

Note: (1): If you are not confident when working with ac current or never did it before, take help from an expert before doing anything or buy a ready-made power supply. If you did anything wrong, you can get an electric shock resulting serious injuries an even death. If such a thing happens, I am not responsible in any way.

(2): Transformer does not convert ac current to dc. It only lowers the ac voltage keeping the type of voltage still ac. A transformer does not work on dc current.

Step 6: Make a Shield for Arduino

For arduino projects, it is a good idea to make a shield for it. This helps when you want to make changes in the code, want to make another project with it. This helps to easily plug it off and use it for multiple projects. This also helps in easy connections and to remove them.

Take a small part of perfboard and solder some female headers such that all the pins of your arduino fit in easily. Now solder some male headers corresponding to each pin on arduino. Connect each male header to its corresponding pin. Add some extra headers for gnd as well as 5v pin as these will be used the most for connections.

Step 7: Connect the Bluetooth Module

Every home automation system is incomplete without wireless connectivity with the user and the device. However in this project, most of the settings are done manually by pressing some buttons, I chose Bluetooth for connectivity. I didn't use WiFi or Ethernet for this project as there is no important use of connectivity so there is no need for long range connectivity. The role of Bluetooth here is no know what's happening in the device and around you using the serial monitor.

The Bluetooth module used here is the most commonly used hc-05. You can also use hc-06 or jy-mcu. All of them provide a range of.around 10-15m. Connect it according to the following-

  1. Vcc ---- Arduino 3.3v or 5v (according to your module)
  2. Gnd ---- Arduino gnd
  3. Tx ---- Arduino Rx
  4. Rx ---- Arduino Tx

Step 8: Connect the PIR Motion Sensor

PIR motion sensor is a device that can detect motion of a human up to a range of 6m. PIR stands for passive infrared. It works on the principle that every human emits a radiation of certain wavelength. The sensor detects those radiations and gives a high output when a motion is detected. So here when you will wave your hand in front of it, the outlet will get switched on. It is also used in security mode at to detect motion at night. Connect the sensor according to the text given below To check your sensor, upload the code given below to your arduino.

  1. Vcc ---- Arduino 5v
  2. Gnd ---- Arduino gnd
  3. Vout ---- Arduino analog pin 1
int calibrationTime = 30;
long unsigned int lowIn;        
long unsigned int pause = 5000;
boolean lockLow = true;
boolean takeLowTime;  
int pirPin = A1;    
int ledPin = 13;
void setup()
{
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(pirPin, LOW);                     
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}

void loop()
{     
if(digitalRead(pirPin) == HIGH)
{
digitalWrite(ledPin, HIGH);   
if(lockLow)
{  
         //makes sure we wait for a transition to LOW before any further output is made:
         lockLow = false;            
         Serial.println("---");
         Serial.print("motion detected at ");
         Serial.print(millis()/1000);
         Serial.println(" sec"); 
         delay(50);
         }         
         takeLowTime = true;
       }</p><p>     if(digitalRead(pirPin) == LOW){       
       digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state</p><p>       if(takeLowTime){
        lowIn = millis();          //save the time of the transition from high to LOW
        takeLowTime = false;       //make sure this is only done at the start of a LOW phase
        }
       //if the sensor is low for more than the given pause, 
       //we assume that no more motion is going to happen
       if(!lockLow && millis() - lowIn > pause){  
           //makes sure this block of code is only executed again after 
           //a new motion sequence has been detected
           lockLow = true;                        
           Serial.print("motion ended at ");      //output
           Serial.print((millis() - pause)/1000);
           Serial.println(" sec");
           delay(50);
           }
       }
  }</p>

Step 9: Connect the Ultrasonic Sensor

Ultrasonic sensor is a device used to measure distance up to 500cm. It works by sending ultrasonic sound and measuring the time that it takes to reflect and come back. Here it used as a presence sensor which means that it can detect if someone is sitting in front of it or not. This works by setting a threshold distance a little more than the distance between you and the device when sitting in front of it.

Connect it according to the text given below. Upload the code given below to your arduino if you want to check your sensor.

  1. Vcc ---- Arduino 5v
  2. Gnd ---- Arduino gnd
  3. Trigger ---- Arduino analog pin 2
  4. Echo ---- Arduino analog pin 3
#include <NewPing.h><br>
#define TRIGGER A2  
#define ECHO A3 
#define MAX 500
NewPing sonar(TRIGGER, ECHO, MAX);
void setup() 
{
Serial.begin(9600);
Serial.println("Start");
}
void loop() 
{
unsigned int uS = sonar.ping(); 
Serial.print("Distance: ");
Serial.print(uS / US_ROUNDTRIP_CM); 
Serial.println("cm");
delay(1000);
}

Step 10: Connect the DHT11 Sensor

You may be familiar with the DHT range of humidity and temperature sensors. The good thing about them that they are too cheap (around $2 each) and can measure both temperature and humidity. There are many of them like dht11, dht21, dht22 etc. I will be using dht11. Here it will only be used to measure humidity as lm35 can provide much accurate readings than the dht sensor however you can skip the use of lm35 if you want to reduce the total cost. With the help of this sensor,a humidity sensor mode is added in this project that can be used to run low power air coditioners or room coolers. Often a situation comes when using swamp coolers for a long time, the air becomes excessively humid causing a feeling of stickiness which reduces the cooling effect. This mode can be used to turn off the cooler for a certain time to give better cooling.

Connect your sensor as per the text given under. Upload the code given below with the same connections to your arduino if you want to check your sensor if it works or not. I ordered mine from amazon which works great!

  1. Pin 1 (Vcc) ---- Arduino 5v
  2. Pin 2 (Out) ---- Arduino analog pin 4
  3. Pin 3 (Nc) ---- Not connected
  4. Pin 4 (Gnd) ---- Arduino gnd
<p>#include "DHT.h"<br>#define DHTPIN A4     
#define DHTTYPE DHT11   
DHT dht(DHTPIN, DHTTYPE);
void setup() 
{
  Serial.begin(9600); 
  Serial.println("DHTxx test!");
 dht.begin();
}</p><p>void loop() 
{
  delay(2000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);
  if (isnan(h) || isnan(t) || isnan(f)) 
  {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float hi = dht.computeHeatIndex(f, h);
  Serial.print("Humidity: "); 
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
}</p>

Step 11: Connect the LCD

A 16x2 liquid crystal display is added to show what is happening inside without opening the serial monitor or connecting through bluetooth. It also helps in selecting the correct mode by pressing the buttons. For checking your display, upload the code given below to your arduino. It should show "Hello World" after uploading. If it doesn't, turn the preset a little or check the connections. Following are the connections of the lcd-

  1. Pin 4 ---- Arduino digital pin 8
  2. Pin 6 ---- Arduino digital pin 7
  3. Pin 11 ---- Arduino digital pin 6
  4. Pin 12 ---- Arduino digital pin 5
  5. Pin 13 ---- Arduino digital pin 4
  6. Pin 14 ---- Arduino digital pin 3
  7. Pin 3 ---- Preset pin 2
  8. Pin 2 ---- Preset pin 3, Arduino 5v, lcd pin 15 through 100ohm resistor
  9. Pin 1, 5, 16 ---- Arduino gnd

#include <LiquidCrystal.h><br>
LiquidCrystal lcd(8, 7, 6, 5, 4, 3);
void setup() 
{
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop() 
{
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}

Step 12: Connect the Microphone

You may have used 'the clapper' in old days that was used to switch electronic appliances on and off by clapping. So to do the same thing here, I added an electret microphone that detects clap or a loud sound and sets the power outlet high. Clapping again turns it off. The circuit that is used to connect the microphone to arduino was originally taken from 'Clap controlled power outlet' instructable by Diy hacks and how to's. The reason for such a circuit is that the microphone gives a very low output even when it detects a loud sound. This circuit increases it to a certain extent so that it can be read easily using arduino. Connect the microphone according to the schematic given above. The analog pin for it is A5. Upload the following code to your arduino to check your microphone. Open the serial monitor and you would see analog readings as 20-60 when you clap loudly. You can also blow air towards it or tap it.

void setup() 
{
Serial.begin(9600);
Serial.println("Start");
}
void loop() 
{
Serial.println(analogRead(A5));
delay(500);
}

Step 13: Connect the LDR

LDR or light dependent resistor is a device that detects the intensity of light falling on it and gives a high resistance in darkness while a low resistance in well lighted conditions. In this project it is used to turn on a lamp or any kind of light source when the light falling on it becomes less or it turns dark. This can be very useful when you are reading. The lights will automatically get on when it turns dark and you don't have to manually switch it on. This kind of device is also used in street lights. Wire your ldr according to the text given. Upload the code present below to your arduino for checking your sensor.

  1. Pin 1 ---- Arduino analog pin 6
  2. Pin 1 through a 10K resistor ---- Arduino 5v
  3. Pin 2 ---- Arduino gnd
void setup() 
{
Serial.begin(9600);
Serial.println("Start");
}
void loop() 
{
Serial.println(analogRead(A6));
delay(1000);
}

Step 14: Connect the Temperature Sensor

As the name tells, this device is used to measure temperature of the surroundings. In this project lm35, the cheapest temperature sensor is used. It works by giving a certain output voltage that changes with change in temperature. The readings given by the sensor are far accurate so this can be used to control low power air conditioners, desktop fans, room coolers, heaters and other appliances. By setting the threshold of a certain temperature, the device will switch on when the temperature exceeds that threshold temperature. Upload the code given below to your arduino to check your sensor. Following are the connections of the temperature sensor.

  1. Pin 1 (Vcc) ---- Arduino 5v
  2. Pin 2 (Vout) ---- Arduino analog pin 0
  3. Pin 3 (Gnd) ---- Arduino gnd
<p>float temp = 1;</p><p>void setup() 
{
Serial.begin(9600);
Serial.println("Start");
}</p><p>void loop() </p><p>{
temp = analogRead(A0);
temp = temp * 0.48828125;
Serial.print("Temperature = ");
Serial.print(temp);
Serial.print("*C");
Serial.println();  
delay(1000);
}</p>

Step 15: Connect the Buttons

You may have seen several home automation projects that use a smartphone or a laptop to control the system. The problem with these kind of projects is that you have to hunt for your smartphone or have to switch on your laptop to set the device. This can be helpful in big projects that offer complete home automation of your house but since this is a small project, I used buttons to control the device. The best part is that everything can be controlled using only two buttons present on the device. This allows you to easily control it without the need to connect to your smartphone or any other device. Connect your buttons to your arduino as per the connections given below. Upload the code present below to check if you have wired correctly or not.

Button 1:

  1. Pin 1 ---- Arduino digital pin 10
  2. Pin 2 ---- Arduino 5v
  3. Pin 1 through a 10K resistor ---- Arduino gnd

Button 2:

  1. Pin 1 ---- Arduino digital pin 11
  2. Pin 2 ---- Arduino 5v
  3. Pin 1 through a 10K resistor ---- Arduino gnd
<p>int button1 = 10;<br>int button2 = 11;
String a = "Unpressed";
String b = "Unpressed";</p><p>void setup()
{
pinMode(button1, OUTPUT);
pinMode(button2, OUTPUT);
Serial.begin(9600);
Serial.println("Start");
}</p><p>void loop()
{
if(digitalRead(button1) == HIGH)
{
a = "Pressed";
}
if(digitalRead(button1) == LOW)
{
a = "Unpressed";
}
if(digitalRead(button2) == HIGH)
{
b = "Pressed";
}
if(digitalRead(button2) == LOW)
{
b = "Unpressed";
}
Serial.print("Button 1: ");
Serial.print(a);
Serial.print("       ");
Serial.print("Button 2: ");
Serial.print(b);
Serial.println();
delay(1000);
}</p>

Step 16: Connect the Indicator LEDs

Two indicator leds are added here of red and green colors. The green one tells that if the device is on or off which means that it acts as a power indicator while the red one tells that if the power outlet is on or off. Connect them in the following manner. You need to add some current limiting resistors or the leds would burn out due to excessive current.

LED 1 (Green) :

  1. Negative ---- Arduino gnd
  2. Positive through a 100ohm resistor ---- Arduino 5v

LED 2 (Red) :

  1. Negative ---- Arduino gnd
  2. Positive through a 100ohm resistor ---- Arduino digital pin 13

Step 17: Connect the Buzzer

As already told, this project has a security mode which can protect you from thieves at night by detecting motion of a human being at night. So when a thief is detected, a loud noise should be made to wake you up or to scare the thief. For this I added a loud piezo buzzer that beeps when a motion is detected at night. The buzzer is loud enough to wake anyone up when both you and the device are inside the bedroom.

Connect the positive terminal of the buzzer to arduino digital pin 12 (d12) and the negative terminal to arduino gnd. For checking your buzzer, simply connect it to a 9v battery and it would produce a sound if it works.

Step 18: Add a Reset Button (Optional)

Since the arduino goes to an infinite loop (a loop that never ends) when you select your mode so that you can run the device for as much time as you want, there is a need for you to reset it when you want to change the mode. If you don't reset, the device will run forever until it gets a constant power supply. To reset the arduino you have to open the box each time and press the small button on the arduino everytime. To solve this problem you can simply add a reset button ablove the box.

This step is optional and is only for the ease to control the device. Connect the pin 1 of button to arduino RESET pin and pin 2 to arduino gnd.

Step 19: Make the Relay Board

Relay is a device that has two contacts that get connected to each other when it receives a low power signal. In other words, it is a device used to switch on high power appliances by a low power signal. I acts as a switch which on detecting a signal closes it and opens it when no current is detected. Here it is used to switch the power outlet on or off by receiving signals through arduino. The type of relay used here has 5 terminals out of which only 4 are used. For more information about relays, visit the Wikipedia page on relays.

When choosing your relay, make sure that your relay terminals can bear atleast 120-240v, 5A ac current. Any relay with ratings below this should not be used here. The coil voltage should be maximum 12v. Anything below that should work here. I used a 12v relay which usually has high ratings thus is suitable for this project. Also make sure that you only use a SPDT or a SPST relay only.

Make a relay board as per the schematic given above. For now, leave the NC, NO, and Pole terminals of the relay unconnected. They will be connected later.

Step 20: Hot Glue Everything Inside the Box

Stick all the parts inside the box using hot glue. You can also use double sided tape if you want to easily remove it for changing any connection. Make a small hole for the buzzer and stick it to the wall so that the sound of the buzzer can easily reach outside the box.

Step 21: Connect the Power Supply to Arduino

Connect the 5v output from the power supply that we made to arduino Vin pin, that receives the current. Finally connect the gnd from the power supply to arduino gnd. Finally insulate the connections properly with electrical tape. If the + and - terminals from the power supply get accidentally joined to each other, it will result a short circuit which can lead to problems. Remember not to connect the 12v pin to arduino but the 5v pin. 12v was connected to the realy earlier as it required a coil voltage of 12v.

Step 22: Insulate All the Open Connections

Since there is a power outlet present on the box, the wire connected to it is always carrying ac mains which can be accidentally connected to anyone of the sensors or the bare wires. All the sensors and arduino operate on 5v which is far less that the dangerous 240v. So you can already imagine what will happen when such a thing becomes true.

To prevent this, insulate all the connections using transparent tape or electrical tape. You can also use got glue to insulate the connections.

Step 23: Connect the Power Socket and Plug

The last step to complete the hardware setup of this project is to connect a power plug to the power supply and to connect the outlet through relay.

For this, first make a small hole in the box for the plug cable to pass through. Pass the cable trough the box and make a small knot inside the box so that the cable doesn't come out on pulling. Now connect both the wires of the cable to the transformer in either way.

Use some thick cable to connect to the power socket. Connect a wire to the wire on the plug. Connect the other part of the same wire to the NO contact of relay. Connect the pole contact of the relay to one of the terminals of power socket. Now take a wire and connect one part of it to the left out wire of the plug and the other part to the leftout terminal of the power socket. Leave the earth terminal of power socket remain unconnected. Refer to the schematic above for reference

Step 24: Upload the Code

Remove the arduino from the shield or remove the rx and tx wires from the bluetooth module. Copy the code given below and paste it to your arduino ide. Upload the code to your arduino.

/*The Bedroom automation box
Made by- Saiaym Agrawal
*/
#include "DHT.h"          // add all the libraries
#include <LiquidCrystal.h><br>#include <NewPing.h> 

String state = "OFF";    // initialize all the values
int temppin = A0;
int pir = A1;
int mic = A5;
int ldr = A6;
int incoming = 0;
int count = 1;
int thres1 = 0;
int thres2 = 0;
int thres3 = 0;
int thres4 = 0;
int thres5 = 0;
int state1 = 0;
int state2 = 0; 
int state3 = 0;
int light = 0;
int distance = 0;
int sound = 0;
int minute = 0;
int hour = 0;
float temp = 1;
float humidity = 0;
DHT dht(A4, DHT11);  // tell the dht pin and the type of sensor
LiquidCrystal lcd(8, 7, 6, 5, 4, 3); // tell the lc pins
NewPing sonar(A2, A3, 400); // tell the sensor pins and the maximum distance</p><p>void setup() 
{
pinMode(13, OUTPUT); // set the pins as output
pinMode(12, OUTPUT);
lcd.begin(16,2); // start up the lcd
dht.begin();
Serial.begin(9600);
Serial.println("Bedroom Home Automation");
lcd.clear();
lcd.print("  Bedroom Home  ");
lcd.setCursor(0,1);
lcd.print("   Automation   ");
delay(3000);
}</p><p>void loop() 
{
lcd.clear();
lcd.print("Select A Mode");
Serial.println("Select a mode through buttons");
delay(2000);
  
while(digitalRead(11) == LOW) // exit the loop when second button is pressed
{
if(digitalRead(10) == HIGH)
{
delay(500);
count++;    // count the number of times the button is pressed
}
lcd.clear();
lcd.print("Choice:");
lcd.print(count);
delay(100);
}</p><p>switch(count) // Enter the mode according to the number selected earlier
{</p><p>  case 1:  // Toggle mode
  {
    delay(1000);
    Serial.println("Toggle mode selected");
    Serial.println("Press the button to switch the outlet on/off");
    int a8 = 0;
    while(a8 == 0)
    {
    lcd.clear();
      lcd.print("Curr. State: ");
    lcd.print(state);
    lcd.setCursor(0,1);
    lcd.print("Press Button");
    if(digitalRead(10) == HIGH && state3 == 0) // if button pressed and outlet is off, turn it on
   {
    digitalWrite(13, HIGH);
    state = "ON";
    state3++;
    Serial.println("Switched on");
    delay(1000);
   }
   if(digitalRead(10) == HIGH && state3 == 1) // if button pressed and outlet is on, turn it off
   {
     digitalWrite(13, LOW);
     state = "OFF";
     state3--;
     Serial.println("Switched off");
     delay(1000);
   }
    delay(100);
  }
  }
  
  case 2: // temperature sensor mode
  {
  delay(1000);
  Serial.println("Temperature mode selected");
  Serial.println("Set Threshold");
  lcd.clear();
  lcd.print("Set Threshold");
  delay(2000);
    while(digitalRead(11) == LOW) // set threshold through buttons
   {
   if(digitalRead(10) == HIGH)
   {
     delay(500);
   thres1++;
   }
   lcd.clear();
   lcd.print("Threshold:");
   lcd.print(thres1);
   delay(100);
 }
  Serial.print("Threshold Temperature: ");
  Serial.print(thres1);
  int a = 0;
  while(a == 0)
  {
    
    temp = analogRead(temppin);
    temp = temp * 0.48828125;
    lcd.clear();
    lcd.print("Curr. State: ");
    lcd.print(state);
    lcd.setCursor(0,1);
    lcd.print("Temp:");
    lcd.print(temp);
    lcd.print("*C");
    if(temp >= thres1)
    {
      digitalWrite(13, HIGH);
      state = "ON";
    }
    if(temp < thres1)
    {
      digitalWrite(13, LOW);
      state = "OFF";
    }
    delay(1000);
  }
}
  break;
  
  case 3:  // Humidity sensor mode
  delay(1000);
  Serial.println("Humidity sensor mode selected");
  Serial.println("Set Threshold");
  lcd.clear();
  lcd.print("Set Threshold");
  {
   while(digitalRead(11) == LOW)
   {
   
   if(digitalRead(10) == HIGH)
   {
     delay(500);
   thres2++;
   }
  lcd.clear();
   lcd.print("Threshold:");
   lcd.print(thres2);
 delay(100);
}
  Serial.print("Threshold Humidity %: ");
  Serial.print(thres2);
  Serial.print("%");
  int a1 = 0;
  while(a1 == 0)
  {
    humidity = dht.readHumidity();
    lcd.clear();
    lcd.print("Curr. State: ");
    lcd.print(state);
    lcd.setCursor(0,1);
    lcd.print("Humid:");
    lcd.print(humidity);
    lcd.print("%");
    if(humidity <= thres2)
    {
      digitalWrite(13, HIGH);
      state = "ON";
    }
    if(humidity > thres2)
    {
      digitalWrite(13, LOW);
      state = "OFF";
    }
    delay(1000);
  }
}
  break;
 
 case 4: // light sensor mode
 {
  delay(1000);
   Serial.println("Light sensor selected");
   Serial.println("Set Threshold");
   lcd.clear();
  lcd.print("Set Threshold");
   while(digitalRead(11) == LOW)
   {
   
   if(digitalRead(10) == HIGH)
   {
     delay(500);
   thres3+=10;
   }
   lcd.clear();
   lcd.print("Threshold:");
   lcd.print(thres3);
   delay(100);
 }
 Serial.print("Threshold intensity: ");
 Serial.print(thres3);
 Serial.print("units of 999");
 int a2 = 0;
  while(a2 == 0)
  {
     light = analogRead(ldr);
    light = map(light, 0, 1023, 999, 0);
    lcd.clear();
    lcd.print("Curr. State: ");
    lcd.print(state);
    lcd.setCursor(0,1);
    lcd.print("Light:");
    lcd.print(light);
    lcd.print("un.");
    if(light <= thres3)
    {
      digitalWrite(13, HIGH);
      state = "ON";
    }
    if(light > thres3)
    {
      digitalWrite(13, LOW);
      state = "OFF";
    }
    delay(1000);
  }
 }
  break;</p><p>  case 5: // clap sensor mode
  {
   Serial.println("Clap sensor mode selected");
   Serial.println("Clap to turn on/off");
    int a3 = 0;
    while(a3 == 0)
    {
      lcd.clear();
      lcd.print("Curr. State: ");
      lcd.print(state);
      lcd.setCursor(0,1);
      lcd.print("Clap Clap!");
      if(analogRead(mic) > 5 && state1 == 0)
      {
        digitalWrite(13, HIGH);
        state = "ON";
        state1++;
      delay(1000);
    }
      
      if(analogRead(mic) > 5 && state1 == 1)
      {
        digitalWrite(13, LOW);
        state = "OFF";
        state1--;
      delay(1000);
    }
     delay(100); 
    }
    }
    break;    
   
   case 6: // motion sensor mode
   {
     Serial.println("Motion sensor mode selected");
     for(int b=30; b>0; b--)
     {
       Serial.println("Preparing Sensor.....");
       lcd.clear();
       lcd.print("Preparing");
       lcd.setCursor(0,1);
       lcd.print("Sensor...");
       lcd.print(b);
       delay(1000);
     }
     
     int a4 = 0;
     while(a4 == 0)
     {
       lcd.clear();
     lcd.print("Curr. State: ");
     lcd.print(state);
     lcd.setCursor(0,1);
     lcd.print("Motion Sensor");  
       if(digitalRead(pir) == HIGH && state2 == 0)
     {
       digitalWrite(13, HIGH);
       state = "ON";
       state2++;
      delay(4100);
   }
    
   if(digitalRead(pir) == HIGH && state2 == 1)
   {
     digitalWrite(13, LOW);
     state = "OFF";
     state2--;
   delay(4100);
   }
 delay(100);
 }
 
   }
   break;</p><p>case 7: // presence sensor mode
{
  delay(1000);
  Serial.println("Presence sensor mode selected");
  Serial.println("Set Threshold");
  lcd.clear();
  lcd.print("Set Threshold");
  while(digitalRead(11) == LOW)
   {
   
   if(digitalRead(10) == HIGH)
   {
     delay(500);
   thres4+=10;
   }
   lcd.clear();
   lcd.print("Threshold:");
   lcd.print(thres4);
   delay(100);
 }
 int a5 = 0; 
 while(a5 == 0)
  {
   delay(50);
    distance = sonar.ping();
    distance = distance / US_ROUNDTRIP_CM;
    lcd.clear();
    lcd.print("Curr. State: ");
    lcd.print(state);
    lcd.setCursor(0,1);
    lcd.print("Dist.");
    lcd.print(distance);
    lcd.print("cm");
    if(distance <= thres4)
    {
      digitalWrite(13, HIGH);
      state = "ON";
    }
    if(distance > thres4)
    {
      digitalWrite(13, LOW);
      state = "OFF";
    }
    delay(1000);
  }
 }
  break;</p><p>case 8: // security mode
{
  Serial.println("Security mode selected");  
  Serial.println("Preparing sensor.....");
  delay(1000);
  for(int d = 30; d > 0; d--)
  {
    lcd.clear();
    lcd.print("Preparing");
    lcd.setCursor(0,1);
    lcd.print("Sensor...");
    lcd.print(d);
    delay(1000);
  }
  lcd.clear();
  lcd.print("Security Mode");
  delay(10000);
  int a6 = 0;
  while(a6 == 0)
  {
   if(digitalRead(pir) == HIGH)
    {
      digitalWrite(13, HIGH);
      Serial.println("Thief detected");
      lcd.clear();
      lcd.print("Thief Detected");
      lcd.setCursor(0,1);
      lcd.print("!!!!!!!!!!!!!!!!");
      int c = 0;
     while(c == 0)
    {
     digitalWrite(12, HIGH);
     delay(1000);
     digitalWrite(12, LOW);
     delay(1000);
    }
  }
    delay(1000);
}
}
break;   </p><p>case 9: // timer mode
{
  delay(1000);
   Serial.println("Timer Mode selected");
   lcd.clear();
    lcd.print("Enter Hours");
    delay(2000);
   while(digitalRead(11) == LOW)
   {
   if(digitalRead(10) == HIGH)
   {
     delay(500);
    hour++;
   }
   lcd.clear();
   lcd.print(hour);
   delay(100);
 }
 lcd.clear();
 lcd.print("Enter Minutes");
 delay(2000);
 while(digitalRead(11) == LOW)
   {
   if(digitalRead(10) == HIGH)
   {
     delay(500);
    minute++;
   }
   lcd.clear();
   lcd.print(minute);
   delay(100);
 }
 delay(1000);
int e2 = minute;
int e3 = 0;
  for(int e1 = hour; e1 >= 0; e1--)
 {
   for(e2; e2 >=0; e2--)
   {
     for(e3; e3 >= 0; e3--)
     {
       digitalWrite(13, HIGH);
       lcd.clear();
       lcd.print("Time Left:");
       lcd.setCursor(0,1);
       lcd.print(e1);
       lcd.print(":");
       lcd.print(e2);
       lcd.print(":");
       lcd.print(e3);
     delay(1000);
   }
e3 = 59;
}
e2 = 59; 
}
  int a9 = 0;
  while(a9 == 0)
  {
 digitalWrite(13, LOW);
 lcd.print("Time Finished");
 lcd.clear();
delay(100);  
}
  }
break; 
 
case 10: // weather display mode
{
  int a7 = 0;
  while(a7 == 0)
  {
   float t = dht.readTemperature();
  float f = dht.readTemperature(true);
    humidity = dht.readHumidity();
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print("*C");
    Serial.print("       ");
    Serial.print(f);
    Serial.print("*F");
    
    Serial.println() ;
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print("%");
    Serial.println();
    lcd.clear();
    lcd.print("T:");
    lcd.print(t);
    lcd.print("C ");
    lcd.print(f);
    lcd.print("F");
    lcd.setCursor(0,1);
    lcd.print("H:");
    lcd.print(humidity);
    lcd.print("%");
    delay(1000);
  }
}
break;
}
}</p>

Step 25: How to Operate/ Connect Through Bluetooth

After uploading the code, switch on your device by connecting it to a power source. Below are the operating instructions and how to connect the device through bluetooth.

Operating Instructions:

On switching on the device, it will ask you to select a mode. For this press button 1 to advance the number and button 2 to select the number. For selecting threshold, press button 1 to advance it and button 2 to select it. To reset the device press the reset button connected earlier. Following are all the mode numbers:

  • 1 - Toggle Mode
  • 2 - Temperature Sensor Mode
  • 3 - Humidity Sensor Mode
  • 4 - Light Sensor Mode
  • 5 - Clap Sensor Mode
  • 6 - Motion Sensor Mode
  • 7 - Presence Sensor Mode
  • 8 - Security Mode
  • 9 - Timer Mode
  • 10 - Weather Display Mode

Connecting Through Bluetooth:

Through Arduino IDE (Laptop or PC):

  1. Switch on your device so the bluetooth module is activated.
  2. Go to devices and printers on your laptop (Windows)
  3. Select add a device
  4. On searching for devices, it would find one named HC-05, HC-06 or any other name.
  5. Select the module and enter the pairing code as 1234. The device driver would be installed.
  6. Select your module and check the serial port in properties eg. COM65, COM47.
  7. Open arduino ide. Select your serial port to your module's port (not your arduino's port)
  8. Open the serial monitor. Now your device would be connected. You would be able to see messages from your device.

Through an Android Device:

  1. Download an app named 'Blue term'.
  2. Pair your android device to the module.
  3. Connect to the module in the app. You will see messages from your device.

Step 26: You're Done! / Further Improvements

So you're finally done making your bedroom automation box. Switch it on and power almost any appliance with this smart box. Set the security mode to keep your valuables safe!

Home automation projects are like- the more you tinker, the more you learn and the better it will get. Those projects are never ending projects which can have infinite features. Here here some ideas for further improvement of the project.

(1): You can add as many power outlets as your want. You will just need the same amount of relay to power each one. This was just a simple try of the project so I added only one. But when I will post anther version, I will surely add more of them.

(2): You can use better connections and GUI's apart from bluetooth. Wifi or ethernet would provide better range as well as better capabilities of the box to do things.

(3): You can make the box internet controlled or can add some features like getting calls or email notifications in security mode and get a weather report of your bedroom.

(4): Even with bluetooth, you can design the code in such a way to set the mode and threshold wirelessly and to switch the outlet on using smarphone.

(5): You can add even more sensors like gas sensors, flame sensors, water leak sensors, tilt sensors to notify you when there is a fire, water leak or an earthquake.

(6): You can use bigger relays to power even high power appliances.

(7): Not just a bedroom automation box, you can modify it to make it for kitchen, living room or garage.

Finally, that's the end of this instructable. Hope you liked it. Please post your pictures if you've made it and suggest me of any further improvements. Feel free to comment or ask any questions.

Please vote for me in the contest if you think this instructable deserves a win. THANK YOU :)

Home Automation

First Prize in the
Home Automation

Unusual Uses Challenge

Participated in the
Unusual Uses Challenge

Bedroom Contest

Participated in the
Bedroom Contest