Introduction: Build Your Own (at)tiny Colour Sensor.




A few months ago, I saw an instructable by fjordcarver on how to build a coloursensor with an RGB led and an LDR. It inspired me to try whether I could improve his design.

Here are the things that I wanted:
  1. The sensor should have as few pins as possible.
  2. It should work as a stand-alone device. All calculations should be done on the device.
  3. It should have a triggered mode and a continuous mode.
  4. All parameters should be programmable.
  5. Calibration parameters should be stored in the EEPROM of the microcontroller.
  6. Firmware updates should be made possible
  7. And finally: size does matter ⇒ The smaller the better.

I did choose an smd attiny85 as the brain of the sensor. It has a small footprint but a large enough flash for the calculations. It also has just enough pins for the project (all eight pins are used).

One of the problems of the attiny is that it doesn't have a build in bootloader sector. So I needed to find a bootloader that would work on it. I tried a few and most didn't work for me, until I stumbled on the tinyloader bootloader. It's fast and only 256 bytes long and it did the trick. It also allowed me to make a sensor with only 4 pins : Vcc, GND, TX and RX.

I made my sensor with smd parts but you can ofcourse make yours with thru-hole parts



Step 1: How Does It Work?

Colour is nothing more than different wavelengths of light that are reflected by objects. Each material will reflect a different combination of wavelengths, resulting in different colours.
With our led we send 3 different wavelenghts to a surface (aka red, green and blue). The LDR measures how much of each wavelength is reflected and sets its resistance accordingly. The LDR is set up with a 10K resistor as a voltage divider. The resulting voltage for each wavelength is read by the ADC of the microcontroller and the results are 3 bytes, one for each colour.
These 3 bytes can then be used to reconstruct the colour.

Step 2: What Do You Need?

We actually don't need a lot for this project:
  1. An attiny85
  2. An RGB led (4-pin PLCC)
  3. An LDR
  4. A 10K resistor
  5. A 220Ohm resistor
  6. a row of 4 male header pins
  7. Stuff to make your own pcb
Other things that can come in handy:
  1. Solderpaste and a hot air soldering station
  2. An SOP to DIL converter so that you can program the bootloader into the attiny
  3. An USB to UART bridge

Step 3: The Circuit

The circuit is pretty similar to Fjordcarvers original design. The only exception is that I used an LED with common anode, instead of a common cathode.

Step 4: Code Part 1: the Bootloader

I was looking for a bootloader on the internet and after a long night of searching, I found a folder named tinyload3 with a bootloader, build by Peter Dannegger, that fitted in my attiny85. I used that one, but later found out that it was a predecessor of the 'fast tiny & mega UART bootloader' -project that can be found on AVRfreaks.
As I used the old version, I'll explain that one but the new version works pretty similar.

When you open tinyload3.rar, you'll find a bunch of premade bootloaders for all kinds of attiny's and mega's. However there is no bootloader for the attiny85. To make one, you can open the assembler file of one of the other AVR's, make the needed changes and rebuild the hex-file.

I used the T45.asm. I changed .include "tn45def.inc" to .include "tn85def.inc" and changed the pins for RX and TX to the appropriate ones. Pb5 for TX and Pb3 for RX.

I added the hex-file for the attiny85 just to save you some work.

Now you can use your regular programmer to upload the bootloader. When you do so, you also need to set the fuses so that the attiny uses the 8MHz internal Osc and so that the reset is disabled on pin 5. Remember that when you disable the reset, you won't be able to program the attiny with your ISP programmer anymore.

If everything worked ok, you should be able now to upload code into your attiny via the bootloader. To do so, connect the newly created TX and RX pins of the attiny to those of your USB-UART bridge and connect it to you pc. Make sure that you can switch the power of your attiny separately.
In the tinyload3 folder, you can find tboot.exe. To use it enter the following command:

tboot.exe -cnumber COMport -pfilname

Only COM0 to COM4 are accepted, so you might have to change the number of the COMport that you are using.

Hit enter and only then switch on the power to the attiny. This way the bootloader will detect that there is some code to be uploaded and you'll see the uploading process progressing.






Step 5: Code Part 2: Colour Sensing.

To do the colour sensing, I basically used Fjordcarvers code without any modifications. I only translated it to bascom and added serial communication to set the modes and the parameters, but the basic concept didn't change. I would like to suggest that you read his description of the code as he provides a very clear explanation of all the steps.

I added my code in the txt-file. It's still a bit rough around the edges, but it works. I also added a flowchart of the code to explain how the code works.

Comments

author
vjain3 made it! (author)2017-02-28

Hey,

It would be a lot of help if you can upload a video of the same.

author
evildoctorbluetooth made it! (author)2015-10-09

Hey dude, thats really cool.

Could you output the .brd as a dxf file?

author
janw made it! (author)janw2015-11-05

I'll try...

