Skip to content

Instantly share code, notes, and snippets.

@elktros
Created February 12, 2018 11:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elktros/ec80707a22d9ab93a08cab5edfb5abba to your computer and use it in GitHub Desktop.
Save elktros/ec80707a22d9ab93a08cab5edfb5abba to your computer and use it in GitHub Desktop.
Python Script for Raspberry Pi based Color Detector using TCS3200 Color Sensor.
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
red = NUM_CYCLES / duration
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
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
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
def endprogram():
GPIO.cleanup()
if __name__=='__main__':
setup()
try:
loop()
except KeyboardInterrupt:
endprogram()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment