multiple color recognition sensors on one arduino?

Hi everyone,

I have followed this tutorial:

to use a Virtuabotix Color Recognition Sensor with an RGB LED. I want to expand on this tutorial with multiple LEDs and color recognition sensors (trying to make a simplified screen with 8 LEDS). Would I need 8 separate arduinos for this, or can multiple sensors run from a single arduinos, registering different output colors to separate LEDs?

Appreciate any advice, below I have uploaded my code incase it is helpful.

sketch_for_color_sensor.ino (2.94 KB)

Each sensor needs 3 pins to get the signal from the sensor. So you can have 6 sensor on one UNO, and more than 8 on a Mega.

Please use an external 5V power supply, for the no arduino will be able to output the current needed safely. (If you attempt to do this, you may harm the arduino or even burn it out).

Okay, thanks. Which pins can share a port on the arduino and which must have their own?

I dont know yet, so for now I would skip digital pins 0 & 1(skipping these allows you to use the serial monitor), and start from digital pin 2 and so on.

I'm not sure how to redefine the int pins in the code for the second sensor and so on- obviously I can't define them as contradicting pin numbers, but I want one sensor to power one LED, another to power a different LED and so on.

This should get you started. (Please note, I used dummy values for the pins. The LED pins should not be A0 - A2, but on a pin that has the ~ symbol next to it.)

Kinda fancy, but it should work as a good example.

int redPin = A0; //red pin 
int greenPin = A1; //green pin
int bluePin = A2; //blue pin

//int S2= 7; //color sensor pin s2 to ard pin 7
//int S3=8; //color sensor pin s3 to ard pin 8
//int outPin=4; //color sensor pin OUT to ard pin 4

// 5 Sensors MAX on an UNO, NANO or similar
// +8 on a MEGA
#define Num_of_Sensors 4

byte Sensors[Num_of_Sensors][3] = {
  //{S2, S3, Outpin}
  {2, 3, 4}, //Sensor 1
  {5, 6, 7}, // Sensor 2
  {8, 9, 10}, // Sensor 3
  {11, 12, 13} // sensor 4 and so on
};

// JUST MAKE OE TO VCC
//int OE=3; //i have no idea what OE does but it differs from the virtuabotix sensor used in the tutorial. commenters suggest OE must be set to HIGH

byte rColorStrength, gColorStrength, bColorStrength;
byte _sensor = 0;

void setup ()
{
  //put your setup code here, to run once:

  Serial.begin(115200); //turns on the serial port

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  for (byte i = 0; i < Num_of_Sensors; i++)
  {
    pinMode(Sensors[i][0], OUTPUT); // S2
    pinMode(Sensors[i][1], OUTPUT); // S3
    pinMode(Sensors[i][2], INPUT); // output 
  }
  //pinMode(OE, INPUT);
}

void loop ()
{
  //digitalWrite(OE, HIGH); // not needed

  //put your main code here, to run repeatedly:
  //start by reading the red component of the color
  //S2 and S3 should be set LOW, as follows:

  //Such Fanciness
  Color( _sensor , &rColorStrength, &gColorStrength, &bColorStrength); // cycle throught the sensors and collect the colors for each 

  if ((rColorStrength > gColorStrength) && (gColorStrength > bColorStrength)) 
  {
    rColorStrength = 255;
    gColorStrength = gColorStrength / 2;
    bColorStrength = 0;
  }

  if (rColorStrength > bColorStrength && bColorStrength > gColorStrength) 
  {
    rColorStrength = 255;
    bColorStrength = bColorStrength / 2;
    gColorStrength = 0;
  }

  if (gColorStrength > rColorStrength && rColorStrength > bColorStrength) 
  {
    gColorStrength = 255;
    rColorStrength = rColorStrength / 2;
    bColorStrength = 0;
  }

  if (gColorStrength > bColorStrength && bColorStrength > rColorStrength) 
  {
    gColorStrength = 255;
    bColorStrength = bColorStrength / 2;
    rColorStrength = 0;
  }

  if (bColorStrength > rColorStrength && rColorStrength > gColorStrength) 
  {
    bColorStrength = 255;
    rColorStrength = rColorStrength / 2;
    gColorStrength = 0;
  }

  if (bColorStrength > gColorStrength && gColorStrength > rColorStrength) 
  {
    bColorStrength = 255;
    gColorStrength = gColorStrength / 2;
    rColorStrength = 0;
  }

  bColorStrength = float(bColorStrength) * .5;
  gColorStrength = float(gColorStrength) * .75;
  
  Serial.print("Sensor: ");
  Serial.print(_sensor);
  Serial.print(" | ");
  Serial.print(rColorStrength);
  Serial.print(", ");
  Serial.print(gColorStrength);
  Serial.print(", ");
  Serial.println(bColorStrength);
  if(_sensor == 3)
    Serial.println("-----------------------");


  analogWrite(redPin, rColorStrength);
  analogWrite(greenPin, gColorStrength);
  analogWrite(bluePin, bColorStrength);

  if (_sensor < Num_of_Sensors)
    _sensor++;
  else
    _sensor = 0;

}

void Color(byte idx, byte * R, byte * G, byte * B)
{
  const byte colors[3] = {0x0, 0x3, 0x1};
  unsigned int pulseWidth[3] = {0};

  byte ColorValue;
  for (byte C = 0; C < 3; C++)
  {
    // 0 = RED, 1 = BLUE, 3 = GREEN
    digitalWrite(Sensors[idx][0], colors[C] & 0x2); // these strip off the correct bit per color
    digitalWrite(Sensors[idx][1], colors[C] & 0x1);

    pulseWidth[idx] = pulseIn (Sensors[idx][2], LOW);

    ColorValue = (255 - (pulseWidth[idx] / 400) );
    switch (C)
    {
      case 0:
        *R = ColorValue;
        break;
      case 3:
        *G = ColorValue;
        break;
      case 1:
        *B = ColorValue;
        break;
    }
  }
}

I hooked up the sensors according to your code, but the sensors are not registering color and there is no data coming in on the sensor.

Do some debugging in the color function to track what is doing what. I don't have your sensors.

Also, I'm not sure but wouldn't the RGB pins need to be assigned pins on the sensor to correspond?

It's picking up no data at all- in the code I sent over the sensor works fine. Just trying to get multiple sensors operating under that code.

Put your original pins in that first row of the array an set the number of sensors to 1.

If you still can't get it to work, I will take another look at it when I get home tonight.

I modified the code slightly.

int redPin = A0; //red pin
int greenPin = A1; //green pin
int bluePin = A2; //blue pin

//int S2= 7; //color sensor pin s2 to ard pin 7
//int S3=8; //color sensor pin s3 to ard pin 8
//int outPin=4; //color sensor pin OUT to ard pin 4

// 5 Sensors MAX on an UNO, NANO or similar
// +8 on a MEGA
#define Num_of_Sensors 1

byte Sensors[Num_of_Sensors][3] = {
  //{S2, S3, Outpin}
  {2, 3, 4}, //Sensor 1
  /*{5, 6, 7}, // Sensor 2
  {8, 9, 10}, // Sensor 3
  {11, 12, 13} // sensor 4 and so on*/
};

// JUST MAKE OE TO VCC
//int OE=3; //i have no idea what OE does but it differs from the virtuabotix sensor used in the tutorial. commenters suggest OE must be set to HIGH

byte rColorStrength, gColorStrength, bColorStrength;
byte _sensor = 0;

void setup ()
{
  //put your setup code here, to run once:

  Serial.begin(115200); //turns on the serial port

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);

  for (byte i = 0; i < Num_of_Sensors; i++)
  {
    pinMode(Sensors[i][0], OUTPUT); // S2
    pinMode(Sensors[i][1], OUTPUT); // S3
    pinMode(Sensors[i][2], INPUT); // output
  }
  //pinMode(OE, INPUT);
}

void loop ()
{
  //digitalWrite(OE, HIGH); // not needed

  //put your main code here, to run repeatedly:
  //start by reading the red component of the color
  //S2 and S3 should be set LOW, as follows:
  
  if (_sensor >= Num_of_Sensors)
    _sensor = 0;

  //Such Fanciness
  Color( _sensor , &rColorStrength, &gColorStrength, &bColorStrength); // cycle throught the sensors and collect the colors for each

  if ((rColorStrength > gColorStrength) && (gColorStrength > bColorStrength))
  {
    rColorStrength = 255;
    gColorStrength = gColorStrength / 2;
    bColorStrength = 0;
  }

  if (rColorStrength > bColorStrength && bColorStrength > gColorStrength)
  {
    rColorStrength = 255;
    bColorStrength = bColorStrength / 2;
    gColorStrength = 0;
  }

  if (gColorStrength > rColorStrength && rColorStrength > bColorStrength)
  {
    gColorStrength = 255;
    rColorStrength = rColorStrength / 2;
    bColorStrength = 0;
  }

  if (gColorStrength > bColorStrength && bColorStrength > rColorStrength)
  {
    gColorStrength = 255;
    bColorStrength = bColorStrength / 2;
    rColorStrength = 0;
  }

  if (bColorStrength > rColorStrength && rColorStrength > gColorStrength)
  {
    bColorStrength = 255;
    rColorStrength = rColorStrength / 2;
    gColorStrength = 0;
  }

  if (bColorStrength > gColorStrength && gColorStrength > rColorStrength)
  {
    bColorStrength = 255;
    gColorStrength = gColorStrength / 2;
    rColorStrength = 0;
  }

  bColorStrength = float(bColorStrength) * .5;
  gColorStrength = float(gColorStrength) * .75;

  Serial.print("Sensor: ");
  Serial.print(_sensor);
  Serial.print(" | ");
  Serial.print(rColorStrength);
  Serial.print(", ");
  Serial.print(gColorStrength);
  Serial.print(", ");
  Serial.println(bColorStrength);
  if (_sensor == 3)
    Serial.println("-----------------------");


  analogWrite(redPin, rColorStrength);
  analogWrite(greenPin, gColorStrength);
  analogWrite(bluePin, bColorStrength);
  
  _sensor++;
}

void Color(byte idx, byte * R, byte * G, byte * B)
{
  const byte colors[3] = {0x0, 0x3, 0x1};
  unsigned int pulseWidth[3] = {0};

  byte ColorValue;
  for (byte C = 0; C < 3; C++)
  {
    // 0 = RED, 1 = BLUE, 3 = GREEN
    digitalWrite(Sensors[idx][0], colors[C] & 0x2); // these strip off the correct bit per color
    digitalWrite(Sensors[idx][1], colors[C] & 0x1);

    pulseWidth[idx] = pulseIn (Sensors[idx][2], LOW);

    ColorValue = (255 - (pulseWidth[idx] / 400) );
    switch (C)
    {
      case 0:
        *R = ColorValue;
        break;
      case 3:
        *G = ColorValue;
        break;
      case 1:
        *B = ColorValue;
        break;
    }
  }
}

Did you jump the OE pin to the VCC, or not? Because it should be connected to VCC, maybe that is why nothing is working right.

Do you mean that I should connect OE to the 5v rail that VCC is also connected to? If so, no it still doesn't work.

What exactly isn't working post your code with your changed pins.

red pin = 11
green pin = 10
blue pin = 6

s0 = gnd
s1 = 5v
oe is not connected
gnd = gnd
vcc = 5v
out = pin 4
s2 = pin 2
s3 = pin 3

Still no data coming in from the sensor. Code is attached

arduino_code_color_sensor.ino (3.71 KB)

OE must be connected to VCC. did you check to see if it still works on your old code?