Color Recognition Lock




About: My name is Jason Poel Smith. In my free time, I am an Inventor, Maker, Hacker, Tinker, and all around Mad Genius

There are a lot of ways that you can activate an electronic lock. You can use passwords, radio signals, or even voice commands. In this project, I am going to show you how to make a lock box that opens and closes based on color recognition.

With a color recognition lock, anything can be a key. You can set the lock to recognize the color of a cereal box, or the cover of your favorite book. You could even use pictures on your phone as the key. With so many different color combinations, a color lock is also very difficult to break into.

Step 1: Watch the Video

Here is a video walkthrough of the project.

Step 2: Background: How Color Recognition Works

White light is composed of all the colors of the rainbow. When it hits a surface, some of the colors are absorbed and some of the colors are reflected. The reflected colors are what we perceive as the color of that object.

In order to measure and quantify color with an electronic circuit, you need to measure the intensity of the various wavelengths of light that are reflected off the surface. The easiest way to do this is to shine a single color of light on that surface at a time, and then measure the total intensity of the reflected light.

At a minimum, you need to use each of the primary colors of light (Red, Blue and Green). So a basic color sensor will have a red LED, a blue LED, a green LED, and light sensor such as a photoresistor. By measuring the reflected light for each color, you can calculate the color of the object.

Step 3: Materials

Here are the materials and tools that you will need for this project.


Arduino microcontroller

Wooden Box

Full Color LED (or separate red, blue and green LEDs)

Photoresistor (light dependent resistor)

2 x 51 ohm resistor

120 ohm resistor

100 kohm resistor

Perfboard (or other printed circuit board)

Servo motor

Jumper wires

Paper or cardstock



3 x Eye screws

Wood Spacers

9V battery

9V battery connector


Soldering Iron


Hot Glue Gun


Drill and bit set

Small wood saw

Step 4: Construct the Color Sensor

The color sensor has two basic parts. There are LEDs that emit red, blue and green light. There is also a light sensor that detects the intensity of the light.

For the LEDs I used a single full color LED that can emit all three colors. This is basically four LEDs in one. It has four leads. Each color has its own lead for its cathode (negative side). The forth lead is a common anode (positive lead) for all three LEDs. I used markers to color code the leads to help me keep track of them.

For the light sensor I used a photoresistor (light dependent resistor). This is a resistor that changes its resistance depending on how much light hits the front face.

In order to use these parts as a color sensor, you just need to connect a couple of resistors and wires. Each color LED needs its own series resistor to bring the voltage down to the appropriate level. I used a 51 ohm resistor for the blue lead, another 51 ohm resistor for the green lead and a 120 ohm resistor for the red lead. The photoresistor also needs a series resistor so that you can make a voltage divider and measure the output of the sensor. The value of this resistor will depend of the specific photoresistor that you are using. For mine, I used a 100 kohm resistor. I soldered all the parts onto a perf board to hold them together.

Step 5: Add a Light Shield Around the Color Sensor

We want the photoresistor to only measure light from the LEDs. So we need to block out as much ambient light as possible. To do this, I made a simple light shield for it.

Take a small piece of card stock and fold it so that it can wrap around the sensor. Cut slots in the bottom of the card stock to make tabs that can fold under the circuit board. Wrap the card around the color sensor and tape all the sides to hold it in place. This won't block 100 percent of the light but it will drastically improve the performance of the sensor.

Step 6: Connect the Color Sensor to the Arduino

Now you are ready to connect the sensor to your Arduino. First connect the LED. Connect the three cathode leads of the LED to digital pins 8, 9, and 10. Then connect the common anode of the LED to the 5V pin.

Next connect the light sensor. Connect the free lead of the photoresitor to the GND pin. Connect the free pin of the fixed resistor to the 5V pin. Lastly, connect the center pin between the two resistors to the analog input pin 0.

Step 7: Attach a Servo to the Arduino

I decided to use a small servo motor to open and close the lock. This can be connected directly to the Arduino. To attach it, connect the brown wire on the servo to a GND pin on the Arduino. Then connect the red wire on the servo to the 5V pin on the Arduino. Lastly connect the orange wire on the servo to digital pin 6 on the Arduino.

Now when you run the code, you should see the servo actuate whenever you place the appropriate color in front of the senor.

Step 8: Sample Arduino Code

//Here is some sample Arduino code that you can use.

//You will need to adjust some of the values to calibrate it to your setup.


Servo myservo; // create servo object to control a servo

int GreenLedPin = 8; // Green LED connected to digital pin 8 int RedLedPin = 9; // Red LED connected to digital pin 9 int BlueLedPin = 10; // Blue LED connected to digital pin 10

int analogPin = 0; // photoresistor connected to analog pin 0 int GreenVal = 0; // variable to store the value of reflected Green light int RedVal = 0; // variable to store the value of reflected Red light int BlueVal = 0; // variable to store the value of reflected Blue light

int GreenRedDifference = 0; int GreenBlueDifference = 0;

int GreenRedLockCode = -31; // lock value int GreenBlueLockCode = 47; // lock value

int sensitivity = 5; // set sensitivity of the color sensor

void setup() { myservo.attach(6); // attaches the servo on pin 6 to the servo object

Serial.begin(9600); // setup serial pinMode(GreenLedPin, OUTPUT); // sets the digital pin as output pinMode(RedLedPin, OUTPUT); // sets the digital pin as output pinMode(BlueLedPin, OUTPUT); // sets the digital pin as output} } void loop() { delay(1000); digitalWrite(GreenLedPin, HIGH); // sets the Green LED off digitalWrite(RedLedPin, HIGH); // sets the Red LED off digitalWrite(BlueLedPin, HIGH); // sets the Blue LED off delay(1000); // waits for a second digitalWrite(GreenLedPin, LOW); // sets the Green LED on delay(100); GreenVal = 1023 - analogRead(analogPin); // read the input pin Serial.println(); Serial.print("Green "); Serial.println(GreenVal); // debug value delay(1000); // waits for a second digitalWrite(GreenLedPin, HIGH); // sets the Green LED off delay(1000); // waits for a second

digitalWrite(RedLedPin, LOW); // sets the Red LED on delay(100); RedVal = 1023 - analogRead(analogPin); // read the input pin Serial.print("Red "); Serial.println(RedVal); // debug value delay(1000); // waits for a second digitalWrite(RedLedPin, HIGH); // sets the Red LED off delay(1000); // waits for a second

digitalWrite(BlueLedPin, LOW); // sets the Blue LED on delay(100); BlueVal = 1023 - analogRead(analogPin); // read the input pin Serial.print("Blue "); Serial.println(BlueVal); // debug value delay(1000); // waits for a second digitalWrite(BlueLedPin, HIGH); // sets the Blue LED off

GreenRedDifference = GreenVal - RedVal; Serial.print("Green-Red Difference "); Serial.println(GreenRedDifference); // debug value GreenBlueDifference = GreenVal - BlueVal; Serial.print("Green-Blue Difference "); Serial.println(GreenBlueDifference); // debug value

if((abs(GreenRedLockCode-GreenRedDifference) < sensitivity) && (abs(GreenBlueLockCode-GreenBlueDifference) < sensitivity)) //compare measured color value to code value { Serial.println("Unlock"); // unlock the box myservo.write(45); } else { Serial.println("Lock"); // lock the box myservo.write(135); }

delay(2000); }

Step 9: Set Up the Locking Mechanism Inside the Box

To lock the box, I am using a three eye screws and a locking pin. Two eye screws are mounted to the inside of the front panel of the box. A third eye screw is mounted to the inside of the lid of the box. When the lid is closed, the holes of the eye screws line up. The box is locked by inserting a pin through all three holes while the box is closed. The box is unlocked by removing the pin.

The pin is a simple piece of steel wire (straightened paperclip). It is moved by the servo. The wire is attached to the servo at one of the holes on the servo horn (actuator arm).

