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:
- The sensor should have as few pins as possible.
- It should work as a stand-alone device. All calculations should be done on the device.
- It should have a triggered mode and a continuous mode.
- All parameters should be programmable.
- Calibration parameters should be stored in the EEPROM of the microcontroller.
- Firmware updates should be made possible
- 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?
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.