Last active
December 22, 2020 19:04
Détection de couleur par le capteur TCS3200 branché à un Raspberry Pi
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
''' | |
Détection de couleur par le TCS3200 branché à un Raspberry Pi. | |
Pour plus d'informations: | |
https://electroniqueamateur.blogspot.com/2020/12/capteur-de-couleurs-tcs3200-gy-31-et.html | |
''' | |
import RPi.GPIO as GPIO | |
import time | |
# définition des broches utilisées | |
broche_S0 = 27 # S0 du TSC3200 à GPIO27 du Raspberry Pi | |
broche_S1 = 22 # S1 du TSC3200 à GPIO22 du Raspberry Pi | |
broche_S2 = 23 # S2 du TSC3200 à GPIO23 du Raspberry Pi | |
broche_S3 = 24 # S3 du TSC3200 à GPIO24 du Raspberry Pi | |
broche_OUT = 25 # OUT du TSC3200 à GPIO25 du Rasperry Pi | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(broche_OUT,GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(broche_S0,GPIO.OUT) | |
GPIO.setup(broche_S1,GPIO.OUT) | |
GPIO.setup(broche_S2,GPIO.OUT) | |
GPIO.setup(broche_S3,GPIO.OUT) | |
# réglage de la fréquence de sortie du capteur (2% du maximum) | |
GPIO.output(broche_S0,GPIO.LOW) | |
GPIO.output(broche_S1,GPIO.HIGH) | |
def mesure(): | |
time.sleep(0.1) | |
debut = time.time() | |
for impulse_count in range(10): | |
GPIO.wait_for_edge(broche_OUT, GPIO.FALLING) | |
return time.time() - debut | |
while(1): | |
GPIO.output(broche_S2,GPIO.LOW) | |
GPIO.output(broche_S3,GPIO.LOW) | |
rouge = mesure() | |
print("Rouge: ",rouge) | |
GPIO.output(broche_S2,GPIO.LOW) | |
GPIO.output(broche_S3,GPIO.HIGH) | |
bleu = mesure() | |
print("Bleu: ",bleu) | |
GPIO.output(broche_S2,GPIO.HIGH) | |
GPIO.output(broche_S3,GPIO.HIGH) | |
vert = mesure() | |
print("Vert: ",vert) | |
GPIO.output(broche_S2,GPIO.HIGH) | |
GPIO.output(broche_S3,GPIO.LOW) | |
total = mesure() | |
print("Sans filtre: ",total) | |
# ici, vous pouvez ajouter des conditions afin de détecter | |
# un objet connu, par exemple: | |
if ((abs(rouge - 0.22) < 0.02) and (abs(bleu - 0.25) < 0.02) and (abs(vert - 0.32) < 0.02)): | |
print("C'est le carton noir!") | |
print("\n") | |
time.sleep(1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment