pinpong example tcs34725

Reading Values from I2C TCS34725 Color Sensor

Here is the Python code to read values from an I2C TCS34725 color sensor:

# -*- coding: UTF-8 -*-
# Experiment effect: Read the values of the I2C TCS34725 color sensor
# Wiring: Connect an Arduino main control board to a Windows or Linux computer, and connect the TCS34725 color sensor to the I2C port SCL and SDA

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_tcs34725 import TCS34725 # Import the tcs34725 library from libs

Board("uno").begin()               # Initialization, choose the board type (uno, leonardo, xugu) and port number. If the port number is not entered, automatic recognition will be performed
#Board("uno","COM36").begin()      # Initialization with specified port on Windows
#Board("uno","/dev/ttyACM0").begin() # Initialization with specified port on Linux
#Board("uno","/dev/cu.usbmodem14101").begin()   # Initialization with specified port on Mac

tcs = TCS34725() # Sensor initialization

print("Color View Test!");
while True:
  if tcs.begin(): # Look for the sensor, return True if found
    print("Found sensor")
    break # If found, break the loop
  else:
    print("No TCS34725 found ... check your connections")
    time.sleep(1)

while True:
  r,g,b,c = tcs.get_rgbc() # Get the RGBC data
  print(r,g,b,c)
  print("C=%d\tR=%d\tG=%d\tB=%d\t"%(c,r,g,b))

  '''
  # Data conversion
  r /= c
  g /= c
  b /= c
  r *= 256
  g *= 256
  b *= 256;
  print("------C=%d\tR=%d\tG=%d\tB=%d\t"%(c,r,g,b))
  '''
  time.sleep(1)