help with code for reading a linear CCD

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
ianjohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: help with code for reading a linear CCD

Post by ianjohnson »

Although the linear array is capable of running over a wide range of operating frequencies up to a maximum
of 5 MHz, the speed of the A/D converter used in the application is likely to be the limiter for the maximum clock
frequency. The voltage output is available for the whole period of the clock, so the setup and hold times required
for the analog-to-digital conversion must be less than the clock period.
Could this be a problem? I'm not sure what speeds everything is happening at. Is it possible that clocks are getting sent too fast for the sensor, or faster than the pixel can be read into the array?


Edit-
It seems that digital read/writes take about 3.5us for 10.5us per pixel which is about 100khz, so that is way under the 5Mhz max. With ShiftIn I presume Clk=Low won't happen until the digitalRead is done.

My other question is how is 2v-3v out of the sensor going to be interpreted by the digital input, especially with the pulldown resistor in there? The fact that I am using 270ohm instead of 320 couldn't be critical, could it?

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

nah, the absolute fastest an arduino could toggle that pin would be 8MHz in an unrolled loop, using direct port manipulation. put it in a loop with just a jump and you've cut the speed to 4MHz. increment a counter and test the result and you're down to 2Mz. then those digitalWrites are much slower than manipulating the ports directly.

and the two calls to delayMicroseconds get you down to .4Mz all by their lonesomes.

i would try this...

Code: Select all

void setup (void) {
  pinMode (si, OUTPUT);
  pinMode (clk, OUTPUT);
  pinMode (ao, INPUT);
  pinMode (so2, INPUT);
  pinMode (a_pin_with_an _led_attached, OUTPUT);
}

void loop (void) {
  digitalWrite (si, 1);
  delayMicroseconds (1);
  digitalWrite (clk, 1);
  delayMicroseconds (1);
  digitalWrite (si, 0);
  for (int i = 0; i < 128; i++) {
    digitalRead (ao);
    delayMicroseconds (60);
    digitalWrite (clk, 0);
    delayMicroseconds (2);
    digitalWrite (clk, 1);
  }
  if (digitalRead (so2)) 
    digitalWrite (a_pin_with_an_led_attached, 1);
  else
    digitalWrite (a_pin_with_an_led_attached, 0);
  digitalWrite (clk, 0);
}
just let it run. connect so1 to si2, like you did before. connect ao0 and ao1. shine a light on the device and put your meter on ao0. this code should make it so that the sensor's measured voltage is on the output pin almost all the time. it will still give you an approximately 8ms integration time, but will minimize the time that the output is at high impedance, so you should get a pretty good read on your meter. if you've got a 'scope, all the better.

this will verify that the chip functions the way we think it does. hopefully, you get a non zero voltage on your meter and the led comes on on that 129th clock pulse and stays on.

if the output voltage is too low, increase the long delay. if the sensor appears saturated, decrease it. try to illuminate the whole thing evenly.

if nothing happens at all, i've misunderstood something.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

oh, yeah. try to find a cap to put across the supply pins on the sensor.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

just as an aside... i got my raspberry pi camera module working after a brief struggle. (this involved several rounds of reseating both ends of my cable, after which i decided to check the continuity on it since it was kinked on arrival. while removing it from the camera board i noticed that the sensor itself was not seated in its connector. press. snap. problem solved.) don't know what your application is, but if a $25 linux computer with a $25 5Mp camera module wouldn't blow your budget it might be worth consideration.

User avatar
ianjohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: help with code for reading a linear CCD

Post by ianjohnson »

I'm adding this into an existing Arduino project (http://www.thingiverse.com/thing:81662 , which is also the only programming experience I've had so far, so I want to stick with Arduino. It also gives me the ability to incorporate the arduino into a custom PCB if I get kits going.

Trying to get this working has taught me a lot more. I knew this wasn't going to work the first time, because that's just not the way the world works. I was worried about how I was going to troubleshoot it since my understanding of it was pretty shaky to begin with, so I appreciate all the help.

The LED didn't light for SO2 or SO1, even when I tried putting it into the loop. It would light for the clock. I get 5v from the + and GND rails, 4.5V from the clock, and 0 from SI. If I added SI ticks into the loop with a 60us delay, then it would start to show on the meter. Nothing from AO1 or SO1, and a steady 1.2v from AO2. When I disconnected everything and left only 5V and GND on the sensor, I still got 1.2V from AO2. Since you aren't supposed to get a voltage from the sensor unless you ask for it, I'm thinking that both of my sensor chips have a short or something in them. I don't know if it could have been caused by ESD, or if they are defective. I was thinking I would prefer the TSL1402 with 256 pixels anyway, so I will order one of those and give it another try.

