| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| en:examples:sensor:color [2012/06/07 00:00] – raivo.sell | en:examples:sensor:color [2020/07/20 12:00] (current) – external edit 127.0.0.1 |
|---|
| ====== Color sensor ====== | ====== Color sensor ====== |
| |
| //Necessary knowledge: [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:lcd]], [AVR] [[en:avr:adc]], [LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]// | //Necessary knowledge: [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:digi]], [AVR] [[en:avr:adc]], [LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]// |
| |
| ===== Theory ===== | ===== Theory ===== |
| The ColorPAL from Parallax ([[http://www.pololu.com/file/0J306/28380ColorPAL.pdf|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 ColorPAL from Parallax ([[http://www.pololu.com/file/0J306/28380ColorPAL.pdf|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 following spectral sensitivity curve (taken from the TSL13T datasheet and superimposed with the LED wavelengths: | 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. |
| |
| [{{ :en:examples:sensor:sensitivity_curve.png?300|ColorPAL spectral sensitivity curve}}] | [{{ :en:examples:sensor:sensitivity_curve.png?300|ColorPAL spectral sensitivity curve}}] |
| |
| The sensor outputs a voltage proportional to all the light that it sees weighted by the above curve. 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. | 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. |
| | |
| The ColorPAL sensor requires only three connections +5V supply, ground and serial data. It can be plugged directly into Homelab sensor board ADC3 pin group. 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. | 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. |
| | |
| | [{{ :examples:sensor:color:colorpal_wiring1.png?580 |ColorPAL wiring schematic}}] |
| | |
| | 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. |
| |
| <code c> | <code c> |
| #include <homelab/usart.h> | #include <homelab/usart.h> |
| |
| // | |
| // Determining USART interface. | // Determining USART interface. |
| // | |
| usart port = USART(0); | usart port = USART(0); |
| |
| | // Main program |
| // | |
| //MAIN | |
| // | |
| int main(void) | int main(void) |
| { | { |
| char blue[7]; | char blue[7]; |
| |
| // Variable for all color data. | // Variable for all color data. |
| char data[9]; | char data[9]; |
| |
| while (1) | while (1) |
| { | { |
| // Send a command to the sensor. | // Send a command to the sensor. |
| usart_send_string(port, "=m!"); | usart_send_string(port, "=m!"); |
| | |
| c = usart_read_char(port); | c = usart_read_char(port); |
| |
| // The first three characters (=m!), which we have sent, it is not necessary. | // The first three characters (=m!), |
| | // which we have sent, it is not necessary. |
| if(i>3) | if(i>3) |
| { | { |
| data[i-3] = c; | data[i-3] = c; |
| } | } |
| | |
| } | } |
| | |
| // Converting the data to the suitable form and print it on the screen. | // 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]); | sprintf(red, "Red: %c%c%c", data[0], data[1], data[2]); |
| lcd_gfx_goto_char_xy(0, 3); | lcd_gfx_goto_char_xy(0, 3); |
| } | } |
| } | } |
| |
| |
| |
| </code> | </code> |