Pyboard and linear ccd sensor(TSL 1410R)

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by JimTal001 » Thu May 12, 2016 12:14 am

I've tried the suggested code, as listed below, but I get an error on line 7 :
a = array['i', 1281]

The error message is: TypeError: 'module' object is not subscriptable

Code: Select all

import array
from pyb import Pin, ADC

pin_si = Pin('X1', Pin.OUT_PP)
pin_clk = Pin('X2', Pin.OUT_PP)
pin_ao = ADC(Pin('X3'))
a = array['i', 1281]

def read():
    pin_clk.low()
    pin_si.high()
    pin_clk.high()
    pin_si.low()
    for i in range(1281):
        pin_clk.low()
        a[i] = pin_ao.read() # store raw integer data
        pin_clk.high()
		
data = read() # discard 1st dataset
pyb.delay(1) # 1mS is more than enough (20uS min)
data = read()
print([x* 3.3/4096 for x in a])

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by dhylands » Thu May 12, 2016 2:53 am

I see a few of problems:
1 - You used import array, which means you need to use array.array. If you used from array import array then you could use just array.
2 - You used square brackets instead of parenthesis
3 - You passed an int as an initializer rather than a list or tuple
This works:

Code: Select all

>>> from array import array
>>> a = array('i', (1281,))
>>> len(a)
1
>>> a[0]
1281
EDIT: Oh you wanted an array of 1281 elements. In which case you should do:

Code: Select all

>>> from array import array
>>> a = array('i', (0,) * 1281)
>>> len(a)
1281
>>> a[0] = 1
>>> a[1] = 22
>>> a[0]
1
>>> a[1280] = 7
>>> a[1280]
7
>>> a[1]
22

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

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by pythoncoder » Thu May 12, 2016 5:25 pm

Good grief. I definitely had a "senior moment" when I posted that :oops: I've edited the original in case anyone else tries to use it. I wonder if it's easier on RAM to initialise the array with

Code: Select all

a = array('i', (0 for _ in range(1281)))
Otherwise we create a 1281 element tuple only to discard it.
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by JimTal001 » Fri May 13, 2016 11:58 pm

Has anyone continued work with this sensor? Or willing to? I've been working with the sensor for the past few days and have not quite figured it out. I can get representative data when running the code below but it is not very repeatable. I am masking a small area close to the sensor to collect this data.

Image

Code: Select all

import pyb
from array import array
from pyb import Pin, ADC
import os

pin_si = Pin('X1', Pin.OUT_PP)
pin_clk = Pin('X2', Pin.OUT_PP)
pin_ao = ADC(Pin('X3'))
a = array('i', (0,) * 1281)

def deleteFile(filePath=""):
	try:
		os.remove(filePath)
	except OSError:		
		print('exception: deleteFile()')


def logData():
	file = open('/sd/data/ls_data.txt', 'a') 
	try:
		for i in range(1280):
			V = a[i]*3.3/4096
			file.write(str(V) + '\n') 

		file.close()
	except:
		print('exception: logData')

def read():
    pin_clk.low()
    pin_si.high()
    pin_clk.high()
    pin_si.low()
    for i in range(1281):
        pin_clk.low()
        a[i] = pin_ao.read() # store raw integer data
        pin_clk.high()

deleteFile('/sd/data/ls_data.txt')
data = read() # discard 1st dataset
pyb.delay(1) # 1mS is more than enough (20uS min)
data = read()
logData()

If I run the code below it has much better repeatability (arduino sample extract). I believe repeatability has a lot to do with integration time.

Code: Select all

import pyb
from array import array
from pyb import Pin, ADC
import os

pin_si = Pin('X1', Pin.OUT_PP)
pin_clk = Pin('X2', Pin.OUT_PP)
pin_ao = ADC(Pin('X3'))
a = array('i', (0,) * 1281)

def deleteFile(filePath=""):
	try:
		os.remove(filePath)
	except OSError:		
		print('exception: deleteFile()')

def logData():
	file = open('/sd/data/ls_data.txt', 'w') 
	try:
		for i in range(1280):
			V = a[i]*3.3/4096
			file.write(str(V) + '\n') 

		file.close()
	except:
		print('exception: logData')

# This function generates an outgoing clock pulse from the Arduino digital pin 'CLKpin'. This clock
# pulse is fed into pin 3 of the linear sensor:
def ClockPulse():
	pyb.udelay(1)
	pin_clk.high()
	pin_clk.low()	
		
def setup():
	# Clock out any existing SI pulse through the ccd register:
	for i in range(1281): ClockPulse()
	# Create a new SI pulse and clock out that same SI pulse through the sensor register:
	pin_si.high()
	ClockPulse()
	pin_si.low()
	for i in range(1281): ClockPulse()

# Read all 1281 pixels in parallell. Store the result in the array. Each clock pulse 
# causes a new pixel to expose its value on the two outputs:		
def read():		
    for i in range(1281):
		pyb.udelay(20) # Delay to stabilize the AO output from the sensor
		a[i] = pin_ao.read() # store raw integer data
		pin_clk.high()
		ClockPulse()

# Stop the ongoing integration of light quanta from each photodiode by clocking in a SI pulse 
# into the sensors register:		
def stopIntegration():			
	pin_si.high()
	ClockPulse()
	pin_si.low()	
	
def main():
	setup()
	deleteFile('/sd/data/ls_data.txt')
	data = read() # discard 1st dataset
	pyb.delay(1) # 1mS is more than enough (20uS min)
	data = read()
	logData()

main()
I am trying to collect multiple sample of the output and this is where I am failing miserably (absolutely no representative output). I have tried the following with code base 1:

Code: Select all

	for i in range(10):	
		pyb.delay(1000) # wait 1 sec before collection the next output
		data = read()
		logData()
and I have tried the code below with the second code base:

Code: Select all

	for i in range(10):	
		stopIntegration()
		read()
		stopIntegration()
		logData(i)
		
		for j in range(18): ClockPulse() # based on datasheet
If anyone has input I would appreciate it.

Regards
Jim

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

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by pythoncoder » Sat May 14, 2016 5:33 am

How are you illuminating the sensor? If any component of the light source is from an AC mains source it's likely to be heavily modulated at twice the line frequency. Mains LED lamps may be modulated at higher frequencies. This could introduce variability. To test the idea try natural light or light from DC driven LED's.
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by JimTal001 » Sat May 14, 2016 3:28 pm

Actually I am not providing any supplemental light, just natural light from a window. Even in a fairly darkened room (blinds closed) the sensor is reporting max voltage where there is no mask over the sensors.

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

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by pythoncoder » Sun May 15, 2016 7:02 am

OK, nice try but no cigar for me ;) But even natural light can vary between runs. I'd exclude natural light and illuminate with a DC driven LED before delving into a major investigation. But with care and a cloudless sky you could get away with natural light.
Peter Hinch
Index to my micropython libraries.

JimTal001
Posts: 176
Joined: Thu Jul 30, 2015 4:59 pm

Re: Pyboard and linear ccd sensor(TSL 1410R)

Post by JimTal001 » Mon May 16, 2016 6:47 pm

As previously mentioned, running the code above which performs a single scan appears to work reasonably well, but if I try to perform multiple scan, like:

Code: Select all

   
   for i in range(10):   
      pyb.delay(1000) # wait 1 sec before collection the next output
      data = read()
      logData()
The returned data is not representative of the masked off areas. Any ideal as to why multiple sensor scans fails to return representative data?

Post Reply