High-performance Color Detector for Under 1$

196

2

1

Posted in TechnologySensors

Introduction: High-performance Color Detector for Under 1$

Introduction

This project is the first part of a larger project to build an electronic chess board, allowing for 1 vs 1 or 1 vs computer. The goal is that the board can light the chess square for allowed chess moves and make suggestions. With this in mind, there is a need for an RGB LED on each square of the board for UI purposes. There is also a need to build a system for detecting which pieces are where on the board. After exploring many options, I settled on an optical principle with using an RGB LED and a single phototransistor. Turning on individual colors in sequence and taking analog reading from the phototransistor detection circuit, you can then develop an algorithm for color detection.

Phototransistor vs photocell

In this particular project, I decided to use a phototransistor

as opposed to a photocell which is more common amongst hobbyists. There are 3 types of components that can be used to sense light:

  1. A photocell (also called photoresistor) is light variable resistor. The most commonly available type is the CdS photocell which contains Cadmium and damages the environment. Also, photocells are mostly available in through-hole package and are rather large which would make the integration difficult in the chess board.
  2. A photodiode is a special diode that the reverse characteristics change with light conditions. Since a photodiode has a very small signal amplitude, you need a Op Amp to amplify the signal, which would make the cost of the device explode.
  3. A phototransistor is a simply NPN transistor, with the base being activated by incoming light. This is widely available in surface mount package and has sufficient signal amplitude to be used directly with a microcontroller.

Step 1: Step 1: the Sensing Circuit

The circuit

Don’t expect a very complicated circuit here, especially with the 1$ target price.

Component Selection

The first step is to select this RGB LED. Requirements are as follow:

  • RGB in a single package.
  • Forward voltage below 3.3V
  • Widely available
  • Lost-cost
  • Built-in diffuser (for blending the colors together to create the UI)
  • Surface-mount (to have the smallest PCB)
  • Easy to solder by hand

The LED selected for this specific project is the Cree inc. CLV1L-FKB, which meets all these requirements and is available for a whopping ~30 cents, even in small quantities. For the LED, it is easier to select an LED that is more powerful than required and then reduce the light intensity afterwards.

Then we need to select the phototransistor. Requirements are as follow:

  • Sensitivity in the light spectrum of the selected LED
  • Collector-Emitter Breakdown voltage above 3.3V
  • Widely available
  • Lost-cost
  • Surface-mount (to have the lowest cost PCB)
  • Easy to solder by hand

The phototransistor for this specific project is the Everlight Electronics PT26-21C/TR8, which meets all these requirements and is available for ~40 cents in single piece quantity, but with price dropping rapidly below 30 cents in quantities of 10 or more. This is ideal for this project, because the end goal is to build 64 chess color-detecting light squares

Resistors and capacitor can be selected based on cost-alone. I always like to go with X5R or X7R capacitors, and purchase passives in the 0805 package which is easy to solder by hand. People experienced in hand-soldering can further reduce costs by using 0603 or 0402 resistors and capacitors.

Step 2: Step 2: Calibrating the Phototransistor

Test PCB

This is where the project gets tricky. I made a test PCB by placing phototransistors at different distances from the LED. There are many cheap board houses in China with discounted fast shipping to US. I used the website https://pcbshopper.com/ to find the best deal at the moment, which turned out to be around ~15 USD.

Fortunately, with this LED and phototransistor combo, the cross sensitivity of the LED shining directly on the phototransistor is very reasonable (could be because of the lens on the phototransistor, but this manufacturer does not specify the viewing angle), even in the closest configuration that I am willing to hand solder two parts next to each other which is just about 3mm. This will allow for a compact color detector that can be easily integrated in the final chess board.

Note that these is an extra IC in the top left corner which I didn’t talk about. At first, I wasn’t certain if I could distinguish 12 different colors reliably, so I planned on carefully placing magnets and a using hall effect sensor to detect black and white pieces using north and south poles. This turned out to be unnecessary, further reducing the cost of the build. A pure optic solution was chosen which makes it easier to use off-the shelf chess pieces, simply replacing the felt underneath with different colors for different pieces. More on that in Part 2 of the project write-up

The first this I did after validating that my circuit works is to build an actual test setup. Since the minimum order quantity for the PCBs is 5, but I only needed 1, I used the extra PCBs as a shim and drilled a hole on top to place my piece of felt.

Tests at 100% intensity

The first test is to verify that we have enough signal amplitude to actually measure the color. In this test I simply turned on the LED at 100% intensity and measured the output using the ADC of the chosen SAMD21 microcontroller (more on the microcontroller in part 2). I quickly discovered that this saturates the output of the ADC for the white color which is a good thing since we can dim the LED by using PWM in the microcontroller code.

The microcontroller has a range of VCC / 1.48. which comes down to 2.23V when using 3.3V as a supply rail. As you can see in the picture with the LED at 100% intensity, the voltage is 3.1V which is outside of the range my ADC.

Then I test the black color at 100% LED power to see if we had enough amplitude for a measurement of the black color as well. Of course, in the ideal case, I would like to use a single pulse to detect all the colors, but in case I do not have enough dynamic range I can always start at 100% intensity and reduce it in further readings after we eliminate the dark colors.

It turned out that the even the black color gave me a good signal amplitude, which gives me confidence that we can make seriously performant sensor with this setup.

This is a good place to mention that the screenshots above come from the Saleae Logic software which is tied to the Logic 8 analyzer that I am using. More information on Saleae on their website: https://www.saleae.com

