# CCD Linear Sensor Array (from flatbed scanner) and Arduino

**URL:** <https://forum.arduino.cc/t/ccd-linear-sensor-array-from-flatbed-scanner-and-arduino/525187>\
**Category:** Sensors\
**Created:** [May 9, 2018, 7:30pm UTC](https://forum.arduino.cc/t/ccd-linear-sensor-array-from-flatbed-scanner-and-arduino/525187 "2018-05-09T19:30:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![e-San](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@e-San](https://forum.arduino.cc/u/e-San)\
**Post date:** [May 9, 2018, 7:30pm UTC](https://forum.arduino.cc/t/ccd-linear-sensor-array-from-flatbed-scanner-and-arduino/525187/1 "2018-05-09T19:30:31Z")

</div>

Hi!

So I got some idea and I need to build own scanner.  
I tried to get some sensors from few old scanners and finally found one which I was able to find datasheet - µPD3799CY from NEC (attachment).

So connected it as suggested in datasheet except resistors, NOT-gates and preamp - input directly from arduino, output (V\_out3) directly to arduino. Power supply - 12V.

I analyzed bilion times graph on page 8 and tried to make program:

```cpp
int fiRB = 8;
int fi1 = 9;
int fi2 = 10;
int fiTG = 11;

int line = 0;

// PORTB maps to Arduino digital pins 8 to 13 
// The two high bits (6 & 7) map to the crystal pins and are not usable;
//
// PORTB = {X1} {X2} {13} {12}  {11}  {10}  {9}  {8};
// PORTB = {X1} {X2} {13} {12} {fiTG} {fi1} {fi2} {RB};
// PORTB = 00000000;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(fiRB, OUTPUT);
  pinMode(fi1, OUTPUT);
  pinMode(fi2, OUTPUT);
  pinMode(fiTG, OUTPUT);

  pinMode(A0, INPUT);

  delay(100);
  
  Serial.println("");
  Serial.println("Hit enter for measurement.");
}

void loop() {

  line = 0;
  
  Serial.println("Waiting for input.");
  
  while(!Serial.available()){}
  while( Serial.available()){ Serial.read(); }

  PORTB = B00000100;
  delayMicroseconds(1);
  PORTB = B00000101;
  delayMicroseconds(1);
  PORTB = B00000011;
  delayMicroseconds(1);
  
  // PORTB = {X1} {X2} {13} {12} {fiTG} {fi1} {fi2} {fiRB};

  for(int k=1; k<20; k++){

    for(int f = 0; k < 3; k++){
      PORTB = B00000100;
      delayMicroseconds(1);
      PORTB = B00000101;
      delayMicroseconds(1);
      PORTB = B00000011;
      delayMicroseconds(1);
      line++;
    }
     
    Serial.print(line++);
    Serial.print('\t');
      
    // PORTB = {X1} {X2} {13} {12} {fiTG} {fi1} {fi2} {fiRB};
    PORTB = B00000100;
    Serial.print("B00000100");
    Serial.print(":");
    Serial.print(analogReadFast(A0));
    Serial.print(" ");
  
    PORTB = B00000101;
    Serial.print("B00000101");
    Serial.print(":");
    Serial.print(analogReadFast(A0));
    Serial.print(" ");
      
    PORTB = B00000011;
    Serial.print("B00000011");
    Serial.print(":");
    Serial.println(analogReadFast(A0));
  }
  
  Serial.println("---------");

  for(int j=0; j < 6; j++){
  
    for(int i=1; i<900; i++){
      line++;
      
      // PORTB = {X1} {X2} {13} {12} {fiTG} {fi1} {fi2} {fiRB};
      PORTB = B00000100;
      delayMicroseconds(1);
      PORTB = B00000101;
      delayMicroseconds(1);
      PORTB = B00000011;
      delayMicroseconds(1);
    }

    Serial.print(line++);
    Serial.print('\t');
    
    // PORTB = {X1} {X2} {13} {12} {fiTG} {fi1} {fi2} {fiRB};
    PORTB = B00000100;
    Serial.print("B00000100");
    Serial.print(":");
    Serial.print(analogReadFast(A0));
    Serial.print(" ");
    
    PORTB = B00000101;
    Serial.print("B00000101");
    Serial.print(":");
    Serial.print(analogReadFast(A0));
    Serial.print(" ");
      
    PORTB = B00000011;
    Serial.print("B00000011");
    Serial.print(":");
    Serial.print(analogReadFast(A0));
    Serial.println("");

  }

}

int analogReadFast(byte ADCpin) 
{ byte ADCSRAoriginal = ADCSRA; 
  ADCSRA = (ADCSRA & B11111000) | 4; 
  int adc = analogRead(ADCpin);  
  ADCSRA = ADCSRAoriginal;
  return adc;
}

```

The problem is that when I cover whole sensor, I get this:

```cpp
2	B00000100:1023 B00000101:1023 B00000011:1023
5	B00000100:1023 B00000101:1023 B00000011:1023
8	B00000100:1023 B00000101:1023 B00000011:1023
11	B00000100:1023 B00000101:1023 B00000011:1023
14	B00000100:1023 B00000101:1023 B00000011:1023
17	B00000100:1023 B00000101:1023 B00000011:1023
20	B00000100:1023 B00000101:1023 B00000011:1023
23	B00000100:1023 B00000101:1023 B00000011:1023
26	B00000100:1023 B00000101:1023 B00000011:1023
29	B00000100:1023 B00000101:1023 B00000011:1023
32	B00000100:1023 B00000101:1023 B00000011:1023
35	B00000100:1023 B00000101:1023 B00000011:1023
38	B00000100:1023 B00000101:1023 B00000011:1023
41	B00000100:1023 B00000101:1023 B00000011:1023
44	B00000100:1023 B00000101:1023 B00000011:1023
47	B00000100:1023 B00000101:1023 B00000011:1023
50	B00000100:1023 B00000101:1023 B00000011:1023
53	B00000100:1023 B00000101:1023 B00000011:1023
56	B00000100:1023 B00000101:1023 B00000011:1023
---------
956	B00000100:1023 B00000101:1023 B00000011:1023
1856	B00000100:1023 B00000101:1023 B00000011:1023
2756	B00000100:1023 B00000101:1023 B00000011:1023
3656	B00000100:1023 B00000101:1023 B00000011:1023
4556	B00000100:1023 B00000101:1023 B00000011:1023
5456	B00000100:1023 B00000101:1023 B00000011:1023

```

Torch directly onto sensor:

```cpp
2	B00000100:729 B00000101:452 B00000011:481
5	B00000100:1023 B00000101:487 B00000011:491
8	B00000100:1023 B00000101:531 B00000011:531
11	B00000100:1023 B00000101:532 B00000011:534
14	B00000100:1023 B00000101:531 B00000011:531
17	B00000100:1023 B00000101:512 B00000011:511
20	B00000100:1023 B00000101:539 B00000011:538
23	B00000100:1023 B00000101:535 B00000011:535
26	B00000100:1023 B00000101:536 B00000011:536
29	B00000100:1023 B00000101:534 B00000011:534
32	B00000100:1023 B00000101:539 B00000011:540
35	B00000100:1023 B00000101:536 B00000011:536
38	B00000100:1023 B00000101:532 B00000011:535
41	B00000100:1023 B00000101:519 B00000011:518
44	B00000100:1023 B00000101:534 B00000011:534
47	B00000100:1023 B00000101:534 B00000011:535
50	B00000100:1023 B00000101:534 B00000011:534
53	B00000100:1023 B00000101:533 B00000011:532
56	B00000100:1023 B00000101:535 B00000011:536
---------
956	B00000100:1023 B00000101:492 B00000011:495
1856	B00000100:1023 B00000101:489 B00000011:494
2756	B00000100:1023 B00000101:489 B00000011:494
3656	B00000100:1023 B00000101:489 B00000011:494
4556	B00000100:1023 B00000101:489 B00000011:494
5456	B00000100:1023 B00000101:487 B00000011:494

```

Until now it seems to be everything ok, but I've covered about 1/3 of sensor with green tape and the output is the same...

I've been trying different delays (after PORTB) and whole bunch of stuff i can do with software, but it seems to not affect the output. Even removing part before first for does not change anything...

[UPD3799CY (3x 5k pix).pdf](https://forum.arduino.cc/uploads/short-url/5VGKe0CVfXCC3tlHHfeGUFOr45z.pdf) (118 KB)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:21am UTC](https://forum.arduino.cc/t/ccd-linear-sensor-array-from-flatbed-scanner-and-arduino/525187/2 "2021-05-06T10:21:33Z")

</div>


