Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
Showing results for 
Search instead for 
Did you mean: 
Welcome to the second part of the Home Automation Workshop 2.

In our previous post, we discussed how we can automate our house doors using Ultrasonic Sensor.

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


Set up your room lighting according to the daylight using LDR 

Components Involved:  

  • Arduino Uno R3 

  • Ultrasonic Distance Sensor 

  • Micro Servo Motor 

  • Breadboard 

  • Jumper Wires 

  • Photoresistor (LDR) 

  • Resistor 10KΩ 

  • Resistor 221Ω 

  • LED 


  1. Connect one terminal of the LDR to the positive terminal of the breadboard. Also, connect the other end of the LDR through the resistor to the ground as shown in the figure.

  2. Next, connect one end of the LDR to the analog signal of the Arduino board. This will help the LDR to collect the light values.

  3. Let’s connect the central LED. This LED will glow whenever the light is less. Connect the other resistor (221 Ohm) to the LED so that our LED doesn’t burst. Join the other end of the resistor to the D6 pin of the Arduino board (as shown by pink in the figure). Also, connect the cathode to the ground. 

  4. Now, we move onto the code
    #include <Servo.h>
    Servo servo1;
    int trigPin = 9;
    int echoPin = 8;
    const int lamp = 6;
    void Setup() {​
    pinMode(trigPin, OUTPUT);
    pinMode(echoPin, INPUT);
    pinMode(lamp, OUTPUT);
    void loop() {​
    int c = analogRead(A0);
    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;
    if(cm<=50) {​
    if(c<300) {​
    digitalWrite(6, HIGH);
    else {​
    digitalWrite(6, LOW);
    else {​
    digitalWrite(6, LOW);
    long microsecondsToInches(long microseconds) {​
    return microseconds / 74 / 2;
    long microsecondsToCentimeters(long microseconds) {​
    return microseconds / 29 / 2;

  5. Now we will try simulating it. We see our LDR is switched on to the dark mode. When our object moves closer to the sensor, the LED starts glowing. 

  6. Similarly, if it’s Day time and the person walks in, only the door will open, and the LED is still off. 

 I hope you enjoyed our home automation workshop series. We will come back with more amazing workshops. Stay Tuned! 🙂