Label detector with KY-033 sensor

Hi
I tried to carry out a project to detect white dots [with a shiny surface] with a diameter of mm. 30
These stamps are applied at a programmed distance on a black [fairly shiny] polyethylene pipe that "flows" at 20 m / min. about
The project was carried out with

  • n ° 1 ARDUINO UNO REV 3 [A000066]
  • n ° 1 Elegoo 4 Channel DC 5V Relay Module
  • n ° 1 Quimat LCD Display, IIC / I2C / TWI 2004/20 x 4 LCD Display Module
  • n ° 1 STABILIZED POWER SUPPLY 220Vac 12Vdc 5A
  • n ° 1 Safe Alarm 120dB indoor siren for 12Vdc alarm
  • n ° 3 BUTTON KEY MODULE, KEY SWITCH MODULE, ARDUINO SENSOR KY-004
  • n ° 1 Intelligent Car Tracing Line Hunt Sensor KY-033

How does it work
The KY-033 sensor when reading [working on rising edge] "black/pipe" starts a T1 timer [shown on runtime on the display]
When the KY-033 sensor reads [working on on rising edge] "white/dot", it stops the timer and, before resetting it, it stores the value of timer T1 and puts it in a register T2 [shown on the display]
On the diplay we therefore have 2 values, a runtime value corresponding to the passing time and a static one containing the last recorded value
If the distance between the stickers is 1 meter and the tube runs at 20 meters per minute. on the display at the T2 value we will have about 3 seconds value [s. 2.90 - 3.05]
1 START button
2 STOP button
Button 3 Increases control time

The system starts in the STOP state
Press button 3 to increase the control time TC to a value greater than the timer T2 [TC for example 5 seconds]
Press button 1 and the system goes to START
If the frequency of the stamps is regular, the T2 value never exceeds the value of about 3 seconds, but if the label maker "loses" 1 stamp and does not apply it to the tube,
T2 becomes greater than TC and therefore the alarm siren is activated [through Elegoo 4 Channel DC 5V Relay Module]
The operator switches off the siren by pressing the STOP button, checks the correct operation of the label, then puts the system back into START

So far so good, the difficulty lies in the fact that the KY-033 sensor [connected on pin 2, so attachInterrupt could be used if necessary] is very [but very] difficult to adjust, it reads "black" well, but flickers on white , the trimmer is already almost set to the minimum [anti-clockwise], just change the distance from the pipe even by only 1 mm. which begins to flicker by detecting the white of the sticker even when there is simply the black tube underneath, perhaps "tricked" by the shiny surface of the tube.
The KY-033 sensor has a red LED that lights up when it detects "white" and I see that even when set to the minimum the LED remains partially / "slightly" lit when the black pipe slides under it, a sign that it is thinking of "reading" something.

So I wanted to ask for your help / advice if there is any more reliable sensor to detect the color change.

Now I am testing the TCRT5000 IR sensor module but what they sent me must be faulty as it does not detect the color change, even by acting on the trimmer, this sensor would be more comfortable because it has the trimmer on the opposite side to the optics and is easier to adjust during the tests.

Thank you all

Video: testing TCRT5000 sensor

The part of the code responsible for detecting the color change follows

void loop()
{

 // conserviamo nella variabile var lo stato del sensore
 int valSensore = digitalRead(pinSensore);

 // verifichiamo se viene letto "NERO"
 // se vero accendiamo il led
 if (valSensore == HIGH && valSensoreOld == LOW)
 {

 delay(500); // attendo x00 millisecondi
 valSensoreOld = valSensore;

 // Pulisce l'LCD e pone il cursore all'inizio
 //lcd.clear();
 lcd.setCursor(0, 0);          // 1° colonna - 1° riga
 // scrivo 'HIGH'
 lcd.print("NERO        ");
 statoLettura = 1;
 currentMillis = previousMillis = millis();
 measuredMillis = measuringMillis;
 lcd.setCursor(11, 1); // 11° colonna - 2° riga
 lcd.print("T:"); lcd.print((measuredMillis) / 1000.0, 2);  lcd.print("  ");

 }
 // altrimenti spegniamo il led
 if (valSensore == LOW && valSensoreOld == HIGH)
 {
 valSensoreOld = valSensore;


 // Pulisce l'LCD e pone il cursore all'inizio
 //lcd.clear();
 lcd.setCursor(0, 0);          // 1° colonna - 1° riga
 // scrivo 'LOW'
 lcd.print("BIANCO         ");
 statoLettura = 0;


 }