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.
Chemist
Posts: 20
Joined: Wed Mar 04, 2015 5:25 pm

Pyboard and linear ccd sensor(TSL 1410R)

Post by Chemist » Fri Nov 13, 2015 4:55 am

Hello everybody,

For my detector I was advised by blmorris to use one of the AMS (formerly TAOS) linear sensors. I decided to go with TSL 1410R (http://ams.com/eng/Products/Light-Senso ... s/TSL1410R ). Unfortunately, I couldn't find any information how to read data from this sensor, so I put together some pieces of code from different places written for similar sensor using Arduino board. I tried to transfer the arduino code to pyboard. This is what I got:

Code: Select all

import pyb
from pyb import Pin, ADC

exposure_time = 5
pin_si = Pin('X1', Pin.OUT_PP)
pin_clk = Pin('X2', Pin.OUT_PP)
pin_ao = ADC(Pin('X3'))

def clock_pulse():
    pin_clk.high()
    pin_clk.low()

def write_pin_si():
    pin_si.high()
    clock_pulse()
    pin_si.low()    

def read():    
    write_pin_si()
    for i in range(1281):
        clock_pulse()
    pyb.delay(exposure_time)
    write_pin_si()
    data = [pin_ao.read()* 3.3/4096 for i in range(1280)]
    return data

data = read()
print(data)
A good thing is that the generated data reflects the level of the light the sensor was expose to. I just don't know if the code generates the correct data. Please take a look at the attached picture(the sensor was under semi-dark environment). I would highly appreciate if you can share your opinion about correctness of the code.

Thank you for you time,
Vitali
Attachments
tsl1410r.png
Linear ccd sensor
tsl1410r.png (94.21 KiB) Viewed 9970 times

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 » Fri Nov 13, 2015 8:02 am

Looking at the datasheet I can see a number of problems here. Firstly, if you're running the device from 5V the analog output saturation voltage is 4.8V. This exceeds the maximum for the Pyboard ADC. OK if you're running it from 3.3V.

Secondly the timing sequence of your code looks wrong. I believe what you're reading is the voltage on the ADC after the analog output of the device goes tri-state. You need to read the ADC after each clock pulse. Also, it seems to me that you'll only get valid data from the ADC after the device has gone through an entire sequence of 1280 reads. In other words the very first set of data you read from the chip won't be valid because the integrators haven't had time to operate. Also the clock phasing looks wrong to me.

From the datasheet it looks as if the code should do something along the following lines. Obviously, lacking the device, I can't test this. In the interests of speed I suggest storing the raw data from the sensor in an array and do the floating point stuff when you print the results.

Code: Select all

from array import array
a = array('i', (0 for _ in range(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])
[EDIT 12 May 16 to squash bug]
This is very much a first pass. If you get plausible data it can be tidied up. The last pixel is garbage (there are only 1280). You could save RAM by using an array of half words in place of 32 bit integers. My approach to coding this kind of stuff is to get something working, then take a second pass to sort out the detail. To produce test data, how about physically masking off some of the pixels to give a recognisable light-dark pattern?

Out of interest, what's your application?
Last edited by pythoncoder on Thu May 12, 2016 5:28 pm, edited 1 time in total.
Peter Hinch
Index to my micropython libraries.

Chemist
Posts: 20
Joined: Wed Mar 04, 2015 5:25 pm

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

Post by Chemist » Fri Nov 13, 2015 7:18 pm

Dear @pythoncoder,

Thank you very much for examining and correcting my code and providing a clear explanation of my mistakes. Data, gathered by the updated code, you can see in the attached files: 1. Sensor was covered by a box. 2. A half-covered sensor. The shape of the line (covered sensor) this time is a little different from what I got using my code(see the picture posted yesterday). Just in case, here is my wiring diagram, maybe the wiring is incorrect.

I am trying to build a spectrophotometer, something similar to this one http://publiclab.org/notes/bhickman/10- ... ectrometer. The basic idea is to measure the intensity of the light before it passes through a cuvette with a chemical compound and after it passes the cuvette. After performing some calculations, we can get a spectrum of a compound (wavelength vs absorbance).

Thanks,
Vitali
Attachments
covered_sensor.png
Covered_sensor
covered_sensor.png (91.67 KiB) Viewed 9945 times
half_covered.png
Half_covered_sensor
half_covered.png (47.36 KiB) Viewed 9945 times
wiring.png
Wiring
wiring.png (43.99 KiB) Viewed 9945 times

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

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

Post by blmorris » Sat Nov 14, 2015 4:09 am

Vitali - looks like you have it working! Those plots are pretty much what i would expect to see given the set up that you described. The saturation seen in the pixels up to ~810 isn't surprising, the next thing you probably want to do would be to establish more precise control over the integration time. There are several threads here on the forum about tricks to achieve more precise timing when bit-banging GPIO pins.

I followed the link you sent - an open-source photo spectrometer is such a cool project! Good luck!

-Bryan

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 Nov 14, 2015 8:57 am

@Chemist That schematic is identical to that shown on the device datasheet so we can assume it's correct. I'd agree with @blmorris and say that's working. With point source illumination you might get a sharper edge on your graph. As for more precise timing, from my reading of the datasheet the device is uncritical in that respect. It is entirely synchronous.

You might want to consider the graph in the datasheet entitled "photodiode spectral responsivity". I'm unfamiliar with spectrophotometers but I gather that the aim of the optics is to produce a signal where distance along the sensor is proportional to frequency as with a prism. If so, you might want to consider creating a coefficient array from the graph and scaling each pixel to linearise the response.

Great project! Do post details of the build when it's up and running.
Peter Hinch
Index to my micropython libraries.

Chemist
Posts: 20
Joined: Wed Mar 04, 2015 5:25 pm

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

Post by Chemist » Sun Nov 15, 2015 7:45 pm

Dear Pythoncoder and Bryan,

Thank you very much for you support. I hope that this version (with linear ccd sensor) will give much better resolution than my previous attempt. If I make it working as I intended, maybe I will commercialize it :D .

Best,
Vitali

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 » Mon Nov 16, 2015 10:41 am

I'd be interested to hear about your results when you have got it running. You might want to write up the build in the Hardware Projects subforum. Good luck with this interesting project ;)
Peter Hinch
Index to my micropython libraries.

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 » Mon Nov 16, 2015 5:54 pm

I'm also interested. I picked up a TAOS sensor a few years ago intending to use it for detecting lines on a line following robot and never got around to buillding the robot.

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

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

Post by JimTal001 » Sat Apr 16, 2016 11:04 pm

Can anyone explain how the voltage output of a single pixel from this sensor is converted to an RGB value?

Thanks for your help.

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 Apr 17, 2016 9:12 am

As discussed previously it doesn't produce RGB data. The sensor is monochrome. The spectrophotometer uses a prism to produce a pattern of light where distance corresponds to frequency. This is projected onto the sensor so that successive pixels represent successively higher light frequencies. RGB doesn't come into it: the aim is to produce a plot of amplitude against frequency.
Peter Hinch
Index to my micropython libraries.

Post Reply