Near-field Reflected Colour

Similar to the previous ambient light/colour recipe, this uses the same TCS34725 RGB colour sensor, but activates the onboard illumination LED so that objects held right over the sensor can have their colour registered. Take note that the wiring is slightly different (the LED pin is now connected to the INT pin). The code is exactly the same as the other TCS34725 recipe.

I2C devices communicate to the host microcontroller (in our case, the Photon) via 2 important pins – SCL and SDA. On the Photon, pin D0 = SDA, and D1 = SCL. These cannot be reassigned, so every time you use a sensor with I2C, you have to reserve D0 and D1 for communicating with them.

We also need to adjust the code to allow the on-board illumination LED to turn on when we bring an object over the sensor.

Because these are I2C sensors, getting Blynk to ‘talk’ to it will require a bit more code. Check the sample below and notice how we are assigning ‘virtual’ pins so that your Blynk app can pick up on them.

Using the IFTTT service to log data using this sensor is pretty much the same – the publish command works just like the analog sensors to log, for example, a new row to Google spreadsheets.


Libraries Used

(learn how to import them in the Build IDE):


Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_TCS34725.h>
#include <math.h>

// how long between each log update? feel free to change this value,
// but this must not be anything less than 1000 milliseconds!
#define INTERVAL_BETWEEN_LOGS 5000

// initialise the timer
Timer dataTimer(INTERVAL_BETWEEN_LOGS, doDataUpdate);

// initialise the TCS34725 light sensor
char szInfo[128];       // temporary C string to store
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);


// code in this setup function runs just once, when Photon is powered up or reset
void setup() {
    Serial.begin(9600);         // Open a connection via the Serial port / USB cable – useful for debugging

    if (tcs.begin()) {
        Serial.println("Found sensor");
    } else {
        Serial.println("No TCS34725 found ... check your connections");
    }

    delay(5000);                // Common practice to allow board to 'settle' after connecting online

    dataTimer.start();
}

// code in this loop function runs forever, until you cut power!
// for the A/D blackbox, there is nothing to do in here because data updates are handled by our timer
void loop() {

}

// doDataUpdate runs every interval set in INTERVAL_BETWEEN_LOGS
void doDataUpdate() {
    // IMPORTANT: to prevent server overload, the Particle cloud can only accept
    // update rates of once per second, with the option to 'burst' 4 updates in a
    // second (but then you'll get blocked for the next 4 seconds). 'Ration' your
    // INTERVAL_BETWEEN_LOGS and the number of readings you are publishing; in our
    // default example, we are frugally using just 1 publish, by concatenating
    // all the data we want into a single publish statement

    // first we save what we want to read into temporary variables first.
    // feel free to add/remove these lines as you see fit in your application:

    uint16_t clear, red, green, blue;

    // grab colour data and store into variables
    tcs.getRawData(&red, &green, &blue, &clear);        // takes 50ms to read!

    delay(60);

    // Figure out some basic hex code for visualization
    uint32_t sum = clear;
    float r, g, b;

    r = red; r /= sum;
    g = green; g /= sum;
    b = blue; b /= sum;

    // converts RGB to 8-bit values
    r *= 256; g *= 256; b *= 256;

    String res = "R:" + String(r) + ",G:" + String(g) + ",B:" + String(b);

    Serial.println(res);
    Particle.publish("sensorData", res);

}