I used the logic analyzer for debugging my PWM function, adjusting the signal amplitude and to quickly select the felt colors with the analog sampling. For this project it was really useful because it so easy to use. A single measurement with an oscilloscope may take 5 minutes of adjustments, whereas it only takes 10sec to configure the logic analyzer so you get a lot more work done on a weekday night. Because it replaces an oscilloscope for the majority of hobby projects, and is far superior to the bus pirate and the likes as a logic analyzer, I would recommend it to anyone doing electronics as a hobby.

Adjusting the PWM function

At this point in the project it was clear to me that the light needed to be dimmed in order to get the reading the ADC range of my microcontroller. My initial test was using software PWM that did not yield so good results.

The software PWM had a limited frequency and causes a lot of noise in the signal. I tried to fix this using an analog filter, but that slowed down the response time too much. Anyhow after figuring out how to enable true hardware PWM on this microcontroller, I found that the best compromise was to use an 80kHz PWM, with an RC circuit of 10k ohm and 0.1uF on my phototransistor. With the microcontroller running at 48Mhz, even at that speed my PWM can be adjusted in increments of 1%. This gave me a nice and noise-free signal. I started testing with 33% light intensity on all channels.

Calibrating for sensitivity

Now the whole idea behind this project is to accurately detect color for cheap, but actually when placing a white piece of felt, I get a very different signal on the 3 channels, when I would want them to be pretty much the same.

This can be caused by a variety of reasons, split down into 2 categories:

  • Different amount of light emitted
    • LED current limiting resistors manufacturing tolerances
    • LED forward voltage manufacturing tolerances
  • Difference sensitivity to light received
    • Phototransistor different sensitivity at different wavelengths
    • Phototransistor manufacturing tolerances
    • Phototransistor load resistor manufacturing tolerances

Now of all the reasons above, my best guess is that the sensitivity to different wavelength is not the same which causes the majority of the effect, and that all the manufacturing tolerances could be neglected. When I make 64 more, I will update this post with the answer and solution. It could be that each cell will require a 2-point calibration (black, white) instead of the 1-point calibration I am currently planning on using (black) or even no calibration at all if all the components are nicely matched.

I set out the goal to calibrate the intensity so that the white color use 75% of the ADC range for all three colors which is about 1.67V. While adjusting the PWM outputs, I discovered that the response is very linear for all 3 colors which was a nice surprise and the first indicator that it would not only work be also be really high performance.

Step 3: Step 3: How to Use It

Selecting the colors

With that all setup I purchased a kit of 40 assorted felt colors and placed them one after the other on the detector and recording it with the logic analyzer. I then tried to identified 12 pieces that would look drastically different and that I could easily distinguish in software for my chess board. One interesting thing to note here is that the color detector could be transformed in a rudimentary 1-pixel camera, but my end goal was color detection, not reproduction. Here are the final color selection and their respective output.

Here are some of the output when read in RGB sequence:

One interesting thing to point out is that Pale Yellow and Dark Yellow may seem difficult to distinguish in real life, they have very different ratio of blue reflected which makes them easily distinguishable by the microcontroller.

Seeing it in action

In this writeup I focused mostly on the color sensing aspect, but since the phototransistor is detecting visible light, it makes for a very good ambient light sensor. Using it as such I can also add a 4th channel which is when there are no LEDs lit at all and use that to detect if there is a piece is placed on a certain square or not. Only then will I fire the LEDs to detect the color, which means the user will never notice the light shining underneath the chess piece.

I wrote a small test program which automatically detects the color of the felt when the piece is placed on it. See it in action in the video below. (I actually slowed down the detection for the camera. The real detection takes about 30ms at the moment, but could be further optimized)

Step 4: Conclusion

Conclusion

To wrap up, if you are willing to put some time in building it yourself, you can design and build a very cheap high-performance color detector. For the purpose of the electronic chess board project, this little 1$ circuit acts as both the user interface, a piece detector and automatic adjustment of light intensity depending on where the board is played.

Since I promised a 1$ sensor, here is a breakdown of all the costs to fill-up a chess board with 64 sensors. Note that due to ordering quantities, there is an extra 9 boards in case nothing gets damaged during hand assembly.

Cost breakdown for 64 boards:
75 pcs 10mm x 10mm 1-layer PCB           17.00 USD
100 pcs Phototransistor                  14.26 USD
75 pcs RGB LEDs                          25.50 USD
100 0.1uF 0805 capacitors                 2.96 USD
150 10 ohm 0805 resistors                 1.01 USD
100 68 ohm 0805 resistors                 0.67 USD
100 10k ohm 0805 resistors                0.67 USD

Total:                                   62.07 USD
Price per piece (based on 64):          0.9698 USD
Price per piece (based on 75):          0.8276 USD

About the Author

I studied electrical engineering in university and have been working in the consumer electronic industry for a few years. I have a passion for designing cost-effective circuits, which is the #1 engineering challenge in many types of projects. This is especially true for hobbyists, where we don’t have the same negotiating power as large companies with regards to component pricing. I hope that by documenting this project I can get more people interested in hardware engineering and demonstrate that it isn’t that hard after all, you just have to spend the time and do it.

Share

    Recommendations

    • Microcontroller Contest

      Microcontroller Contest
    • Science of Cooking

      Science of Cooking
    • Spotless Contest

      Spotless Contest
    user

    We have a be nice policy.
    Please be positive and constructive.

    Tips

    Questions

    Comments