Read on to find out how you can make your own color sensor!
The how-to video
For this post I decided to do something slightly different and therefore made a video about the subject. You can view it below.
Schematics and planning
Here are the schematics. Sorry that they hand-drawn, but I was away when I made these and did not have Eagle on my netbook, so pen and paper (and some color markers) had to do.
Photos
Below you will find photos of the finished project.
|
|
|
|
|
| The protoboard I used. | Wires on the back side of the sensor. It was literally hell to solder these with a standard 10 buck soldering iron and a thick tip. If you want something better, transfer the design to Eagle, head over to BatchPCB and order a professional quality PCB. | This is the sensor without the cardboard. | The sensor fully assembled. Side view. | The sensor fully assembled showing the main components behind the cardboard. |
Source code
Finally, the source code.
- serial_color_decode.cpp.This is a small C++ program which runs on Linux and uses SDL to display color. It is well commented and should by all means be the first one you try (at least if you are using Linux).
- colorsensorreader.tar.bz2 or ColorSensorReader.jar.zip. On the other hand this one is coded in Java and has a nice GUI (see screenshot below). It was tested on Linux, but should work on Windows as well. Getting the serial communications to work with Java on Linux x64 was not straight forward, but follow this description of the process from Arduino’s official website and you will avoid most of the trouble.
- color_sensor.pde. Arduino sketch code. (Missing in the original version of this blog post. Thanks, Vitali, for noticing!)
Conclusion
Hope you found this useful! Feel free to suggest any improvements















































