# Ultra Fast and Simple Colour Sensor

1,991

13

1

## Introduction: Ultra Fast and Simple Colour Sensor

Much colour sensors are expensive or difficult to implement.

I have made a sensor that is very easy to handle ultra fast and ultra low cost.

It uses only a RGB LED and a ultra fast Phototransistor.

## Supplies

1x RGB LED (works best with a LED from a 12V RGB Strip)

1x SFH 320 Phototransistor

2x 470 Ohm resistors

1x 240 Ohm resistor

1x 10 Kohm resistor

wires

Arduino (any 5V model will work)

## Step 1: Make the Circuit

The wiring diagram is very simple.
You can build the circuit on a breadboard, or if you can etch or mill boards blanks, of course, this would be the better option.

The green and blue LEDs need 470 ohms and the red LED 240 ohms. This is because of the different operating voltages of the LEDs. The LEDs must all shine at the same brightness.

The phototransistor is provided with a 10k ohm pull down resistor.

That´s it.

The only thing to remember is that the transistor is placed directly next to the LED

## Step 2: How Does It Work?

As simple as it is set up, it works so easily. The colors of the LED are alternately switched. First red then green then blue. For each of the colors, the reflection intensity is measured.
For example, for a red object, the reflection is stronger for red than for the other two colors. For a purple object, red and blue are more intense than green. All that's left to do is to read in the analogue value via the Arduino's ADC, so you get the RGB values of the object.

See the example of the stuffed animal
in a normal light, all colors are normal. in the case of red light the fur reflects a bit, in green it is almost black and in the blue light it reflects a lot which means that the violet has a high blue content

Why is the sensor so fast? The phototransistor has a response time of 14μs per color which means a response time of 42μs for all colors.

If you calculate f = 1 / t then you will get 23.8 KHz. That means 23800 measurements in one second.

Your ADC is never that fast, he is brought to his performance limit.

However, the best results are obtained by giving the sensor more time to adjust, so in about 1 ms per color, which would still mean 166 Hz.

In order to minimize the noise of the sensor, you should make about 5 measurements and calculate an arithmetic average of these measurements.

## Step 3: Connect It to Your Arduino and Use It

To connect the sensor to your Arduino you should have four free pins.

gnd -> gnd

5V -> 5V

out -> A0

r -> 2

b -> 3

g -> 4

Now copy the code into the Arduino IDE and compile it.

This is a minimalist code without noise reduction (described in step 2).

```#include <arduino.h>
int sensor = A0;
int red = 2;
int blue = 3;
int green = 4;
int colour[] = {0,0,0};
char text[20];
int n =0;
void setup() {
// put your setup code here, to run once:
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(green,OUTPUT);
Serial.begin(9600);
}</arduino.h>```
```void loop() {
// put your main code here, to run repeatedly:
detect();
n=sprintf(text,"r %d g %d b %d\n" ,colour[1],colour[2],colour[3]);
Serial.write(text,n);
delay(1000);
}```
```void detect()  {

digitalWrite(red, HIGH);
delay(1);
digitalWrite(red, LOW);
delay(1);
digitalWrite(green, HIGH);
delay(1);
digitalWrite(green, LOW);
delay(1);
digitalWrite(blue, HIGH);
delay(1);
digitalWrite(blue, LOW);
delay(1);
colour[1]=colour[1]/4;
colour[2]=colour[2]/4;
colour[3]=colour[3]/4;

}```

After the upload to your arduino you have to start the serial monitor to see the RGB values.
The sensor works best with a distance between 0.5 and 1cm. At a further distance, the colors become darker. The sensor is ideal for color detection tasks and line finders. You can paint the PCB and parts matt black to reduce reflections. Do not forget to leave the windows of the components free.

This sensor is not a prototype. The sensor is used in the Student Research Center South Württemberg Germany in the rescue line teams at Robocup German. For this purpose I built an 8-fold sensor with integrated evaluation. He sends the already finished and calibrated RGB values to the central processor.

### Attachments

Participated in the
Sensors Contest

16 3.2K
113 9.2K
73 6.3K
23 3.0K