Color Detector Sensor
This page describes a color detector sensor made by Atlas Scientific and how to use it to implement a BriteLED chasing demo.
Description
Atlas Scientific is on a mission to make high-quality sensors for environmental monitoring available to everyday hackers and makers. All of their kits are easy to calibrate and connect to your microcontroller-based project. https://www.sparkfun.com/products/11195
Features
https://www.sparkfun.com/products/11195
- R,G,B data is in true 8-bit RGB format from 0-255
- light intensity is output in lux (lx)
- lx given for R ,G and B as well as full spectrum lx
- RGB color data and lx can be output simultaneously
- lx range from 0- 3,235 lux
- Light saturation indicator if lx is over 3,235
- Three different output modes:
- RGB only
- Lx only
- RGB and lx
- Data output is a single comma separated string
- Simple RS-232 connectivity (voltage swing 0-VCC)
- Harsh environment ready: NEMA 6P and IP 68 rated
- Wide operating temperature range: -40 Celsius to +85 Celsius
- Simple instruction set consisting of only 5 commands
- Reading time: 620ms
- Wide operating voltage range: 3.1V to 5.5V
- Shock resistant to 16 mega Pascals
Refresh Rate
This color sensor can only take a measurement every 620 millisecond.
Wiring
mbed | shiftbrite with random color | shiftbrite with color sensor |
---|---|---|
p5 | DI | |
p7 | CI | |
p22 | LI | |
p21 | E1 | |
p11 | DI | |
p13 | CI | |
p15 | LI | |
p23 | EI |
Code
/* This program demonstrates the ability of a color sensor made by * Atlas Scientific through a shiftbrite LED chasing game. * Bi Ge bge6@gatech.edu */ #include "mbed.h" DigitalOut latch(p15); PwmOut enable(p23); // use PWM instead of DigitalOut to dim the LED Serial color_sensor(p9, p10); DigitalOut out_latch(p22); PwmOut out_enable(p21); SPI spi(p11, p12, p13); // the random shiftbrite SPI out_spi(p5, p6, p7); // this shiftbrite is attached to the color sensor // http://mbed.org/users/4180_1/notebook/shiftbrite1/ void RGB_LED(int red, int green, int blue) { unsigned int low_color=0; unsigned int high_color=0; high_color=(blue<<4)|((red&0x3C0)>>6); low_color=(((red&0x3F)<<10)|(green)); spi.write(high_color); spi.write(low_color); latch=1; latch=0; } /* This function is exactly the same as the one above, the only * difference is now it outputs value to the second shiftbrite */ void out_RGB_LED(int red, int green, int blue) { unsigned int low_color=0; unsigned int high_color=0; high_color=(blue<<4)|((red&0x3C0)>>6); low_color=(((red&0x3F)<<10)|(green)); out_spi.write(high_color); out_spi.write(low_color); out_latch=1; out_latch=0; } void split_RGB(char* color_str) { char *save_ptr; // this ptr is used by strtok_r to save // the current parsing state char *p = strtok_r(color_str, ",", &save_ptr); // parse with "," int red = strtol(p, NULL, 10); p = strtok_r(NULL, ",", &save_ptr); int green = strtol(p, NULL, 10); p = strtok_r(NULL, ",", &save_ptr); int blue = strtol(p, NULL, 10); out_RGB_LED(red, green, blue); return; } void get_color() { if(color_sensor.readable()) { char str[11]; int i = 0; char c; /* * The color sensor sends its output in form * "rrr,ggg,bbb\r" in char. It can also output the * light intensity. See product document. */ while(c=color_sensor.getc()) { // wait for the carriage return if(c!='\r') { str[i] = c; i++; } else { break; } } split_RGB(str); } return; } int main() { int red=0; int green=0; int blue=0; spi.format(16,0); spi.frequency(500000); out_spi.format(16,0); out_spi.frequency(500000); enable=0.5; // set PWM to half, full on is too bright latch=0; out_enable=0.5; out_latch=0; color_sensor.baud(38400); wait(2); while(1) { red = rand() % 50; green = rand() % 50; blue = rand() % 50; RGB_LED( red, green, blue); /* IMPORTANT: The color sensor can only take a measurement every 620ms * Have to wait for the instruction to complete. */ get_color(); wait(1); } }
Program
Import programmini
demo program of color sensor
Please log in to post comments.