Color Detection Using Raspberry Pi & Python Animation Tools

0 19366 Medium

Story

 

 

 

 

In this tutorial I showed you how to perform color detection using TCS34725 RGB color sensor with IR filter and Python turtle tools.

 

The TCS34725 color sensor can detect a wide variety of colors based on their wavelength. This sensor is specially useful for color recognition projects such as color matching, color sorting, test strip reading and much more.

The TCS34725 is a highly accurate RGB color and ambient light sensor that includes interrupts based on color thresholds.

The module combines the sensor with a logic controllable white LED for illuminating the object to be measured and it comes with the easy to use I2C interface.

The module includes logic level shifting on the I2C bus for easy interfacing to both 3.3V and 5V microcontrollers.

 

 

 

It is important to note that the color detection of the photodiodes themselves is affected by infrared and toget a better result that is not affected by infrared you have to use an infrared filter

 

 

 

 

RGB full color LED Module KY-009, emitsa range of colors by mixing red, green and blue. The amount of each primary color is adjusted using PWM.

 

A Brief Note on Python Turtle Module

Python Turtle is something that evolved from Logo programming language, invented in 1966 by Wally Feurzig. With the aid of Object Oriented Programming approach, we can create an impressive set of animations easily.

 

 

 

TCS34725 color sensor Library

To install the library from source (recommended) run the following commands on a Raspberry Pi:

Hardware Design and Implementation

Connect the Sensor as bellow:

Pin 3V3 to sensorVIN

Pin GND to sensorGND

Pin SCL to sensorSCL

Pin SDA to sensorSDA

Experiment

When we place the blue objectin front of the sensor (1-3 cm), the blue frequency (B) values oscillate between 5 and 40.

Check the values displayed onTerminal. R & G & B readings – see figure below.

 

 

I Repeat this process with a green and red objects and write down the upper and bottom frequency limits for each color.

 

To distinguish between different colors we have three conditions:

•When the R is the maximum value (in RGB parameters) we know we have a red object

•When G is the maximum value, we know we have a green object

•When B is the maximum value, we know we have a blue object

Now, place something in front ofthe sensor. It should print the color detected: red, green or blue.

The sensor can also detect other colors with more conditions.

 

 

 

 

 

 

My code :

https://gist.github.com/aula9/69473ef635e3f8610dcc36815116b4f4

https://drive.google.com/file/d/15ABWtYKKCOR3ZTmqzluHFvd4Qtql49Ut/view?usp=drive_open

 

Note: The code in progress to test new colors

I hope you found it useful!

In this tutorial we are going present how to detect colors using Raspberry Pi, TCS34725 sensor, and Python animation tools.

 

Color Detection Using Raspberry Pi & Python Animation Tools

Things used in this project

 

Hardware components

HARDWARE LIST
1 Raspberry Pi 3 Model B
1 RGB Diffused Common Cathode
1 DFRobot Gravity: TCS34725 RGB Color Sensor For Arduino

Custom parts and enclosures

 

icon colorsensor_JldET1ZvTK.zip 1.75MB Download(0)

Schematics

 

icon aula_EQoSzoKFiz.zip 6KB Download(0)

Code

 

My code :

Color Sensing Python Code Note : The code in progress to test new colors

CODE
import time
import Adafruit_TCS34725
import smbus
import RPi.GPIO as GPIO
import turtle 
tcs = Adafruit_TCS34725.TCS34725()
tcs.set_interrupt(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(32, GPIO.OUT)
GPIO.setup(36, GPIO.OUT)
GPIO.setup(38, GPIO.OUT)
pr = GPIO.PWM(32,50)
pg = GPIO.PWM(36,50)
pb = GPIO.PWM(38,50)
pr.start(20)
pg.start(20)
pb.start(20)
i =0
w=" "
while i <10:
    i =i+1
    r, g, b, c = tcs.get_raw_data()
    color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b)
    lux = Adafruit_TCS34725.calculate_lux(r, g, b)
    print('Color: red={0} green={1} blue={2} clear={3}'.format(r, g, b, c))
    time.sleep(1)
    if((r > b) and (r >g)):
        if(b <5):
            pr.ChangeDutyCycle(80)
            pg.ChangeDutyCycle(1)
            pb.ChangeDutyCycle(1)
            w="red"
            print(w)
        elif (r-g <20 and r-b > 10 and r>15):
            pr.ChangeDutyCycle(80)
            pg.ChangeDutyCycle(65)
            pb.ChangeDutyCycle(1)
            w="yellow"
            print(w)
    elif((r < b) and (b >5)):
        pr.ChangeDutyCycle(1)
        pg.ChangeDutyCycle(1)
        pb.ChangeDutyCycle(80)
        w="blue"
        print(w)
    elif((r < g) and (b <g) and (g>20)):
        if (b<10):
            pr.ChangeDutyCycle(1)
            pg.ChangeDutyCycle(80)
            pb.ChangeDutyCycle(1)
            w="green"
            print(w)
        elif(b>10 and r-g <6):
            pr.ChangeDutyCycle(60)
            pg.ChangeDutyCycle(5)
            pb.ChangeDutyCycle(70)
            w="purple"
            print(w)
    elif((r > g) and (b > g) and (r>45)):
        pr.ChangeDutyCycle(0)
        pg.ChangeDutyCycle(70)
        pb.ChangeDutyCycle(70)
        w="cyan"
        print(w)
    elif((r > 100) and (b > 90) and (g> 100)):
        pr.ChangeDutyCycle(0)
        pg.ChangeDutyCycle(70)
        pb.ChangeDutyCycle(70)
        w="white"
        print(w)
    #elif((r < 25) and (b <20) and (g < 20)):
        #pr.ChangeDutyCycle(0)
        #pg.ChangeDutyCycle(0)
        #pb.ChangeDutyCycle(0)
        #w="black"
        #print(w)
    else:
        pr.ChangeDutyCycle(20)
        pg.ChangeDutyCycle(20)
        pb.ChangeDutyCycle(20)
time.sleep(9)
trtl = turtle.Turtle()    #making a turtle object of Turtle class for drawing
screen=turtle.Screen()    #making a canvas for drawing
screen.setup(400,300)    #choosing the screen size
screen.bgcolor('black')    #making canvas black
trtl.pencolor(w)    #making colour of the pen red
trtl.pensize(5)    #choosing the size of pen nib
trtl.speed(1)    #choosing the speed of drawing
trtl.shape('turtle')   #choosing the shape of pen nib
trtl.forward(100)    #top line
trtl.right(90)
trtl.forward(100)    # right vertical line
trtl.right(90)
trtl.forward(100)   # bottom line
trtl.right(90)
trtl.forward(100)   # left vertical line
# information printing
trtl.penup()
trtl.setpos(-120,100)
trtl.pendown()
trtl.pencolor(w)
trtl.write(w, font=("Arial", 16, "bold"))
trtl.penup()
trtl.ht()        
# Print out color temperature.
#if color_temp is None:
  #  print('Too dark to determine color temperature!')
#else:
   # print('Color Temperature: {0} K'.format(color_temp))
#print('Luminosity: {0} lux'.format(lux))
tcs.set_interrupt(True)
tcs.disable()
pr.stop()
pg.stop()
pb.stop()
License
All Rights
Reserved
licensBg
0