Color sensor RGB

Hello everyone,

I am really new to Arduino, so sorry, if i ask noob questions here.

i am planning to connect the color sensor adafruit TCS34725 (https://eu.mouser.com/datasheet/2/737/TCS34725-932784.pdf) to my arduino micro and i would like to transform the captured data into midi CC messages. So this thing should act like a MIDI-USB Device.

First i have a question about the wiring:
Here are my connections:
VIN to 5V
Gnd to Gnd
SDA to 2
SCL to 3

Is this correct? Here is a link to my arduino board pdf: https://content.arduino.cc/assets/Pinout-Micro_latest.pdf

Later i would like to include buttons and potentiometers, but i thought, at first i need to make this work. So i first just try to get Data from the Sensor.

So i copied this CODE and uploaded it.
MODERATOR EDIT- added code tags

#include "Wire.h"
#include "Adafruit_TCS34725.h"

/* Example code for the Adafruit TCS34725 breakout library */
 
/* Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground */
 
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();
 
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
 
void setup(void) {
Serial.begin(9600);
 
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
 
// Now we're ready to get readings!
}
 
void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;
 
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
 
Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
Serial.println(" ");
}

But i dont get any data. Any idea why?

I know this code was initially written for Arduino uno. And i found out (but also not sure), that this wire.h library is not for micro boards. Is that the problem? And how can i solve this?

Many many thanks in advance!
Norbert

Why do newcomers not read and follow the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

Sorry! Didn't read. I hope this is right now:

#include <Wire.h>
#include "Adafruit_TCS34725.h"

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL to analog 5
  Connect SDA to analog 4
  Connect VDD to 3.3V DC
  Connect GROUND to common ground */

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
//Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup(void) {
  Serial.begin(9600);

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

  // Now we're ready to get readings!
}

void loop(void) {
  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature(r, g, b);
  lux = tcs.calculateLux(r, g, b);

  Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
  Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
  Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
  Serial.println(" ");
}

I hope this is all the information you need.
Thanks in advance

how are you powering your Arduino? (if you want 5V it's better to take the regulated 5V output probably depending how you power your arduino)

which SCL and SDA lines did you use?

do you have a naked TCS3472 or the adafruit board

posting a clear drawing of your wiring & power supply would probably help

(of course you serial monitor is opened at 9600 bauds, right ?)

Hello @nrbt
Which component are you gonna run through the midi usb and what is the use of Color sensor ?
Are you gonna run the output of middi usb to some musical instrument or just a mixer ?
Have you got any schematic or hand made circuit diagram of what are you gonna do ?
Need more information to help
Lucky

Hi, new user here, but not new to Arduino environments and I have played around with the TCS3472 for a bit (as well as a few other color sensing methods).

@nrbt Your problem is that you haven't set up and started a measurement before you start reading out color values in your main loop. You need to configure the TCS and set the desired integration time and indicate what measurement method you want to use (continuous measurement would be appropriate in your case, most likely). Check the datasheet of the TCS3472 and the .h file of the library you use; it's explained all quite well. In the datasheet pay in particular attention to the sections where the different measurement modes are explained and take a look at the state diagrams for the sensor. This will make clear what settings you need to apply over I2C before you can start reading out sensor values.

Note also that you will have to think about the integration time which essentially determines the sensitivity of your measurement (it will also influence how often you can get new data from the sensor, or in other words, influence your sampling rate).

Hi, thanks for all your answers! My first forum moment to be honest.

@J-M-L:
I am powering my arduino via usb. I have the adafruit that is shown on your image.
I made a fritzing schematics here:

Yes, i have my serial monitor opened.

@justinlucky:
I finally want to send the data into pure data and control frequencies or fx with it. So i want my arduino to send out cc values. That would be best.

@anon35827816:
thanks a lot, i will dig into the datasheet and try my best. maybe i come back with more questions...

Have a go at it. Keep in mind that as long as you haven't started an actual measurement on the sensor (I usually do this in setup()), there will never be any data to read. The examples of the library you use likely make this clear as well. Just scan for lines that refer to the TCS object and ask yourself what each function call does and why. Once you can explain that to yourself, you should be able to set up your own measurements.

ok. thanks!

That is not a schematic, it is a "breadboard view". Switch Fritzing to "Schematic View". You will need to draw the wiring in schematic view, like you did in breadboard view, but you will notice that Fritzing puts dotted lines to remind you where to draw the wires, based on the connections from the breadboard view.

@PaulRB
ok, true. Thanks.

Ok, i solved my first problem: It wasn't a problem with the code. It was the wiring on the breadboard. It was corrupt. Now this is solved and i really get messages out of my serial monitor. Sorry for this, i should have checked that twice.

My next step was to "convert" the data (so Lux, RGB ...) into MIDI CC. My first thought was to orient on my last project, in which i made a light sensor to work with MIDI. The Code that i had (as it is now) is here:

#include "MIDIUSB.h"
 
void setup()
{
    Serial.begin(9600);
  }
 
void loop()
{
  uint16_t analogValue = analogRead(A0);   
  uint8_t  analogSend = analogValue >> 3;
  midiEventPacket_t midiCc = { 0x0B, 0xB0, 0, analogSend };
  MidiUSB.sendMIDI(midiCc);
  MidiUSB.flush();
  delay(20);

}

You think it is possible to adapt it to this project? Or is analogRead (sure with another pin...)
I will need the map function, right?

Sorry again, if this is too nobish!

sure

the magic in your other project was this line

  uint8_t  analogSend = analogValue >> 3;

where you were dividing by 8 the analog value you read to get a number between 0 and 127

you need to find a mathematical relation that will map Lux or RGB data into a similar value.

Great! Thanks! I think it works!
I managed to get midi values out for Red with this code:

#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "MIDIUSB.h"

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL to analog 5
  Connect SDA to analog 4
  Connect VDD to 3.3V DC
  Connect GROUND to common ground */

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
//Adafruit_TCS34725 tcs = Adafruit_TCS34725();

/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
uint8_t valueR = 0;
uint8_t valueG = 0;
uint8_t valueB = 0;

void setup(void) {
  Serial.begin(9600);

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

  // Now we're ready to get readings!
}

void loop(void) {
  uint16_t r, g, b, c, colorTemp, lux;

  tcs.getRawData(&r, &g, &b, &c);
  colorTemp = tcs.calculateColorTemperature(r, g, b);
  lux = tcs.calculateLux(r, g, b);

  Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
  Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
  Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
  Serial.println(" ");

  uint8_t  valueR = r >> 8;
  uint8_t  valueG = g >> 8;
  uint8_t  valueB = b >> 8;
  Serial.print("valueR: "); Serial.print(valueR, DEC); Serial.print(" ");
  Serial.print("valueG: "); Serial.print(valueG, DEC); Serial.print(" ");
  Serial.print("valueB: "); Serial.print(valueB, DEC); Serial.print(" ");
  Serial.println(" ");
  midiEventPacket_t midiCc = { 0x0B, 0xB0, 1, valueR };

  MidiUSB.sendMIDI(midiCc);
  MidiUSB.flush();
  delay(50);

}

Now i am thinking about, how i can send out 3 different midi control messages.

Thanks again! This is really great help!

... i meant different cc messages for each colour.

which MIDIUSB library are you using ?

It is this:
midiusb.h

OK you should look at what are the possibilities of a midiEventPacket_t and send different events

Thanks. i will try to understand the midiEventPacket_t argument.

Do you also think i will have to work with voids? I am not so familiar with this but i am trying to dive into it.

Now another problem occured: I tested it out in ableton, where i control a synth with it. It works nicely. But in pure data, the cc messages are delayed by around 2 Seconds. I find this very weird. I know this isn't a pure data forum. But i ask myself, if it has something to do with my arduino code... I also tried to set a delay after my midiEventPacket_t but it didn't change anything... Any idea?

What you are calling voids are actually called functions. Function definitions are preceded with a data type indicating what type of data the function will return. If no data is to be returned then the data type of void is used

what does take 2 seconds ? the moment in the code where you do MidiUSB.sendMIDI(midiCc);and the moment you see it on the other side ?