Example Code for Arduino-Digital Color Detection

In digital mode, the user can have two color data records through simple button operations. To achieve two-color detections you simply need to monitor high and low outputs, and therefore requires no more configuration after being set up.

Hardware Preparation

Software Preparation

Wiring Diagram

Grayscale Sensor Connection Diagram in digital mode

Other Preparation Work

  1. Long click the button until the LED slow blinking (which means first color detecting ready).
  2. Let the grayscale sensor face to the first color (such as white paper)
  3. Meanwhile short click the button and the LED gets dark (which means first color detected).
  4. Short click the button and make the LED fast blink (which means second color detecting ready).
  5. Let the grayscale sensor face to the second color (such as black paper)
  6. Meanwhile short click the button and the LED gets dark (which means second color detected).
  7. The first color (such as white paper) will be Logical '1', and the LED becomes bright.
  8. The second color (such as black paper) will be Logical '0', and the LED becomes dark.
  9. This setting maintains even if the power is off, so for one purpose, one setting is enough.

Sample Code

int grayscalePin=8;
void setup()
{
 Serial.begin(9600);
 pinMode(grayscalePin,INPUT);
 Serial.println("Smart Grayscale sensor:");
}
void loop()
{
  Serial.print("Grayscale:");
  if(digitalRead(grayscalePin))
  {
    Serial.println("White detected");
  }
  else
  {
    Serial.println("Black detected");
  }

  delay(500);
}

Result

The first color (such as white paper) will be Logical '1', and the LED becomes bright. The second color (such as black paper) will be Logical '0', and the LED becomes dark. The serial monitor outputs "White detected" when the first color is detected and "Black detected" when the second color is detected.

LED gets dark when facing the black paper

LED gets bright when facing the white paper

Was this article helpful?

TOP