To set the servo at the right height, I mounted it on top of several wooden spacers. I carefully lined up all the parts and glued them in place.

Step 10: Cut a Hole for the Color Sensor

There are several modifications that we need to make to our wooden box. First we need to cut a hole in one side for the color sensor. I decided to mount the color sensor to the bottom of the box. That way it is less conspicuous.

I started by drilling small holes through the bottom panel. Then I used a small saw to carefully cut through board. I then used a small file to smooth off the edges.

Step 11: Mount the Rest of the Components to the Inside of the Box

Now we need to mount the rest of the components to the inside of the box. Position the color sensor in place. Then apply hot glue around the color sensor on both sides of the bottom panel. Then mount the Arduino board and a 9V battery to the inside of the box with hot glue. If the wires are too jumbled up, you can group them together with tape or zip ties or glue them to the side of the box with more hot glue.

Step 12: Add a Power Switch to Save Batteries

To conserve battery life, I added power switch to bottom of the box. This is a simple momentary switch that connects the 9V battery to the Vin pin on the Arduino. When the button is not being pressed, the power is disconnected so that the battery won't run out of juice while the box is closed.

Alternate design: Another way to manage the power is to route the 9V connector cable outside the box. That way it will never run out of power with the box closed. By disconnecting the 9V battery, It can also add a step in the unlocking process. Then you will need both the color code and a 9V battery.

Step 13: Use Your New Color Lock Box

To set the color lock, open up the Arduino code and set the "GreenBlueLockCode" and "GreenRedLockCode" values. You can see the values for a given color using the Serial Monitor tool. Once, you have the color code set, try it out a few times with the box open to make sure that everything is working properly. Then disconnect the Arduino from your computer and connect the 9V battery.

When you hold up the appropriate color, the box should unlock. An incorrect (or no color at all) will cause the box to lock. Have fun and enjoy your new color activated lock box.

Tech Contest

Participated in the
Tech Contest

Microcontroller Contest

Participated in the
Microcontroller Contest

Make it Glow!

Participated in the
Make it Glow!



    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest
    • Colors of the Rainbow Contest

      Colors of the Rainbow Contest
    • Classroom Science Contest

      Classroom Science Contest

    46 Discussions


    2 years ago

    Hi! I'm trying out your project but I am stuck on the part where I set up the lock. The values are all similar. Do I use the color differences as the values to set the lock?

    5 replies

    You will need to calibrate it for the lighting in your room. If there's too much light, it can saturate the sensor and cause problems.

    MarkG77DIY Hacks and How Tos

    Reply 2 years ago

    hi sir plss help me. how to make the sensor only recognize your desired color .. it all opens in all colors RGB How to know 1 color ?

    DIY Hacks and How TosMarkG77

    Reply 2 years ago

    You are probably getting too much light from the room to the sensor and it is saturating the output.


    Thanks, I will try that. Once I calibrate it, I should use the Green-Red Difference value and the Green-Blue Difference value to set the lock, Correct?


    2 years ago

    hi sir can you plss help me im done making it but how it works ?

    1 reply

    3 years ago

    Hey! Great project. I am planning to build this one in the future. My only worries are regarding the fotoresistor value. How should I calculate the value of the resistor in series with the photoresistor? Is there a certain output voltage necessary between the two resistors? Which value of the fotoresistor did you use?

    Thankyou very much in advance for clarifying!

    2 replies

    The biggest problem with photo resistors is that they aren't standardized and you can't buy specific values. It is kind of random. So you need to do a little trial and error.

    ankur sharma

    3 years ago

    it works
    but whenever I connect it first opens the lock then after 2 ~3 seconds of locked

    1 reply
    ankur sharma

    3 years ago

    it's not working
    I am using 100k resister in place of 10k
    my ldr is comparatively smaller than yours

    1 reply

    4 years ago on Step 7

    Note that different servos have different color coding schemes for their wires.





    1 reply