Menu

Embedded programing

Arduino

TESTING Color Sensor

Color Sensor Circuit

This is based on the color sensor circuit made for input devices.

My setup and pinout is shown below:
Color Sensor --> Arduino
RED --> Pin 9
GREEN --> Pin 10
BLUE --> Pin 11
WHITE --> Pin 6
ANALOG --> Pin A0
VCC --> VCC
GND --> GND

Connect the Arduino Uno to the USB port. Open the Arduino software select the port connected to the arduino (Tools -> Port). Load the following code into a new sketch and save it as 'teston'.

int COLORSENSOR[4] = {9,10,11,6},i, j ;   // DECLARE  R   G   B   W PINS
float readValue[100];                     // initiate float array with 100 elements to store the values read in from the analog pin.
float averagereadmap[4];                  // initiate float array to store the average value (mapped) read from the analog pin. Four elements to store the average for R,G,B,W  
float averageread[4]={0,0,0,0};           // initiate float array to store running total of read values.
long int inthex[4];                       // initiate an int array to convert float stored in avaeragereadmap array to int.
String hex[4];                            // initiate a string array to store hex values for each led input.
void setup()                              // all code in setup will only run through once.
{
 
  Serial.begin(9600);                     // Sets the data rate in bits per second (baud) for serial data transmission.
  for(i=0;i<4;i++){                       // set LED pins of color sensor to OUTPUT 
  pinMode(COLORSENSOR[i], OUTPUT);}
  for(j = 0 ; j < 4 ; j++)                //  CYCLE  PINS
    {
      digitalWrite(COLORSENSOR[j],HIGH);   // Switch LED corresponding to j pin value on 
      delay(100);                          // Delay before reading to allow led to get to full brightness.
      for(i=0 ; i<100 ; i++)               // Read analog input 100 times.
      {
        /*if(j==0) {readValue[i] = analogRead(A0)-99;}
        if(j==1) {readValue[i] = analogRead(A0)-122;}
        if(j==2) {readValue[i] = analogRead(A0)-77;}
        if(j==3) {readValue[i] = analogRead(A0)-168;}*/
        readValue[i] = analogRead(A0);                  
        Serial.print(readValue[i]);                      //to view these values open serial monitor
        Serial.print('\n');
        delay(10);                                       
        averageread[j] = averageread[j]+readValue[i];     //add value to a running total of read values form analog input
      }
      digitalWrite(COLORSENSOR[j],LOW);                   // Switch LED corresponding to j pin value off
      averageread[j] =averageread[j]/100;                 // take average of read values
      if     (j==0) { scaleAndProduceHexString(0, 911);} //Red : The value on the left should correspond to the maximum value read in serial monitor when RED led is on - THIS CAN ONLY BE CALLIBRATED WHEN FACING A WHITE MATTE PAPER.
      else if(j==1) { scaleAndProduceHexString(1, 991);} //Green : The value on the left should correspond to the maximum value read in serial monitor when GREEN led is on - THIS CAN ONLY BE CALLIBRATED WHEN FACING A WHITE MATTE PAPER.
      else if(j==2) { scaleAndProduceHexString(2, 659);} //Blue : The value on the left should correspond to the maximum value read in serial monitor when BLUE led is on - THIS CAN ONLY BE CALLIBRATED WHEN FACING A WHITE MATTE PAPER.
      else          { scaleAndProduceHexString(3, 980);} //White : The value on the left should correspond to the maximum value read in serial monitor when WHITE led is on - THIS CAN ONLY BE CALLIBRATED WHEN FACING A WHITE MATTE PAPER.
      delay(1000);
    }
}

void loop()             // loop is empty because we do not need any code to continuously loop.
{
  
}


Create a new tab in the same sketch, name it 'scaleAndProduceHexString' and copy the following code into it.

void scaleAndProduceHexString(int col, int scale){    
  averagereadmap[col]=map(averageread[col],0,scale,0,255);      // Scale value to an RGB value
  averagereadmap[col]=constrain(averagereadmap[col],0,255);     // Constrain value between 0 and 255.
  Serial.print(averagereadmap[col]);
  Serial.print('\n');
  inthex[col] = averagereadmap[col];
  hex[col] = String(inthex[col],HEX);                           // Convert value from decimal scale to hexidecimal.
  if (hex[col].length()==1)                                     // Not necessary to test the colorsensor, but required for the digit display. This makes sure that single digit hext values appear on the correct digit of the display.
  {
    hex[col] = '0'+ hex[col];
  }
  Serial.print(hex[col]);
  Serial.print('\n');
}