Page 1 of 1

TCS3414CS Color Sensor

Posted: Mon Feb 26, 2018 6:47 am
by nikhiledutech
Hello everyone,

If anyone has module for interfacing TCS3414CS color sensor with STM32F4 please share the link.

I have tried to interface Color sensor on STM32F4 disc board. Below is my code

# -- put your code here!
# /**I2C1 GPIO Configuration
# PB8 ------> I2C1_SCL
# PB9 ------> I2C1_SDA

import pyb
from pyb import I2C

#/* Control Register */
COLOR_SENSOR_REG_CTL = const(0x80)
REG_CTL_POWER = const(0x01)
REG_CTL_ADC_EN = const(0x02)

#/* Color read Register */
COLOR_SENSOR_REG_CMD = const(0x90) #The REG_BLOCK_READ and REG_GREEN_LOW direction are the same

REG_GREEN_LOW = const(0x90)
REG_GREEN_HIGH = const(0x91)
REG_RED_LOW = const(0x92)
REG_RED_HIGH = const(0x93)
REG_BLUE_LOW = const(0x94)
REG_BLUE_HIGH = const(0x95)
REG_CLEAR_LOW = const(0x96)
REG_CLEAR_HIGH = const(0x97)

i2c = I2C(1)
sensor = i2c.scan()

* @brief To transmit data on I2C
* @param Wr_Addr Register address on which Wr_Data will be written
* @param Wr_Data data
* @return value if data written successful, it will return 0 otherwise 1

def I2C_WriteRegister(Wr_Addr, Wr_Data):
global i2c, sensor

def I2C_ReadRegister(Rd_Addr):

global i2c, sensor
i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)

# @brief This Function configures and Initializes I2C1

global i2c, sensor

i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate = 100000)

# @brief This function is for De-Initializing I2C1

def BSP_COLOR_SENSOR_I2C1_MspDeInit():


def ColorSensor_Init():
global i2c, sensor

sensor = i2c.scan() #Only sensor will be detected in list
if i2c.mem_write(1,sensor[0],1,timeout=500) == False:
# if((i2c.is_ready(sensor[0])):

print("Unable to initialize color sensor \n")

def ColorSensor_ReadColor(colour):

ColorRegistersCommand = None
colorvalue = bytearray(2)
color = None
ColorRegistersCommand = REG_GREEN_LOW + (2 * (colour))
colorvalue[0] = I2C_ReadRegister(ColorRegistersCommand)
colorvalue[1] = I2C_ReadRegister((ColorRegistersCommand + 1))
color = ((colorvalue[1]<<8)*256)|colorvalue[0]
return color

def main():
colour = green_ = red_ = blue_ = clear_ = None
displayvalue = bytearray(5)
tmp = maxColor = None

while True:

# for (colour = GREEN; colour <= NUM_COLORS; colour++):
colour = GREEN
if (colour <= NUM_COLORS):
displayvalue[colour] = ColorSensor_ReadColor(colour)

green_ = displayvalue[0]
red_ = displayvalue[1]
blue_ = displayvalue[2]
clear_ = displayvalue[3]

red_ = red_ * 1.70
blue_ = blue_ * 1.35

maxColor = max(red_, green_)
maxColor = max(maxColor, blue_)

if(maxColor > 255):

tmp = 250.0/maxColor
green_ *= tmp
red_ *= tmp
blue_ *= tmp



while True:
So i am facing problems like:

1) TypeError: can't convert NoneType to int in the bold line in code

2) Whenever i use I2C.mem_write and read function to write a register on I2C enabled device the written value on a particular register and the read value is not the same. Why ?

Re: TCS3414CS Color Sensor

Posted: Mon Feb 26, 2018 8:39 am
by pythoncoder
The second line here should be producing an error message

Code: Select all

color = None
ColorRegistersCommand = REG_GREEN_LOW + (2 * (colour))
because you can't multiply None by an integer.

Re: TCS3414CS Color Sensor

Posted: Mon Feb 26, 2018 8:52 am
by nikhiledutech
i have changed the value of color to 0 and other None values to zero. Still in the line you mentioned, i am multiplying different variable "colour" with int instead of "color".

Re: TCS3414CS Color Sensor

Posted: Tue Feb 27, 2018 9:20 am
by pythoncoder
Oops :oops: I hadn't spotted that you were using color and colour in the same function. I think this is a bad idea on the grounds of readability. Otherwise one day you'll come back to this code and curse it...