You are on page 1of 14

Color sensor

I built this color sensor to equip my third implementaton of brick sorter. Michael Gasperi presents such a sensor in an article of Nuts and Volts magazine. His circuit is based on an input multiplexor. Three CdS photo-resistors, connected to this multiplexor, receive light through RGB colored filters (cleverly done with Lego transparent bricks). This one takes a somewhat opposite route: a multiplexor (similar to Michael's) drives three RGB leds that cast light onto the object. Reflected light is measured by a single photo-resistor. Another (non-Lego) home built color sensor, based on the same idea, can be found in David Cook's robotics pages.

Circuit analysis
The heart of this circuit is a CD4017 (IC1), CMOS counter with decoded output. On each pulse received on its clock input, this IC drives high the next output (all the other are driven low).

Output 0, 1 and 2 drives red, green and blue LEDs through transistors Q2, Q3, Q4 (emitter follower is used to avoid the need of a base resistor). During the first three phases, output 4 is driven low and provide ground reference for photo-resistor R8: the RCX can read light level reflected in each color. In phase 4 photo-resistor is no longer grounded, value returned to RCX is maximum (note the 1 Mohm resistor paralleled with R8 to prevent maximum value to be reached in phases 1/2/3). RCX can detect this special condition and synchronize its readings to CD4017 state.

The oscilloscope hard-copy shows clock pulse on top trace, output 1/2/3 below. To generate a clock pulse, you have to switch from active sensor mode (sensor voltage is higher than Zener D4 threshold, Q1 is conducting, clock input is low) to passive sensor mode (sensor voltage is lower than D4 threshold, Q1 is blocked, clock input is high) and back to active sensor. C1 filters out short power supply pulses occuring in active mode evey 3ms while RCX reads value. The oscilloscope hard-copy shows clock pulse on top trace, sensor voltage on bottom trace showing 2 short reading pulses and wide command pulse generated by sensor mode switching. When reaching step 5, CD4017 activates D04 and reset itself to step1 through R4/C3. This RC network delays reset pulse well after clock transition: without it, the current rush of the LED, coupled through power supply, modifies clock threshold and CD4017 goes directly to step 2... Power supply is rectified by D1 and filtered by C2. As in my other sensors, I keep on with single diode instead of full bridge to minimize components count (3 diodes instead of 8!) but as you can see I built the breadboard with a full bridge module I constructed to ease my prototypes.

The power-supply module I use for breadboards.

The breadboard

Component selection
Nothing is really critical: D1 to D3 are small signal diodes (i.e. 1N4148), Q1 to Q4 are general purpose NPN transistors (BC548, 2N2222...). Photo-resistor I used is

a MPY54C569 (any similar size LDR will probably work fine). Leds are high efficiency 3mm ones. I used Agilent: red HLMP-K105 for D5, blue HLMP-KB45 for D8, green HLMP-1540 for D6 and D7. I was somewhat disappointed by green Leds efficiency. Though the photoresistor has its peak sensitivity around green wavelength, read value was fairly low with green lighting. After testing different Led types and brands, I finally chose to use two green leds with maximum current. Even then color sensor sometimes has troubles to distinguish between green and black (!) Lego bricks... Green bricks reflects surprisingly very little light, I guess the problem is that my green leds have a too yellowish wavelength. Most of the components were bought from Farnell. Led current is limited by R5, R6 and R7. To set their values, I placed the breadboard in front of a white piece of paper and adjusted them to get about the same reading for all colors.

Building color sensor: photo gallery

Everything fits in a block composed of four 4 x 2 hollowed out bricks

I used two right-angle mounted perf. boards

The main board receives power supply, CD4017 and clock

The other one receives Leds and photoresistor on one side, transistors and resistors are SMD mounted on the other side to keep size down. Photoresistor is shielded from Leds direct light by heat-shrink tubing. (I used white tubing for leds hoping to get a few

generation. It uses standard through-holes more photons...) components. Leds are slanted as much as possible towards the center .

Test programs
test programs (requires RCX 2.0 firmware that you can get here)

test_color_sensor.nqc: displays 3 digits on LCD, corresponding to RVB reflected levels.

Real applications

Brick Sorter 3: see it here ! Color follower: when I was a Mindstorms newbie, I tried the roverbot-andlight-sensor line follower on my tiled floor. Unfortunately there is not enough contrast between tiles to be able to get a reliable working. With the color sensor, the difference between blue and red channels enables to tell blue from grey tiles and is relatively independent of ambient light level. The robot must be slow though because of the time it takes to cycle through color sensor states. Get NQC program for the color follower.

Color follower on tiled floor

QuickTime movie of the color follower (200K)

Author: Giorgos Lazaridis

Light / Dark Activated Relay


A Light / Dark activated switch is a circuit that will somehow measure the light level and will turn on or off a relay accordingly. We will use

an LDR (Light Depended Resistor) to measure the light level. Also, we will not demonstrate only one circuit but instead, three circuit will be put under the microscope. Each one will have different characteristics but the operation will be the same.

The first circuit - The blind man's sensor

The circuit assembled on a breadboard

The circuit is is a simple transistor switch with the base of the transistor connected to a voltage divider. The voltage divider has two resistors. The first is the 100K potentiometer plus the protective 1K resistor. the second resistor is the LDR. This is the schematic of the circuit:

As light falls on the surface of the LDR, the LDR changes it's resistance. The more the light, the less the resistance of the LDR, the less the resistance, the less the voltage drop across it. The less the light, the more the resistance and thus the more the voltage drop across it. As the voltage drop increases, so does the VB of the 2N2222 transistor and therefore the ICE increases accordingly, until the time that the current is enough to actuate the relay. The amount of light needed to actuate the relay can be changed by changing the 100K potentiometer. Basically, any change to the potentiometer will have an effect to the voltage drop of the LDR, as they are both members of the voltage divider described above. The 1N4001 diode is used to eliminate any back voltage when the relay is disarmed. It is very important to have this diode because without it, the transistor may be damaged.

The second circuit - Increased sensitivity

This has a better sensitivity than the previous

The above circuit works fine as far as the activation is concerned. There will be no problem to detect dark or light and actuate the relay, but there will be a problem when the relay needs to be released back again. At this point, the circuit has a big hysteresis. therefore, we need to further amplify the signal before we apply it to the switching transistor. We will use the BC517 NPN Darlington pair transistor. We will put it between the 2N2222 and the LDR, as the following circuit indicates:

With this addition, the sensitivity of the circuit is further increased. The hysteresis window is significantly decreased, although there is still a region that when the relay is activated, it will not be deactivated with the same amount of light that existed just before it's activation.

Selecting different parts

The above circuits may work with different voltage and/or parts. For example, you may change the voltage to 5 volts, but you should then consider changing the 1K resistor into 560 Ohms, the potentiometer into 10K and the relay of course must have the appropriate coil voltage. You may use any kind of NPN transistor for switching the relay, as long as it is capable to work under your selected voltage and also be able to provide enough current (ICB) for the relay.

Adjusting the circuits

Only one adjustment needs to be made and that would be (of course) the potentiometer. Your goal is to make the circuit actuate the relay when you have equal or less light to the pre-defined value. The easiest way to do this is as follows: Let the LDR be lighted with the amount of light you want. Keep the potentiometer into it's highest value. Then start slowly turning the potentiometer and reducing it's resistance. When you hear the 'click' of the relay, you have found your set point. From then on, every time the light is less or equal (or more if the circuit is configured as "light activated") to the light that you made this pre-setting, the relay shall be activated.

Convert into light detectors

The above circuits operate as dark detectors. This means that when the light level falls under a preselected value (read previous paragraph), the relay is actuated. In case you want a light detector that will actuate the relay when the light level is increased above the preselected value, just remove the protective 1K resistor and switch places between the LDR and the potentiometer, readjust and that's it.

The third circuit - Sensitivity to higher levels!!!

The next circuit has nothing to do with the above. It uses a 741 opamp to achieve maximum sensitivity. This circuit can sense very slight light changes and can be really fine adjusted. Let's take a look at the circuit:

This circuit has so much sensitivity and so low reaction time, that is sometimes improper to be used

As you may have notice, the 741 is connected as a voltage comparator. Two voltage dividers are easy to be found: The first one is the LDR and the 100K resistor. The second one is composed by the two 470 Ohms resistors and the potentiometer. Both the outputs of the dividers are connected as inputs to the voltage comparator.

The second voltage divider will settle the reference voltage. The first voltage comparator that contains the LDR, will change it's voltage according to the light level. When the voltage across the negative input of the comparator is less than the voltage to the positive input of the comparator, the output is held low. When the voltage on the negative input rises, there will be a time that it becomes greater than or equal to the positive (pre-selected) voltage, and then the output becomes high and the relay through the 2N2222 is actuated.

Selecting different values

As long as the transistor is concerned, any NPN switching transistor capable to drive your relay will do. As for the LDR, you need to make sure that it pairs with the 100K resistor. This means that the mid-value of the LDR is almost the same as this resistor. Any pair will work theoretically, but i have not test others than this pair. If you have problems please let me know. The circuit is designed to work with 12V, but it can operate in lower voltages as well, as long as you make sure you select the right relay for the occasion.

Convert into light detector

This circuit, just like the previous two circuit, operate as dark activated switch. If you want to change the functionality of this circuit, simply exchange places between the 100K resistor and the LDR.

Which one to use?

This section could also be named "advantages and disadvantages". But i chose this name as my goal is to help you find the proper circuit for each occasion. Also, it would be unfair for a circuit to name advantages or disadvantages in it's name, as there are actually none! Instead, there is proper and improper use and/or application for each one of them.

Starting with circuit #1. This is a very easy and cheap circuit. Excluding the relay, it would cost about a Euro or less. This circuit is proper for detecting large light changes. I would use it for example if i wanted to detect the light in my room or in a hall if it works or not. Small changes like shadows and staff does not affect this circuit and thus it gives a straight answer to the question: - Is the light turned on? Is my car's rear stop light working? The second circuit on the other hand is much more sensitive to changes. The Darlington pair transistor will significantly increase the slight current changes from the LDR. Still there is a big window between activation and deactivation of the relay. This makes it ideal for outdoor uses to detect if there is ambient light. It could be perfect for example to control your automatic lights. It will not be affected by shadows from a bird flying against the sun or a cat is passing near by the sensor trying to catch this bird. Or even the human with larger shadow area, that tries to save the bird from the cat. Nevertheless, it will be accurate as far as the light level detection is concerned. The automatic lights shall indeed be turned off when the sun start shining the day. The third and last circuit is the most accurate and the most sensitive. If for example a shadow falls and covers the 2/5 of the LDR it may not actuate the relay, but if the shadow covers the 3/5 it may actuate it. Small light changes may result into relay state change. This makes it completely inappropriate for the pre-mentioned applications. It would be very good in human detection from light level changes. For other applications, you should consider adding a delay circuit at the output of the 741. If the light level is very close to the preselected value, the relay will flicker due to the almost zero light level window that with this circuit is accomplished. It would also work very well as light signal receiver.

You might also like