Arduino library · Sensors

MD_TCS230: an example that runs

Yes. You can use the MD_TCS230 library online in Mokxi with nothing to install, and the sketch runs in your browser against the simulated part (Color sensor, TCS3200 / TCS230 (GY-31)). The example below, "Read the color of a card with a TCS3200", runs on the Arduino Uno R3.

Built in on Mokxi: #include <MD_TCS230.h> and it builds, with nothing to install. Free, and no account needed.

What MD_TCS230 does

TCS230 and TCS3200 color sensors over FreqCount: MD_TCS230 CS(s2, s3, s0, s1, oe), begin(), setFrequency(), setDarkCal(), setWhiteCal(), read(), available(), getRGB(), getRaw() and readSingle(). That is what MD_TCS230, by Marco Colli (MajicDesigns), covers. The sketch below uses it on the Arduino Uno: “Read the color of a card with a TCS3200”.

The MD_TCS230 library's interface (Marco Colli, MajicDesigns, 1.2), for the TCS230 and TCS3200 color-to-frequency sensors, read with FreqCount.

S0 and S1 set the output scaling (setFrequency(): TCS230_FREQ_HI 100%, MID 20%, LO 2%, OFF powered down), S2 and S3 the filter, and OE, when wired, is driven low by setEnable(true). The constructors that leave S0 and S1 or OE out are for boards with those pins strapped.

The calls follow release 1.2.3 of the library, so a sketch written here builds on a real board too. The example is the one Mokxi's own tests build and run, so what is printed here is what runs.

Library Manager nameMD_TCS230
AuthorMarco Colli (MajicDesigns)
Include#include <MD_TCS230.h>
BoardsArduino Uno, Arduino Nano, Arduino Pro Mini 3.3 V
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesColor sensor, TCS3200 / TCS230 (GY-31)

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Pro Mini 3.3 V.

In Mokxi it is built in: #include <MD_TCS230.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "MD_TCS230" by Marco Colli (MajicDesigns) from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add MD_TCS230 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.2.3, so a sketch written here builds there too.

What to know in the simulator: OUT goes to pin 5, FreqCount's pin, so it builds where FreqCount.h does. getRGB() reads 0 until the dark and white calibrations are set.

The calls you will use

In the order a sketch meets them.

MD_TCS230 CS(S2_OUT, S3_OUT, S0_OUT, S1_OUT, OE_OUT)

Makes CS, the MD_TCS230 object the calls below are made on.

CS.begin()

Starts it up. Call it once in setup().

CS.setFrequency(TCS230_FREQ_MID)

Sets the frequency to the value you pass in.

CS.setDarkCal(&darkCal)

Sets the dark cal to the value you pass in.

CS.setWhiteCal(&whiteCal)

Sets the white cal to the value you pass in.

Example: Read the color of a card with a TCS3200

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <MD_TCS230.h>
#include <FreqCount.h>

// OUT goes to pin 5, the pin FreqCount counts on.
#define S2_OUT 8
#define S3_OUT 9
#define S0_OUT 10
#define S1_OUT 11
#define OE_OUT 12

MD_TCS230 CS(S2_OUT, S3_OUT, S0_OUT, S1_OUT, OE_OUT);

// Raw readings in Hz of a black card and a white one, from the calibration sketch.
sensorData darkCal = { { 900, 990, 900 } };
sensorData whiteCal = { { 17920, 19830, 17920 } };

void setup() {
  Serial.begin(57600);
  CS.begin();
  CS.setFrequency(TCS230_FREQ_MID);  // 20% scaling
  CS.setDarkCal(&darkCal);
  CS.setWhiteCal(&whiteCal);
  CS.read();
}

void loop() {
  if (CS.available()) {
    sensorData raw;
    colorData rgb;
    CS.getRaw(&raw);
    CS.getRGB(&rgb);
    Serial.print("Hz: ");
    Serial.print(raw.value[TCS230_RGB_R]);
    Serial.print(" ");
    Serial.print(raw.value[TCS230_RGB_G]);
    Serial.print(" ");
    Serial.println(raw.value[TCS230_RGB_B]);
    Serial.print("RGB: ");
    Serial.print(rgb.value[TCS230_RGB_R]);
    Serial.print(" ");
    Serial.print(rgb.value[TCS230_RGB_G]);
    Serial.print(" ");
    Serial.println(rgb.value[TCS230_RGB_B]);
    CS.read();
  }
}

Wiring

  • Arduino Uno R3 5 to Color sensor, TCS3200 / TCS230 (GY-31) OUT
  • Arduino Uno R3 8 to Color sensor, TCS3200 / TCS230 (GY-31) S2
  • Arduino Uno R3 9 to Color sensor, TCS3200 / TCS230 (GY-31) S3
  • Arduino Uno R3 10 to Color sensor, TCS3200 / TCS230 (GY-31) S0
  • Arduino Uno R3 11 to Color sensor, TCS3200 / TCS230 (GY-31) S1
  • Arduino Uno R3 12 to Color sensor, TCS3200 / TCS230 (GY-31) OE
  • Arduino Uno R3 5V to Color sensor, TCS3200 / TCS230 (GY-31) VCC
  • Arduino Uno R3 GND to Color sensor, TCS3200 / TCS230 (GY-31) GND

Open it in the editor, wired and ready to run

Try MD_TCS230 without installing anything

Open the example, change the sketch, and keep your version in a free account.