Skip to content

Instantly share code, notes, and snippets.

@elktros
Created February 12, 2018 11:34
Python Script for interfacing TCS3200 Color Sensor with Raspberry Pi and obtaining RAW RGB Values.
import RPi.GPIO as GPIO
import time
s2 = 23
s3 = 24
signal = 25
NUM_CYCLES = 10
def setup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(signal,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(s2,GPIO.OUT)
GPIO.setup(s3,GPIO.OUT)
print("\n")
def loop():
temp = 1
while(1):
GPIO.output(s2,GPIO.LOW)
GPIO.output(s3,GPIO.LOW)
time.sleep(0.3)
start = time.time()
for impulse_count in range(NUM_CYCLES):
GPIO.wait_for_edge(signal, GPIO.FALLING)
duration = time.time() - start #seconds to run for loop
red = NUM_CYCLES / duration #in Hz
print("red value - ",red)
GPIO.output(s2,GPIO.LOW)
GPIO.output(s3,GPIO.HIGH)
time.sleep(0.3)
start = time.time()
for impulse_count in range(NUM_CYCLES):
GPIO.wait_for_edge(signal, GPIO.FALLING)
duration = time.time() - start
blue = NUM_CYCLES / duration
print("blue value - ",blue)
GPIO.output(s2,GPIO.HIGH)
GPIO.output(s3,GPIO.HIGH)
time.sleep(0.3)
start = time.time()
for impulse_count in range(NUM_CYCLES):
GPIO.wait_for_edge(signal, GPIO.FALLING)
duration = time.time() - start
green = NUM_CYCLES / duration
print("green value - ",green)
time.sleep(2)
def endprogram():
GPIO.cleanup()
if __name__=='__main__':
setup()
try:
loop()
except KeyboardInterrupt:
endprogram()
@jellebraak
Copy link

help! i'm using this code but instead of giving me the raw rgb values i get numbers far into the thousands and green and blue are always the highest even if i show red. plus they are almost always the same.
with kind regards jelle, (first year university student robotics)

@rahulm8867
Copy link

Yes I'm got same kind of problem

@Jaisriram3600
Copy link

i don't want raw values what should i do now ?

@jazurdia
Copy link

This code should give you big numbers. Adding the following code will print the colors that are being detected.

if green<7000 and blue<7000 and red>12000: print("red") temp=1 elif red<12000 and blue<12000 and green>12000: print("green") temp=1 elif green<7000 and red<7000 and blue>12000: print("blue") temp=1 elif red>10000 and green>10000 and blue>10000 and temp==1: print("place the object.....") temp=0

Source: https://www.electronicshub.org/raspberry-pi-color-sensor-tutorial/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment