TCS3414CS Color Sensor

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

TCS3414CS Color Sensor

Post by nikhiledutech » Mon Feb 26, 2018 6:47 am

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

# main.py -- 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)
i2c.init(I2C.MASTER,baudrate=200000)
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
i2c.mem_write(Wr_Data,sensor[1],Wr_Addr,timeout=1000)



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

def BSP_COLOR_SENSOR_I2C1_Init():
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():

i2c.deinit()



def ColorSensor_Init():
global i2c, sensor

BSP_COLOR_SENSOR_I2C1_Init()
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])):

while(1):
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)
colour+=1

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


print(red_)
print(green_)
print(blue_)

pyb.delay(500)


while True:
main()
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 ?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TCS3414CS Color Sensor

Post by pythoncoder » Mon Feb 26, 2018 8:39 am

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.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: TCS3414CS Color Sensor

Post by nikhiledutech » Mon Feb 26, 2018 8:52 am

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".

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: TCS3414CS Color Sensor

Post by pythoncoder » Tue Feb 27, 2018 9:20 am

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...
Peter Hinch
Index to my micropython libraries.

Post Reply