Necessary knowledge: [HW] Sensors Module, [HW] User Interface Module, [AVR] Analog-to-digital Converter, [LIB] Analog to Digital Converter, [LIB] Graphic LCD, [LIB] Sensors
The color sensor detects the color of the surface, usually in the RGB scale. Color is the result of interaction between a light source, an object and an observer. In case of reflected light, light falling on an object will be reflected or absorbed depending on surface characteristics, such as reflectance and transmittance. For example, green paper will absorb most of the reddish and bluish part of the spectrum while reflecting the greenish part of the spectrum, making it appear greenish to the observer.
Measuring colors of the ingredients are basically two ways. The easiest way is to use a color-changing light source and a sensor that measures the intensity of the light. Most industrial color sensors contain a white light emitter and three separate receivers. There are usually three sets of color source or color filter with peak sensitivities at wavelengths that we identify as red (580nm), green (540nm) and blue (450nm). All colors can be derived by their components.
Color sensors have a variety of applications including detection of environment, choosing the right product and sorting. The detection of color compared to the vision sensor is much faster and cheaper.
The ColorPAL from Parallax (Datasheet) is a miniature color and light sensor. The ColorPAL uses an RGB LED to illuminate a sample, one color at a time, along with a broad-spectrum light-to-voltage converter to measure the light reflected back. The amount of light reflected from the sample under illumination from each red, green and blue LED can be used to determine the samples color. The subject must be reflective and non-fluorescent. The color of objects that emit light (e.g. LEDs) cannot be detected.
The light sensor used in the ColorPAL is a TSL13T, which has the spectral sensitivity curve (taken from the TSL13T datasheet) as seen on the figure and superimposed with the LED wavelengths.
Sensor outputs a voltage, proportional to all the light that it sees weighted by the curve on the figure. Therefore, when a subject is illuminated with a red LED only it will respond with a voltage proportional to the red component of the subjects color and similarly with blue and green. When there is ambient light mixed in with the LEDs illumination, its effect can be eliminated by sampling first without any LEDs turned on and then subtracting this reading, in turn, from each of the red, green, and blue components. This reference measurement should be taken before each color measurement to eliminate any effects from varying ambient conditions.
ColorPAL sensor requires only three connections: +5 V supply, ground and serial data. It can be plugged into Robotic Homelab communication board EXT_UART connector. Separate TxD and RxD pins needs to be connected together using diode. Communication with the ColorPAL takes place using serial I/O transmitting and receiving at between 2400 and 7200 baud using a non-inverted open-drain protocol.
Example code enabling to read the RGB values which is printed on the screen using hex format. Measurements with ColorPAL sensor will be made automatically.
#include <stdio.h> #include <homelab/delay.h> #include <homelab/module/lcd_gfx.h> #include <homelab/usart.h> // Determining USART interface. usart port = USART(0); // Main program int main(void) { // Variable, which is stored in the read characters. char c; // Variables for each color text which is printed on the screen. char red[7]; char green[7]; char blue[7]; // Variable for all color data. char data[9]; // The set-up of the USART interface. usart_init_async ( port, USART_DATABITS_8, USART_STOPBITS_ONE, USART_PARITY_NONE, USART_BAUDRATE_ASYNC(2400) ); // LCD init lcd_gfx_init(); // Clear screen lcd_gfx_clear(); // Turn on backlight. lcd_gfx_backlight(true); // Print program name lcd_gfx_goto_char_xy(3, 1); lcd_gfx_write_string("ColorPAL"); while (1) { // Send a command to the sensor. usart_send_string(port, "=m!"); // There are 12 character to read (3 command and 9 data character). for(int i = 0; i < 12; i++) { // Waiting for incoming data. while (!usart_has_data(port)){} // Read out the received character. c = usart_read_char(port); // The first three characters (=m!), // which we have sent, it is not necessary. if(i>3) { // Store the received data character. data[i-3] = c; } } // Converting the data to the suitable form // and print it on the screen. sprintf(red, "Red: %c%c%c", data[0], data[1], data[2]); lcd_gfx_goto_char_xy(0, 3); lcd_gfx_write_string(red); sprintf(green, "Green: %c%c%c", data[3], data[4], data[5]); lcd_gfx_goto_char_xy(0, 4); lcd_gfx_write_string(green); sprintf(blue, "Blue: %c%c%c", data[6], data[7], data[8]); lcd_gfx_goto_char_xy(0, 5); lcd_gfx_write_string(blue); // Delay sw_delay_ms(500); } }