The best tools to make your project dreams come true

Login or Signup
USD


By Maker.io Staff

Build Your Own Photon NeoPixel Sensor

Previously, we learned how to use the TCS3200 sensor on the Photon to read color values and how to use NeoPixels with the Photon. In this project, we will combine the two to create a color scanner that sets the colors on a NeoPixel to the read color.

 

BOM

Scheme-It

 

Scheme-It

The Code

Copy Code
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>
#include <math.h>
#include "Particle.h"

#define PIXEL_COUNT 24 // 24 Pixels on our ring
#define PIXEL_PIN D6 // Ring uses D6 as default pin
#define PIXEL_TYPE WS2812B // Ring uses WS2812 Pixels

Adafruit_NeoPixel ring(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE); // Create out “ring” object

// Function prototypes
void sensor_red(void);
void sensor_green(void);
void sensor_blue(void);
float get_colorIntensity(void);

// Min / Max values for the recorded colors
int rMax = 861;
int rMin = 15000;
int gMax = 15;
int gMin = 270;
int bMax = 17;
int bMin = 300;


// Setup function
void setup()
{
Serial.begin(9600);

// TCS3200 connections
pinMode(D0, INPUT); // OUT
pinMode(D1, OUTPUT); // S2
pinMode(D2, OUTPUT); // S3
pinMode(D3, OUTPUT); // OE
pinMode(D4, OUTPUT); // S0
pinMode(D5, OUTPUT); // S1

// TCS3200 Frequency
digitalWrite(D4, HIGH); // 20% frequency
digitalWrite(D5, LOW);

// Enable TCS3200 output (active low)
digitalWrite(D3, LOW);

// Start the NeoPixel object
ring.begin();

// Set the brightness of the pixels to 30%
ring.setBrightness(30);

}


// Main loop
void loop()
{
// This code is used to calibrate the sensor output and uncommented once min / max values have been recorded
/*
sensor_red();
Serial.print("R: " + String(get_colorIntensity()) + " ");
sensor_green();
Serial.print("G: " + String(get_colorIntensity()) + " ");
sensor_blue();
Serial.print("B: " + String(get_colorIntensity()) + " ");
Serial.println();
*/

// Read each color and map it accordingly
sensor_red();
int redValue = map(int(get_colorIntensity()), rMin, rMax, 0, 255);
sensor_green();
int greenValue = map(int(get_colorIntensity()), gMin, gMin, 0, 255);
sensor_blue();
int blueValue = map(int(get_colorIntensity()), bMin, bMax, 0, 255);

// Set all the NeoPixels to the Red, Green, and Blue values recorded by the TCS3200
for(int i = 0; i < PIXEL_COUNT; i ++)
{
ring.setPixelColor(i, uint8_t(redValue), uint8_t(greenValue), uint8_t(blueValue));
}

// Update the NeoPixel
ring.show();
}


// S2 = 0 and S3 = 0
void sensor_red(void)
{
digitalWrite(D2, LOW);
digitalWrite(D3, LOW);
}

// S2 = 0 and S3 = 1
void sensor_blue(void)
{
digitalWrite(D2, LOW);
digitalWrite(D3, HIGH);
}

// S2 = 1 and S3 = 1
void sensor_green(void)
{
digitalWrite(D2, HIGH);
digitalWrite(D3, HIGH);
}


float get_colorIntensity(void)
{
return pulseIn(D0, HIGH);
}

Configuration

The idea behind this project is to create a color display that sets its color to whatever color is presented to a TCS3200 sensor. While this could be achieved with multicolor LEDs, NeoPixels provide an incredibly bright and easy-to-use interface that accepts digital numbers (between 0 and 255) commonly used in computer displays. However, getting this project to work requires a calibration step, consideration of the surrounding lighting, and a determination of the distance between the TCS3200 and the object being measured.

When running the code for the first time, the section of code that is normally commented out should be uncommented, and then a serial connection formed between a PC and the Photon.

Copy Code
    // This code is used to calibrate the sensor output and uncommented once min / max values have
// been recorded
/*
sensor_red();
Serial.print("R: " + String(get_colorIntensity()) + " ");
sensor_green();
Serial.print("G: " + String(get_colorIntensity()) + " ");
sensor_blue();
Serial.print("B: " + String(get_colorIntensity()) + " ");
Serial.println();
*/

This serial connection will have a range of values dumped to the terminal that show the raw values from the get_colorIntensity() function. With the TCS3200 mounted in the final location, hold a small white card in front of the sensor (where objects will be measured) and record the values from the terminal window. Then hold a small black card in front of the sensor and record these values. You should now have a max and min value for each color and insert these values into the code variables shown below. The sensor will now be configured and calibrated for further use.

Copy Code
// Min / Max values for the recorded colors
int rMax = 861;
int rMin = 15000;
int gMax = 15;
int gMin = 270;
int bMax = 17;
int bMin = 300;

Start-up code

As with every other Arduino-style microcontroller, the start-up code is executed once and is found in the setup() function. In this project, we configure the I/O pins with the TCS3200 accordingly, as well as starting the NeoPixel object. The brightness of the NeoPixel is increased to 30% in this project, as low brightness values make it harder to see combined colors (i.e., the individual LEDs can be seen instead of just one blended color).

Main Loop

The main loop of code (found in loop() ) simply chooses each color, records the value, and scales it using the max and min values obtained from the calibration stage. When each color has been read, the now-adjusted color values are directly passed to the NeoPixel object, which updates the LEDs (after ring.show() is called).

Key Parts and Components

Add all Digi-Key Parts to Cart
  • 1738-1035-ND
  • 1878-1005-ND
  • 1528-1825-ND
  • 1738-1035-ND
  • 1738-1326-ND