Step 3: Build the Colour Sensor

I like to build things in modules and use headers and jumpers to connect them, but it is not strictly necessary. I may want/need this thing again, so I built the sensor as a separate module.

Start by wiring up the CdS photocell. One side should go to a 5V pin on the Arduino. The other end should go out to ground via a 10k resistor, and also to analog pin 2 on the Arduino. (see wiring diagram, it is marked pin 0, but the code uses pin 2, either will work, just make the appropriate changes)

The sensitivity of the sensor can be changed by altering the resistor. A higher resistance will provide a better sensitivity to darker colours. Anywhere from 1k up to 10k should be decent. I used a 10k because it was what I had on hand, but I believe that a lower value would have been better for this application.

Once the the Cds photocell is wired up, wire in the 3 LEDs with their grounds going through the 220 ohm resistor. The red LED will connect to digital pin 2 on the Arduino. The green to pin 3, and the blue to pin 4.
Try to arrange the LEDs so that they are equidistant from the photocell. Another hint is to try and leave a little bit of wire leads above your proto-board so that you can gently bend them in order to fine tune the beams. You want the LEDs to illuminate the area beneath the photocell as equally as you can manage. (see wiring diagram)
Use some heat shrink tubing or something else to shroud the CdS from direct exposure from the LEDs. While we will be doing balancing to our readings, we don't want to contend with to much extra light. We are trying to detect what light is detected, and having too much exposure to direct light will throw the reading off. I used a section cut from a cheap black pen to make my shroud, and hot glued it in place.

Once the circuit is built and the shroud is added to the CdS photocell, you can go ahead and shroud the entire sensor ensemble. It is not necessary, but will help the sensor to keep a decent balance even when the ambient light changes. An added bonus is that it hides all the flashing from the sensor doing it's thing. I used a bit of black plastic I had kicking around from a garden light to make mine.
I cut it to fit my project, and also a bit to allow the sensor to sit, and then glued my board in place.
To further cut back on ambient light interfering, I went on to wrap the whole ensemble in black electrical tape.
<p>Hey.. great ible.. I went thorough your ible for detecting colours too.. But I still have a basic question- why do you use leds to provide light which then reflects from the surface (to be colour sensed)? </p><p><br>While writing this question I'm guessing it is to increase the dimensions or channels from 1 to 3. But can you please elaborate a bit on it (if it is correct)<br>Sorry if it is already answered, but the science kept bothering me.</p>
<p>a lovely and unique Project. thumbsup</p>
<p>what shall be the cost of the same?</p>
<p>I love this instructable!!! Enjoying it so much:D<br>This was my first project with arduino and all the prototyping boards and things.</p><p>I was stuck for quite a long time in installing the libraries.</p><p>users/documnts/arduino/libraries/moodlamp(I made it)/moodlamp.cpp and moodlamp.h(from the &quot;rgb lamp with custom moodlamp&quot;)</p><p>I did this and tried compiling chameleon.txt, got a lot of errors that said something like</p><p>Wprogram.h is not found</p><p>lamp is not declared in this scope</p><p>etc...</p><p>what I did was </p><p>1, install the arduino ver22 and use that one instead of the newest release(Thanks to the other comentor!)</p><p>2, changed &quot;Wprogram.h&quot; to &quot;Arduino.h&quot; in the cpp and .h</p><p>and it finally worked!!!;-D yayyy</p><p>now I`m going to get working with the hardware part.</p><p>looking so much forward!<br><br>thanx again for the awesome project!!!</p>
<p>Sooo coool. I've seen products like this on Vat19. This is cool how you made one!!</p>
<p>I finished the project and have a few suggestions. The drawing shows the use of Analog pin 2 while the program calls for using Analog pin 0. Also, I was unable to get usable readings using a 100K resistor with the photocell. I reduced it to 6.2K and it now works well. Thanks for your project !</p>
<p>how do you managed to distinguish the ranges exactly for different color?</p>
<p>This is coolest applied-color-theory <br>I've ever seen. </p><p>Kudos to you sir, this good work! :)</p>
i am not able to calibrate the sensor. please help!
could you guys please help me make a diagram for a RGB LED.
https://www.instructables.com/id/Using-an-RGB-LED-to-Detect-Colours/<br><br>hope this helps
Hi. I got some trouble.<br><br>I put the .ccp and .h files in libreary/moodlamp.<br>I got no skteches folder. <br>Using IDE 0022<br><br>when i try to compile the library i get this error.<br><br><br>sketch_feb23a.cpp:1:22: error: moodlamp.h: No such file or directory<br><br>Any idea what i am doing wrong?<br>
How would you do this using a common anode rgb.<br>would it be okay to use one resistor on the anode?
no, you would use one resistor on each cathode, and your code would be different as well. The leds will light when pins go low as opposed to high.
i am getting errors compiling the code<br>
Could you be more specific?
what does the error say?
it says <br>&quot;moodlamp does not name a type
Sounds like you don't have the library or a typo in your code. Did you install the library?
how do i do that??
From step 6 - For the emulation lamp I am using my custom Moodlamp library, you can get the library files and documentation on my other RGB Instructable, <a href="https://www.instructables.com/id/RGB-lamp-with-Custom-Moodlamp-Library/" rel="nofollow">RGB Moodlamp with Custom Library</a>. Otherwise you can just copy the colour sensor handling code and output to your RGB lamp in your preferred manner.<br> the information that you want is on <a href="https://www.instructables.com/id/RGB-lamp-with-Custom-Moodlamp-Library/step3/Test-Drive-The-Library/" rel="nofollow">step 3</a>.<br> If you are using Arduino 1.0, there is a potential issue with the library which I will address soon(a little busy with Holiday celebrations still). See the post in the comments here for some additional info.
downloaded the libarary successfully <br>it gives another error &quot;pulse not defined in the scope&quot; for the statement pulse whit<br>e
could you show me the code as you have it in your sketch? it sounds as if you have a comment uncommented<br>meaning one of the lines of comment text is on a new line. putting a // in front of the text that is not code is important. either backspace the line feed that has been inadvertantly added, or add // in front of comment text that is on its own line.<br>
If you get an error message that says something about<br><br>&quot;WProgram.h not found&quot; <br><br>This seems to be a bug with the Arduino 1.0 IDE. Switch back to 22 or 23 and it should compile and run fine.
Would this error be when compiling the moodlamp library? I have not seen that, but thanks for the tip.<br>
Im not really familiar with the Arduino IDE and programming with it yet. So I don't know for sure what part of the process caused it. But here is what happened:<br>1. create libraries folder in my sketch book folder<br>2. create Moodlamp folder inside libraries<br>3. paste the .cpp and .h files into Moodlamp folder (you should consider attaching these files to this instructable too. I eventually found them on the other one that you link to, but it threw me off for a while.)<br>4. paste the code into arduino IDE<br>5. press the upload button - at this point there was an error that said something about WProgram.h not found. <br><br>When I switch to IDE 0022 and repeat these steps everything works as expected.
I think if you comment out the #include at the top of the .cpp file, it may work in 1.0...maybe, let me know if you try
Thank you for the awesome feedback. I have not had this error, so I looked it up, it appears that after 1.0 the filename WProgram.h was changed. Your work around appears to be the best method at a glance so thank you for sharing. I have not had the time to delve deeper, but will certainly look and will update the step to reflect your solution as well.<br>I also appreciate you pointing out that I should include the files, I will fix that later as well. I am a newer author here, and really appreciate knowing what people want/expect. <br>If you got everything working I'd love to have a picture posted, it'd be my first.<br><br>I sent you a private message.;) Thanks for being Awesome!!
This is a great instructable, and really well done! Am I correct in assuming you got the idea from ThinkGeek's Huey? :)
Actually no,(believe it or not) but I found out about Huey while I was finishing it up. I was bummed, but I googled Chameleon Lamp to make sure it was the only one (nothing ever is) and found Huey. Huey is cuter, but mine is better. Lol. Actually I like that Huey writes to his eeprom to store his balance scan, I should have done that too. Then you only set the lamp one time. I need to set mine every time I turn it on.
Yep, custom made is always better than store-bought(and usually cheaper, too). How hard do you think it would be to update your code to store balancing data?<br><br>I'll see if I can't build a chameleon lamp myself, and post my result here. Wish me luck!
It would not be too hard, a couple of lines, but I probably won't get around too it. For myself, the thrill of seeing an idea work is all that I am after. I will leave the better version for the next person to build.<br>If you like. build yours, post it, and I will help you (if you need it) to add the functionality to yours. All you would need to do is write the array to eeprom when you take it and add a bit of code to the checkBalance function to verify if the eeprom is storing any value. You could also just check if it is there in setup.
Great project. I will definately try to buid such a colorsensor. <br> <br>definately worth 5 stars
Thanks!! Post a picture if you do!
You have some very cool projects and I'm jealous because I'm in the same contests. Great Job. I'm going to have to build one of these.
Thanks, quite the compliment. You could always vote for me.<br> Your projects are awesome too.<br> I just wrote a <a href="https://www.instructables.com/id/Using-an-RGB-LED-to-Detect-Colours/" rel="nofollow">new one </a>detailing just the sensor aspect on a breadboard, I included Processing code that lets you see what colour the sensor sees, it might be of interest to you.&nbsp;
Hey, in that picture I can see you wrote digital and analog can you explain how this system work of this chameleon lamp works, please!!!
I am not sure what it is that you are missing. Sometimes it is difficult to understand what somebody is having trouble with, so try and be as clear as possible in your questions. <br> Digital and Analog pins correspond to pins on the Arduino. The Arduino is a microcontroller and is doing the 'Thinking'. <br>Most sensors have outputs that in turn connect to inputs somewhere when in use. In this case I am connecting the sensor to an Arduino which in turn is interpreting the data it is recieving. (The output connects to an analog pin as it is outputting an analog voltage) <br>The LEDs are connected to pins on the Arduino as well, digital pins, which in this case (see the code) are setup as outputs. This allows you to turn the lights on and off. <br>This information is all in the Instructable, so please try reading it again.
Hey, can you give me more technical details on how the circuit and the system works of the chameleon lamp!!! I think you you made a cool lamp and I am going to try this out!!!! thanks you!!!!
Thanks for the compliment<br> Step 2, has the details on how it works, Steps 3 and 4 detail the circuit, and the code is on step 6. You will have to be more specific as I am not sure what information you are missing.<br> If you would like to see the sensor simplified and taken out of the lamp you can read my other Instructable, Using an <a href="https://www.instructables.com/id/Using-an-RGB-LED-to-Detect-Colours/" rel="nofollow">RGB LED to Detect Colours</a>.
Very COOL !!!<br>One of the smartest led color &quot;change&quot; application ever seen, I'll made one for sure. Great !<br>
Thanks so much! Be sure to post a picture when you finish!
No Problem! Glad you enjoyed it.
Very nice. I hope I learn to write code like you one day. I am just starting with Arduino and it can be very confusing, to the beginner, at times. Thanks for this great instructable.
Nice instructable, there was a recent instructable on change-of-tilt sensor that may be a nice addition to this when deciding when to chose new colours.<br>When the input to another pin on your arduino is kept constant, i.e the mood sensor is stationary, it would then search for the colour.<br>Is the white and black readings require for every time you choose a colour?<br><br>The other instructable is here:<br>https://www.instructables.com/id/Sensitive-Tilt-Trigger/
The black and white balancing reading only takes place when the lamp is first turned on. I have not tried yet, but you could probably just write the values to eeprom and only have to do it once ever. I read the tilt sensor Instructable and enjoyed it. <br>I was actually inspired by his, and am making my own version of tilt sensor for another project. (less sensitive and simpler but still inspired)<br>Thanks for the compliment!
Very good idea and perfect description! Thanks very much for sharing!
And thank you very much for the kind words. Cheers.

About This Instructable




Bio: Dad, maker, dreamer, hacker, painter.
More by fjordcarver:Make A 2D Video Game with Unity Acoustic/Electric Cookie Tin Unitar Make a Keepsake Book of your Child's Artwork 
Add instructable to: