There are a lot of ways 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.

Project Steps

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 a light sensor such as a photoresistor. By measuring the reflected light for each color, you can calculate the color of the object.

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, and 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 51ohm resistor for the blue lead, another 51ohm resistor for the green lead, and a 120ohm 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 100kohm resistor. I soldered all the parts onto a perf board to hold them together.

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.

Connect the Color Sensor to the Arduino

Now you are ready to connect the sensor to your Arduino. First 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.

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.

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); }

Set Up the Locking Mechanism Inside the Box

To lock the box, I am using 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.

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, so 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.

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 and apply hot glue around it 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.

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.

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 color (or no color at all) will cause the box to lock. Have fun and enjoy your new color activated lock box.