Arduino projects: Color sensor

We’ve learned how to use the LCD module, the microphone module, LEDs and the potentiometer. Those are devices you will make use of most of the time.

Today, we are going to be working with the Arduino color sensor module. It is a quite unpopular piece but often very useful and easy to use. The color sensor can be used in projects like the skittle sorting machine.

This project will consist of letting the sensor read colors and using the LCD to display the current color the sensor is reading. The color sensor is quite hard to calibrate to get the perfect results thus we will display only the following 3 colors: the red, green and blue.

Arduino color sensor

Parts needed

  • 1 x Arduino Uno board
  • 1 x TCS230 color sensor
  • 1 x LCD with I2C converter
  • male-female jumper wires

While using the schematic below to connect the project, remember that if your Arduino board does not have separate SCL and SDA pins, you can connect the pins from the LCD to A4 and A5 (analog pins) of the Arduino. If you do so, remember to indicate the right pins in the code.

This code will read colors from our sensor using the pulseIn function because the TCS230 returns the frequency of each color (red, green, blue). With pulseIn, we can read colors and easily compare them to one another to identify the current color and display it on the LCD.

We will do it every half a second.

The program also prints those colors on the serial, just in case you don’t have an LCD. The only library that you need is for the LCD with I2C converter.

Here is the code:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

int red = 0;
int green = 0;
int blue = 0;

void setup()
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, INPUT);

digitalWrite(8, HIGH);
digitalWrite(9, HIGH);


void loop()
digitalWrite(12, LOW);
digitalWrite(11, LOW);
red = pulseIn(10, digitalRead(10) == HIGH ? LOW : HIGH);
digitalWrite(11, HIGH);
blue = pulseIn(10, digitalRead(10) == HIGH ? LOW : HIGH);
digitalWrite(12, HIGH);
green = pulseIn(10, digitalRead(10) == HIGH ? LOW : HIGH);

if (red < blue && red < green && red < 20)
Serial.println(" Red Color");
lcd.print("Red Color");

else if (blue < red && blue < green)
Serial.println(" Blue Color");
lcd.print("Blue Color");

else if (green < red && green < blue)
Serial.println(" Green Color");
lcd.print("Green Color");

The one thing that can be unclear in this code is this:

digitalRead(10) == HIGH ? LOW : HIGH

It’s just a short form of if and else. (if the read from pin 10 is HIGH, it will return LOW otherwise it will return HIGH). If and else in one line without brackets.

The best materials to use while testing the project are pieces of paper as we’ve done in the video below.

Hope this helped, and I will challenge you to go and create something amazing using the basic tools we have just made use of in the project.


Related Posts