TCS3200 senser when stepper is stationary, fails to detect when stepper is rotating

I am using a 28BYJ-48 stepper motor to rotate a disc that holds M&Ms to be detected and sorted by color using the TCS3200 color sensor.

Here is the code:






#include <tcs3200.h>
#include <Servo.h>
#include <Stepper.h>

#define DecisionServoPin 9
#define numColors 6
#define S0 4
#define S1 5
#define S2 7
#define S3 6
#define sensorOut 8

Servo DecisionServo;
tcs3200 colorSensor(S0, S1, S2, S3, sensorOut);
Stepper myStepper(2048, 13, 11, 12, 10);

unsigned long stepperDelay = 1000;         // Delay between stepper motor steps (in milliseconds)
unsigned long colorSenseInterval = 500;   // Interval between color sensing (in milliseconds)
unsigned long previousStepperTime = 0;
unsigned long previousColorSenseTime = 0;
bool isStepperMoving = false;
int color = 0;
int R;
int B;
int G;
int RedM = 0;
int OrangeM = 0;
int GreenM = 0;
int YellowM = 0;
int BrownM = 0;
int BlueM = 0;
int TotalM = 0;

void setup() {
  DecisionServo.attach(DecisionServoPin);
 
  Serial.begin(9600);
}

void loop() {
  unsigned long currentMillis = millis();

  // Color sensing
  if (currentMillis - previousColorSenseTime >= colorSenseInterval) {
    colorSensing();
    previousColorSenseTime = currentMillis;
  }

  myStepper.setSpeed(15);

  // Stepper motor movement
  if (isStepperMoving) {
    if (currentMillis - previousStepperTime >= stepperDelay) {
      myStepper.step(2048 / 8);
      previousStepperTime = currentMillis;
      isStepperMoving = false;  // Pause after each stepper motor movement
    }
  } else { 
    isStepperMoving = true; 
  }
  

  switch (color) {
    case 1:
      DecisionServo.write(0);
      break;
    case 2:
      DecisionServo.write(30);
      break;
    case 3:
      DecisionServo.write(60);
      break;
    case 4:
      DecisionServo.write(120);
      break;
    case 5:
      DecisionServo.write(150);
      break;
    case 6:
      DecisionServo.write(180);
      break;
    case 0:
      
      break;
  }
  delay(500);
}

void colorSensing() {
  R = colorSensor.colorRead('r', 100);  //reads color value for red
  Serial.print("R= ");
  Serial.print(R);
  Serial.print("    ");

  G = colorSensor.colorRead('g', 100);  //reads color value for green
  Serial.print("G= ");
  Serial.print(G);
  Serial.print("    ");

  B = colorSensor.colorRead('b', 100);  //reads color value for blue
  Serial.print("B= ");
  Serial.print(B);
  Serial.print("    ");

  Serial.println();

  if (R <= 38 && R >= 35 && G <= 45 && G >= 27 && B <= 52 && B >= 33) {
    color = 1;  // Red
    RedM++;
    TotalM++;
    Serial.println("Red: " + String(RedM));
  } else if (R <= 66 && R >= 62 && B <= 58 && B >= 52 && G <= 52 && G >= 50) {
    color = 2;  // Orange
    OrangeM++;
    TotalM++;
    Serial.println("Orange: " + String(OrangeM));
  } else if ( R <= 45 && R >= 43 && G <= 55 && G >= 52 && B <= 58  && B >= 55) {
    color = 3;  // Green
    GreenM++;
    TotalM++;
    Serial.println("Green: " + String(GreenM));
  } else if (R <= 71 && R >= 62 && B <= 62 && B >= 58 && G <= 62 && G >= 58) {
    color = 4;  // Yellow
    YellowM++;
    TotalM++;
    Serial.println("Yellow: " + String(YellowM));
  } else if (R <= 40 && R >= 35 && B <= 45 && B >= 35 && G <= 43 && G >= 35) {
    color = 5;  // Brown
    BrownM++;
    TotalM++;
    Serial.println("Brown: " + String(BrownM));
  } else if (R <= 45 && R >= 38 && B <= 71 && B >= 66 && G <= 50 && G >= 45){
    color = 6;  // Blue
    BlueM++;
    TotalM++;
    Serial.println("Blue: " + String(BlueM));
  } else {
    color = 0;  // Undetermined
  }
  
}

void termination() {
  myStepper.setSpeed(0);
  Serial.println("Total Red: " + String(RedM));
  Serial.println("Total Orange: " + String(OrangeM));
  Serial.println("Total Green: " + String(GreenM));
  Serial.println("Total Yellow: " + String(YellowM));
  Serial.println("Total Brown: " + String(BrownM));
  Serial.println("Total Blue: " + String(BlueM));
  Serial.println("Total: " + String(TotalM));
  delay(5000);

}

The sensor detects colors when calibrated but as soon as I make it rotate it fails to detect the colors calibrated. I am looking for a solution, I ruled out ambient light because the sensor is covered, motion blur also because the stepper stops under the sensor.
thanks in advance.

Do the serial prints give any indication of why things are failing?

@SemperIdem it is not failing just not detecting. maybe I misphrased. It prints the rgb values but not printing the colors

Hi, @kareemrj9
Welcome to the forum.
Thanks for using code tags. :+1: :+1: :+1:

How fast are you rotating the disk?
What model Arduino are you using?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Hey @TomGeorge!

How fast are you rotating the disk?
What model Arduino are you using?

I am rotating the disk it is attached to the stepper which is rotating at a speed of 15RPM and using arduino R3 Uno!

Sounds like either an inadequate power supply not being able to provide the current or interference from the motor adding noise so you can't detect the colour sensor.

It could also be inadequate supply decoupling or poor layout.

@Grumpy_Mike so you think adding noise filters like resistors and such to the circuit can do it? How can I check for adequate power supply when I dont have a multimeter?

No

You buy one from a thrift store, only about £10

The other thing you can try is to disconnect the coils to the stepping motor. If it still misbehaves then it is down to the software. Probably too many delays.

The other thing you can try is to disconnect the coils to the stepping motor. If it still misbehaves then it is down to the software. Probably too many delays.

I don't think its delays, because I tried removing them all and still nothing new came up, tried using a new code, still nothing new.

No

I meant using a ceramic capacitor as suggested by one of my colleagues, he said it would filter out noise. Thank you though! @Grumpy_Mike

You can still try disconnecting the coils because in removing the delays you might be you not doing what you think you are doing.

Yes if it is noise a ceramic capacitor would help, but not a resistor.

@Grumpy_Mike I tried something putting the setSpeed(0) makes the loop only run once. and gives me one read of RGB values, then I commented out the stepper functions and it worked fine. Is this solid proof that it is stepper interference with the sensor?

Well remember that a stepping motor when stopped is actually drawing the maximum current it can draw. Only by disconnecting the motor coils can you prove it is a power supply lack of capacity issue.

But it is looking like you are homing in on the problem.

HI,
Can we please have a circuit diagram?
An image of a hand drawn schematic will be fine, include ALL power supplies, component names and pin labels.

Some images of your project would also help, so we can see your component layout.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.