I'm still thinking about your idea of using a photoresistor and an LED. For the filament extruder, I only need to measure changes in the size of the shadow to feed into a PID function. A real life measurement isn't necessary, I just need to detect changes in diameter of around .01mm. I don't know how much that will change the voltage coming out of the photocell. Do you have any ideas on increasing the sensitivity, or precision? Would there be an issue with noise, drowning out such small variations?

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

ok. that last code i suggested had a... well, let's call it a typo. the limit on the for loop should be 127. maybe, just maybe, it'll see so2, now, but i think the timing's still wrong. i will cogitate further.

User avatar
ianjohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: help with code for reading a linear CCD

Post by ianjohnson »

When it takes SO2 high, do you think it goes low on the next clock, or could it close it at its own speed, maybe too fast for the arduino to pick up? Either way, since there is 0v coming out of the AO1 I doubt it is getting all the way to triggering S02.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

so1 should go high 50ns after the 64th clock pulse and, assuming that so1 is connected to si2, so2 should go high 50ns after the 128th pulse, stay high when the clock goes low again and go low 50ns after the 129th clock.

this is according to figure two and the table Dynamic Characteristics over recommended ranges of supply voltage and operating free-air temperature.

it explains why we can't catch so2, since we test for it more than 50ns after the beginning of the 129th clock.

gives me something to thimk about as i walk to the grocery store.

User avatar
ianjohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: help with code for reading a linear CCD

Post by ianjohnson »

I found a picture someone else posted of their circuit using the same sensor-

Image

The datasheet says there is a dot to indicate Pin 1, but there isn't on the chips I have. There is however a + next to one of the pins, so I figured instead of a dot there is a + to indicate Vin, which is Pin 1. Apparently + means voltage out, for the AO2 pin. On the mouser product page the, + pin is on the upper left further reinforcing my belief that it was Pin 1, so I've had the chip upside down the whole time. Looking at the zoomed in image, there is a splotch or hole or something on the lower right which is the pin 1 indicator, which is not present on my chip.

I have to wait until I get home from work to try it out, hopefully there won't be any more problems, or damage from running everything to the wrong pins.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

any joy?

User avatar
ianjohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: help with code for reading a linear CCD

Post by ianjohnson »

Not quite. I got a TSL1402 which is the 256 pixel version. When I hook the multimeter to the AO2 output, I can see the reading change, but when I use a analog input I get full 1023 most of the time, or 1 on the digital. Without an oscilloscope, I can't really tell what is coming out on the clock pulses. I can't spend $300 for a scope. A cheap $100 scope would be ok, ut reviews say they can't go more than about 100khz, and I'm not sure that is fast enough.

Depending on how you do it, it seems to be easy to overexpose the pixels if you take too much time to read them out. Running a bunch of clocks through to clear the sensor before reading it makes an exposure time of about 2.5 milliseconds which seems like it might be too long. Either that, or maybe the output of pixel readings from the sensor isn't coordinating with the reading of them on the Arduino side somehow. I'm thinking I may need to look into direct port manipulation to run the clocks faster.

I tried one of the 128 pixel sensors, but I think they are both dead. I've read that these are especially vulnerable to ESD, so I will try getting another and being extra careful. There is also a Sony sensor that has 1024 pixels which would be great, but there might be trouble either reading them all fast enough, or storing them all on an Uno.

I've experimented a little with the alternate method of using the voltage output of a phototransistor to detect changes in size of the filament shadow. If I change the analog reference, I can get enough resolution to be useful, about 4 ticks per .01mm of filament diameter. The only problem would be the shadow changing size due to the filament moving toward or away from the LED. If I can position a phototransistor/LED combo on each side of the filament, then I can account for movement. If the filament moves, one shadow will shrink while the other grows. If the filament gets larger, then both shadows will grow.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

maybe try a neutral density filter over the sensor.

User avatar
ianjohnson
 
Posts: 16
Joined: Mon Jan 28, 2013 2:16 pm

Re: help with code for reading a linear CCD

Post by ianjohnson »

I actually got it reading in a decent range at one point by putting a piece of electrical tape over it, which is a pretty serious ND filter. I didn't really pursue it because I was concerned that it would soften the shadow and reduce precision.

The Sony CCD with 1024 pixels apparently has a shutter function of some kind. It would help to have exposure controlled directly rather than as a side effect of the clock speed.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: help with code for reading a linear CCD

Post by tldr »

maybe use something less intense than a laser. an led is a point light source and you could use a pot to calibrate it. that would make timing less critical apropos exposure and if there's a falloff in intensity towards the extremes of the sensor you could compensate for that in software.

User avatar
wiegerthefarmer
 
Posts: 3
Joined: Wed Oct 09, 2013 3:37 pm

Re: help with code for reading a linear CCD

Post by wiegerthefarmer »

Ian, did you end up getting this working? I'm working with a TSL1402 and experiencing similar things to you...

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”