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:
- 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?
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?
- An attiny85
- An RGB led (4-pin PLCC)
- An LDR
- A 10K resistor
- A 220Ohm resistor
- a row of 4 male header pins
- Stuff to make your own pcb
- Solderpaste and a hot air soldering station
- An SOP to DIL converter so that you can program the bootloader into the attiny
- 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.