Created
February 12, 2018 11:34
-
-
Save elktros/e6135be8fba490c99f775ab58753f5dd to your computer and use it in GitHub Desktop.
Python Script for interfacing TCS3200 Color Sensor with Raspberry Pi and obtaining RAW RGB Values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
i don't want raw values what should i do now ?