author
DLM87 made it! (author)DLM872016-01-22

What might be easier is if you export the brd as an image using EAGLE or other schematic software. Then evildoctorbluetooth can convert the image to their preferred format. If they want DXF, import the image in Adobe Inkscape, then Save As... DXF in the list of available formats.

Great work on this project by the way! :) i love attinys.

author
rahul.rattu90 made it! (author)2015-02-22

this is very helpful. i want a sensor that can sense various colors of fabrics and their shades. would i be needing color recognition detector/sensor OR a microspectrometer ?

how far do i have to keep the object in order to sense it correctly.?
please help me with this, i have been searching for the same from a while. this is not my area of expertise.

author
12V made it! (author)2014-10-04

To make this work with moving objects, it should be possible to use a photo transistor and take the red sample, green sample and blue sample at very high speed. LEDs and photo transistors are very fast and the attiny is capable of taking 15 thousand ADC samples per second.(see datasheet page 122)

author
sacredthomas made it! (author)2014-08-12

nice project....I am doing a color sorting application to sort the colored plastic pumps that holds the cotton thread in textile mills....i have attached the sample image of the colored plastic tubes that i want to separate......will this component be helpful for such application??

author
janw made it! (author)janw2014-08-12

It depends. This methode is good to detect the colors of static objects but not of moving objects as the sensor needs a while to detect the color. Also it works best in an area without a lot of ambient light. If there is ambient light, it needs to be at a relatively constant level. But I imagine that those pumps come only in a few colors so it might be doable to work with large detectionmargins.

This device is not as accurate as commercial ic's. Those start to get cheaper these so maybe that is also something you should look at.

author
leddiode made it! (author)2013-12-01

If you want build it on one-sided board, here is download (zip w/ eagle print)
Please read README
link to download (mediafire) http://bit.ly/IDpb7s

author
fahadshihab made it! (author)2013-10-23

but to have more name, we can build this same project but without ICs. You will need three LDRs and put one colored film(red on one, green on another, and blue for the third one) so, we can sense red, green and blue colors separately. Connect those pins to the respective leds. You have made your own color sensor without an IC!

author
janw made it! (author)janw2013-10-25

Good thinking. But you still would need an IC to proces the data. Using the resistance of the LDRs to mix the colors of an RGB led directly isn't a good practise and wouldn't probably give good results.

author
fahadshihab made it! (author)fahadshihab2013-11-05

how'bout working on it?

author
janw made it! (author)janw2013-11-05

You can work on it if you like. I have far to many pojects for the moment to pick this one up myself.

author
sarihan made it! (author)2013-09-24

nice.. i'm doing a similar project on colour sensor.. just checking.. can this project detects different shades of RGB?

author
janw made it! (author)janw2013-09-25

It has build in detection of shades but it is of course not as good in it as commercial dedicated sensors. If you work with arduino, then look up the code in Fjordcarvers project. The link is in this instructable.

author
sarihan made it! (author)sarihan2013-09-25

alright thanks.. sadly im not allowed to use arduino due to some technical reasons.. but I will take note of this project and see how I can implement it in my project..

author
Much to do about nothing made it! (author)2013-07-25

Can't help by notice, did you use rivets for via hole? nice !

author
janw made it! (author)janw2013-07-26

These are special rivets for vias. They come in various sizes from very tiny to sizes where you still can fit a component thru them. Very handy stuff for prototyping.

author
janw made it! (author)2013-01-12

You surely can use some other microcontrollers or an arduino.

author
kazoza made it! (author)2013-01-11

Great instructable, but sadly we don't have attiny in the market here in Egypt any other recommended micro that can replace it?

author
fjordcarver made it! (author)2012-06-08

Nice Instructable!! I haven't been on in a while, good work, it looks great.

author
Bot1398 made it! (author)2012-04-02

We would like to see a video!!And where did you get those small RGB LED's

author
janw made it! (author)janw2012-04-02

I bought them at Leds-buy.nl

author
diy_bloke made it! (author)diy_bloke2012-05-17

kreeg ik eerder al een handig adresje van jou van de smdshop, Leds-buy is ook verdomd handig om te hebben.
Vraagje, hoe programmeer jij practisch je smd microcontrollers? met een dil kan ik hem in een voetje steken en later weer in de desbtrefefnde schakeling maar met SMD gaat dat wat lastig. Enige tips?
Ik wil, als is het maar om het weer eens een keer te proberen, een smd versie van een attiny85 schakeling proberen.

author
diy_bloke made it! (author)diy_bloke2012-05-17

(facepalm) ik had even verder noeten lezen, dan was al veel duidelijk geworden. Bedankt voor je antwoord

author
janw made it! (author)janw2012-05-17

Ik gebruik 3 verschillende methodes afhankelijk van het project:

Als het een project is waar genoeg ruimte is op de print, voorzie ik gewoon programeerpinnen zodat ik een programmer op de print kan aansluiten. Dit werkt gemakkelijk en je kan altijd nog herprogrammeren indien nodig.

Ik heb ook een aantal converters van smd naar dil daanmee kan je je smd microcontroller gewoon inpluggen in je breadboard en daar programmeren en testen. Nadeel is wel dat als de controller eenmaal gesoldeerd is, hij niet meer te programmeren valt.

Een derde methode is met een bootloader zoals beschreven in deze instructable. Ik gebruik het voetje om een bootloader in de controller te programmeren en daarna heb ik maar 2 pinnen nodig op mijn printje om te kunnen programmeren. Dit is handig op extreem kleine printjes en het zorgt er ook voor dat je de reset-pin kan gebruiken voor andere doeleinden. Nadelen zijn dat de bootloader plaats inneemt (hoewel dat op een attiny85 geen probleem mag zijn) en dat je, doordat je de reset-pin afsluit, een high voltage programmer nodig hebt wil je later een andere bootloader ofzo inlezen.

smd8 naar dil8 voetjes zijn voor minder dan 10 euro te koop op ebay en meestal nog gratis verzonden.

SAM_1178.JPGSAM_1177.JPG
author
diy_bloke made it! (author)diy_bloke2012-05-17

OK bedankt. Ik kan hem gewoon in en uit dat voetje halen en als hij geprogrammeerd is in een schakeling solderen.
Als ik daar dan ook de SPI pinen in heb, is hij ook in die schakeling weer te progarmmeren neem ik aan :-)

Bootloader voor de 85, ja, misschien moet ik dat ook eens proberen. Gaat mij erom dat ik een heel klein schakelingetje kan maken. Nou is het met DIL ook wel klein te maken, maar wilde na 35 jaar weer eens smd proberen. Bovendien hebben ze bij smdshop leuke componenten. Enige wat ik er niet kon vinden was een 5V regulator die meer dan 100-150 mA kan leveren
Nogmaals bedankt

author
Chinasaur made it! (author)2012-03-29

Cool design, I guess you are SMD soldering by hand!? It's nice to do SMD boards with all the SMD mounts on one side so that you can do hot plate reflow. You can still use the back face for ground plane or more complicated routing, so most simple designs can be refactored to work this way.

Thanks for info on bootloaders too.

author
janw made it! (author)janw2012-03-29

I solder by hand with solderpaste and a hot air solderingstation. I decided to have parts on both side to save space.

author
destroyer2012 made it! (author)2012-03-29

I have some experience working with this idea as I am building a rubicks cube solver using an arduino micro and two of similar color sensors to detect edge face colors. One thing that I've noticed is that ambient light makes a huge difference on the result and even "blanking" the photoresistor with the LED off before sampling with the LED on doesn't help. I've sort of put that project on the back burner but I'm hoping there's a way to filter out this noise in software, OR a parallax barrier type of shield on top of the photoresistor to block external light.

How much have you experimented with different casings for the color sensor?

author
janw made it! (author)janw2012-03-29

Ambient light is indeed the sensors biggest enemy. I mainly used a black case for it.

author
destroyer2012 made it! (author)destroyer20122012-03-29

What I've done is wrapped black electrical tape all around the sensor and LED in like a tunnel that presses almost up the to cube's face. SO even with that setup the noise is significant.

author
vmgaspar55 made it! (author)2012-03-29

Did you think already that can be useful for people who are blind, if the identification is made by sound types ?

author
janw made it! (author)janw2012-03-29

That's a very interesting thought indeed.

author
ynze made it! (author)2012-03-26

Very nice project. Again!

author
janw made it! (author)janw2012-03-26

Thanks!!

author
stringstretcher made it! (author)2012-03-25

I have often wondered about this idea for color recognition. What kind of range is possible? If you use a lens can you extend that range? I'm so new to Arduino programming that I don't think I can contribute to your code, but that doesn't mean I don't LOVE it!! I have seen it done in gas chromo devices that spin a colored filter wheel in front of a calibrated filament, sort of a reverse version of yours. Perhaps a position indicator on the wheel to coordinate the timing? Can those LEDS be read as sensors instead? They will respond to light with a charge. Do they respond to their colors? That would allow you to skip the LDR.

author
janw made it! (author)janw2012-03-25

It is indeed a rather crude sensor. It is capable to recognize a basic set of colours so don't expect it to sense every shade possible. I was intrigued by the original version of Fjordcarver but also a bit sceptical. But when I build it and tried it, it workt pretty good.

About This Instructable

78,377views

237favorites

License:

Bio: I'm mainly interested in music, food and electronics but I like to read and learn about a lot more than that.
More by janw:Pickled ChilliesExtra hot sriracha sauce (Mme Jeanette)Build a Nest Box for Bumblebees
Add instructable to: