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:
- Connect the 5V power of the Arduino board to the power of the breadboard (plus point). Similarly, connect the power (Vcc) of the Ultrasonic Distance Sensor to the breadboard.

- 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.

- Now, connect the signal of the Servo Motor to the seventh digital pin of the Arduino board. (shown by orange in the fig).

- 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.

- 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;
}
- 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 !