element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • 'Choose another store...'
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Color Sensor how-to.
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: timswift
  • Date Created: 19 Nov 2014 10:32 PM Date Created
  • Views 384 views
  • Likes 1 like
  • Comments 2 comments
  • mega
  • light
  • led
  • arduino
Related
Recommended

Color Sensor how-to.

timswift
timswift
19 Nov 2014

Recently I built a color sensor and thanks to @Paul and @Peter Oakes i got the code to work. so i thought i would share it with you.

 

 

 

Parts i used:

 

(1) Arduino Mega

 

(1) 10K ohm resister

 

(4) 220 ohm resister

 

(1) Red LED

 

(1) Green LED

 

(1) Blue LED

 

(1) White LED

 

(1) Light sensitive resister

 

 

And the schematic:

 

image

 

Finely the code:

 

int sensorPin = A0;

 

  int red = 0;

  int green = 0;

  int blue = 0;

 

  int led1 = 2;

  int led2 = 3;

  int led3 = 4;

 

  int led4 = 8;

 

  int val1 = -12;//Use this to ajost the red value

  int val2 = -4;//Use this to ajost the green value

  int val3 = -18;//Use this to ajost the blue value

 

  int sensorValue = 0; 

 

void setup() {

 

  pinMode(led1, OUTPUT); //Red LED

  pinMode(led2, OUTPUT); //Green LED

  pinMode(led3, OUTPUT); //Blue LED

  pinMode(led4, OUTPUT); //Detecshin LED

  Serial.begin(9600);

}

 

void loop() {

 

 

 

 

 

 

 

  digitalWrite(led1, HIGH);                                                                                                                                                           

  sensorValue = analogRead(sensorPin);

  red = (sensorValue / 10) + val1;

//  Serial.print("red =");

//  Serial.println(red);

 

 

delay(50);

  digitalWrite(led1, LOW);

 

/////////////////////////////////

 

  digitalWrite(led2, HIGH);

  sensorValue = analogRead(sensorPin);

  green = (sensorValue / 10) + val2;

//  Serial.print("green =");

//  Serial.println(green); 

 

 

delay(50);

  digitalWrite(led2, LOW);

 

/////////////////////////////////

 

  digitalWrite(led3, HIGH);

  sensorValue = analogRead(sensorPin); 

  blue = (sensorValue / 10) + val3;

// Serial.print("blue =");

// Serial.println(blue);              

delay(50);

digitalWrite(led3, LOW);

 

 

 

 

if(blue > 13 and blue < 17 and green > 13 and green < 17 and red > 13 and red < 17){

Serial.println("whight");

digitalWrite(led4, HIGH);

delay(1000);

 

}

else{

digitalWrite(led4, LOW);

}

}

 

 

 

This code turns on the detection LED wen a green object is held over the sensor.

  • Sign in to reply

Top Comments

  • peteroakes
    peteroakes over 8 years ago +1
    Good job and thanks for sharing
  • DAB
    DAB over 8 years ago +1
    Good post Tim. DAB
  • DAB
    DAB over 8 years ago

    Good post Tim.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • peteroakes
    peteroakes over 8 years ago

    Good job and thanks for sharing

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube