Skip to Content
Technical Articles
Author's profile photo Pranav Kapoor

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.


Automate Door Mechanism using Ultrasonic Sensor.

Components Involved:  

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


  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() {​​​​​
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    void loop() {​​​​​
    long duration, inches, cm;
    digitalWrite(trigPin, LOW);
    digitalWrite(trigPin, HIGH);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    //convert the time into distance
    inches = microsecondsToInches(duration);
    cm = microsecondsToCentimeters(duration);
    //distance = duration*0.034/2;
    else {​​​​​
    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 !

Assigned Tags

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