Distance sensing Automatic Lights

Automatic Light - Distance based

Category: Energy
Communication: None
Components: Internal
Created By: Gouri Lekshminarayanan
Difficulty Level: Beginner
Input Sensor: https://www.notion.so/Distance-sensor-dc77c74814514890820ac8b68e2c7178
Microcontroller: Arduino UNO
Output Device: https://www.notion.so/Relay-87281abf2e3c447690002dd3258f88fd, https://www.notion.so/LCD-screen-a1d6725d214448e08e68217ba38d3236
Status: Complete


Refer to the first booklet Discover in the Solve Ninja Techno Kit for information on the components available with the Solve Ninja Techno Kit.

Things you will need

  • Solve Ninja Techno Kit which will contain

  • Additional materials


    Questions you need to ask

    Get data. Examples:

    • How does the distance sensor work?
    • What are the relevant units of measurement?
    • How far away should the object be (from the SNT kit) for the light to switch on? What is the distance sensor value for that?
    • Where is the SNT kit located in the room/area and how will the object interact with the Solution Box?

    What are the things you need to learn about the user? Examples:

    • Who is the user?
    • How/ how often do they interact with the light and the switch?
    • Is the switch very far away from the user’s reach/ access, for eg. is it at the other end of the room, very high from the floor, etc.?
    • What is the main problem faced by the user with respect to using the light?
    • How will the user be using the automatic light?
    • What is stopping the user from recognising the problem and taking action?

    What are the things you need to think about to convince the user that wasting water is a problem? Examples:

    • Why is it a problem?
    • Does the user need to be notified as and when the light is automatically switched off/on?
    • Is your goal to encourage the user to switch the light off/on when needed, or is it to automatically switch the light off/on?

    This will help you decide what output device to use, for example-

    • buzzer with an annoying beeeep or a gentle music (like a water filter) if the user has to be alerted at dawn/dusk when light is switched off/ on.
    • multicolour LED that blinks red when it’s dark for more than 12 hrs (like on a cloudy day), nudging the user to switch off light in case it is not needed.
    • LCD screen that displays some encouraging message
    • RFID/ biometric system to tag users and switch on light at the doorstep for a known person…etc.

    Refer to the second booklet Investigate in the Solve Ninja Techno Kit for information on which Input sensors and Output devices to use.


    Refer to the third booklet Solve in the Solve Ninja Techno Kit for information on making connections and Arduino IDE.

    Steps to follow

    1. Take the Solution Box from the Solve Ninja Techno Kit.
    2. Connect the distance sensor to the port on the PCB of the solution box using a 4 pin connecting wire.
    3. Connect the bulb/ tube light to the relay screw terminal using electrical wire (refer to the instructions in Solve booklet on how to connect to the relay screw terminal).
    4. Connect one end of the USB cable to Arduino and other to Computer/laptop.
    5. Plug in one end of the power adapter to the DC jack, and the other end to a power socket.

    You will need a 9V 1A power adapter for using this kit. It will not work with the USB cable alone.

    1. Switch on the Solution Box.

    2. Open Arduino IDE on computer/laptop.

    3. Download, Copy, Paste code from here to the Arduino IDE.

    • Click here for the code

      /* Prototype Name : Automatic Light - Distance based
         Input Sensor   : Distance Sensor
         Output Device  : Relay, LCD screen
         Libraries Used : LiquidCrystalPCF584 ,FastLED to turn off
      #include <LiquidCrystal_PCF8574.h>
      LiquidCrystal_PCF8574 lcd(0x27); //Change accordingly
      #include <FastLED.h>
      #define DATA_PIN 9// Digital pin number for LED (NeoPixel)
      #define NUM_LEDS 3
      CRGB leds[NUM_LEDS];
      #define trigPin 5 //Digital Pin 5
      #define echoPin 6 //Digital Pin 6
      int relay_pin = 4; //Digital Pin 4
      long duration, cm, inches;
      void setup() {
        Serial.begin (9600);
        pinMode(trigPin, OUTPUT);
        pinMode(echoPin, INPUT);
        pinMode(relay_pin, OUTPUT);
        FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
        leds[0] = CRGB::Black;
        lcd.begin(16, 2);
        lcd.home(); lcd.clear();
        lcd.print("Auto Lights");
      void loop() {
        digitalWrite(trigPin, LOW);
        digitalWrite(trigPin, HIGH);
        digitalWrite(trigPin, LOW);
        // Read the signal from the sensor: a HIGH pulse whose
        // duration is the time (in microseconds) from the sending
        // of the ping to the reception of its echo off of an object.
        pinMode(echoPin, INPUT);
        duration = pulseIn(echoPin, HIGH);
        // Convert the time into a distance
        cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343
        //inches = (duration/2) / 74;   // Divide by 74 or multiply by 0.0135
        //Serial.print("in, ");
        if (cm > 1 && cm <= 22) {
          lcd.print("Move hand away");
          digitalWrite(relay_pin, LOW);
        else if(cm == 0){
          lcd.print("Move hand close");
          digitalWrite(relay_pin, HIGH);
        else {
          lcd.print("Move hand close");
          digitalWrite(relay_pin, HIGH);
    1. Then Check for Tools → Board → Port in the IDE.

    2. Compile and Upload the code.

    3. Disconnect USB cable.

    4. Switch off the Solution Box.

    5. Switch on the Solution Box.

    6. Test by holding out your hand/ any opaque object and unblocking the distance sensor. The bulb/ tube light should switch on when the object is within the distance specified, and switch off when the object is outside the distance specified.

    7. Hang the Solution Box on the wall at a suitable place. To check if the place is suitable,

    • make sure that the distance sensor is not being obstructed by another object
    • make sure that the object to be measured is within the range of 7m from the distance sensor
    • the bulb wires/power adapter wire should not be too taut.


    Refer to the fourth booklet Share inside the Solve Ninja Techno Kit to know how you can share your solution.

    • Locally: Once you install the solution in a place, observe if and how the user interacts with the automatic light. Talk to them about the solution and learn what they think of it.
      • Are they aware of the automatic light switching on and off based on distance?
      • Is there any way to collect data to prove that their energy consumption has reduced, as a result of automatically switching off the light when not required?
    • Globally: Share your solution on the Solve Ninja App, and inspire 5 of your friends to take similar action. Continue collecting data; real victory is when your family, school mates, community members realise the change in their behaviour!