Page 1 of 1

Error in using I2C with color sensor

Posted: Fri Apr 13, 2018 7:10 am
by nikhiledutech
Hello,

I am currently interfacing color sensor with STM32Fdisc board. and below i have given the code. It throws up an ""TypeError: can't convert list to int"" .

# 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)

GREEN = 0
RED = 1
BLUE = 2
CLEAR = 3
NUM_COLORS = 4


i2c = I2C(1)
i2c.init(I2C.MASTER,baudrate=100000)
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
result = i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)
if isinstance(result,bytes):
if len(result) > 0:
return[0]
else:
print("Zero length byte object returned")
else:
print(type(result))





# @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 = 0
colorvalue = bytearray(2)
color = 0
ColorRegisters0Command = REG_GREEN_LOW + (2 * (colour))
colorvalue[0] = I2C_ReadRegister(ColorRegistersCommand) #Error
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()
Can any one solve this issue ?

Re: Error in using I2C with color sensor

Posted: Fri Jun 08, 2018 11:58 am
by rhubarbdog
On what line number does the exception happen?

Re: Error in using I2C with color sensor

Posted: Fri Jun 08, 2018 1:00 pm
by Roberthh
Ther error is in the function you call:

Code: Select all

def I2C_ReadRegister(Rd_Addr):

global i2c, sensor
result = i2c.mem_read(1 , sensor[0], Rd_Addr, timeout=1000)
if isinstance(result,bytes):
if len(result) > 0:
return[0]
The last line should be:

Code: Select all

return result[0]
The line further below the one you flagged as error is most likely wrong:

Code: Select all

color = ((colorvalue[1]<<8)*256)|colorvalue[0]
Either multiply by 256 or shift left by 8.