SENSOR DE COLOR CON EL MODULO I2C. TCS34725 PASA VALORES A UNA TERMINAL SERIAL. PRUEBA USANDO EL CAPUCHÓN DE UN MARCADOR DE COLOR ROJO.

Dependencies:   mbed

https://os.mbed.com/media/uploads/tony63/color.png

CONEXION DEL CIRCUITO A UNA PLACA FRDMKL25Z

https://os.mbed.com/media/uploads/tony63/color1.png

RESPUESTA EN UNA CONSOLA SERIAL TERMITE: AL COLOCAR UN MARCADOR ROJO COMO MUESTRA LA FOTO DE ARRIBA

Committer:
tony63
Date:
Tue Mar 10 00:34:31 2020 +0000
Revision:
0:cbc56fa99411
SENSOR DE COLOR TCS34725

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony63 0:cbc56fa99411 1 #include "mbed.h"
tony63 0:cbc56fa99411 2
tony63 0:cbc56fa99411 3 #include "mbed.h"
tony63 0:cbc56fa99411 4
tony63 0:cbc56fa99411 5 I2C i2c(PTE0, PTE1); //pins for I2C communication (SDA, SCL)
tony63 0:cbc56fa99411 6 Serial pc(USBTX, USBRX); //Used to view the colors that are read in
tony63 0:cbc56fa99411 7
tony63 0:cbc56fa99411 8 int sensor_addr = 41 << 1;
tony63 0:cbc56fa99411 9
tony63 0:cbc56fa99411 10 DigitalOut green(LED1);
tony63 0:cbc56fa99411 11 DigitalOut led(PTC12);
tony63 0:cbc56fa99411 12
tony63 0:cbc56fa99411 13 int main() {
tony63 0:cbc56fa99411 14 pc.baud(9600);
tony63 0:cbc56fa99411 15 green = 1; // off
tony63 0:cbc56fa99411 16
tony63 0:cbc56fa99411 17 // Connect to the Color sensor and verify
tony63 0:cbc56fa99411 18
tony63 0:cbc56fa99411 19 i2c.frequency(200000);
tony63 0:cbc56fa99411 20
tony63 0:cbc56fa99411 21 char id_regval[1] = {146};
tony63 0:cbc56fa99411 22 char data[1] = {0};
tony63 0:cbc56fa99411 23 i2c.write(sensor_addr,id_regval,1, true);
tony63 0:cbc56fa99411 24 i2c.read(sensor_addr,data,1,false);
tony63 0:cbc56fa99411 25
tony63 0:cbc56fa99411 26 if (data[0]==68) {
tony63 0:cbc56fa99411 27 green = 0;
tony63 0:cbc56fa99411 28 wait (2);
tony63 0:cbc56fa99411 29 green = 1;
tony63 0:cbc56fa99411 30 } else {
tony63 0:cbc56fa99411 31 green = 1;
tony63 0:cbc56fa99411 32 }
tony63 0:cbc56fa99411 33
tony63 0:cbc56fa99411 34 // Initialize color sensor
tony63 0:cbc56fa99411 35
tony63 0:cbc56fa99411 36 char timing_register[2] = {129,0};
tony63 0:cbc56fa99411 37 i2c.write(sensor_addr,timing_register,2,false);
tony63 0:cbc56fa99411 38
tony63 0:cbc56fa99411 39 char control_register[2] = {143,0};
tony63 0:cbc56fa99411 40 i2c.write(sensor_addr,control_register,2,false);
tony63 0:cbc56fa99411 41
tony63 0:cbc56fa99411 42 char enable_register[2] = {128,3};
tony63 0:cbc56fa99411 43 i2c.write(sensor_addr,enable_register,2,false);
tony63 0:cbc56fa99411 44
tony63 0:cbc56fa99411 45 // Read data from color sensor (Clear/Red/Green/Blue)
tony63 0:cbc56fa99411 46 led = 1;
tony63 0:cbc56fa99411 47 while (true) {
tony63 0:cbc56fa99411 48 char clear_reg[1] = {148};
tony63 0:cbc56fa99411 49 char clear_data[2] = {0,0};
tony63 0:cbc56fa99411 50 i2c.write(sensor_addr,clear_reg,1, true);
tony63 0:cbc56fa99411 51 i2c.read(sensor_addr,clear_data,2, false);
tony63 0:cbc56fa99411 52
tony63 0:cbc56fa99411 53 int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
tony63 0:cbc56fa99411 54
tony63 0:cbc56fa99411 55 char red_reg[1] = {150};
tony63 0:cbc56fa99411 56 char red_data[2] = {0,0};
tony63 0:cbc56fa99411 57 i2c.write(sensor_addr,red_reg,1, true);
tony63 0:cbc56fa99411 58 i2c.read(sensor_addr,red_data,2, false);
tony63 0:cbc56fa99411 59
tony63 0:cbc56fa99411 60 int red_value = ((int)red_data[1] << 8) | red_data[0];
tony63 0:cbc56fa99411 61
tony63 0:cbc56fa99411 62 char green_reg[1] = {152};
tony63 0:cbc56fa99411 63 char green_data[2] = {0,0};
tony63 0:cbc56fa99411 64 i2c.write(sensor_addr,green_reg,1, true);
tony63 0:cbc56fa99411 65 i2c.read(sensor_addr,green_data,2, false);
tony63 0:cbc56fa99411 66
tony63 0:cbc56fa99411 67 int green_value = ((int)green_data[1] << 8) | green_data[0];
tony63 0:cbc56fa99411 68
tony63 0:cbc56fa99411 69 char blue_reg[1] = {154};
tony63 0:cbc56fa99411 70 char blue_data[2] = {0,0};
tony63 0:cbc56fa99411 71 i2c.write(sensor_addr,blue_reg,1, true);
tony63 0:cbc56fa99411 72 i2c.read(sensor_addr,blue_data,2, false);
tony63 0:cbc56fa99411 73
tony63 0:cbc56fa99411 74 int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
tony63 0:cbc56fa99411 75
tony63 0:cbc56fa99411 76 // print sensor readings
tony63 0:cbc56fa99411 77
tony63 0:cbc56fa99411 78 pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value);
tony63 0:cbc56fa99411 79 //The above code displays the red, green, and blue values read in by the color sensor.
tony63 0:cbc56fa99411 80 wait(0.5);
tony63 0:cbc56fa99411 81 }
tony63 0:cbc56fa99411 82
tony63 0:cbc56fa99411 83 }