I really appreciate your video presentation especially.So,I really thanks.
I am my senior project on sensing a color of an object,process it with a PIC and light a bulb with the same color of the sensed object.So, I think I got enough information.
Thank you very much
keep in touch
Million
Comment by Million — 09/03/2011 @ 01:24
Hi, Million!
I am glad to hear that you have found interesting information in my article, and thank you for taking interest in the video.
I wish you luck with your project! Stay tuned for more tutorials in the future!..
Doc
Comment by Dr. Ivan — 09/03/2011 @ 17:19
Hi Dr.
Thank you again for writing me a response.
I am doing my project on adaptive color light, which senses an object color and lights a bulb with the same color of the sensed object.I use the sensor that u propose(color sensor) and by using ATmega micro controller, I have a plan to do this project.
If u can, please try to send me some tutorial on related issues
Thanks in advance
Bye
Comment by Million — 10/03/2011 @ 13:26
Actually, Mill, what you are trying to do is really easy to do. Connect your RGB LED to three output pins of the MCU. Then, instead of sending the input color via serial (as in my example above), you could generate a PWM signal according to the red, green and blue values on each of the pins connected to the LED.
Take a look here, here and here.
Hope this helps
Comment by Dr. Ivan — 11/03/2011 @ 22:41
Thank you very much!!!
I will check it and I will inform you
Have a good time
Bye
Comment by Million — 12/03/2011 @ 08:57
Doctor Ivan, hi
Well i am doing a robot for a class, and i want to do a color sensor to control the velocity of two motor. I have some a questions about your sensor:
1) Do I really need to use the NPN transistors? (I have three MPS 222A)
2) What is going to be the function of the NPN transistors?
I really appreciates your time
Comment by Demian — 10/05/2011 @ 03:25
Hi Demian!
Thanks for checking out my site! To answer your questions:
1) Well, MPS 222A-s are in fact NPN transistors. NPN is a type of transistor (the other type is PNP), while “MPS222A” is a part ID specific to the manufacturer (in your case Motorola). Take a look at the datasheet here.
2) No you don’t really need transistors at all. You have basically two choices here: either power the LEDs from your microcontroller and avoid using transistors OR power LEDs directly from a power source and use your MCU to switch the current on and off. Since the latter puts less strain on the microcontroller, it is preferred – especially if you are going to add more components to your system later.
Hope this helped!
Doc
Comment by Dr. Ivan — 10/05/2011 @ 07:13
Hi Dr.
Thanks for answer my questions so quickly, this has been helpful. Well I am going to connect my sensor directly to the Arduino, because my teacher wants that we use programming language. Now my problem is catch the value of the photo-resistance.
One more time thanks for your time, hope you are fine!
Comment by Demian — 11/05/2011 @ 01:03
Hey Dr. Ivan!
I just finished my colors sensor!
Comment by Demian — 11/05/2011 @ 08:18
Hi Dr,
Nice project! Im trying to do something similar.
How do you calibrate and sample the data from photo-resistors in arduinos ?
Any help on that, i will appreciate..
Thank you!
Comment by Dom — 12/05/2011 @ 04:02
@Demian: Congrats, mate!
@Dom:
Hi there!
Generally speaking, you are using the photoresistor to measure how much light of a given wavelength is reflected from the object. The general way of interacting with photoresistors (and other analog sensors) from an arduino is to hook it up to one of the analog pins (also, don’t forget a pull-down resistor, otherwise you’ll get unstable readings – look it up on wikipedia).
So, what you do is switch on each of your LEDs one at a time, and then read analog input from the photoregister – using the analogRead() function.
Calibration is usually done with a white sheet of paper. You simply do the procedure described above to get RGB values of the white color. These are then used for all consecutive measurements as a baseline to normalize against. For the calibration code, take a look at the source code I linked to in the article (the C++ code is better commented, so give it a try first).
Reasons for doing calibration (or white balancing if you prefer) is to make up for the fact that the LEDs you are using will never be of the same brightness. When you calibrate your sensor you ensure that these differences do not affect your measurements.
Comment by Dr. Ivan — 12/05/2011 @ 18:32
dr.ivan…
can you send to me and tell me detail about the colour sensor…
also sen to me the circuit to construct the colour sensor..
i hope you will help me..
=)
Comment by jabil — 17/05/2011 @ 12:20
Hi, jabil! I’m not quite sure I understand what you are looking for. The schematics are included in the blog post above, so are the instructional video and the full source code.
Comment by Dr. Ivan — 18/05/2011 @ 13:43
Hi friend!
You could show us the code of the arduino?
Thanks
Comment by Vitali — 12/06/2011 @ 20:11
Hi, Vitali!
Thank you for noticing that I haven’t linked to it! It must have totally slipped my mind!
Fixed
(look under the Source code section)
Doc
Comment by Dr. Ivan — 12/06/2011 @ 20:34
Thank you!
Keep it up! I really liked your blog!
Comment by Vitali — 12/06/2011 @ 21:00
Dr.
Where in C++ code the String “ColorString” get values from the serial port??
Thanks
Comment by Vitali — 12/06/2011 @ 21:15
Thank you for your kind words!
It’s line 182: ColorString+=CharFromTTY;
Comment by Dr. Ivan — 12/06/2011 @ 21:18
Dr.
I tested with the bedroom light off, the RGB data values I get are 0 for 3 LEDs.
I put a white paper in front to calibration, and obtained values (on average low in comparison to your image): 153-107-239 (RGB) and when you put a blue object eg I obtained values: 30-40-180 (on average) and the result color is always 255-255-255 why?
The picture of my program:
http://i55.tinypic.com/a14zd3.png
Thanks
Comment by Vitali — 16/06/2011 @ 03:44
Your values will depend on great many things: sensitivity of your photoresistor, value of the pull-down resistor, brightness of your LEDs etc. So that all depends on your setup – and your values aren’t bad, at least for that blue object of yours.
Otherwise I’m not sure what to answer you. What code are you using? Mine? Your own? Are you sure your calculations are correct? Have you tried my example code?
Comment by Dr. Ivan — 16/06/2011 @ 08:15
Hi Dr Ivan,
If I want to scale this up slightly, reading 9 sensor at the same time. Do I build 9 of this and put them side by side? I am trying to read the rubik’s cube, so 9 of the result might or might not be the same. (9 Different values). What might your suggestion be?
Thank you,
George
Comment by George — 17/10/2011 @ 15:09
Hi George!
Thank you for reading my blog post!
To be perfectly honest with you, I do believe that this type of home-built sensor is far too imprecise to distinguish so many different colors. That, of course, depends on what kind of LEDs you are using (intensity) and that you are able to isolate your setup from light.
I would suggest that you build one such sensor and test it to assess the results.
Apart from this, you have basically following choices:
1) Use 9 sensors as you suggest.
pros: straight-forward to build
cons: lots of soldering, many pins required (9 analog, 27 digital – although the latter can be reduced with a multiplexer chip or two)
2) Use 1 sensor and move it around the cube surface
pros: fewer pins, looks cool
cons: have to build an assembly to move either the cube or the sensor around
3) Use a VGA camera
pros: reliable color recognition, extensible for future upgrades
cons: a minimum of image processing knowledge required
Of these three, I would definitely vote for the latter (number 3). It is reliable, rather cheap and far less complex than it seems. In preliminary testing, identify which pixels correspond to which tile on the cube. This of course implies that the cube should always be at a predefined distance from the cam (unless you want to learn image processing in-depth
. When actually scanning a cube, you snap a photo, get pixels from each of the small tiles on the cube – and figure out their color one by one. This can quite easily be done on an Arduino – since you (at least theoretically) don’t need to store frame data in RAM.
Hope this helps!
Dr
Comment by Dr. Ivan — 17/10/2011 @ 16:09
Hi Dr Ivan,
Thank you for the speedy and detailed respond, I appreciate them very much. I like the option 3 as well, but from what I read online, the camera they compressed the image into the jpg, and thus, we are not able to read the 40th pixel on 40 row. (Is it true? I read it from the forums).
For such application, scaning a cube and then collect the information about coloring, what camera module would you recommend? I found it to be a cheerful suggestion, because many people had been telling me it can not be done. The version I see online, the user manually input the orienation (coloring) of the cube. And futhermore, it seems the Arduino does not have the ability to solve the cube itself? The example I see online, the maker he designed a software that runs on the laptop to solve it, and send them back to the Ardunion.
Thank you very much!
George
Comment by George — 17/10/2011 @ 21:05
Hi again George!
I am happy you found my reply informative!
Well, yes and no – camera output depends largely on the camera. As far as I can see this 10$ CMOS cam at Sparkfun (http://www.sparkfun.com/products/8667) can output raw RGB values for you over standard I2C…
As for the algorithm – unfortunately here I’ll have to throw in the towel. I have never worked on a problem such as this, but I can already tell you that a brute force approach will most probably consume more memory than what an ATmega 328 has. From a quick look on Wikipedia there are a number of more efficient algorithms – but how much memory they consume is something you’ll have to experiment with.
Good luck with your project!
Doc
Comment by Dr. Ivan — 17/10/2011 @ 21:33
Hi Dr Ivan,
After a few days spend on posting various arduino website asking for information about the CMOS camera module, I realize there are actually little information about the interfacing, and about none on the existing coding example. Many forum post are like this, the poster (like me) think it can be done, while the responds from mods are describing the difficulty and some suggestion on the difficulty of the interfacing. I stayed up all night yesterday and finally discovered that. Granted, the posters they usually want to get the whole image, or even take video with the camera. It is more advance than mine, (How to read the RGB value from 40th row, 40th column).
I think I come to final realization, that it is the best just to build a color sensor with the photo resistor, maybe get 1 working first, after… might be build 9 so I can save some work on moving it x, y directions. I just want to come back with that information! I think the CMOS camera isn’t really popular in the DIY-maker realm yet, as there are little library, and resource on such topic, it “can” be done, but these I would assume is professional (10 years+) engineers who knows in and out of the camera chip.
Many many thanks to your tutorial, comments and your inputs. I am truly grateful.
Sincerely,
George
Comment by George — 21/10/2011 @ 15:01
Hi there, George!
Well, CMOS cameras are really neat – the problem is that its use is very limited on an Arduino, due to the platform’s strictly limited resources, thus not many tend to use them with this microcontroller. Handling communication with this type of camera is not necessarily difficult. Arduino, I belive, already has an I2C library to handle the underlying communications protocol for you. The rest depends largely on the camera specs, and here you will have to study the documentation that Sparkfun has linked to.
All of this is not done in a day if you do not have any prior experience. I do not think 10 years engineering education is needed strictly speaking. With firm determination and good programming skills you will probably master your setup in a month (assuming you have a job and are doing this as a hobby). But again, that also depends on how much hobby electronics and MCU programming you have done before. This is not a good project if you are new to microelectronics.
I wish you the very best of luck on your project and would love to hear now things progress!
Dr
Comment by Dr. Ivan — 21/10/2011 @ 18:09
Hi Dr. Ivan..
Well actually my final project is about color sensor too …
And I got many information from this…
In your schematic you use photoresistor, can I use photodiode?
What is different between photoresistor and photodiode?
Thank you….
Comment by abdul — 05/01/2012 @ 05:52
Dr,
I am a student and still exploring electronics.I am really interested with this project “color sensor” and I would like to propose this one. But I was wondering if this project will still be functional if it is purely circuits and dont have any program???
I really dont have any idea bout this and really need ur knowldge so much…. I really want this project … soon…. thank u so much
Comment by aamirxyris — 14/01/2012 @ 15:43
@abdul – Hi! Yes, that is possible! Take a look here – http://www.robotroom.com/ReversedLED.html As for the difference, please consult google – there are tons of pages explaining the difference!
@aamirxyris – Hello there! Yup, that would be possible – but probably harder than just doing simple programming on an Arduino. To do without, you would probably need three photoresistors with an LED each. These groups (LED+photoR) must be light isolated from one another. With some circuitry work you will then be able to connect their output to an RGB LED for output.
Hope this helps both of you!
Cheers!
Dr.
Comment by Dr. Ivan — 14/01/2012 @ 16:06
hello Dr,
just found your video and it is very helpful
well i have this project for my electronics lab and i need it urgently, i have to use a microcontroller and a breadboard instead of the pcb…
in addition i have to do a color mixer with it (using syringes) and my sensor should work for red green blue and white as well…
So i dont have any idea yet about the mixer and really need your knowledge…
thank you so much in advance,
Comment by elie — 14/01/2012 @ 19:19
30.hello Dr,
just found your video and it is very helpful
well i have this project for my electronics lab and i need it urgently, i have to use a microcontroller and a breadboard instead of the pcb…
in addition i have to do a color mixer with it (using syringes) and my sensor should work for red green blue and white as well…
So i dont have any idea yet about the mixer and really need your knowledge…
thank you so much in advance,
Comment by elie — 14/01/2012 @ 23:29