Introduction: Color Recognition Lock

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.

Materials:

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

Tape

Paperclip

3 x Eye screws

Wood Spacers

9V battery

9V battery connector

Tools:

Soldering Iron

Knife

Hot Glue Gun

Pliers

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.

#include

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.

Comments

author
elizajoe (author)2016-10-11

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?

author

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.

author

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 ?

author

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

author

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?

author

Yes. You need to use the differences.

author
MarkG77 (author)2016-10-18

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

author

Could you be more specific in your question?

author
MisterE1 (author)2016-03-23

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!

author

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.

author

Oh, OK, I will see what I can do.

author
ankur sharma (author)2015-12-26

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

author

That is strange. I am not sure why it is doing that.

author
ankur sharma (author)2015-12-24

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

author

Post a picture of your circuit so that I can see how everything is connected

author
djsfantasi (author)2014-11-05

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

Futaba http://www.fatlion.com/sailplanes/images/futabaconnector.png

Hitec http://www.fatlion.com/sailplanes/images/hitecconnector.gif

JR http://www.fatlion.com/sailplanes/images/jrconnector.gif

Airtronics http://www.fatlion.com/sailplanes/images/airtronicsconnector.gif

author
dortiz15 (author)djsfantasi2015-12-26

tks just saved me =)

author
ankur sharma (author)2015-12-24

pls help

author
ankur sharma (author)2015-12-23

how it works

author

Read through the tutorial. If you have any specific questions, feel free to leave a comment and ask.

author
headslant (author)2015-11-23

Cool. Would you be okay if me and my friend used a slightly modified version of this in a science fair? How should we put you for credit? Is just a link to this page fine, or would you like your name?

author

Feel free to use anything that you like. I don't particularly care about credit. If your science fair requires references, just provide a link to the page.

author
eseixa (author)2015-08-10

Great project!
I liked!

I can use a strong steel to lock the box! And make a a safe box to put a valuables.

Very nice!
And the idea's comming...


"Unfortunately I had a bad experience when I was editing my instructable since the app, and was erased your comment by this cause (It's a bug! I think). I'm very sad for this!"

author
TIL2 (author)2015-03-02

I'm Hatoru. I am very interesting with project color recognition lock. But I want it more secure. I want to build two eyes color recognition..... but I have a problem with the code. Could you help me ? Thks

author

Sorry. That does sound interesting .But at the moment, I am extremely busy and am not able to take on any new projects. Good luck.

author
suvink456 (author)2014-12-25

I made this.... It was awesome and could u please explain how the code works?

Again thanx for the awesome lesson....

author

The Arduino turns on the different color LED one at a time. While each color is on, the arduino measures the about of light that is reflected off the surface using the photoresistor. The Arduino then compares the measured values for each color to the set lock values in the code. To help adjust for changing light in the room, it uses the difference between the colors rather than the absolute values. If the measured values fall within the set sensitivity tolerance, then the Arduino activates the servo and unlocks the box.

author

Yeah! I got it.... but i placed one object in front of the colour sensor and watched the values of the arduino serial monitor..... I got different values for the same object, same room, same background lightning and the same place... here I'll paste some of them...

Green 194

Red 340

Blue 312

Green-Red Difference -146

Green-Blue Difference -118

Lock

****************************************************

Green 292

Red 277

Blue 306

Green-Red Difference 15

Green-Blue Difference -14

Lock

*****************************************************

Green 633

Red 340

Blue 211

Green-Red Difference 293

Green-Blue Difference 422

Lock

******************************************

This is my problem........ plz help me!

author
shreyask1 (author)2014-11-20

helloo......I made this colour recognition lock...and.....the problem is......it opens for any colour........Can anyone help me to make the lock recognize only one colour..???

author

Try adjusting the sensitivity variable in the code.

author

Very much thanxx for the help.......It's working fine now.......can you send me the code to recognize blue colour....PLZZZZ... ;) ...

author

It all depends on the LED that you are using, the light sensor that you are using and the ambient lighting in the room. You just have to use the serial monitor tool in the arduino software to see what values it senses when you hold the blue color up to it. Then change the values of the "GreenRedLock Code" and the "GreenBlueLockCode" in the code to match.

author
shreyask1 (author)2014-11-20

helloo......I made this colour recognition lock...and.....the problem is......it opens for any colour........Can anyone help me to make the lock recognize only one colour..???

author
shreyask1 (author)2014-11-20

helloo......I made this colour recognition lock...and.....the problem is......it opens for any colour........Can anyone help me to make the lock recognize only one colour..???

author
mimhs (author)2014-11-08

I made it ! just nice and easy. I will try to adapted in a geocaching project in the next future. Thank you.

author
spass (author)mimhs2014-11-09

Love geocaching, would be a cool cache of some sort.

author
salmansheikh (author)2014-11-05

how about making it open a door lock..what mods for that?

author

Just mount it to a door or next to a door. Then hook up an electronic door lock and that should work.

author
nickrrr (author)2014-11-04

Great writeup!

However i am still in a pickle with color detection as i am using a FPGA (no analog i/o) and using a ADC would be no simpler than a color to frequency chip :/

author
akshay.d21 (author)2014-11-03

What an amazingly Brilliant Idea ! :)

Thanks for sharing. Love all of your DIY Hacks and How To Tutorials.

Voted ! :)

Warm regards,

Akshay.

author
mikenaly (author)2014-11-03

I would think that you could measure resistance value of the voltage divider with the leds turned off when you initialize the arduino and set it as a reference so that it will completely (or nearly so anyway) ignore the ambient light altogether.

author
Orange Shadow (author)2014-11-03

cool idea

author
Orange Shadow (author)2014-11-03

cool idea

author
Orange Shadow (author)2014-11-03

cool idea

author
Orange Shadow (author)2014-11-03

cool idea

author
seamster (author)2014-11-03

This is brilliant!

About This Instructable

33,606views

473favorites

License:

Bio: My name is Jason Poel Smith I am a Community Manager here at Instructables. In my free time, I am an Inventor, Maker, Hacker, Tinker ... More »
More by DIY Hacks and How Tos:How to Make a Festivus PoleAdd Wings to an Infant's Halloween CostumeBubble Bath That Never Runs Out Of Bubbles
Add instructable to: