Skip to Content
Technical Articles

SAP d-shop Bangalore Community | Home Automation Workshop 2: Part 1/2

Welcome to the first part of the Home Automation Workshop 2.

In the first workshop, we discussed some basic home automation techniques using Arduino Board and Temperature Sensor.

In this blog post we will get to see how we can automate our house doors.

Scenario:

Automate Door Mechanism using Ultrasonic Sensor.

Components Involved:  

  • Arduino Uno R3 
  • Ultrasonic Distance Sensor 
  • Micro Servo Motor 
  • Breadboard 
  • Jumper Wires 

Procedure: 

  1. Connect the 5V power of the Arduino board to the power of the breadboard (plus point). Similarly, connect the power (Vccof the Ultrasonic Distance Sensor to the breadboard.
  2. Connect the power of the Micro Servo Motor to the breadboard. Similarly, connect the ground (GRND) terminals of the three components to the negative terminal of the breadboard. 
  3. Now, connect the signal of the Servo Motor to the seventh digital pin of the Arduino board. (shown by orange in the fig). 
  4. Next we connect the echo pin of the Ultrasonic Sensor to the D8 pin of the Arduino board and the trigger to the D9 pin. 
  5. Our simple circuit is now ready. Next, we move on to the code. 
    #include <Servo.h>
    
    Servo servo1;
    int trigPin = 9;
    int echoPin = 8; 
    
    void Setup() {​​​​​
    servo1.attach();
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    }​​​​​
    
    void loop() {​​​​​
    long duration, inches, cm;
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    
    //convert the time into distance
    
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);
    
    //distance = duration*0.034/2;
    
    Serial.print(cm);
    servo1.write(0);
    if(cm<=50){​​​​​
    servo1.write(90);
    delay(2000);
    }​​​​​
    else {​​​​​
    servo1.write(0);
    }​​​​​
    }​​​​​
    
    long microsecondsToInches(long microseconds) {​​​​​
    return microseconds / 74 / 2;
    }​​​​​
    
    long microsecondsToCentimeters(long microseconds) {​​​​​
    return microseconds / 29 / 2;
    }
    

     

  6. When you will simulate it, you will observe that as you move the object closer to the sensor, the door gets automatically opened. Similarly, when the distance between the object and the sensor increases the door closes.

 

This is it for our first scenario. Hope you liked the blog post. In the second scenario we will see how we can control our room lighting according to the daylight.

Take care, Stay safe !

Be the first to leave a comment
You must be Logged on to comment or reply to a post.