Unlock Your Door with Li-Fi Technology: Build a Smart Lock Using Arduino and Smartphone Flashlight

Parth ChauhanParth Chauhan
3 min read

Introduction

Li-Fi (Light Fidelity) is a wireless communication technology that uses visible light to transmit data. Unlike Wi-Fi, which uses radio waves, Li-Fi relies on light signals, making it a secure and fast alternative for data transmission. In this project, we will build a Li-Fi smart door lock that unlocks when a smartphone flashlight blinks in a specific pattern. The Arduino reads the blinking light using an LDR sensor, decodes the pattern, and unlocks the door if the password matches.

How It Works

  1. The Android app blinks the flashlight in a binary pattern (e.g., 1010).

  2. The LDR (Light Dependent Resistor) detects the light changes.

  3. The Arduino decodes the pattern and checks if it matches a predefined password.

  4. If the pattern is correct, the servo motor rotates to unlock the door.

Components Required

  • Arduino Uno/Nano

  • LDR Sensor (Light Dependent Resistor)

  • 10K Ohm Resistor

  • Servo Motor

  • Smartphone with Flashlight

  • Android Studio (for the app)

  • Arduino IDE

Circuit Diagram

Connect the components as follows:

  • LDR Sensor

    • One terminal to 5V

    • Other terminal to A0 (Analog Pin) and GND via a 10K resistor

  • Servo Motor

    • VCC to 5V

    • GND to GND

    • Signal to Pin 9

Android App Code (Flashlight Blinker)

This app will blink the smartphone flashlight in a predefined binary password pattern.

Step 1: Add Permissions in AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera.flash"/>

Step 2: MainActivity.java

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private CameraManager cameraManager;
    private String cameraId;
    private String binaryPassword = "1010"; // Flashlight pattern

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button sendSignalButton = findViewById(R.id.sendSignalButton);
        cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);

        try {
            cameraId = cameraManager.getCameraIdList()[0];
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

        sendSignalButton.setOnClickListener(v -> sendLiFiSignal());
    }

    private void sendLiFiSignal() {
        new Thread(() -> {
            try {
                for (char bit : binaryPassword.toCharArray()) {
                    cameraManager.setTorchMode(cameraId, bit == '1');
                    Thread.sleep(500);
                }
                cameraManager.setTorchMode(cameraId, false);
            } catch (CameraAccessException | InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}

Arduino Code (LDR Detection & Door Unlocking)

This Arduino script detects the flashlight pattern and unlocks the door.

#include <Servo.h>

Servo doorLock;
const int ldrPin = A0;
const int threshold = 700;
String receivedCode = "";
String correctCode = "1010";

void setup() {
    Serial.begin(9600);
    doorLock.attach(9);
    doorLock.write(0);
}

void loop() {
    int lightValue = analogRead(ldrPin);
    Serial.println(lightValue);

    if (lightValue > threshold) {
        receivedCode += "1";
    } else {
        receivedCode += "0";
    }

    if (receivedCode.length() >= correctCode.length()) {
        if (receivedCode == correctCode) {
            Serial.println("βœ… Access Granted!");
            unlockDoor();
        } else {
            Serial.println("❌ Access Denied!");
        }
        receivedCode = "";
    }

    delay(500);
}

void unlockDoor() {
    doorLock.write(90);
    delay(5000);
    doorLock.write(0);
}

Testing and Debugging

  • Run the Android app and check if the flashlight blinks in the correct pattern.

  • Open Serial Monitor in Arduino IDE to verify LDR readings.

  • Adjust the threshold based on ambient light conditions.

  • If the door does not unlock, check wiring connections and LDR sensitivity.

Future Enhancements

  • Custom Password Input: Allow users to enter a custom binary password.

  • Li-Fi Signal Encryption: Improve security with Manchester encoding.

  • Wi-Fi Connectivity: Integrate ESP8266 for remote access.

Conclusion

This Li-Fi Smart Lock project demonstrates a unique way to use visible light communication for secure door access. By combining Arduino, LDR sensors, and a smartphone app, you can create an innovative and efficient wireless security system. πŸš€

Would you like me to help with UI design or security enhancements for the project? Let me know in the comments! 😊

1
Subscribe to my newsletter

Read articles from Parth Chauhan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Parth Chauhan
Parth Chauhan

I'm Parth Chauhan, a versatile web developer with expertise in both frontend and backend technologies. My passion lies in crafting clean, minimalistic designs and implementing effective branding strategies to create engaging and user-centric experiences. I am proficient in Java, Python, C, C++, SQL, MongoDB, computer networks, OS, NoSQL, and DSA. I thrive on bringing ideas to life by building solutions from scratch and am always excited to explore and master new technologies. My approach is centered around coding with precision and creativity, ensuring each project is both functional and aesthetically pleasing. Currently, I'm open to freelance opportunities and eager to collaborate on new ventures. Let’s connect to discuss how we can work together to enhance your project and achieve outstanding results.